Skip to content

Commit

Permalink
New tests for model/validations.
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Apr 26, 2010
1 parent 740444f commit a755d47
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
69 changes: 69 additions & 0 deletions geddy-model/test/create_user.js
@@ -0,0 +1,69 @@

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
};

model.registerModel('User');

var testCreateUser = new function () {
var _params = {
login: 'zzz',
password: 'asdf',
confirmPassword: 'asdf',
firstName: 'Neil'
};

//this.setup = function () {};

this.testValid = function () {
var user = User.create(_params);
jum.assertTrue(user.valid());
};

this.testShortLogin = function () {
_params.login = 'zz'; // Too short, invalid
var user = User.create(_params);
jum.assertNotUndefined(user.errors.login);
};

this.testInvalidLoginWithCustomMessage = function () {
_params.login = '2112'; // Contains numbers, invalid
var user = User.create(_params);
// Error message should be customized
jum.assertEquals(user.errors.login, 'Subdivisions!');
};

this.testNoLogin = function () {
delete _params.login; // Contains numbers, invalid
var user = User.create(_params);
// Error message should be customized
jum.assertNotUndefined(user.errors.login);

_params.login = 'zzz'; // Restore to something valid
};

this.testNoConfirmPassword = function () {
_params.confirmPassword = 'fdsa';
var user = User.create(_params);
// Error message should be customized
jum.assertNotUndefined(user.errors.password);

_params.confirmPassword = 'asdf'; // Restore to something valid
};

//this.teardown = function () {};

}();

91 changes: 91 additions & 0 deletions geddy-model/test/datatypes.js
@@ -0,0 +1,91 @@

var ByTor = function () {
this.property('rush', 'Number');
this.property('flyByNight', 'int');
this.property('caressOfSteel', 'Object');
this.property('farewellToKings', 'Array');
};

model.registerModel('ByTor');

var testDatatypes = new function () {
this.testAllOptional = function () {
var params = {};
var byTor = ByTor.create(params);
jum.assertTrue(byTor.valid());
};

this.testNumber = function () {
var byTor;
// Actual number, valid
byTor = ByTor.create({rush: 2112});
jum.assertTrue(byTor.valid());

// Numeric string, valid
byTor = ByTor.create({rush: '2112'});
jum.assertTrue(byTor.valid());

// Non-numeric string, error
byTor = ByTor.create({rush: 'Snow Dog'});
jum.assertNotUndefined(byTor.errors.rush);

};

this.testInt = function () {
var byTor;
// Actual int, valid
byTor = ByTor.create({flyByNight: 2112});
jum.assertTrue(byTor.valid());

// Actual int, valid
byTor = ByTor.create({flyByNight: '2112'});
jum.assertTrue(byTor.valid());

// Float with zero decimal, valid
byTor = ByTor.create({flyByNight: 2112.0});
jum.assertTrue(byTor.valid());

// Float with greater-than-zero decimal, error
byTor = ByTor.create({flyByNight: 2112.2112});
jum.assertNotUndefined(byTor.errors.flyByNight);

// Non-numeric string, error
byTor = ByTor.create({flyByNight: 'away from here'});
jum.assertNotUndefined(byTor.errors.flyByNight);

};

this.testObject = function () {
var byTor;
// Actual Object, valid
byTor = ByTor.create({caressOfSteel: {}});
jum.assertTrue(byTor.valid());

// Sure, technically Arrays are Objects, but this still isn't right
byTor = ByTor.create({caressOfSteel: []});
jum.assertNotUndefined(byTor.errors.caressOfSteel);

// String, should fail
byTor = ByTor.create({caressOfSteel: 'As gray traces of dawn ...'});
jum.assertNotUndefined(byTor.errors.caressOfSteel);

};

this.testArray = function () {
var byTor;
// Actual Array, valid
byTor = ByTor.create({farewellToKings: []});
jum.assertTrue(byTor.valid());

// Sure, technically Arrays are Objects, but this still isn't right
byTor = ByTor.create({farewellToKings: {}});
jum.assertNotUndefined(byTor.errors.farewellToKings);

// String, should fail
byTor = ByTor.create({farewellToKings: 'As gray traces of dawn ...'});
jum.assertNotUndefined(byTor.errors.farewellToKings);

};

}();

12 changes: 12 additions & 0 deletions geddy-model/test/index.html
@@ -0,0 +1,12 @@
<html>
<head>
<script type="text/javascript" src="../../geddy-util/lib/meta.js"></script>
<script type="text/javascript" src="../../geddy-util/lib/string.js"></script>
<script type="text/javascript" src="../lib/model.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
</body>
</html>

23 changes: 23 additions & 0 deletions geddy-model/test/initialize.js
@@ -0,0 +1,23 @@
// A useful shortcut for our test code
var wmAsserts = windmill.controller.asserts;

// Use this to set the order you want your tests to run
var registeredTests = [
'testCreateUser',
'testDatatypes'
];

// Register top-level test namespaces in the order
// we want to run them
windmill.jsTest.register(registeredTests);

// Set this to true to run *only* the registered tests
// Defaults to false, so registered tests run first --
// all others run in the order they're found by the parser
windmill.jsTest.runRegisteredTestsOnly = true;

// Pull in the code for all the tests
windmill.jsTest.require('create_user.js');
windmill.jsTest.require('datatypes.js');


0 comments on commit a755d47

Please sign in to comment.