Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changes current test files to be able to easily add more unit tests

  • Loading branch information...
commit 1f2f92c4edf7a50a7f1aaa0e4a5fa70288cb586c 1 parent e8d7adf
@dresende authored
Showing with 96 additions and 78 deletions.
  1. +29 −49 test/database-helpers.js
  2. +67 −29 test/validators.js
View
78 test/database-helpers.js
@@ -2,54 +2,34 @@ var helpers = require("../lib/databases/helpers");
var should = require("should");
describe("helpers", function () {
- describe(".buildSqlOrder(A)", function () {
- it("should return ' ORDER BY A ASC'", function () {
- helpers.buildSqlOrder("A").should.equal(" ORDER BY A ASC");
- });
- });
- describe(".buildSqlOrder(A Asc)", function () {
- it("should return ' ORDER BY A ASC'", function () {
- helpers.buildSqlOrder("A Asc").should.equal(" ORDER BY A ASC");
- });
- });
- describe(".buildSqlOrder(A Desc)", function () {
- it("should return ' ORDER BY A DESC'", function () {
- helpers.buildSqlOrder("A Desc").should.equal(" ORDER BY A DESC");
- });
- });
- describe(".buildSqlOrder(A B Asc)", function () {
- it("should return ' ORDER BY A ASC, B ASC'", function () {
- helpers.buildSqlOrder("A B Asc").should.equal(" ORDER BY A ASC, B ASC");
- });
- });
- describe(".buildSqlOrder(A Asc B Asc)", function () {
- it("should return ' ORDER BY A ASC, B ASC'", function () {
- helpers.buildSqlOrder("A Asc B Asc").should.equal(" ORDER BY A ASC, B ASC");
- });
- });
- describe(".buildSqlOrder(A Desc B Asc)", function () {
- it("should return ' ORDER BY A DESC, B ASC'", function () {
- helpers.buildSqlOrder("A Desc B Asc").should.equal(" ORDER BY A DESC, B ASC");
- });
- });
- describe(".buildSqlOrder( A B C )", function () {
- it("should return ' ORDER BY A ASC, B ASC, C ASC'", function () {
- helpers.buildSqlOrder(" A B C ").should.equal(" ORDER BY A ASC, B ASC, C ASC");
- });
- });
- describe(".buildSqlOrder(A)", function () {
- it("should return ' ORDER BY A ASC'", function () {
- helpers.buildSqlOrder("A").should.equal(" ORDER BY A ASC");
- });
- });
- describe(".buildSqlLimit(N)", function () {
- it("should return ' LIMIT N'", function () {
- helpers.buildSqlLimit("N").should.equal(" LIMIT N");
- });
- });
- describe(".buildSqlLimit(N, M)", function () {
- it("should return ' LIMIT M,N'", function () {
- helpers.buildSqlLimit("N", "M").should.equal(" LIMIT M, N");
+ var tests = {
+ buildSqlOrder: [
+ [ [ "A" ], " ORDER BY A ASC" ],
+ [ [ "A Asc" ], " ORDER BY A ASC" ],
+ [ [ "A Desc" ], " ORDER BY A DESC" ],
+ [ [ "A B Asc" ], " ORDER BY A ASC, B ASC" ],
+ [ [ "A Asc B Asc" ], " ORDER BY A ASC, B ASC" ],
+ [ [ "A Desc B" ], " ORDER BY A DESC, B ASC" ],
+ [ [ " A B C " ], " ORDER BY A ASC, B ASC, C ASC" ],
+ [ [ "A" ], " ORDER BY A ASC" ]
+ ],
+ buildSqlLimit: [
+ [ [ "N" ], " LIMIT N" ],
+ [ [ "N", "M" ], " LIMIT M, N" ]
+ ]
+ };
+
+ for (var k in tests) {
+ for (var i = 0; i < tests[k].length; i++) {
+ addTest(k, tests[k][i][0], tests[k][i][1]);
+ }
+ }
+});
+
+function addTest(fun, params, expected) {
+ describe("." + fun + "(" + params.join(", ") + ")", function () {
+ it("should return '" + expected + "'", function () {
+ helpers[fun].apply(helpers, params).should.equal(expected);
});
});
-});
+}
View
96 test/validators.js
@@ -1,41 +1,78 @@
-var v = require("../lib/validators");
+var validators = require("../lib/validators");
var should = require("should");
describe("validators", function () {
- describe(".rangeNumber(3, 6)(3)", shouldNotThrow(v.rangeNumber, [ 3, 6 ], [ 3 ]));
- describe(".rangeNumber(3, 6)(4)", shouldNotThrow(v.rangeNumber, [ 3, 6 ], [ 4 ]));
- describe(".rangeNumber(3, 6)(6)", shouldNotThrow(v.rangeNumber, [ 3, 6 ], [ 6 ]));
- describe(".rangeNumber(3, 6)(2)", shouldThrow(v.rangeNumber, [ 3, 6 ], [ -4 ], "out-of-range-number"));
- describe(".rangeNumber(3)(3)", shouldNotThrow(v.rangeNumber, [ 3 ], [ 3 ]));
- describe(".rangeNumber(3)(5)", shouldNotThrow(v.rangeNumber, [ 3 ], [ 5 ]));
- describe(".rangeNumber(3)(2)", shouldThrow(v.rangeNumber, [ 3 ], [ 2 ], "out-of-range-number"));
- describe(".rangeNumber(3)(-2)", shouldThrow(v.rangeNumber, [ 3 ], [ -2 ], "out-of-range-number"));
- describe(".rangeNumber(, 3)(-2)", shouldNotThrow(v.rangeNumber, [ undefined, 3 ], [ -2 ]));
- describe(".rangeNumber(, 3)(2)", shouldNotThrow(v.rangeNumber, [ undefined, 3 ], [ 2 ]));
- describe(".rangeNumber(, 3)(3)", shouldNotThrow(v.rangeNumber, [ undefined, 3 ], [ 3 ]));
- describe(".rangeNumber(, 3)(5)", shouldThrow(v.rangeNumber, [ undefined, 3 ], [ 5 ], "out-of-range-number"));
-
- describe(".rangeLength(3, 6)('abc')", shouldNotThrow(v.rangeLength, [ 3, 6 ], [ "abc" ]));
- describe(".rangeLength(3, 6)('abcdef')", shouldNotThrow(v.rangeLength, [ 3, 6 ], [ "abcdef" ]));
- describe(".rangeLength(3, 6)('ab')", shouldThrow(v.rangeLength, [ 3, 6 ], [ "ab" ], "out-of-range-length"));
- describe(".rangeLength(3, 6)('abcdefg')", shouldThrow(v.rangeLength, [ 3, 6 ], [ "abcdefg" ], "out-of-range-length"));
- describe(".rangeLength(3, 6)()", shouldThrow(v.rangeLength, [ 3, 6 ], [ undefined ], "undefined"));
-
- describe(".insideList([ 1, 2, 'a' ])(1)", shouldNotThrow(v.insideList, [ [ 1, 2, 'a' ] ], [ 1 ]));
- describe(".insideList([ 1, 2, 'a' ])(2)", shouldNotThrow(v.insideList, [ [ 1, 2, 'a' ] ], [ 2 ]));
- describe(".insideList([ 1, 2, 'a' ])('a')", shouldNotThrow(v.insideList, [ [ 1, 2, 'a' ] ], [ 'a' ]));
- describe(".insideList([ 1, 2, 'a' ])('1')", shouldThrow(v.insideList, [ [ 1, 2, 'a' ] ], [ '1' ], "outside-list"));
-
- describe(".outsideList([ 1, 2, 'a' ])('1')", shouldNotThrow(v.outsideList, [ [ 1, 2, 'a' ] ], [ '1' ]));
- describe(".outsideList([ 1, 2, 'a' ])(1)", shouldThrow(v.outsideList, [ [ 1, 2, 'a' ] ], [ 1 ], "inside-list"));
- describe(".outsideList([ 1, 2, 'a' ])(2)", shouldThrow(v.outsideList, [ [ 1, 2, 'a' ] ], [ 2 ], "inside-list"));
- describe(".outsideList([ 1, 2, 'a' ])('a')", shouldThrow(v.outsideList, [ [ 1, 2, 'a' ] ], [ 'a' ], "inside-list"));
+ var tests = {
+ rangeNumber: [
+ [ [ 3, 6 ], [ 3 ] ],
+ [ [ 3, 6 ], [ 4 ] ],
+ [ [ 3, 6 ], [ 6 ] ],
+ [ [ 3, 6 ], [ -4 ], "out-of-range-number" ],
+ [ [ 3 ], [ 3 ] ],
+ [ [ 3 ], [ 5 ] ],
+ [ [ 3 ], [ 2 ], "out-of-range-number" ],
+ [ [ 3 ], [ -2 ], "out-of-range-number" ],
+ [ [ undefined, 3 ], [ -2 ] ],
+ [ [ undefined, 3 ], [ 2 ] ],
+ [ [ undefined, 3 ], [ 3 ] ],
+ [ [ undefined, 3 ], [ 5 ], "out-of-range-number" ]
+ ],
+ rangeLength: [
+ [ [ 3, 6 ], [ "abc" ] ],
+ [ [ 3, 6 ], [ "abcdef" ] ],
+ [ [ 3, 6 ], [ "ab" ], "out-of-range-length" ],
+ [ [ 3, 6 ], [ "abcdefg" ], "out-of-range-length" ],
+ [ [ 3, 6 ], [ undefined ], "undefined" ]
+ ],
+ insideList: [
+ [ [[ 1, 2, "a" ]], [ 1 ] ],
+ [ [[ 1, 2, "a" ]], [ 2 ] ],
+ [ [[ 1, 2, "a" ]], [ "a" ] ],
+ [ [[ 1, 2, "a" ]], [ "1" ], "outside-list" ]
+ ],
+ outsideList: [
+ [ [[ 1, 2, "a" ]], [ "1" ] ],
+ [ [[ 1, 2, "a" ]], [ 1 ], "inside-list" ],
+ [ [[ 1, 2, "a" ]], [ 2 ], "inside-list" ],
+ [ [[ 1, 2, "a" ]], [ "a" ], "inside-list" ]
+ ],
+ equalToProperty: [
+ [ [ "strprop" ], [ "str" ] ],
+ [ [ "strprop" ], [ "not" ], "not-equal-to-property" ],
+ [ [ "numprop" ], [ 2 ] ],
+ [ [ "numprop" ], [ "2" ] ],
+ [ [ "numprop" ], [ 3 ], "not-equal-to-property" ],
+ [ [ "boolprop" ], [ false ] ],
+ [ [ "boolprop" ], [ null ], "not-equal-to-property" ],
+ [ [ "boolprop" ], [ true ], "not-equal-to-property" ],
+ [ [ "boolprop" ], [ undefined ], "not-equal-to-property" ]
+ ]
+ };
+ for (var k in tests) {
+ for (var i = 0; i < tests[k].length; i++) {
+ addTest(k, tests[k][i][0], tests[k][i][1], tests[k][i][2]);
+ }
+ }
});
+function addTest(fun, params1, params2, throwValue) {
+ var test = testName(fun, params1, params2);
+
+ if (!throwValue) {
+ return describe(test, shouldNotThrow(validators[fun], params1, params2));
+ }
+ return describe(test, shouldThrow(validators[fun], params1, params2, throwValue));
+}
+
+function testName(fun, params1, params2) {
+ return "." + fun + "(" + params1.join(", ") + ")(" + params2.join(", ") + ")";
+}
+
function shouldNotThrow(validator, build_params, call_params) {
call_params.push(function (err) {
should.strictEqual(undefined, err);
});
+ call_params.push({ strprop: "str", numprop: 2, boolprop: false });
return function () {
it("should not throw error", function () {
@@ -48,6 +85,7 @@ function shouldThrow(validator, build_params, call_params, error) {
call_params.push(function (err) {
should.strictEqual(error, err);
});
+ call_params.push({ strprop: "str", numprop: 2, boolprop: false });
return function () {
it("should throw error '" + error + "'", function () {
Please sign in to comment.
Something went wrong with that request. Please try again.