2012-04-18 23:20:44 +00:00
|
|
|
require('./spec_helper').init(exports);
|
|
|
|
|
2011-10-03 17:18:44 +00:00
|
|
|
var Schema = require('../index').Schema;
|
|
|
|
var Text = Schema.Text;
|
|
|
|
|
|
|
|
var schemas = {
|
2012-03-11 04:48:38 +00:00
|
|
|
// riak: {},
|
2011-10-23 19:43:53 +00:00
|
|
|
mysql: {
|
2012-01-19 20:16:30 +00:00
|
|
|
database: 'myapp_test',
|
2011-10-23 19:43:53 +00:00
|
|
|
username: 'root'
|
|
|
|
},
|
2012-01-30 15:43:45 +00:00
|
|
|
postgres: {
|
2012-01-30 13:33:45 +00:00
|
|
|
database: 'myapp_test',
|
|
|
|
username: 'postgres'
|
2012-01-10 13:26:24 +00:00
|
|
|
},
|
2012-01-30 15:43:45 +00:00
|
|
|
sqlite3: {
|
|
|
|
database: ':memory:'
|
|
|
|
},
|
2011-10-03 17:18:44 +00:00
|
|
|
neo4j: { url: 'http://localhost:7474/' },
|
2012-09-11 19:22:55 +00:00
|
|
|
// mongoose: { url: 'mongodb://travis:test@localhost:27017/myapp' },
|
2012-03-16 14:42:02 +00:00
|
|
|
mongodb: { url: 'mongodb://travis:test@localhost:27017/myapp' },
|
2012-09-08 13:03:49 +00:00
|
|
|
redis2: {},
|
2012-06-10 11:41:07 +00:00
|
|
|
memory: {},
|
|
|
|
cradle: {}
|
2011-10-03 17:18:44 +00:00
|
|
|
};
|
|
|
|
|
2011-10-16 17:21:08 +00:00
|
|
|
var specificTest = getSpecificTests();
|
2012-08-13 06:16:01 +00:00
|
|
|
var testPerformed = false;
|
2011-10-16 17:21:08 +00:00
|
|
|
|
2011-10-03 17:18:44 +00:00
|
|
|
Object.keys(schemas).forEach(function (schemaName) {
|
|
|
|
if (process.env.ONLY && process.env.ONLY !== schemaName) return;
|
2012-01-30 19:45:06 +00:00
|
|
|
if (process.env.EXCEPT && ~process.env.EXCEPT.indexOf(schemaName)) return;
|
2012-08-13 06:16:01 +00:00
|
|
|
performTestFor(schemaName);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (process.env.ONLY && !testPerformed) {
|
|
|
|
performTestFor(process.env.ONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
function performTestFor(schemaName) {
|
2012-08-17 18:48:14 +00:00
|
|
|
testPerformed = true;
|
2011-10-03 17:18:44 +00:00
|
|
|
context(schemaName, function () {
|
2012-08-13 06:16:01 +00:00
|
|
|
var schema = new Schema(schemaName, schemas[schemaName] || {});
|
2012-03-11 12:42:07 +00:00
|
|
|
|
2012-03-11 04:48:38 +00:00
|
|
|
it('should connect to database', function (test) {
|
2012-03-11 12:42:07 +00:00
|
|
|
if (schema.connected) return test.done();
|
|
|
|
schema.on('connected', test.done);
|
2012-03-11 04:48:38 +00:00
|
|
|
});
|
|
|
|
|
2012-02-01 17:33:08 +00:00
|
|
|
schema.log = function (a) {
|
2012-03-22 20:24:15 +00:00
|
|
|
console.log(a);
|
2012-02-01 17:33:08 +00:00
|
|
|
};
|
2012-03-22 20:24:15 +00:00
|
|
|
|
2011-10-03 17:18:44 +00:00
|
|
|
testOrm(schema);
|
2011-10-16 17:21:08 +00:00
|
|
|
if (specificTest[schemaName]) specificTest[schemaName](schema);
|
2011-10-03 17:18:44 +00:00
|
|
|
});
|
2012-08-13 06:16:01 +00:00
|
|
|
}
|
2011-10-03 17:18:44 +00:00
|
|
|
|
|
|
|
function testOrm(schema) {
|
|
|
|
|
2012-03-01 17:26:52 +00:00
|
|
|
var Post, User, Passport;
|
2011-10-03 17:18:44 +00:00
|
|
|
var start = Date.now();
|
|
|
|
|
|
|
|
it('should define class', function (test) {
|
|
|
|
|
2011-10-05 14:47:26 +00:00
|
|
|
User = schema.define('User', {
|
2012-05-29 11:16:24 +00:00
|
|
|
name: { type: String, index: true },
|
|
|
|
email: { type: String, index: true },
|
2011-10-03 17:18:44 +00:00
|
|
|
bio: Text,
|
|
|
|
approved: Boolean,
|
|
|
|
joinedAt: Date,
|
2011-12-11 11:01:23 +00:00
|
|
|
age: Number,
|
2012-08-13 06:16:01 +00:00
|
|
|
passwd: { type: String, index: true },
|
2012-09-27 07:24:20 +00:00
|
|
|
settings: { type: Schema.JSON },
|
2012-05-14 04:28:37 +00:00
|
|
|
extra: Object
|
2011-10-03 17:18:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Post = schema.define('Post', {
|
2011-10-16 17:21:08 +00:00
|
|
|
title: { type: String, length: 255, index: true },
|
2012-05-14 04:28:37 +00:00
|
|
|
subject: { type: String },
|
2011-10-03 17:18:44 +00:00
|
|
|
content: { type: Text },
|
2012-09-08 13:03:49 +00:00
|
|
|
date: { type: Date, default: function () { return new Date }, index: true },
|
2012-09-10 15:57:21 +00:00
|
|
|
published: { type: Boolean, default: false },
|
|
|
|
likes: [],
|
|
|
|
related: [RelatedPost]
|
2012-03-07 07:29:08 +00:00
|
|
|
}, {table: 'posts'});
|
2011-10-03 17:18:44 +00:00
|
|
|
|
2012-09-10 15:57:21 +00:00
|
|
|
function RelatedPost() { }
|
|
|
|
RelatedPost.prototype.someMethod = function () {
|
|
|
|
return this.parent;
|
|
|
|
};
|
|
|
|
|
2011-11-19 08:58:49 +00:00
|
|
|
Post.validateAsync('title', function (err, done) {
|
|
|
|
process.nextTick(done);
|
|
|
|
});
|
|
|
|
|
2011-10-05 14:47:26 +00:00
|
|
|
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
|
|
|
|
// creates instance methods:
|
|
|
|
// user.posts(conds)
|
2011-10-15 15:57:35 +00:00
|
|
|
// user.posts.build(data) // like new Post({userId: user.id});
|
|
|
|
// user.posts.create(data) // build and save
|
|
|
|
// user.posts.find
|
2011-10-05 14:47:26 +00:00
|
|
|
|
2011-12-09 15:23:29 +00:00
|
|
|
// User.hasOne('latestPost', {model: Post, foreignKey: 'postId'});
|
|
|
|
|
|
|
|
// User.hasOne(Post, {as: 'latestPost', foreignKey: 'latestPostId'});
|
|
|
|
// creates instance methods:
|
|
|
|
// user.latestPost()
|
|
|
|
// user.latestPost.build(data)
|
|
|
|
// user.latestPost.create(data)
|
|
|
|
|
2011-10-05 14:47:26 +00:00
|
|
|
Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
|
|
|
|
// creates instance methods:
|
|
|
|
// post.author(callback) -- getter when called with function
|
|
|
|
// post.author() -- sync getter when called without params
|
|
|
|
// post.author(user) -- setter when called with object
|
|
|
|
|
2012-03-01 17:26:52 +00:00
|
|
|
Passport = schema.define('Passport', {
|
|
|
|
number: String
|
|
|
|
});
|
|
|
|
|
|
|
|
Passport.belongsTo(User, {as: 'owner', foreignKey: 'ownerId'});
|
|
|
|
|
2011-10-03 17:18:44 +00:00
|
|
|
var user = new User;
|
|
|
|
|
|
|
|
test.ok(User instanceof Function);
|
|
|
|
|
|
|
|
// class methods
|
|
|
|
test.ok(User.find instanceof Function);
|
|
|
|
test.ok(User.create instanceof Function);
|
|
|
|
|
|
|
|
// instance methods
|
|
|
|
test.ok(user.save instanceof Function);
|
|
|
|
|
|
|
|
schema.automigrate(function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.log('Error while migrating');
|
|
|
|
console.log(err);
|
|
|
|
} else {
|
|
|
|
test.done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should initialize object properly', function (test) {
|
2011-10-11 19:51:32 +00:00
|
|
|
var hw = 'Hello word',
|
|
|
|
now = Date.now(),
|
2012-01-09 12:59:58 +00:00
|
|
|
post = new Post({title: hw}),
|
|
|
|
anotherPost = Post({title: 'Resig style constructor'});
|
2011-10-11 19:51:32 +00:00
|
|
|
|
2011-10-03 17:18:44 +00:00
|
|
|
test.equal(post.title, hw);
|
2012-10-13 13:59:25 +00:00
|
|
|
test.ok(!post.propertyChanged('title'), 'property changed: title');
|
2011-10-03 17:18:44 +00:00
|
|
|
post.title = 'Goodbye, Lenin';
|
|
|
|
test.equal(post.title_was, hw);
|
|
|
|
test.ok(post.propertyChanged('title'));
|
2011-10-11 19:51:32 +00:00
|
|
|
test.strictEqual(post.published, false);
|
|
|
|
test.ok(post.date >= now);
|
|
|
|
test.ok(post.isNewRecord());
|
2012-01-09 12:59:58 +00:00
|
|
|
test.ok(anotherPost instanceof Post);
|
|
|
|
test.ok(anotherPost.title, 'Resig style constructor');
|
2011-10-03 17:18:44 +00:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be expoted to JSON', function (test) {
|
2011-10-11 19:51:32 +00:00
|
|
|
test.equal(JSON.stringify(new Post({id: 1, title: 'hello, json', date: 1})),
|
2012-10-13 13:59:25 +00:00
|
|
|
'{"title":"hello, json","subject":null,"content":null,"date":1,"published":false,"likes":[],"related":[],"id":1,"userId":null}');
|
2011-10-03 17:18:44 +00:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create object', function (test) {
|
|
|
|
Post.create(function (err, post) {
|
|
|
|
if (err) throw err;
|
2012-01-10 15:43:32 +00:00
|
|
|
test.ok(post.id, 'Id present');
|
2011-10-03 17:18:44 +00:00
|
|
|
test.ok(!post.title, 'Title is blank');
|
|
|
|
Post.exists(post.id, function (err, exists) {
|
|
|
|
if (err) throw err;
|
|
|
|
test.ok(exists);
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-01-27 08:48:37 +00:00
|
|
|
it('should create object without callback', function (test) {
|
|
|
|
var uniqueTitle = 'Unique title ' + Date.now();
|
|
|
|
Post.create({title: uniqueTitle});
|
|
|
|
|
|
|
|
setTimeout(delayedCallback, 100);
|
|
|
|
|
|
|
|
function delayedCallback() {
|
|
|
|
Post.all({where: {title: uniqueTitle}}, function (err, posts) {
|
|
|
|
test.equal(posts.length, 1);
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-10-03 17:18:44 +00:00
|
|
|
it('should save object', function (test) {
|
|
|
|
var title = 'Initial title', title2 = 'Hello world',
|
|
|
|
date = new Date;
|
|
|
|
|
|
|
|
Post.create({
|
|
|
|
title: title,
|
|
|
|
date: date
|
|
|
|
}, function (err, obj) {
|
2011-10-23 19:43:53 +00:00
|
|
|
test.ok(obj.id, 'Object id should present');
|
2011-10-03 17:18:44 +00:00
|
|
|
test.equals(obj.title, title);
|
|
|
|
// test.equals(obj.date, date);
|
|
|
|
obj.title = title2;
|
|
|
|
test.ok(obj.propertyChanged('title'), 'Title changed');
|
|
|
|
obj.save(function (err, obj) {
|
|
|
|
test.equal(obj.title, title2);
|
|
|
|
test.ok(!obj.propertyChanged('title'));
|
2012-03-03 09:55:29 +00:00
|
|
|
|
|
|
|
var p = new Post({title: 1});
|
|
|
|
p.title = 2;
|
|
|
|
p.save(function (err, obj) {
|
|
|
|
test.ok(!p.propertyChanged('title'));
|
|
|
|
p.title = 3;
|
|
|
|
test.ok(p.propertyChanged('title'));
|
|
|
|
test.equal(p.title_was, 2);
|
|
|
|
p.save(function () {
|
|
|
|
test.equal(p.title_was, 3);
|
|
|
|
test.ok(!p.propertyChanged('title'));
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
2011-10-03 17:18:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create object with initial data', function (test) {
|
|
|
|
var title = 'Initial title',
|
2012-01-18 18:49:30 +00:00
|
|
|
date = new Date;
|
2011-10-03 17:18:44 +00:00
|
|
|
|
|
|
|
Post.create({
|
|
|
|
title: title,
|
|
|
|
date: date
|
|
|
|
}, function (err, obj) {
|
|
|
|
test.ok(obj.id);
|
|
|
|
test.equals(obj.title, title);
|
|
|
|
test.equals(obj.date, date);
|
|
|
|
Post.find(obj.id, function () {
|
|
|
|
test.equal(obj.title, title);
|
2011-10-23 19:43:53 +00:00
|
|
|
test.equal(obj.date.toString(), date.toString());
|
2011-10-03 17:18:44 +00:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-12-11 08:58:34 +00:00
|
|
|
it('should save only schema-defined field in database', function (test) {
|
|
|
|
Post.create({title: '1602', nonSchemaField: 'some value'}, function (err, post) {
|
|
|
|
test.ok(!post.nonSchemaField);
|
|
|
|
post.a = 1;
|
|
|
|
post.save(function () {
|
|
|
|
test.ok(post.a);
|
|
|
|
post.reload(function (err, psto) {
|
2012-04-11 15:36:10 +00:00
|
|
|
test.ok(!psto.a);
|
2011-12-11 08:58:34 +00:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-04-11 15:36:10 +00:00
|
|
|
/*
|
2011-10-03 17:18:44 +00:00
|
|
|
it('should not create new instances for the same object', function (test) {
|
|
|
|
var title = 'Initial title';
|
|
|
|
Post.create({ title: title }, function (err, post) {
|
|
|
|
test.ok(post.id, 'Object should have id');
|
|
|
|
test.equals(post.title, title);
|
|
|
|
Post.find(post.id, function (err, foundPost) {
|
|
|
|
if (err) throw err;
|
|
|
|
test.equal(post.title, title);
|
|
|
|
test.strictEqual(post, foundPost);
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2012-04-11 15:36:10 +00:00
|
|
|
*/
|
2011-10-03 17:18:44 +00:00
|
|
|
|
|
|
|
it('should not re-instantiate object on saving', function (test) {
|
|
|
|
var title = 'Initial title';
|
|
|
|
var post = new Post({title: title});
|
|
|
|
post.save(function (err, savedPost) {
|
|
|
|
test.strictEqual(post, savedPost);
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should destroy object', function (test) {
|
|
|
|
Post.create(function (err, post) {
|
|
|
|
Post.exists(post.id, function (err, exists) {
|
|
|
|
test.ok(exists, 'Object exists');
|
|
|
|
post.destroy(function () {
|
|
|
|
Post.exists(post.id, function (err, exists) {
|
2011-10-23 19:43:53 +00:00
|
|
|
if (err) console.log(err);
|
2011-10-03 17:18:44 +00:00
|
|
|
test.ok(!exists, 'Hey! ORM told me that object exists, but it looks like it doesn\'t. Something went wrong...');
|
|
|
|
Post.find(post.id, function (err, obj) {
|
|
|
|
test.equal(obj, null, 'Param obj should be null');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-12-11 11:01:23 +00:00
|
|
|
it('should handle virtual attributes', function (test) {
|
|
|
|
var salt = 's0m3s3cr3t5a1t';
|
|
|
|
|
2011-12-11 19:25:13 +00:00
|
|
|
User.setter.passwd = function (password) {
|
|
|
|
this._passwd = calcHash(password, salt);
|
2011-12-11 11:01:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function calcHash(pass, salt) {
|
|
|
|
var crypto = require('crypto');
|
|
|
|
var hash = crypto.createHash('sha256');
|
|
|
|
hash.update(pass);
|
|
|
|
hash.update(salt);
|
|
|
|
return hash.digest('base64');
|
|
|
|
}
|
|
|
|
|
|
|
|
var u = new User;
|
2011-12-11 19:25:13 +00:00
|
|
|
u.passwd = 's3cr3t';
|
|
|
|
test.equal(u.passwd, calcHash('s3cr3t', salt));
|
2011-12-11 11:01:23 +00:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
|
2012-08-14 14:47:59 +00:00
|
|
|
// it('should serialize JSON type', function (test) {
|
|
|
|
// User.create({settings: {hello: 'world'}}, function (err, user) {
|
|
|
|
// test.ok(user.id);
|
|
|
|
// test.equal(user.settings.hello, 'world');
|
|
|
|
// User.find(user.id, function (err, u) {
|
|
|
|
// console.log(u.settings);
|
|
|
|
// test.equal(u.settings.hello, 'world');
|
|
|
|
// test.done();
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
2012-05-29 11:16:24 +00:00
|
|
|
|
2011-10-03 17:18:44 +00:00
|
|
|
it('should update single attribute', function (test) {
|
|
|
|
Post.create({title: 'title', content: 'content', published: true}, function (err, post) {
|
|
|
|
post.content = 'New content';
|
|
|
|
post.updateAttribute('title', 'New title', function () {
|
|
|
|
test.equal(post.title, 'New title');
|
|
|
|
test.ok(!post.propertyChanged('title'));
|
2012-10-13 13:59:25 +00:00
|
|
|
console.log('hahaha', post.content, post.__data.content);
|
2011-11-27 06:08:40 +00:00
|
|
|
test.equal(post.content, 'New content', 'dirty state saved');
|
2011-10-03 17:18:44 +00:00
|
|
|
test.ok(post.propertyChanged('content'));
|
2012-04-11 15:36:10 +00:00
|
|
|
post.reload(function (err, post) {
|
2011-10-03 17:18:44 +00:00
|
|
|
test.equal(post.title, 'New title');
|
2012-04-11 19:31:10 +00:00
|
|
|
test.ok(!post.propertyChanged('title'), 'title not changed');
|
2012-08-13 06:16:01 +00:00
|
|
|
console.log(post.content);
|
2011-11-27 06:08:40 +00:00
|
|
|
test.equal(post.content, 'content', 'real value turned back');
|
|
|
|
test.ok(!post.propertyChanged('content'), 'content unchanged');
|
2011-10-03 17:18:44 +00:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-01-30 13:27:26 +00:00
|
|
|
var countOfposts, countOfpostsFiltered;
|
2011-10-03 17:18:44 +00:00
|
|
|
it('should fetch collection', function (test) {
|
|
|
|
Post.all(function (err, posts) {
|
|
|
|
countOfposts = posts.length;
|
|
|
|
test.ok(countOfposts > 0);
|
|
|
|
test.ok(posts[0] instanceof Post);
|
2012-01-30 13:27:26 +00:00
|
|
|
countOfpostsFiltered = posts.filter(function (p) {
|
2012-09-08 13:03:49 +00:00
|
|
|
console.log(p.title);
|
2012-01-30 13:27:26 +00:00
|
|
|
return p.title === 'title';
|
|
|
|
}).length;
|
2011-10-03 17:18:44 +00:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fetch count of records in collection', function (test) {
|
|
|
|
Post.count(function (err, count) {
|
2012-09-08 13:03:49 +00:00
|
|
|
console.log(countOfposts, count);
|
|
|
|
test.equal(countOfposts, count, 'unfiltered count');
|
2012-01-30 13:27:26 +00:00
|
|
|
Post.count({title: 'title'}, function (err, count) {
|
2012-09-08 13:03:49 +00:00
|
|
|
console.log(countOfpostsFiltered, count, 'filtered count');
|
2012-03-11 04:48:38 +00:00
|
|
|
test.equal(countOfpostsFiltered, count, 'filtered count');
|
2012-01-30 13:27:26 +00:00
|
|
|
test.done();
|
|
|
|
});
|
2011-10-03 17:18:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find filtered set of records', function (test) {
|
2012-09-08 13:03:49 +00:00
|
|
|
var wait = 1;
|
2011-10-03 17:18:44 +00:00
|
|
|
|
|
|
|
// exact match with string
|
2011-11-04 07:30:25 +00:00
|
|
|
Post.all({where: {title: 'New title'}}, function (err, res) {
|
2011-10-03 17:18:44 +00:00
|
|
|
var pass = true;
|
|
|
|
res.forEach(function (r) {
|
|
|
|
if (r.title != 'New title') pass = false;
|
|
|
|
});
|
2012-01-13 20:06:57 +00:00
|
|
|
test.ok(res.length > 0, 'Exact match with string returns dataset');
|
2011-10-03 17:18:44 +00:00
|
|
|
test.ok(pass, 'Exact match with string');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
// matching null
|
2012-09-08 13:03:49 +00:00
|
|
|
// Post.all({where: {title: null}}, function (err, res) {
|
2012-01-10 15:43:32 +00:00
|
|
|
|
2012-09-08 13:03:49 +00:00
|
|
|
// var pass = true;
|
|
|
|
// res.forEach(function (r) {
|
|
|
|
// if (r.title != null) pass = false;
|
|
|
|
// });
|
|
|
|
// test.ok(res.length > 0, 'Matching null returns dataset');
|
|
|
|
// test.ok(pass, 'Matching null');
|
|
|
|
// done();
|
|
|
|
// });
|
2011-10-03 17:18:44 +00:00
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (--wait === 0) {
|
|
|
|
test.done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2012-10-09 18:19:38 +00:00
|
|
|
|
|
|
|
it('should find records filtered with multiple attributes', function (test) {
|
|
|
|
Post.create({title: 'title', content: 'content', published: true, date: 1}, function (err, post) {
|
|
|
|
Post.all({where: {title: 'title', date: 1}}, function (err, res) {
|
|
|
|
test.ok(res.length > 0, 'Exact match with string returns dataset');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-10-05 14:47:26 +00:00
|
|
|
it('should handle hasMany relationship', function (test) {
|
|
|
|
User.create(function (err, u) {
|
|
|
|
if (err) return console.log(err);
|
|
|
|
test.ok(u.posts, 'Method defined: posts');
|
2011-10-15 15:57:35 +00:00
|
|
|
test.ok(u.posts.build, 'Method defined: posts.build');
|
|
|
|
test.ok(u.posts.create, 'Method defined: posts.create');
|
|
|
|
u.posts.create(function (err, post) {
|
2011-10-05 14:47:26 +00:00
|
|
|
if (err) return console.log(err);
|
2011-11-04 07:30:25 +00:00
|
|
|
// test.ok(post.author(), u.id);
|
2011-10-05 14:47:26 +00:00
|
|
|
u.posts(function (err, posts) {
|
2012-04-11 15:52:46 +00:00
|
|
|
test.equal(posts.pop().id, post.id);
|
2011-10-05 14:47:26 +00:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-12-09 15:23:29 +00:00
|
|
|
// it('should handle hasOne relationship', function (test) {
|
|
|
|
// User.create(function (err, u) {
|
|
|
|
// if (err) return console.log(err);
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
|
2011-10-15 15:57:35 +00:00
|
|
|
it('should support scopes', function (test) {
|
|
|
|
var wait = 2;
|
|
|
|
|
|
|
|
test.ok(Post.scope, 'Scope supported');
|
2011-11-04 07:30:25 +00:00
|
|
|
Post.scope('published', {where: {published: true}});
|
2011-10-15 15:57:35 +00:00
|
|
|
test.ok(typeof Post.published === 'function');
|
2012-10-13 13:59:25 +00:00
|
|
|
test.ok(Post.published._scope.where.published === true);
|
2011-10-15 15:57:35 +00:00
|
|
|
var post = Post.published.build();
|
|
|
|
test.ok(post.published, 'Can build');
|
|
|
|
test.ok(post.isNewRecord());
|
|
|
|
Post.published.create(function (err, psto) {
|
|
|
|
if (err) return console.log(err);
|
|
|
|
test.ok(psto.published);
|
|
|
|
test.ok(!psto.isNewRecord());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
User.create(function (err, u) {
|
|
|
|
if (err) return console.log(err);
|
|
|
|
test.ok(typeof u.posts.published == 'function');
|
2011-11-04 07:30:25 +00:00
|
|
|
test.ok(u.posts.published._scope.where.published);
|
2012-10-13 13:59:25 +00:00
|
|
|
console.log(u.posts.published._scope);
|
2011-11-04 07:30:25 +00:00
|
|
|
test.equal(u.posts.published._scope.where.userId, u.id);
|
2011-10-15 15:57:35 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (--wait === 0) test.done();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2011-10-03 17:18:44 +00:00
|
|
|
it('should destroy all records', function (test) {
|
|
|
|
Post.destroyAll(function (err) {
|
2011-11-04 07:30:25 +00:00
|
|
|
if (err) {
|
|
|
|
console.log('Error in destroyAll');
|
2012-03-11 04:48:38 +00:00
|
|
|
console.log(err);
|
2011-11-04 07:30:25 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2011-10-03 17:18:44 +00:00
|
|
|
Post.all(function (err, posts) {
|
|
|
|
test.equal(posts.length, 0);
|
|
|
|
Post.count(function (err, count) {
|
|
|
|
test.equal(count, 0);
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-01-18 15:20:05 +00:00
|
|
|
it('should return type of property', function (test) {
|
|
|
|
test.equal(Post.whatTypeName('title'), 'String');
|
|
|
|
test.equal(Post.whatTypeName('content'), 'Text');
|
|
|
|
var p = new Post;
|
|
|
|
test.equal(p.whatTypeName('title'), 'String');
|
|
|
|
test.equal(p.whatTypeName('content'), 'Text');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
|
2012-01-18 18:49:30 +00:00
|
|
|
it('should handle ORDER clause', function (test) {
|
2012-05-14 04:28:37 +00:00
|
|
|
var titles = [ { title: 'Title A', subject: "B" },
|
|
|
|
{ title: 'Title Z', subject: "A" },
|
|
|
|
{ title: 'Title M', subject: "C" },
|
|
|
|
{ title: 'Title A', subject: "A" },
|
|
|
|
{ title: 'Title B', subject: "A" },
|
|
|
|
{ title: 'Title C', subject: "D" }];
|
2012-10-01 08:20:55 +00:00
|
|
|
var isRedis = Post.schema.name === 'redis';
|
2012-05-14 04:28:37 +00:00
|
|
|
var dates = isRedis ? [ 5, 9, 0, 17, 10, 9 ] : [
|
2012-01-19 20:16:30 +00:00
|
|
|
new Date(1000 * 5 ),
|
|
|
|
new Date(1000 * 9),
|
|
|
|
new Date(1000 * 0),
|
|
|
|
new Date(1000 * 17),
|
2012-05-14 04:28:37 +00:00
|
|
|
new Date(1000 * 10),
|
2012-01-19 20:16:30 +00:00
|
|
|
new Date(1000 * 9)
|
|
|
|
];
|
2012-01-18 18:49:30 +00:00
|
|
|
titles.forEach(function (t, i) {
|
2012-05-14 04:28:37 +00:00
|
|
|
Post.create({title: t.title, subject: t.subject, date: dates[i]}, done);
|
2012-01-18 18:49:30 +00:00
|
|
|
});
|
|
|
|
|
2012-01-19 16:18:57 +00:00
|
|
|
var i = 0, tests = 0;
|
2012-01-18 18:49:30 +00:00
|
|
|
function done(err, obj) {
|
|
|
|
if (++i === titles.length) {
|
2012-01-19 20:25:50 +00:00
|
|
|
doFilterAndSortTest();
|
2012-03-01 19:57:48 +00:00
|
|
|
doFilterAndSortReverseTest();
|
2012-01-18 18:49:30 +00:00
|
|
|
doStringTest();
|
|
|
|
doNumberTest();
|
2012-05-14 04:28:37 +00:00
|
|
|
|
|
|
|
if (schema.name == 'mongoose') {
|
|
|
|
doMultipleSortTest();
|
|
|
|
doMultipleReverseSortTest();
|
|
|
|
}
|
2012-01-18 18:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 04:28:37 +00:00
|
|
|
function compare(a, b) {
|
|
|
|
if (a.title < b.title) return -1;
|
|
|
|
if (a.title > b.title) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-18 18:49:30 +00:00
|
|
|
// Post.schema.log = console.log;
|
|
|
|
|
|
|
|
function doStringTest() {
|
2012-01-19 16:18:57 +00:00
|
|
|
tests += 1;
|
2012-01-18 18:49:30 +00:00
|
|
|
Post.all({order: 'title'}, function (err, posts) {
|
2012-01-19 20:16:30 +00:00
|
|
|
if (err) console.log(err);
|
2012-05-14 04:28:37 +00:00
|
|
|
test.equal(posts.length, 6);
|
|
|
|
titles.sort(compare).forEach(function (t, i) {
|
|
|
|
if (posts[i]) test.equal(posts[i].title, t.title);
|
2012-01-18 18:49:30 +00:00
|
|
|
});
|
|
|
|
finished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function doNumberTest() {
|
2012-01-19 16:18:57 +00:00
|
|
|
tests += 1;
|
2012-01-18 18:49:30 +00:00
|
|
|
Post.all({order: 'date'}, function (err, posts) {
|
2012-01-19 20:16:30 +00:00
|
|
|
if (err) console.log(err);
|
2012-05-14 04:28:37 +00:00
|
|
|
test.equal(posts.length, 6);
|
2012-01-18 18:49:30 +00:00
|
|
|
dates.sort(numerically).forEach(function (d, i) {
|
2012-03-11 04:48:38 +00:00
|
|
|
if (posts[i])
|
2012-09-08 13:03:49 +00:00
|
|
|
test.equal(posts[i].date.toString(), d.toString(), 'doNumberTest');
|
2012-01-18 18:49:30 +00:00
|
|
|
});
|
|
|
|
finished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:18:57 +00:00
|
|
|
function doFilterAndSortTest() {
|
|
|
|
tests += 1;
|
2012-09-08 13:03:49 +00:00
|
|
|
Post.all({where: {date: new Date(1000 * 9)}, order: 'title', limit: 3}, function (err, posts) {
|
2012-01-19 20:16:30 +00:00
|
|
|
if (err) console.log(err);
|
2012-10-01 08:20:55 +00:00
|
|
|
console.log(posts.length);
|
2012-01-19 16:18:57 +00:00
|
|
|
test.equal(posts.length, 2, 'Exactly 2 posts returned by query');
|
|
|
|
[ 'Title C', 'Title Z' ].forEach(function (t, i) {
|
|
|
|
if (posts[i]) {
|
2012-09-08 13:03:49 +00:00
|
|
|
test.equal(posts[i].title, t, 'doFilterAndSortTest');
|
2012-01-19 16:18:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
finished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-03-01 19:57:48 +00:00
|
|
|
function doFilterAndSortReverseTest() {
|
|
|
|
tests += 1;
|
2012-09-08 13:03:49 +00:00
|
|
|
Post.all({where: {date: new Date(1000 * 9)}, order: 'title DESC', limit: 3}, function (err, posts) {
|
2012-03-01 19:57:48 +00:00
|
|
|
if (err) console.log(err);
|
|
|
|
test.equal(posts.length, 2, 'Exactly 2 posts returned by query');
|
|
|
|
[ 'Title Z', 'Title C' ].forEach(function (t, i) {
|
|
|
|
if (posts[i]) {
|
2012-09-08 13:03:49 +00:00
|
|
|
test.equal(posts[i].title, t, 'doFilterAndSortReverseTest');
|
2012-03-01 19:57:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
finished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-05-14 04:28:37 +00:00
|
|
|
function doMultipleSortTest() {
|
|
|
|
tests += 1;
|
|
|
|
Post.all({order: "title ASC, subject ASC"}, function(err, posts) {
|
|
|
|
if (err) console.log(err);
|
|
|
|
test.equal(posts.length, 6);
|
|
|
|
test.equal(posts[0].title, "Title A");
|
|
|
|
test.equal(posts[0].subject, "A");
|
|
|
|
test.equal(posts[1].title, "Title A");
|
|
|
|
test.equal(posts[1].subject, "B");
|
|
|
|
test.equal(posts[5].title, "Title Z");
|
|
|
|
finished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function doMultipleReverseSortTest() {
|
|
|
|
tests += 1;
|
|
|
|
Post.all({order: "title ASC, subject DESC"}, function(err, posts) {
|
|
|
|
if (err) console.log(err);
|
|
|
|
test.equal(posts.length, 6);
|
|
|
|
test.equal(posts[0].title, "Title A");
|
|
|
|
test.equal(posts[0].subject, "B");
|
|
|
|
test.equal(posts[1].title,"Title A");
|
|
|
|
test.equal(posts[1].subject, "A");
|
|
|
|
test.equal(posts[5].title, "Title Z");
|
|
|
|
finished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-01-18 18:49:30 +00:00
|
|
|
var fin = 0;
|
|
|
|
function finished() {
|
2012-01-19 16:18:57 +00:00
|
|
|
if (++fin === tests) {
|
2012-01-18 18:49:30 +00:00
|
|
|
test.done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: do mixed test, do real dates tests, ensure that dates stored in UNIX timestamp format
|
|
|
|
|
|
|
|
function numerically(a, b) {
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2012-09-26 05:18:43 +00:00
|
|
|
if (
|
|
|
|
!schema.name.match(/redis/) &&
|
|
|
|
schema.name !== 'memory' &&
|
|
|
|
schema.name !== 'neo4j' &&
|
|
|
|
schema.name !== 'cradle'
|
|
|
|
)
|
2012-02-01 17:33:08 +00:00
|
|
|
it('should allow advanced queying: lt, gt, lte, gte, between', function (test) {
|
|
|
|
Post.destroyAll(function () {
|
|
|
|
Post.create({date: new Date('Wed, 01 Feb 2012 13:56:12 GMT')}, done);
|
|
|
|
Post.create({date: new Date('Thu, 02 Feb 2012 13:56:12 GMT')}, done);
|
|
|
|
Post.create({date: new Date('Fri, 03 Feb 2012 13:56:12 GMT')}, done);
|
|
|
|
Post.create({date: new Date('Sat, 04 Feb 2012 13:56:12 GMT')}, done);
|
|
|
|
Post.create({date: new Date('Sun, 05 Feb 2012 13:56:12 GMT')}, done);
|
|
|
|
Post.create({date: new Date('Mon, 06 Feb 2012 13:56:12 GMT')}, done);
|
|
|
|
Post.create({date: new Date('Tue, 07 Feb 2012 13:56:12 GMT')}, done);
|
|
|
|
Post.create({date: new Date('Wed, 08 Feb 2012 13:56:12 GMT')}, done);
|
|
|
|
Post.create({date: new Date('Thu, 09 Feb 2012 13:56:12 GMT')}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
var posts = 9;
|
|
|
|
function done() {
|
|
|
|
if (--posts === 0) makeTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeTest() {
|
|
|
|
// gt
|
|
|
|
Post.all({where: {date: {gt: new Date('Tue, 07 Feb 2012 13:56:12 GMT')}}}, function (err, posts) {
|
2012-03-10 11:56:23 +00:00
|
|
|
test.equal(posts.length, 2, 'gt');
|
2012-02-01 17:33:08 +00:00
|
|
|
ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
// gte
|
|
|
|
Post.all({where: {date: {gte: new Date('Tue, 07 Feb 2012 13:56:12 GMT')}}}, function (err, posts) {
|
2012-03-10 11:56:23 +00:00
|
|
|
test.equal(posts.length, 3, 'gte');
|
2012-02-01 17:33:08 +00:00
|
|
|
ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
// lte
|
|
|
|
Post.all({where: {date: {lte: new Date('Tue, 07 Feb 2012 13:56:12 GMT')}}}, function (err, posts) {
|
2012-03-10 11:56:23 +00:00
|
|
|
test.equal(posts.length, 7, 'lte');
|
2012-02-01 17:33:08 +00:00
|
|
|
ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
// lt
|
|
|
|
Post.all({where: {date: {lt: new Date('Tue, 07 Feb 2012 13:56:12 GMT')}}}, function (err, posts) {
|
2012-03-10 11:56:23 +00:00
|
|
|
test.equal(posts.length, 6, 'lt');
|
2012-02-01 17:33:08 +00:00
|
|
|
ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
// between
|
|
|
|
Post.all({where: {date: {between: [new Date('Tue, 05 Feb 2012 13:56:12 GMT'), new Date('Tue, 09 Feb 2012 13:56:12 GMT')]}}}, function (err, posts) {
|
2012-03-10 11:56:23 +00:00
|
|
|
test.equal(posts.length, 5, 'between');
|
2012-02-01 17:33:08 +00:00
|
|
|
ok();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = 5;
|
|
|
|
function ok() {
|
|
|
|
if (--tests === 0) test.done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-03-06 12:46:18 +00:00
|
|
|
it('should handle order clause with direction', function (test) {
|
|
|
|
var wait = 0;
|
|
|
|
var emails = [
|
|
|
|
'john@hcompany.com',
|
|
|
|
'tom@hcompany.com',
|
|
|
|
'admin@hcompany.com',
|
|
|
|
'tin@hcompany.com',
|
|
|
|
'mike@hcompany.com',
|
|
|
|
'susan@hcompany.com',
|
|
|
|
'test@hcompany.com'
|
|
|
|
];
|
|
|
|
User.destroyAll(function () {
|
|
|
|
emails.forEach(function (email) {
|
|
|
|
wait += 1;
|
|
|
|
User.create({email: email, name: 'Nick'}, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
var tests = 2;
|
|
|
|
function done() {
|
|
|
|
process.nextTick(function () {
|
|
|
|
if (--wait === 0) {
|
|
|
|
doSortTest();
|
|
|
|
doReverseSortTest();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function doSortTest() {
|
|
|
|
User.all({order: 'email ASC', where: {name: 'Nick'}}, function (err, users) {
|
|
|
|
var _emails = emails.sort();
|
|
|
|
users.forEach(function (user, i) {
|
|
|
|
test.equal(_emails[i], user.email, 'ASC sorting');
|
|
|
|
});
|
|
|
|
testDone();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function doReverseSortTest() {
|
|
|
|
User.all({order: 'email DESC', where: {name: 'Nick'}}, function (err, users) {
|
|
|
|
var _emails = emails.sort().reverse();
|
|
|
|
users.forEach(function (user, i) {
|
|
|
|
test.equal(_emails[i], user.email, 'DESC sorting');
|
|
|
|
});
|
|
|
|
testDone();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDone() {
|
|
|
|
if (--tests === 0) test.done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-20 18:33:11 +00:00
|
|
|
it('should return id in find result even after updateAttributes', function (test) {
|
|
|
|
Post.create(function (err, post) {
|
|
|
|
var id = post.id;
|
|
|
|
test.ok(post.published === false);
|
|
|
|
post.updateAttributes({title: 'hey', published: true}, function () {
|
|
|
|
Post.find(id, function (err, post) {
|
2012-02-20 18:44:02 +00:00
|
|
|
test.ok(!!post.published, 'Update boolean field');
|
2012-02-20 18:33:11 +00:00
|
|
|
test.ok(post.id);
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-03-01 17:26:52 +00:00
|
|
|
it('should handle belongsTo correctly', function (test) {
|
|
|
|
var passport = new Passport({ownerId: 16});
|
|
|
|
// sync getter
|
|
|
|
test.equal(passport.owner(), 16);
|
|
|
|
// sync setter
|
|
|
|
passport.owner(18);
|
|
|
|
test.equal(passport.owner(), 18);
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
|
2012-03-06 12:46:18 +00:00
|
|
|
it('should query one record', function (test) {
|
2012-03-11 04:48:38 +00:00
|
|
|
test.expect(4);
|
2012-03-06 12:46:18 +00:00
|
|
|
Post.findOne(function (err, post) {
|
2012-03-11 04:48:38 +00:00
|
|
|
test.ok(post && post.id);
|
2012-03-06 12:46:18 +00:00
|
|
|
Post.findOne({ where: { title: 'hey' } }, function (err, post) {
|
2012-03-11 04:48:38 +00:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return test.done();
|
|
|
|
}
|
2012-09-08 13:03:49 +00:00
|
|
|
test.equal(post && post.constructor.modelName, 'Post');
|
|
|
|
test.equal(post && post.title, 'hey');
|
2012-03-06 12:46:18 +00:00
|
|
|
Post.findOne({ where: { title: 'not exists' } }, function (err, post) {
|
|
|
|
test.ok(typeof post === 'undefined');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-03-22 19:46:16 +00:00
|
|
|
if (schema.name !== 'mongoose' && schema.name !== 'neo4j')
|
|
|
|
it('should update or create record', function (test) {
|
|
|
|
var newData = {
|
|
|
|
id: 1,
|
|
|
|
title: 'New title (really new)',
|
|
|
|
content: 'Some example content (updated)'
|
|
|
|
};
|
|
|
|
Post.updateOrCreate(newData, function (err, updatedPost) {
|
|
|
|
if (err) throw err;
|
|
|
|
test.ok(updatedPost);
|
|
|
|
if (!updatedPost) throw Error('No post!');
|
|
|
|
|
2012-04-19 16:06:38 +00:00
|
|
|
if (schema.name !== 'mongodb') {
|
|
|
|
test.equal(newData.id, updatedPost.toObject().id);
|
|
|
|
}
|
2012-03-22 19:46:16 +00:00
|
|
|
test.equal(newData.title, updatedPost.toObject().title);
|
|
|
|
test.equal(newData.content, updatedPost.toObject().content);
|
|
|
|
|
|
|
|
Post.find(updatedPost.id, function (err, post) {
|
|
|
|
if (err) throw err;
|
|
|
|
if (!post) throw Error('No post!');
|
2012-04-19 16:06:38 +00:00
|
|
|
if (schema.name !== 'mongodb') {
|
|
|
|
test.equal(newData.id, post.toObject().id);
|
|
|
|
}
|
2012-03-22 19:46:16 +00:00
|
|
|
test.equal(newData.title, post.toObject().title);
|
|
|
|
test.equal(newData.content, post.toObject().content);
|
|
|
|
Post.updateOrCreate({id: 100001, title: 'hey'}, function (err, post) {
|
|
|
|
if (schema.name !== 'mongodb') test.equal(post.id, 100001);
|
|
|
|
test.equal(post.title, 'hey');
|
|
|
|
Post.find(post.id, function (err, post) {
|
|
|
|
if (!post) throw Error('No post!');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-04-19 15:01:40 +00:00
|
|
|
it('should work with custom setters and getters', function (test) {
|
2012-08-13 06:16:01 +00:00
|
|
|
User.schema.defineForeignKey('User', 'passwd');
|
2012-04-19 15:01:40 +00:00
|
|
|
User.setter.passwd = function (pass) {
|
|
|
|
this._passwd = pass + 'salt';
|
|
|
|
};
|
|
|
|
var u = new User({passwd: 'qwerty'});
|
|
|
|
test.equal(u.passwd, 'qwertysalt');
|
|
|
|
u.save(function (err, user) {
|
|
|
|
User.find(user.id, function (err, user) {
|
|
|
|
test.ok(user !== u);
|
|
|
|
test.equal(user.passwd, 'qwertysalt');
|
2012-04-19 15:20:10 +00:00
|
|
|
console.log(user.id);
|
|
|
|
User.all({where: {passwd: 'qwertysalt'}}, function (err, users) {
|
2012-04-19 15:01:40 +00:00
|
|
|
test.ok(users[0] !== user);
|
|
|
|
test.equal(users[0].passwd, 'qwertysalt');
|
|
|
|
User.create({passwd: 'asalat'}, function (err, usr) {
|
|
|
|
test.equal(usr.passwd, 'asalatsalt');
|
2012-04-19 16:06:38 +00:00
|
|
|
User.upsert({passwd: 'heyman'}, function (err, us) {
|
|
|
|
test.equal(us.passwd, 'heymansalt');
|
|
|
|
User.find(us.id, function (err, user) {
|
|
|
|
test.equal(user.passwd, 'heymansalt');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
2012-04-19 15:01:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-09-10 15:57:21 +00:00
|
|
|
it('should work with typed and untyped nested collections', function (test) {
|
|
|
|
var post = new Post;
|
|
|
|
var like = post.likes.push({foo: 'bar'});
|
|
|
|
test.equal(like.constructor.name, 'ListItem');
|
|
|
|
var related = post.related.push({hello: 'world'});
|
|
|
|
test.ok(related.someMethod);
|
2012-09-11 16:51:31 +00:00
|
|
|
post.save(function (err, p) {
|
|
|
|
test.equal(p.likes.nextid, 2);
|
|
|
|
p.likes.push({second: 2});
|
|
|
|
p.likes.push({third: 3});
|
|
|
|
p.save(function (err) {
|
|
|
|
Post.find(p.id, function (err, pp) {
|
|
|
|
test.equal(pp.likes.length, 3);
|
|
|
|
test.ok(pp.likes[3].third);
|
|
|
|
test.ok(pp.likes[2].second);
|
|
|
|
test.ok(pp.likes[1].foo);
|
|
|
|
pp.likes.remove(2);
|
|
|
|
test.equal(pp.likes.length, 2);
|
|
|
|
test.ok(!pp.likes[2]);
|
|
|
|
pp.likes.remove(pp.likes[1]);
|
|
|
|
test.equal(pp.likes.length, 1);
|
|
|
|
test.ok(!pp.likes[1]);
|
|
|
|
test.ok(pp.likes[3]);
|
|
|
|
pp.save(function () {
|
|
|
|
Post.find(p.id, function (err, pp) {
|
|
|
|
test.equal(pp.likes.length, 1);
|
|
|
|
test.ok(!pp.likes[1]);
|
|
|
|
test.ok(pp.likes[3]);
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2012-09-10 15:57:21 +00:00
|
|
|
});
|
|
|
|
|
2012-01-18 18:49:30 +00:00
|
|
|
it('all tests done', function (test) {
|
|
|
|
test.done();
|
|
|
|
process.nextTick(allTestsDone);
|
|
|
|
});
|
|
|
|
|
2011-10-03 17:18:44 +00:00
|
|
|
function allTestsDone() {
|
2011-10-21 12:46:09 +00:00
|
|
|
schema.disconnect();
|
2011-10-03 17:18:44 +00:00
|
|
|
console.log('Test done in %dms\n', Date.now() - start);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-10-16 17:21:08 +00:00
|
|
|
|
|
|
|
function getSpecificTests() {
|
|
|
|
var sp = {};
|
|
|
|
|
|
|
|
sp['neo4j'] = function (schema) {
|
|
|
|
|
|
|
|
it('should create methods for searching by index', function (test) {
|
|
|
|
var Post = schema.models['Post'];
|
|
|
|
test.ok(typeof Post.findByTitle === 'function');
|
|
|
|
Post.create({title: 'Catcher in the rye'}, function (err, post) {
|
|
|
|
if (err) return console.log(err);
|
|
|
|
test.ok(!post.isNewRecord());
|
|
|
|
Post.findByTitle('Catcher in the rye', function (err, foundPost) {
|
|
|
|
if (err) return console.log(err);
|
|
|
|
if (foundPost) {
|
|
|
|
test.equal(post.id, foundPost.id);
|
|
|
|
test.done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return sp;
|
|
|
|
}
|