Permalink
Browse files

Restored model-tests.

  • Loading branch information...
1 parent 3fd1410 commit 1c07cececea91c2b4cbb8ac86e0570ea10b0b9b0 @mde mde committed Nov 25, 2011
Showing with 281 additions and 1 deletion.
  1. +1 −1 README.md
  2. +78 −0 test/model_create_user.js
  3. +202 −0 test/model_datatypes.js
View
2 README.md
@@ -261,7 +261,7 @@ is valid.
## Running the tests
In the geddy project directory, run `jake test`. The tests simply
-use NodeJS's `assert` library, which throw an error on failure.
+use NodeJS's `assert` library, which throws an error on failure.
If there are no errors, the tests all ran successfully.
## API Docs
View
78 test/model_create_user.js
@@ -0,0 +1,78 @@
+var utils = require('../lib/utils')
+ , model = require('../lib/model')
+ , assert = require('assert')
+ , tests;
+
+geddy.model = model;
+
+var User = function () {
+ this.property('login', 'string', {required: true});
+ this.property('password', 'string', {required: true});
+ this.property('lastName', 'string');
+ this.property('firstName', 'string');
+
+ this.validatesPresent('login');
+ this.validatesFormat('login', /[a-z]+/, {message: 'Subdivisions!'});
+ this.validatesLength('login', {min: 3});
+ this.validatesConfirmed('password', 'confirmPassword');
+};
+
+User.prototype.someMethod = function () {
+ // Do some stuff on a User instance
+};
+
+User = geddy.model.register('User', User);
+
+var tests = new (function () {
+ var _params = {
+ login: 'zzz',
+ password: 'asdf',
+ confirmPassword: 'asdf',
+ firstName: 'Neil'
+ };
+
+ this.testValid = function () {
+ var user = User.create(_params);
+ assert.ok(user.isValid());
+ };
+
+ this.testShortLogin = function () {
+ _params.login = 'zz'; // Too short, invalid
+ var user = User.create(_params);
+ assert.ok(typeof user.errors.login != 'undefined');
+ };
+
+ this.testInvalidLoginWithCustomMessage = function () {
+ _params.login = '2112'; // Contains numbers, invalid
+ var user = User.create(_params);
+ // Error message should be customized
+ assert.ok(user.errors.login, 'Subdivisions!');
+ };
+
+ this.testNoLogin = function () {
+ delete _params.login; // Contains numbers, invalid
+ var user = User.create(_params);
+ // Error message should be customized
+ assert.ok(typeof user.errors.login != 'undefined');
+
+ _params.login = 'zzz'; // Restore to something valid
+ };
+
+ this.testNoConfirmPassword = function () {
+ _params.confirmPassword = 'fdsa';
+ var user = User.create(_params);
+ // Error message should be customized
+ assert.ok(typeof user.errors.password != 'undefined');
+
+ _params.confirmPassword = 'asdf'; // Restore to something valid
+ };
+
+})();
+
+for (var p in tests) {
+ if (typeof tests[p] == 'function') {
+ tests[p]();
+ }
+}
+
+
View
202 test/model_datatypes.js
@@ -0,0 +1,202 @@
+var utils = require('../lib/utils')
+ , model = require('../lib/model')
+ , assert = require('assert')
+ , tests;
+
+geddy.model = model;
+
+var ByTor = function () {
+ this.property('numberProp', 'number');
+ this.property('intProp', 'int');
+ this.property('objectProp', 'object');
+ this.property('arrayProp', 'array');
+ this.property('dateProp', 'date');
+ this.property('datetimeProp', 'datetime');
+ this.property('timeProp', 'time');
+};
+
+ByTor = geddy.model.register('ByTor', ByTor);
+
+var tests = new (function () {
+ this.testAllOptional = function () {
+ var params = {};
+ var byTor = ByTor.create(params);
+ assert.ok(byTor.isValid());
+ };
+
+ this.testNumber = function () {
+ var byTor;
+ // Actual number, valid
+ byTor = ByTor.create({numberProp: 2112});
+ assert.ok(byTor.isValid());
+
+ // Numeric string, valid
+ byTor = ByTor.create({numberProp: '2112'});
+ assert.ok(byTor.isValid());
+
+ // Non-numeric string, error
+ byTor = ByTor.create({numberProp: 'Snow Dog'});
+ assert.notStrictEqual(byTor.errors.numberProp, undefined);
+
+ };
+
+ this.testInt = function () {
+ var byTor;
+ // Actual int, valid
+ byTor = ByTor.create({intProp: 2112});
+ assert.ok(byTor.isValid());
+
+ // Actual int, valid
+ byTor = ByTor.create({intProp: '2112'});
+ assert.ok(byTor.isValid());
+
+ // Float with zero decimal, valid
+ byTor = ByTor.create({intProp: 2112.0});
+ assert.ok(byTor.isValid());
+
+ // Float with greater-than-zero decimal, error
+ byTor = ByTor.create({intProp: 2112.2112});
+ assert.notStrictEqual(byTor.errors.intProp, undefined);
+
+ // Non-numeric string, error
+ byTor = ByTor.create({intProp: 'away from here'});
+ assert.notStrictEqual(byTor.errors.intProp, undefined);
+
+ };
+
+ this.testObject = function () {
+ var byTor;
+ // Actual Object, valid
+ byTor = ByTor.create({objectProp: {}});
+ assert.ok(byTor.isValid());
+
+ // Sure, technically Arrays are Objects, but this still isn't right
+ byTor = ByTor.create({objectProp: []});
+ assert.notStrictEqual(byTor.errors.objectProp, undefined);
+
+ // string, should fail
+ byTor = ByTor.create({objectProp: 'As gray traces of dawn ...'});
+ assert.notStrictEqual(byTor.errors.objectProp, undefined);
+
+ };
+
+ this.testArray = function () {
+ var byTor;
+ // Actual Array, valid
+ byTor = ByTor.create({arrayProp: []});
+ assert.ok(byTor.isValid());
+
+ // Sure, technically Arrays are Objects, but this still isn't right
+ byTor = ByTor.create({arrayProp: {}});
+ assert.notStrictEqual(byTor.errors.arrayProp, undefined);
+
+ // string, should fail
+ byTor = ByTor.create({arrayProp: 'As gray traces of dawn ...'});
+ assert.notStrictEqual(byTor.errors.arrayProp, undefined);
+
+ };
+
+ this.testDate = function () {
+ var byTor;
+ var dates = [
+ '12/27/1968'
+ , '12-27-1968'
+ , '12.27.1968'
+ , '1968/12/27'
+ , '1968-12-27'
+ , '1968.12.27'
+ , [1968, 12, 27]
+ , new Date(1968, 11, 27)
+ , new Date('12/27/1968')
+ , 'Fri, 27 Dec 1968'
+ ];
+ var dt;
+ for (var i = 0, ii = dates.length; i < ii; i++) {
+ dt = dates[i];
+ byTor = ByTor.create({dateProp: dt});
+ assert.ok(byTor.isValid(), 'Testing valid');
+ assert.equal(byTor.dateProp.getFullYear(), 1968, 'Testing getFullYear');
+ assert.equal(byTor.dateProp.getMonth(), 11, 'Testing getMonth'); // Zero-based
+ assert.equal(byTor.dateProp.getDate(), 27, 'Testing getDate');
+ }
+
+ };
+
+ this.testDatetime = function () {
+ var byTor;
+ var dates, dt;
+ // Dates with no set time -- time should be set to 12 midnight
+ dates = [
+ '1968/12/27'
+ , '1968-12-27'
+ , '1968.12.27'
+ , [1968, 12, 27]
+ , new Date(1968, 11, 27)
+ , new Date('12/27/1968')
+ , 'Fri, 27 Dec 1968'
+ ];
+ for (var i = 0, ii = dates.length; i < ii; i++) {
+ dt = dates[i];
+ byTor = ByTor.create({datetimeProp: dt});
+ assert.ok(byTor.isValid());
+ assert.equal(byTor.datetimeProp.getFullYear(), 1968);
+ assert.equal(byTor.datetimeProp.getMonth(), 11); // Zero-based
+ assert.equal(byTor.datetimeProp.getDate(), 27);
+ assert.equal(byTor.datetimeProp.getHours(), 0);
+ assert.equal(byTor.datetimeProp.getMinutes(), 0);
+ assert.equal(byTor.datetimeProp.getSeconds(), 0);
+ }
+ // Dates with times
+ dates = [
+ '1968-12-27 16:10:03'
+ , [1968, 12, 27, 16, 10, 3]
+ , new Date(1968, 11, 27, 16, 10, 3)
+ , 'Fri, 27 Dec 1968 16:10:03'
+ ];
+ for (var i = 0, ii = dates.length; i < ii; i++) {
+ dt = dates[i];
+ byTor = ByTor.create({datetimeProp: dt});
+ assert.ok(byTor.isValid());
+ assert.equal(byTor.datetimeProp.getFullYear(), 1968);
+ assert.equal(byTor.datetimeProp.getMonth(), 11); // Zero-based
+ assert.equal(byTor.datetimeProp.getDate(), 27);
+ assert.equal(byTor.datetimeProp.getHours(), 16);
+ assert.equal(byTor.datetimeProp.getMinutes(), 10);
+ assert.equal(byTor.datetimeProp.getSeconds(), 3);
+ }
+
+ };
+
+ this.testTime = function () {
+ var byTor;
+ var dates, dt, vals;
+ // Obj key is the input string, value is the list of values
+ // for the parse Date object's h/m/s/ms
+ dates = {
+ '21:12' : [21, 12, 0, 0]
+ , '1:11': [1, 11, 0, 0]
+ , '1:11:03': [1, 11, 3, 0]
+ , '1:11:03.999': [1, 11, 3, 999]
+ };
+ for (var p in dates) {
+ dt = p;
+ vals = dates[p];
+ byTor = ByTor.create({timeProp: dt});
+ assert.ok(byTor.isValid());
+ assert.equal(byTor.timeProp.getHours(), vals[0]);
+ assert.equal(byTor.timeProp.getMinutes(), vals[1]);
+ assert.equal(byTor.timeProp.getSeconds(), vals[2]);
+ assert.equal(byTor.timeProp.getMilliseconds(), vals[3]);
+ }
+
+ };
+
+})();
+
+for (var p in tests) {
+ if (typeof tests[p] == 'function') {
+ tests[p]();
+ }
+}
+
+

0 comments on commit 1c07cec

Please sign in to comment.