Skip to content
Snippets Groups Projects
Commit 895d88e3 authored by Johannes Hörmann's avatar Johannes Hörmann
Browse files

test mandatory errors

parent 55e3c87d
No related branches found
No related tags found
No related merge requests found
......@@ -598,17 +598,110 @@ var dbWrapperTests = new TestSuite([
})
var mandatoryErrorTests = new TestSuite([
// and
["and without parameter should error", function(pTester)
{
new SqlBuilder().and();
}, SqlBuilder.ERROR_NO_PARAMETER_PROVIDED()],
["and with '' as value should error", function(pTester)
{
new SqlBuilder().and("PERSON.FIRSTNAME", "");
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY()],
["and with null as value should error", function(pTester)
{
new SqlBuilder().and("PERSON.FIRSTNAME", null);
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY()],
["and with a jdito-var containing null should error", function(pTester)
{
vars.set("$global.TestingVarNull", null);
new SqlBuilder().and("PERSON.FIRSTNAME", "$global.TestingVarNull");
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()],
["and with a jdito-var containing '' should error", function(pTester)
{
vars.set("$global.TestingVarEmptyString", "");
new SqlBuilder().and("PERSON.FIRSTNAME", "$global.TestingVarEmptyString");
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()],
["and with a jdito-var containing '' should error", function(pTester)
{
vars.set("$global.TestingVarEmptyString", "");
new SqlBuilder().and("PERSON.FIRSTNAME", "$global.TestingVarEmptyString");
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()],
["and with an empty sql-builder as subquery should error", function(pTester)
{
new SqlBuilder().and("PERSON.FIRSTNAME", new SqlBuilder());
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY()],
["and with an empty prepared statement as subquery should error", function(pTester)
{
new SqlBuilder().and("PERSON.FIRSTNAME", ["", []]);
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY()],
// or
["or without parameter should error", function(pTester)
{
new SqlBuilder().or();
}, SqlBuilder.ERROR_NO_PARAMETER_PROVIDED()],
["or with '' as value should error", function(pTester)
{
new SqlBuilder().or("PERSON.FIRSTNAME", "");
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY()],
["or with null as value should error", function(pTester)
{
new SqlBuilder().or("PERSON.FIRSTNAME", null);
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY()],
["or with a jdito-var containing null should error", function(pTester)
{
vars.set("$global.TestingVarNull", null);
new SqlBuilder().or("PERSON.FIRSTNAME", "$global.TestingVarNull");
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()],
["or with a jdito-var containing '' should error", function(pTester)
{
vars.set("$global.TestingVarEmptyString", "");
new SqlBuilder().or("PERSON.FIRSTNAME", "$global.TestingVarEmptyString");
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()],
["or with a jdito-var containing '' should error", function(pTester)
{
vars.set("$global.TestingVarEmptyString", "");
new SqlBuilder().or("PERSON.FIRSTNAME", "$global.TestingVarEmptyString");
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR()],
["or with an empty sql-builder as subquery should error", function(pTester)
{
new SqlBuilder().or("PERSON.FIRSTNAME", new SqlBuilder());
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY()],
["or with an empty prepared statement as subquery should error", function(pTester)
{
new SqlBuilder().or("PERSON.FIRSTNAME", ["", []]);
}, SqlBuilder.ERROR_VALUE_IS_MANDATORY()],
])
var tester = new Tester("Test SqlBuilder.and()");
var tester = new Tester("Test SqlBuilder");
tester.test(validAndUsageTests);
tester.test(validOrUsageTests);
tester.test(combinedAndOrTests);
tester.test(ifSetTests);
tester.test(mandatoryErrorTests);
tester.test(dbWrapperTests);
tester.test(mandatoryErrorTests);
logging.log("-------------------------");
tester.printResults();
......@@ -676,11 +676,6 @@ SqlBuilder.ERROR_INVALID_CONDITION_VALUE_TYPE = function()
return new Error(translate.text("SqlBuilder.and/or: invalid value-type for pCondition"));
}
SqlBuilder.ERROR_INVALID_SUBQUERY_TYPE = function()
{
return new Error(translate.text("SqlBuilder.and/or: pValue has to be a valid value. In this case a fully quallified subselect-SqlBuilder (e.g. select, from, ... have to be set)"));
}
SqlBuilder.ERROR_NO_CONDITION = function()
{
return new Error(translate.text("SqlBuilder.and/or: if you use a subQuery (e.g. SqlBuilder) you have to provide pCondition (e.g. \"exists ?\")"));
......@@ -688,16 +683,12 @@ SqlBuilder.ERROR_NO_CONDITION = function()
SqlBuilder.ERROR_INVALID_SUBQUERY_TYPE = function()
{
return new Error(translate.text("SqlBuilder.and/or: invalid value-type for pFieldOrCond. It can be a fully qualified SqlBuilder, a string or an jdito-prepared-statement array"));
return new Error(translate.text("SqlBuilder.and/or: invalid value-type for pFieldOrCond. It can be a fully qualified SqlBuilder (e.g. select, from, ... have to be set) or an jdito-prepared-statement array"));
}
SqlBuilder.ERROR_VALUE_IS_MANDATORY = function()
{
return new Error(translate.text("SqlBuilder.and/or: pValue has to be not null, \"\""));
}
SqlBuilder.ERROR_SUBQUERY_IS_MANDATORY = function() {
return new Error(translate.text("SqlBuilder.and/or: pFieldOrCond has to be not null, \"\", empty prepared statement or empty SqlBuilder (if you use only the first parameter)"));
return new Error(translate.text("SqlBuilder.and/or: pValue has to be not null or \"\""));
}
SqlBuilder.ERROR_VALUE_IS_MANDATORY_JDITO_VAR = function()
......@@ -720,6 +711,11 @@ SqlBuilder.ERROR_NO_TABLE = function()
return new Error(translate.text("SqlBuilder.deleteDat/updateData: You have to specify a tablename"));
}
SqlBuilder.ERROR_NO_PARAMETER_PROVIDED = function()
{
return new Error(translate.text("SqlBuilder.and/or: You have to specify at least one parameter"));
}
/**
* Alternative way of creating a new SqlBuilder object that allows to use
* methods on it directly without having to put brackets around it
......@@ -936,10 +932,9 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition
var sql = pSubquery;
var typeofSql = typeof sql;
// the field is a simple string -> just add the string, no prepared statement
// the field is an array -> it is a prepared statement which already SHOULD contain exists or another condition
// Both can be handled by _prepare
if (Array.isArray(sql) || typeofSql == "string")
if (Array.isArray(sql))
{
if (sql[0])
{
......@@ -947,7 +942,7 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition
}
else if (pMandatory)
{
throw SqlBuilder.ERROR_INVALID_SUBQUERY_TYPE();
throw SqlBuilder.ERROR_VALUE_IS_MANDATORY();
}
return this;
......@@ -963,8 +958,7 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition
{
throw SqlBuilder.ERROR_NO_CONDITION();
}
logging.log(JSON.stringify([subQuery], null, "\t"))
if (subQuery.isFullSelect())
{
var preparedObj = subQuery.build();
......@@ -972,13 +966,12 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition
}
else if (pMandatory)
{
throw SqlBuilder.ERROR_INVALID_SUBQUERY_TYPE();
throw SqlBuilder.ERROR_VALUE_IS_MANDATORY();
}
return this;
}
if (pMandatory)
throw SqlBuilder.ERROR_INVALID_SUBQUERY_TYPE();
throw SqlBuilder.ERROR_INVALID_SUBQUERY_TYPE();
}
/**
......@@ -997,6 +990,11 @@ SqlBuilder.prototype._whereSubquery = function(pSubquery, pMandatory, pCondition
*/
SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCond, pFieldType, pAddPreparedConditionCallback)
{
if (pFieldOrCond === undefined && pValue === undefined && pCond === undefined && pFieldType === undefined)
{
throw SqlBuilder.ERROR_NO_PARAMETER_PROVIDED();
}
// For now: treat "" as null just like db.insert, db.table etc. Maybe otion for later: more pMandatory-types
if (pValue === "")
pValue = null;
......@@ -1005,7 +1003,7 @@ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCon
pMandatory = true;
// just call the andCondition function if it is only a Condition
if (!pFieldOrCond !== undefined && (pValue === undefined) && pCond === undefined && pFieldType === undefined)
if (pFieldOrCond !== undefined && pValue === undefined && pCond === undefined && pFieldType === undefined)
{
return this._whereCondition(pFieldOrCond, pMandatory, pAddPreparedConditionCallback, true);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment