Skip to content

Commit

Permalink
testing model validations
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Jan 28, 2011
1 parent 951e835 commit cd45790
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -22,7 +22,7 @@ test: mkdirtmp
operators/test_find_stream operators/test_find_stream_chained \
recovery/collection_recovery_test \
replication/test_master replication/test_slave replication/test_slave_reconnect replication/test_master_temp_roll replication/test_master_seek \
model/test_model_save model/test_model_properties
model/test_create model/test_properties model/test_inspect model/test/validations

benchmark: mkdirtmp mkdirresults
node tools/benchmarks.js benchmark_find benchmark_collection benchmark_collection_filter benchmark_key_map benchmark_key_map_each_with_pos benchmark_indexed_key_map \
Expand Down
16 changes: 12 additions & 4 deletions lib/alfred/meta/model/validator.js
@@ -1,3 +1,11 @@
var defaultErrorMessages = {
"minLength": "is too short",
"maxLength": "is too long",
"optional": "is not optional",
"type": "is of the wrong type",
"enum": "is invalid",
"patten": "is invalid"
};

var Validator = function(object, schema) {
this.errors = [];
Expand Down Expand Up @@ -75,7 +83,7 @@ Validator.prototype._validateProperty = function (property, schema) {
constrain('divisibleBy', value, function (a, e) { return a % e === 0 });
}
} else {
error('type', property, typeof(value), schema);
this.error('type', property, typeof(value), schema);
}

function constrain(name, value, assert) {
Expand All @@ -86,12 +94,12 @@ Validator.prototype._validateProperty = function (property, schema) {
};

Validator.prototype.error = function(attribute, property, actual, schema) {
var message = schema.messages && schema.messages[property] || "no default message";

var message = schema.messages && schema.messages[attribute] || defaultErrorMessages[attribute] || "no default message";
this.errors.push({
attribute: attribute,
property: property,
expected: schema[attribute] || exports.defaultSchema[attribute],
expected: schema[attribute],
actual: actual,
message: message
});
Expand Down
Empty file added test/model/test_atomic.js
Empty file.
File renamed without changes.
Empty file added test/model/test_destroy.js
Empty file.
1 change: 0 additions & 1 deletion test/model/test_doc_inspect.js

This file was deleted.

Empty file added test/model/test_find.js
Empty file.
58 changes: 58 additions & 0 deletions test/model/test_inspect.js
@@ -0,0 +1,58 @@
// Test doc.inspect()

var assert = require('assert')
, fs = require('fs')
, util = require('util');

var DB_PATH = __dirname + '/../../tmp/db';

var USER = {name: 'Pedro', age: 35, sex: 'm'};
var EXPECTED_INSPECT = "{ name: 'Pedro',\n\
age: 35,\n\
sex: 'm',\n\
id: '<<<ID>>>' }";

module.exports.setup = function(next) {
fs.readdirSync(DB_PATH).forEach(function(dir) {
fs.unlinkSync(DB_PATH + '/' + dir);
});
next();
};

module.exports.run = function(next) {
var alfred = require('../../lib/alfred');

var timeout = setTimeout(function() {
throw new Error('timeout');
}, 5000);

alfred.open(DB_PATH, function(err, db) {
if (err) { next(err); return; }

db.on('error', function(err) {
next(err);
});

var User = db.define('User');
User.property('name');
User.property('age', Number);
User.property('sex', 'string', {
required: true,
minimum: 1,
maximum: 1
});

var user = User.new(USER);
user.save(function(errors) {
if (errors) { next(new Error('validation errors: ' + util.inspect(errors))); return; }
User.get(user.id, function(gotUser) {
assert.ok(user.equal(gotUser), 'user ('+user.toString()+') and gotten user ('+gotUser.toString()+') are different');
EXPECTED_INSPECT = EXPECTED_INSPECT.replace('<<<ID>>>', user.id);
assert.equal(gotUser.inspect(), EXPECTED_INSPECT);
clearTimeout(timeout);
db.close(next);
});
});

});
};
File renamed without changes.
101 changes: 101 additions & 0 deletions test/model/test_validations.js
@@ -0,0 +1,101 @@
var assert = require('assert')
, fs = require('fs')
, util = require('util');

var DB_PATH = __dirname + '/../../tmp/db';

var WRONG_USER = {name: 'Pedro Gonçalves Teixeira', sex: 'k', address: 'a', married: 1, email: 'pupup@gugu'};
var EXPECTED_ERRORS = [
{ attribute: 'maxLength',
property: 'name',
expected: 10,
actual: 24,
message: 'is too long' },
{ attribute: 'minLength',
property: 'address',
expected: 3,
actual: 1,
message: 'is too short' },
{ attribute: 'optional',
property: 'age',
expected: undefined,
actual: true,
message: 'is not optional' },
{ attribute: 'type',
property: 'age',
expected: 'number',
actual: 'undefined',
message: 'is of the wrong type' },
{ attribute: 'enum',
property: 'sex',
expected: [ 'f', 'm' ],
actual: 'k',
message: 'is invalid' },
{ attribute: 'type',
property: 'married',
expected: 'boolean',
actual: 'number',
message: 'is of the wrong type' },
{ attribute: 'pattern',
property: 'email',
expected: /^([a-zA-Z0-9_\.-])+@(([a-zA-Z0-9-])+\.)+[a-zA-Z0-9]{2,4}/,
actual: 'pupup@gugu',
message: 'IINNVVAALLIID' }
];

var RIGHT_USER = {name: 'Pedro', age: 30, sex: 'm', address: 'Estrada 1', email: 'pupup@gugu.co.uk', married: true};

module.exports.setup = function(next) {
fs.readdirSync(DB_PATH).forEach(function(dir) {
fs.unlinkSync(DB_PATH + '/' + dir);
});
next();
};

module.exports.run = function(next) {
var alfred = require('../../lib/alfred');

var timeout = setTimeout(function() {
throw new Error('timeout');
}, 5000);

alfred.open(DB_PATH, function(err, db) {
if (err) { next(err); return; }

db.on('error', function(err) {
next(err);
});

var User = db.define('User');
User.property('name', 'string', {
minLength: 3,
maxLength: 10
});
User.property('address', 'string', {
minLength: 3,
maxLength: 10
});
User.property('age', Number, { required: true });
User.property('sex', 'string', {
enum: ['f', 'm'],
required: true,
});
User.property('married', 'boolean');
User.property('email', 'string', {
pattern: /^([a-zA-Z0-9_\.-])+@(([a-zA-Z0-9-])+\.)+[a-zA-Z0-9]{2,4}/,
messages: {pattern: 'IINNVVAALLIID'}
});

var user = User.new(WRONG_USER);
user.save(function(errors) {
assert.ok(errors !== null, 'errors is null');
assert.deepEqual(errors, EXPECTED_ERRORS);
var rightUser = User.new(RIGHT_USER);
rightUser.save(function(errors) {
assert.ok(errors === null, util.inspect(errors) + " is not null");
db.close(next);
});
});

});
};

0 comments on commit cd45790

Please sign in to comment.