Permalink
Browse files

testing model properties

  • Loading branch information...
1 parent d02e871 commit 951e83594671ad4912a7fb9c1a04beb5b1c08457 @pgte committed Jan 28, 2011
View
@@ -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_save model/test_model_properties
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 \
@@ -62,10 +62,12 @@ Document.prototype.reload = function(callback) {
});
};
-Document.prototype.toString = function() {
+Document.prototype.inspect = function() {
return util.inspect(this.doc);
};
+Document.prototype.toString = Document.prototype.inspect;
+
Document.prototype.equal = function(obj) {
if (!obj) {
console.log('B');
@@ -145,11 +145,13 @@ Model.prototype.find = function(query) {
};
Model.prototype.instantiate = function(doc) {
- var doc = Document.create(this, doc);
+ var doc = Document.create(this, doc || {});
doc.__proto__ = this.protoBase;
return doc;
};
+Model.prototype.new = Model.prototype.instantiate;
+
Model.prototype.property = function(name, typeOrSchema, schema) {
var definer = {};
var type = (function () {
@@ -1 +1,53 @@
-// Test if when saving a document only the declard properties get saved
+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 UNKNOWN_PROP_NAME = 'unknownProperty';
+
+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[UNKNOWN_PROP_NAME] = 'abc';
+ 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');
+ assert.ok(gotUser[UNKNOWN_PROP_NAME] === undefined);
+ clearTimeout(timeout);
+ db.close(next);
+ });
+ });
+
+ });
+};
@@ -48,9 +48,9 @@ module.exports.run = function(next) {
var users = [];
USERS.forEach(function(USER) {
- var user = User.instantiate(USER);
+ var user = User.new(USER);
user.save(function(errors) {
- if (errors) { next(new Error('validation errors: ' + util.inspect(errors))); }
+ if (errors) { next(new Error('validation errors: ' + util.inspect(errors))); return; }
users.push(user);
if (-- left === 0) {
left = users.length;
@@ -59,7 +59,7 @@ module.exports.run = function(next) {
assert.ok(user.equal(gotUser), 'user ('+user.toString()+') and gotten user ('+gotUser.toString()+') are different');
if (-- left === 0) {
clearTimeout(timeout);
- next();
+ db.close(next)
}
});
});

0 comments on commit 951e835

Please sign in to comment.