Permalink
Browse files

Adds mocha unit tests to database helpers and validators

This is still an early stage, still need a lot of tests.

To run them:
npm install -g mocha
cd /path/to/orm
mocha -R spec
  • Loading branch information...
1 parent 6110f61 commit 3ff9173b7190d859a679609a1af134c604d49a47 @dresende committed Jun 15, 2012
Showing with 112 additions and 0 deletions.
  1. +55 −0 test/database-helpers.js
  2. +57 −0 test/validators.js
View
@@ -0,0 +1,55 @@
+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");
+ });
+ });
+});
View
@@ -0,0 +1,57 @@
+var v = 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"));
+});
+
+function shouldNotThrow(validator, build_params, call_params) {
+ call_params.push(function (err) {
+ should.strictEqual(undefined, err);
+ });
+
+ return function () {
+ it("should not throw error", function () {
+ validator.apply(null, build_params).apply(null, call_params);
+ });
+ };
+}
+
+function shouldThrow(validator, build_params, call_params, error) {
+ call_params.push(function (err) {
+ should.strictEqual(error, err);
+ });
+
+ return function () {
+ it("should throw error '" + error + "'", function () {
+ validator.apply(null, build_params).apply(null, call_params);
+ });
+ };
+}

0 comments on commit 3ff9173

Please sign in to comment.