loopback-datasource-juggler/test/common_test.js

1154 lines
39 KiB
JavaScript
Raw Normal View History

// Copyright IBM Corp. 2011,2019. All Rights Reserved.
2016-04-01 22:25:16 +00:00
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-08-22 19:55:22 +00:00
'use strict';
2018-12-07 14:54:29 +00:00
const Schema = require('../index').Schema;
const Text = Schema.Text;
2011-10-03 17:18:44 +00:00
2018-12-07 14:54:29 +00:00
let nbSchemaRequests = 0;
2011-10-16 17:21:08 +00:00
2018-12-07 14:54:29 +00:00
let batch;
let schemaName;
2012-08-13 06:16:01 +00:00
function it(name, cases) {
2014-01-24 17:09:53 +00:00
batch[schemaName][name] = cases;
2012-08-13 06:16:01 +00:00
}
function skip(name) {
2014-01-24 17:09:53 +00:00
delete batch[schemaName][name];
}
module.exports = function testSchema(exportCasesHere, dataSource) {
2014-01-24 17:09:53 +00:00
batch = exportCasesHere;
schemaName = dataSource.name;
if (dataSource.name.match(/^\/.*\/test\/\.\.$/)) {
schemaName = schemaName.split('/').slice(-3).shift();
}
2018-12-07 14:54:29 +00:00
let start;
2012-03-22 20:24:15 +00:00
2016-04-01 11:48:17 +00:00
batch['should connect to database'] = function(test) {
2014-01-24 17:09:53 +00:00
start = Date.now();
if (dataSource.connected) return test.done();
dataSource.on('connected', test.done);
};
2016-04-01 11:48:17 +00:00
dataSource.log = function(a) {
2014-01-24 17:09:53 +00:00
console.log(a);
nbSchemaRequests++;
};
2014-01-24 17:09:53 +00:00
batch[schemaName] = {};
2012-12-14 17:44:38 +00:00
2014-01-24 17:09:53 +00:00
testOrm(dataSource);
2012-12-14 17:44:38 +00:00
2016-04-01 11:48:17 +00:00
batch['all tests done'] = function(test) {
2014-01-24 17:09:53 +00:00
test.done();
process.nextTick(allTestsDone);
};
2012-12-14 17:44:38 +00:00
2014-01-24 17:09:53 +00:00
function allTestsDone() {
// dataSource.disconnect();
2014-01-24 17:09:53 +00:00
console.log('Test done in %dms\n', Date.now() - start);
}
};
2011-10-03 17:18:44 +00:00
2012-12-14 17:44:38 +00:00
Object.defineProperty(module.exports, 'it', {
2014-01-24 17:09:53 +00:00
writable: true,
enumerable: false,
configurable: true,
2016-04-01 11:48:17 +00:00
value: it,
2012-12-14 17:44:38 +00:00
});
Object.defineProperty(module.exports, 'skip', {
2014-01-24 17:09:53 +00:00
writable: true,
enumerable: false,
configurable: true,
2016-04-01 11:48:17 +00:00
value: skip,
});
2018-06-12 07:13:32 +00:00
/* eslint-disable mocha/handle-done-callback */
function testOrm(dataSource) {
2018-12-07 14:54:29 +00:00
const requestsAreCounted = dataSource.name !== 'mongodb';
2011-10-03 17:18:44 +00:00
2018-12-07 14:54:29 +00:00
let Post, User, Passport, Log, Dog;
2011-10-03 17:18:44 +00:00
2016-04-01 11:48:17 +00:00
it('should define class', function(test) {
2014-01-24 17:09:53 +00:00
User = dataSource.define('User', {
2016-08-19 17:46:59 +00:00
name: {type: String, index: true},
email: {type: String, index: true},
2014-01-24 17:09:53 +00:00
bio: Text,
approved: Boolean,
joinedAt: Date,
age: Number,
2016-08-19 17:46:59 +00:00
passwd: {type: String, index: true},
2014-01-24 17:09:53 +00:00
});
2014-01-24 17:09:53 +00:00
Dog = dataSource.define('Dog', {
2016-08-19 17:46:59 +00:00
name: {type: String, limit: 64, allowNull: false},
2014-01-24 17:09:53 +00:00
});
2013-02-09 20:53:37 +00:00
2014-01-24 17:09:53 +00:00
Log = dataSource.define('Log', {
2016-08-19 17:46:59 +00:00
ownerId: {type: Number, allowNull: true},
name: {type: String, limit: 64, allowNull: false},
2014-01-24 17:09:53 +00:00
});
2013-02-09 20:53:37 +00:00
2016-08-19 17:46:59 +00:00
Log.belongsTo(Dog, {as: 'owner', foreignKey: 'ownerId'});
2013-02-09 20:53:37 +00:00
2014-01-24 17:09:53 +00:00
dataSource.extendModel('User', {
2016-08-19 17:46:59 +00:00
settings: {type: Schema.JSON},
2016-04-01 11:48:17 +00:00
extra: Object,
2014-01-24 17:09:53 +00:00
});
2011-10-03 17:18:44 +00:00
2018-12-07 14:54:29 +00:00
const newuser = new User({settings: {hey: 'you'}});
2014-01-24 17:09:53 +00:00
test.ok(newuser.settings);
Post = dataSource.define('Post', {
2016-08-19 17:46:59 +00:00
title: {type: String, length: 255, index: true},
subject: {type: String},
content: {type: Text},
date: {type: Date, default: function() {
2016-04-01 11:48:17 +00:00
return new Date;
2016-08-19 17:46:59 +00:00
}, index: true},
published: {type: Boolean, default: false, index: true},
2014-01-24 17:09:53 +00:00
likes: [],
2016-04-01 11:48:17 +00:00
related: [RelatedPost],
2016-08-19 17:46:59 +00:00
}, {table: 'posts'});
2014-01-24 17:09:53 +00:00
function RelatedPost() {
}
2011-11-19 08:58:49 +00:00
2016-04-01 11:48:17 +00:00
RelatedPost.prototype.someMethod = function() {
2014-01-24 17:09:53 +00:00
return this.parent;
};
2012-03-01 17:26:52 +00:00
2016-04-01 11:48:17 +00:00
Post.validateAsync('title', function(err, done) {
2014-01-24 17:09:53 +00:00
process.nextTick(done);
});
2012-03-01 17:26:52 +00:00
2016-08-19 17:46:59 +00:00
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
2014-01-24 17:09:53 +00:00
// creates instance methods:
// user.posts(conds)
// user.posts.build(data) // like new Post({userId: user.id});
// user.posts.create(data) // build and save
// user.posts.find
// 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)
2016-08-19 17:46:59 +00:00
Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
2014-01-24 17:09:53 +00:00
// 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
Passport = dataSource.define('Passport', {
2016-04-01 11:48:17 +00:00
number: String,
2014-01-24 17:09:53 +00:00
});
2011-10-03 17:18:44 +00:00
2016-08-19 17:46:59 +00:00
Passport.belongsTo(User, {as: 'owner', foreignKey: 'ownerId'});
User.hasMany(Passport, {as: 'passports', foreignKey: 'ownerId'});
2011-10-03 17:18:44 +00:00
2018-12-07 14:54:29 +00:00
const user = new User;
2011-10-03 17:18:44 +00:00
2014-01-24 17:09:53 +00:00
test.ok(User instanceof Function);
2011-10-03 17:18:44 +00:00
2014-01-24 17:09:53 +00:00
// class methods
test.ok(User.find instanceof Function);
test.ok(User.create instanceof Function);
2011-10-03 17:18:44 +00:00
2014-01-24 17:09:53 +00:00
// instance methods
test.ok(user.save instanceof Function);
2011-10-03 17:18:44 +00:00
2016-04-01 11:48:17 +00:00
dataSource.automigrate(function(err) {
2014-01-24 17:09:53 +00:00
if (err) {
console.log('Error while migrating');
console.log(err);
} else {
2011-10-03 17:18:44 +00:00
test.done();
2014-01-24 17:09:53 +00:00
}
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should initialize object properly', function(test) {
2018-12-07 14:54:29 +00:00
const hw = 'Hello word',
2014-01-24 17:09:53 +00:00
now = Date.now(),
2016-08-19 17:46:59 +00:00
post = new Post({title: hw}),
anotherPost = Post({title: 'Resig style constructor'});
2014-01-24 17:09:53 +00:00
test.equal(post.title, hw);
test.ok(!post.propertyChanged('title'), 'property changed: title');
post.title = 'Goodbye, Lenin';
test.equal(post.title_was, hw);
test.ok(post.propertyChanged('title'));
test.strictEqual(post.published, false);
test.ok(post.date >= now);
test.ok(post.isNewRecord());
test.ok(anotherPost instanceof Post);
test.ok(anotherPost.title, 'Resig style constructor');
test.done();
});
2016-04-01 11:48:17 +00:00
it('should save object', function(test) {
2018-12-07 14:54:29 +00:00
const title = 'Initial title', title2 = 'Hello world',
2014-01-24 17:09:53 +00:00
date = new Date;
Post.create({
title: title,
2016-04-01 11:48:17 +00:00
date: date,
}, function(err, obj) {
2014-01-24 17:09:53 +00:00
test.ok(obj.id, 'Object id should present');
test.equals(obj.title, title);
// test.equals(obj.date, date);
obj.title = title2;
test.ok(obj.propertyChanged('title'), 'Title changed');
2016-04-01 11:48:17 +00:00
obj.save(function(err, obj) {
2014-01-24 17:09:53 +00:00
test.equal(obj.title, title2);
test.ok(!obj.propertyChanged('title'));
2018-12-07 14:54:29 +00:00
const p = new Post({title: 1});
2014-01-24 17:09:53 +00:00
p.title = 2;
2016-04-01 11:48:17 +00:00
p.save(function(err, obj) {
2014-01-24 17:09:53 +00:00
test.ok(!p.propertyChanged('title'));
p.title = 3;
test.ok(p.propertyChanged('title'));
test.equal(p.title_was, 2);
2016-04-01 11:48:17 +00:00
p.save(function() {
2014-01-24 17:09:53 +00:00
test.equal(p.title_was, 3);
test.ok(!p.propertyChanged('title'));
test.done();
});
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
it('should save objects when createAll is invoked', function(test) {
const title = 'Initial title',
title2 = 'Hello world',
date = new Date();
Post.createAll(
[{
title: title,
date: date,
}],
function(err, objs) {
const obj = objs[0];
test.ok(obj.id, 'Object id should present');
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'));
const 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();
});
});
});
},
);
});
2016-04-01 11:48:17 +00:00
it('should create object with initial data', function(test) {
2018-12-07 14:54:29 +00:00
const title = 'Initial title',
2014-01-24 17:09:53 +00:00
date = new Date;
Post.create({
title: title,
2016-04-01 11:48:17 +00:00
date: date,
}, function(err, obj) {
2014-01-24 17:09:53 +00:00
test.ok(obj.id);
test.equals(obj.title, title);
test.equals(obj.date, date);
2016-04-01 11:48:17 +00:00
Post.findById(obj.id, function() {
2014-01-24 17:09:53 +00:00
test.equal(obj.title, title);
test.equal(obj.date.toString(), date.toString());
test.done();
});
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should save only dataSource-defined field in database', function(test) {
2016-08-19 17:46:59 +00:00
Post.create({title: '1602', nonSchemaField: 'some value'}, function(err, post) {
2014-01-24 17:09:53 +00:00
test.ok(!post.nonSchemaField);
post.a = 1;
2016-04-01 11:48:17 +00:00
post.save(function() {
2014-01-24 17:09:53 +00:00
test.ok(post.a);
2016-04-01 11:48:17 +00:00
post.reload(function(err, psto) {
2014-01-24 17:09:53 +00:00
test.ok(!psto.a);
test.done();
2011-12-11 08:58:34 +00:00
});
2014-01-24 17:09:53 +00:00
});
2011-12-11 08:58:34 +00:00
});
2014-01-24 17:09:53 +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.findById(post.id, function (err, foundPost) {
if (err) throw err;
test.equal(post.title, title);
test.strictEqual(post, foundPost);
test.done();
});
});
});
*/
2016-04-01 11:48:17 +00:00
it('should not re-instantiate object on saving', function(test) {
2018-12-07 14:54:29 +00:00
const title = 'Initial title';
const post = new Post({title: title});
2016-04-01 11:48:17 +00:00
post.save(function(err, savedPost) {
2014-01-24 17:09:53 +00:00
test.strictEqual(post, savedPost);
test.done();
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should destroy object', function(test) {
Post.create(function(err, post) {
Post.exists(post.id, function(err, exists) {
2014-01-24 17:09:53 +00:00
test.ok(exists, 'Object exists');
2016-04-01 11:48:17 +00:00
post.destroy(function() {
Post.exists(post.id, function(err, exists) {
2014-01-24 17:09:53 +00:00
if (err) console.log(err);
2016-04-01 13:23:42 +00:00
test.ok(!exists, 'Hey! ORM told me that object exists, ' +
' but it looks like it doesn\'t. Something went wrong...');
2016-04-01 11:48:17 +00:00
Post.findById(post.id, function(err, obj) {
2014-01-24 17:09:53 +00:00
test.equal(obj, null, 'Param obj should be null');
test.done();
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
2011-10-03 17:18:44 +00:00
2016-04-01 11:48:17 +00:00
it('should handle virtual attributes', function(test) {
2018-12-07 14:54:29 +00:00
const salt = 's0m3s3cr3t5a1t';
2011-12-11 11:01:23 +00:00
2016-04-01 11:48:17 +00:00
User.setter.passwd = function(password) {
2014-01-24 17:09:53 +00:00
this._passwd = calcHash(password, salt);
};
2011-12-11 11:01:23 +00:00
2014-01-24 17:09:53 +00:00
function calcHash(pass, salt) {
2018-12-07 14:54:29 +00:00
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
2014-01-24 17:09:53 +00:00
hash.update(pass);
hash.update(salt);
return hash.digest('base64');
}
2011-12-11 11:01:23 +00:00
2018-12-07 14:54:29 +00:00
const u = new User;
2014-01-24 17:09:53 +00:00
u.passwd = 's3cr3t';
test.equal(u.passwd, calcHash('s3cr3t', salt));
test.done();
});
// 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();
// });
// });
// });
2016-04-01 11:48:17 +00:00
it('should update single attribute', function(test) {
2016-08-19 17:46:59 +00:00
Post.create({title: 'title', content: 'content', published: true}, function(err, post) {
2014-01-24 17:09:53 +00:00
post.content = 'New content';
2016-04-01 11:48:17 +00:00
post.updateAttribute('title', 'New title', function() {
2014-01-24 17:09:53 +00:00
test.equal(post.title, 'New title');
test.ok(!post.propertyChanged('title'));
test.equal(post.content, 'New content', 'dirty state saved');
test.ok(post.propertyChanged('content'));
2016-04-01 11:48:17 +00:00
post.reload(function(err, post) {
2014-01-24 17:09:53 +00:00
test.equal(post.title, 'New title');
test.ok(!post.propertyChanged('title'), 'title not changed');
test.equal(post.content, 'content', 'real value turned back');
test.ok(!post.propertyChanged('content'), 'content unchanged');
test.done();
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
2018-12-07 14:54:29 +00:00
let countOfposts, countOfpostsFiltered;
2016-04-01 11:48:17 +00:00
it('should fetch collection', function(test) {
Post.all(function(err, posts) {
2014-01-24 17:09:53 +00:00
countOfposts = posts.length;
test.ok(countOfposts > 0);
test.ok(posts[0] instanceof Post);
2016-04-01 11:48:17 +00:00
countOfpostsFiltered = posts.filter(function(p) {
2014-01-24 17:09:53 +00:00
return p.title === 'title';
}).length;
test.done();
2011-10-03 17:18:44 +00:00
});
2014-01-24 17:09:53 +00:00
});
2011-10-03 17:18:44 +00:00
2016-04-01 11:48:17 +00:00
it('should find records filtered with multiple attributes', function(test) {
2018-12-07 14:54:29 +00:00
const d = new Date;
2016-08-19 17:46:59 +00:00
Post.create({title: 'title', content: 'content', published: true, date: d}, function(err, post) {
Post.all({where: {title: 'title', date: d, published: true}}, function(err, res) {
2014-01-24 17:09:53 +00:00
test.equals(res.length, 1, 'Filtering Posts returns one post');
test.done();
});
});
2014-01-24 17:09:53 +00:00
});
if (
!dataSource.name.match(/redis/) &&
dataSource.name !== 'memory' &&
dataSource.name !== 'neo4j' &&
dataSource.name !== 'cradle'
2018-06-12 07:13:32 +00:00
)
2016-04-01 11:48:17 +00:00
it('relations key is working', function(test) {
2014-01-24 17:09:53 +00:00
test.ok(User.relations, 'Relations key should be defined');
test.ok(User.relations.posts, 'posts relation should exist on User');
test.equal(User.relations.posts.type, 'hasMany', 'Type of hasMany relation is hasMany');
test.equal(User.relations.posts.multiple, true, 'hasMany relations are multiple');
test.equal(User.relations.posts.keyFrom, 'id', 'keyFrom is primary key of model table');
test.equal(User.relations.posts.keyTo, 'userId', 'keyTo is foreign key of related model table');
test.ok(Post.relations, 'Relations key should be defined');
test.ok(Post.relations.author, 'author relation should exist on Post');
test.equal(Post.relations.author.type, 'belongsTo', 'Type of belongsTo relation is belongsTo');
test.equal(Post.relations.author.multiple, false, 'belongsTo relations are not multiple');
test.equal(Post.relations.author.keyFrom, 'userId', 'keyFrom is foreign key of model table');
test.equal(Post.relations.author.keyTo, 'id', 'keyTo is primary key of related model table');
test.done();
2012-12-14 15:28:29 +00:00
});
2016-04-01 11:48:17 +00:00
it('should handle hasMany relationship', function(test) {
User.create(function(err, u) {
2014-01-24 17:09:53 +00:00
if (err) return console.log(err);
test.ok(u.posts, 'Method defined: posts');
test.ok(u.posts.build, 'Method defined: posts.build');
test.ok(u.posts.create, 'Method defined: posts.create');
2016-04-01 11:48:17 +00:00
u.posts.create(function(err, post) {
2014-01-24 17:09:53 +00:00
if (err) return console.log(err);
2016-04-01 11:48:17 +00:00
u.posts(function(err, posts) {
2014-01-24 17:09:53 +00:00
test.equal(posts.pop().id.toString(), post.id.toString());
test.done();
2011-10-05 14:47:26 +00:00
});
2014-01-24 17:09:53 +00:00
});
2011-10-05 14:47:26 +00:00
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should navigate variations of belongsTo regardless of column name', function(test) {
2016-08-19 17:46:59 +00:00
Dog.create({name: 'theDog'}, function(err, obj) {
2014-01-24 17:09:53 +00:00
test.ok(obj instanceof Dog);
2016-08-19 17:46:59 +00:00
Log.create({name: 'theLog', ownerId: obj.id}, function(err, obj) {
2014-01-24 17:09:53 +00:00
test.ok(obj instanceof Log);
2016-04-01 11:48:17 +00:00
obj.owner(function(err, obj) {
2014-01-24 17:09:53 +00:00
test.ok(!err, 'Should not have an error.'); // Before cba174b this would be 'Error: Permission denied'
if (err) {
console.log('Found: ' + err);
}
test.ok(obj, 'Should not find null or undefined.'); // Before cba174b this could be null or undefined.
test.ok(obj instanceof Dog, 'Should find a Dog.');
if (obj) { // Since test won't stop on fail, have to check before accessing obj.name.
test.ok(obj.name, 'Should have a name.');
}
if (obj && obj.name) {
test.equal(obj.name, 'theDog', 'The owner of theLog is theDog.');
}
test.done();
});
2014-01-24 17:09:53 +00:00
});
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('hasMany should support additional conditions', function(test) {
User.create(function(e, u) {
u.posts.create({}, function(e, p) {
2016-08-19 17:46:59 +00:00
u.posts({where: {id: p.id}}, function(e, posts) {
2014-01-24 17:09:53 +00:00
test.equal(posts.length, 1, 'There should be only 1 post.');
test.done();
2012-12-15 12:06:17 +00:00
});
2014-01-24 17:09:53 +00:00
});
2012-12-15 12:06:17 +00:00
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 13:23:42 +00:00
/* eslint-disable max-len */
2016-04-01 11:48:17 +00:00
it('hasMany should be cached', function(test) {
// User.create(function (e, u) {
2014-01-24 17:09:53 +00:00
// u.posts.create({}, function (e, p) {
// find all posts for a user.
// Finding one post with an existing author associated
2016-04-01 11:48:17 +00:00
Post.all(function(err, posts) {
2014-01-24 17:09:53 +00:00
// We try to get the first post with a userId != NULL
2018-12-07 14:54:29 +00:00
for (let i = 0; i < posts.length; i++) {
const post = posts[i];
2014-01-24 17:09:53 +00:00
if (post.userId) {
// We could get the user with belongs to relationship but it is better if there is no interactions.
2016-04-01 11:48:17 +00:00
User.findById(post.userId, function(err, user) {
User.create(function(err, voidUser) {
2016-08-19 17:46:59 +00:00
Post.create({userId: user.id}, function() {
2014-01-24 17:09:53 +00:00
// There can't be any concurrency because we are counting requests
// We are first testing cases when user has posts
2016-04-01 11:48:17 +00:00
user.posts(function(err, data) {
2018-12-07 14:54:29 +00:00
const nbInitialRequests = nbSchemaRequests;
2016-04-01 11:48:17 +00:00
user.posts(function(err, data2) {
2014-01-24 17:09:53 +00:00
test.equal(data.length, 2, 'There should be 2 posts.');
test.equal(data.length, data2.length, 'Posts should be the same, since we are loading on the same object.');
requestsAreCounted && test.equal(nbInitialRequests, nbSchemaRequests, 'There should not be any request because value is cached.');
if (dataSource.name === 'mongodb') { // for the moment mongodb doesn\'t support additional conditions on hasMany relations (see above)
test.done();
} else {
2016-08-19 17:46:59 +00:00
user.posts({where: {id: data[0].id}}, function(err, data) {
2014-01-24 17:09:53 +00:00
test.equal(data.length, 1, 'There should be only one post.');
requestsAreCounted && test.equal(nbInitialRequests + 1, nbSchemaRequests, 'There should be one additional request since we added conditions.');
2016-04-01 11:48:17 +00:00
user.posts(function(err, data) {
2014-01-24 17:09:53 +00:00
test.equal(data.length, 2, 'Previous get shouldn\'t have changed cached value though, since there was additional conditions.');
requestsAreCounted && test.equal(nbInitialRequests + 1, nbSchemaRequests, 'There should not be any request because value is cached.');
// We are now testing cases when user doesn't have any post
2016-04-01 11:48:17 +00:00
voidUser.posts(function(err, data) {
2018-12-07 14:54:29 +00:00
const nbInitialRequests = nbSchemaRequests;
2016-04-01 11:48:17 +00:00
voidUser.posts(function(err, data2) {
2014-01-24 17:09:53 +00:00
test.equal(data.length, 0, 'There shouldn\'t be any posts (1/2).');
test.equal(data2.length, 0, 'There shouldn\'t be any posts (2/2).');
requestsAreCounted && test.equal(nbInitialRequests, nbSchemaRequests, 'There should not be any request because value is cached.');
2016-04-01 11:48:17 +00:00
voidUser.posts(true, function(err, data3) {
2014-01-24 17:09:53 +00:00
test.equal(data3.length, 0, 'There shouldn\'t be any posts.');
requestsAreCounted && test.equal(nbInitialRequests + 1, nbSchemaRequests, 'There should be one additional request since we forced refresh.');
2012-12-15 12:06:17 +00:00
2014-01-24 17:09:53 +00:00
test.done();
});
2012-12-15 12:06:17 +00:00
});
2014-01-24 17:09:53 +00:00
});
2012-12-15 12:06:17 +00:00
});
2014-01-24 17:09:53 +00:00
});
}
});
});
});
});
});
break;
}
}
});
});
2016-04-01 13:23:42 +00:00
/* eslint-enable max-len */
2014-01-24 17:09:53 +00:00
// it('should handle hasOne relationship', function (test) {
// User.create(function (err, u) {
// if (err) return console.log(err);
// });
// });
2016-04-01 11:48:17 +00:00
it('should support scopes', function(test) {
2018-12-07 14:54:29 +00:00
let wait = 2;
2014-01-24 17:09:53 +00:00
test.ok(Post.scope, 'Scope supported');
2016-08-19 17:46:59 +00:00
Post.scope('published', {where: {published: true}});
2014-01-24 17:09:53 +00:00
test.ok(typeof Post.published === 'function');
test.ok(Post.published._scope.where.published === true);
2018-12-07 14:54:29 +00:00
const post = Post.published.build();
2014-01-24 17:09:53 +00:00
test.ok(post.published, 'Can build');
test.ok(post.isNewRecord());
2016-04-01 11:48:17 +00:00
Post.published.create(function(err, psto) {
2014-01-24 17:09:53 +00:00
if (err) return console.log(err);
test.ok(psto.published);
test.ok(!psto.isNewRecord());
done();
});
2011-10-15 15:57:35 +00:00
2016-04-01 11:48:17 +00:00
User.create(function(err, u) {
2014-01-24 17:09:53 +00:00
if (err) return console.log(err);
test.ok(typeof u.posts.published == 'function');
test.ok(u.posts.published._scope.where.published);
console.log(u.posts.published._scope);
test.equal(u.posts.published._scope.where.userId, u.id);
done();
2011-10-15 15:57:35 +00:00
});
2014-01-24 17:09:53 +00:00
function done() {
if (--wait === 0) test.done();
}
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should return type of property', function(test) {
2014-01-24 17:09:53 +00:00
test.equal(Post.getPropertyType('title'), 'String');
test.equal(Post.getPropertyType('content'), 'Text');
2018-12-07 14:54:29 +00:00
const p = new Post;
2014-01-24 17:09:53 +00:00
test.equal(p.getPropertyType('title'), 'String');
test.equal(p.getPropertyType('content'), 'Text');
test.done();
});
2016-04-01 11:48:17 +00:00
it('should handle ORDER clause', function(test) {
2018-12-07 14:54:29 +00:00
const titles = [
2016-08-19 17:46:59 +00:00
{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'},
2014-01-24 17:09:53 +00:00
];
2018-12-07 14:54:29 +00:00
const isRedis = Post.dataSource.name === 'redis';
const dates = isRedis ? [5, 9, 0, 17, 10, 9] : [
2014-01-24 17:09:53 +00:00
new Date(1000 * 5),
new Date(1000 * 9),
new Date(1000 * 0),
new Date(1000 * 17),
new Date(1000 * 10),
2016-04-01 11:48:17 +00:00
new Date(1000 * 9),
2014-01-24 17:09:53 +00:00
];
2016-04-01 11:48:17 +00:00
titles.forEach(function(t, i) {
2016-08-19 17:46:59 +00:00
Post.create({title: t.title, subject: t.subject, date: dates[i]}, done);
2012-01-18 15:20:05 +00:00
});
2018-12-07 14:54:29 +00:00
let i = 0, tests = 0;
2014-01-24 17:09:53 +00:00
function done(err, obj) {
if (++i === titles.length) {
doFilterAndSortTest();
doFilterAndSortReverseTest();
doStringTest();
doNumberTest();
2014-01-24 17:09:53 +00:00
if (dataSource.name == 'mongoose') {
doMultipleSortTest();
doMultipleReverseSortTest();
2012-05-14 04:28:37 +00:00
}
2014-01-24 17:09:53 +00:00
}
}
2012-05-14 04:28:37 +00:00
2014-01-24 17:09:53 +00:00
function compare(a, b) {
if (a.title < b.title) return -1;
if (a.title > b.title) return 1;
return 0;
}
2014-01-24 17:09:53 +00:00
// Post.dataSource.log = console.log;
2014-01-24 17:09:53 +00:00
function doStringTest() {
tests += 1;
2016-08-19 17:46:59 +00:00
Post.all({order: 'title'}, function(err, posts) {
2014-01-24 17:09:53 +00:00
if (err) console.log(err);
test.equal(posts.length, 6);
2016-04-01 11:48:17 +00:00
titles.sort(compare).forEach(function(t, i) {
2014-01-24 17:09:53 +00:00
if (posts[i]) test.equal(posts[i].title, t.title);
});
finished();
});
}
2014-01-24 17:09:53 +00:00
function doNumberTest() {
tests += 1;
2016-08-19 17:46:59 +00:00
Post.all({order: 'date'}, function(err, posts) {
2014-01-24 17:09:53 +00:00
if (err) console.log(err);
test.equal(posts.length, 6);
2016-04-01 11:48:17 +00:00
dates.sort(numerically).forEach(function(d, i) {
2014-01-24 17:09:53 +00:00
if (posts[i])
test.equal(posts[i].date.toString(), d.toString(), 'doNumberTest');
});
finished();
});
}
2014-01-24 17:09:53 +00:00
function doFilterAndSortTest() {
tests += 1;
2016-08-19 17:46:59 +00:00
Post.all({where: {date: new Date(1000 * 9)}, order: 'title', limit: 3}, function(err, posts) {
2014-01-24 17:09:53 +00:00
if (err) console.log(err);
console.log(posts.length);
test.equal(posts.length, 2, 'Exactly 2 posts returned by query');
2016-04-01 11:48:17 +00:00
['Title C', 'Title Z'].forEach(function(t, i) {
2014-01-24 17:09:53 +00:00
if (posts[i]) {
test.equal(posts[i].title, t, 'doFilterAndSortTest');
}
});
finished();
});
}
2014-01-24 17:09:53 +00:00
function doFilterAndSortReverseTest() {
tests += 1;
2016-08-19 17:46:59 +00:00
Post.all({where: {date: new Date(1000 * 9)}, order: 'title DESC', limit: 3}, function(err, posts) {
2014-01-24 17:09:53 +00:00
if (err) console.log(err);
test.equal(posts.length, 2, 'Exactly 2 posts returned by query');
2016-04-01 11:48:17 +00:00
['Title Z', 'Title C'].forEach(function(t, i) {
2014-01-24 17:09:53 +00:00
if (posts[i]) {
test.equal(posts[i].title, t, 'doFilterAndSortReverseTest');
}
});
finished();
});
}
2012-05-14 04:28:37 +00:00
2014-01-24 17:09:53 +00:00
function doMultipleSortTest() {
tests += 1;
2016-08-19 17:46:59 +00:00
Post.all({order: 'title ASC, subject ASC'}, function(err, posts) {
2014-01-24 17:09:53 +00:00
if (err) console.log(err);
test.equal(posts.length, 6);
2016-04-01 11:48:17 +00:00
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');
2014-01-24 17:09:53 +00:00
finished();
});
}
2012-05-14 04:28:37 +00:00
2014-01-24 17:09:53 +00:00
function doMultipleReverseSortTest() {
tests += 1;
2016-08-19 17:46:59 +00:00
Post.all({order: 'title ASC, subject DESC'}, function(err, posts) {
2014-01-24 17:09:53 +00:00
if (err) console.log(err);
test.equal(posts.length, 6);
2016-04-01 11:48:17 +00:00
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');
2014-01-24 17:09:53 +00:00
finished();
});
}
2018-12-07 14:54:29 +00:00
let fin = 0;
2014-01-24 17:09:53 +00:00
function finished() {
if (++fin === tests) {
test.done();
}
}
2014-01-24 17:09:53 +00:00
// TODO: do mixed test, do real dates tests, ensure that dates stored in UNIX timestamp format
2014-01-24 17:09:53 +00:00
function numerically(a, b) {
return a - b;
}
});
// if (
// !dataSource.name.match(/redis/) &&
// dataSource.name !== 'memory' &&
// dataSource.name !== 'neo4j' &&
// dataSource.name !== 'cradle' &&
// dataSource.name !== 'nano'
// )
// 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) {
// test.equal(posts.length, 2, 'gt');
// ok();
// });
// // gte
// Post.all({where: {date: {gte: new Date('Tue, 07 Feb 2012 13:56:12 GMT')}}}, function (err, posts) {
// test.equal(posts.length, 3, 'gte');
// ok();
// });
// // lte
// Post.all({where: {date: {lte: new Date('Tue, 07 Feb 2012 13:56:12 GMT')}}}, function (err, posts) {
// test.equal(posts.length, 7, 'lte');
// ok();
// });
// // lt
// Post.all({where: {date: {lt: new Date('Tue, 07 Feb 2012 13:56:12 GMT')}}}, function (err, posts) {
// test.equal(posts.length, 6, 'lt');
// 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) {
// test.equal(posts.length, 5, 'between');
// ok();
// });
// }
// var tests = 5;
// function ok() {
// if (--tests === 0) test.done();
// }
// });
// if (
// dataSource.name === 'mysql' ||
// dataSource.name === 'postgres'
// )
// it('should allow IN or NOT IN', function (test) {
// User.destroyAll(function () {
// User.create({name: 'User A', age: 21}, done);
// User.create({name: 'User B', age: 22}, done);
// User.create({name: 'User C', age: 23}, done);
// User.create({name: 'User D', age: 24}, done);
// User.create({name: 'User E', age: 25}, done);
// });
// var users = 5;
// function done() {
// if (--users === 0) makeTest();
// }
// function makeTest() {
// // IN with empty array should return nothing
// User.all({where: {name: {inq: []}}}, function (err, users) {
// test.equal(users.length, 0, 'IN with empty array returns nothing');
// ok();
// });
// // NOT IN with empty array should return everything
// User.all({where: {name: {nin: []}}}, function (err, users) {
// test.equal(users.length, 5, 'NOT IN with empty array returns everything');
// ok();
// });
// // IN [User A] returns user with name = User A
// User.all({where: {name: {inq: ['User A']}}}, function (err, users) {
// test.equal(users.length, 1, 'IN searching one existing value returns 1 user');
// test.equal(users[0].name, 'User A', 'IN [User A] returns user with name = User A');
// ok();
// });
// // NOT IN [User A] returns users with name != User A
// User.all({where: {name: {nin: ['User A']}}}, function (err, users) {
// test.equal(users.length, 4, 'IN [User A] returns users with name != User A');
// ok();
// });
// // IN [User A, User B] returns users with name = User A OR name = User B
// User.all({where: {name: {inq: ['User A', 'User B']}}}, function (err, users) {
// test.equal(users.length, 2, 'IN searching two existing values returns 2 users');
// ok();
// });
// // NOT IN [User A, User B] returns users with name != User A AND name != User B
// User.all({where: {name: {nin: ['User A', 'User B']}}}, function (err, users) {
// test.equal(users.length, 3, 'NOT IN searching two existing values returns users with name != User A AND name != User B');
// ok();
// });
// // IN works with numbers too
// User.all({where: {age: {inq: [21, 22]}}}, function (err, users) {
// test.equal(users.length, 2, 'IN works with numbers too');
// ok();
// });
// // NOT IN works with numbers too
// User.all({where: {age: {nin: [21, 22]}}}, function (err, users) {
// test.equal(users.length, 3, 'NOT IN works with numbers too');
// ok();
// });
// }
// var tests = 8;
// function ok() {
// if (--tests === 0) test.done();
// }
// });
2016-04-01 11:48:17 +00:00
it('should handle order clause with direction', function(test) {
2018-12-07 14:54:29 +00:00
let wait = 0;
const emails = [
2014-01-24 17:09:53 +00:00
'john@hcompany.com',
'tom@hcompany.com',
'admin@hcompany.com',
'tin@hcompany.com',
'mike@hcompany.com',
'susan@hcompany.com',
2016-04-01 11:48:17 +00:00
'test@hcompany.com',
2014-01-24 17:09:53 +00:00
];
2016-04-01 11:48:17 +00:00
User.destroyAll(function() {
emails.forEach(function(email) {
2014-01-24 17:09:53 +00:00
wait += 1;
2016-08-19 17:46:59 +00:00
User.create({email: email, name: 'Nick'}, done);
2014-01-24 17:09:53 +00:00
});
});
2018-12-07 14:54:29 +00:00
let tests = 2;
2014-01-24 17:09:53 +00:00
function done() {
2016-04-01 11:48:17 +00:00
process.nextTick(function() {
2014-01-24 17:09:53 +00:00
if (--wait === 0) {
doSortTest();
doReverseSortTest();
}
2014-01-24 17:09:53 +00:00
});
}
2014-01-24 17:09:53 +00:00
function doSortTest() {
2016-08-19 17:46:59 +00:00
User.all({order: 'email ASC', where: {name: 'Nick'}}, function(err, users) {
2018-12-07 14:54:29 +00:00
const _emails = emails.sort();
2016-04-01 11:48:17 +00:00
users.forEach(function(user, i) {
2014-01-24 17:09:53 +00:00
test.equal(_emails[i], user.email, 'ASC sorting');
});
testDone();
});
}
2014-01-24 17:09:53 +00:00
function doReverseSortTest() {
2016-08-19 17:46:59 +00:00
User.all({order: 'email DESC', where: {name: 'Nick'}}, function(err, users) {
2018-12-07 14:54:29 +00:00
const _emails = emails.sort().reverse();
2016-04-01 11:48:17 +00:00
users.forEach(function(user, i) {
2014-01-24 17:09:53 +00:00
test.equal(_emails[i], user.email, 'DESC sorting');
});
2014-01-24 17:09:53 +00:00
testDone();
});
}
2014-01-24 17:09:53 +00:00
function testDone() {
if (--tests === 0) test.done();
}
});
2016-04-01 11:48:17 +00:00
it('should return id in find result even after updateAttributes', function(test) {
Post.create(function(err, post) {
2018-12-07 14:54:29 +00:00
const id = post.id;
2014-01-24 17:09:53 +00:00
test.ok(post.published === false);
2016-08-19 17:46:59 +00:00
post.updateAttributes({title: 'hey', published: true}, function() {
2016-04-01 11:48:17 +00:00
Post.find(id, function(err, post) {
2014-01-24 17:09:53 +00:00
test.ok(!!post.published, 'Update boolean field');
test.ok(post.id);
test.done();
});
});
2012-03-01 17:26:52 +00:00
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should handle belongsTo correctly', function(test) {
2018-12-07 14:54:29 +00:00
const passport = new Passport({ownerId: 16});
2014-01-24 17:09:53 +00:00
// sync getter
test.equal(passport.owner(), 16);
// sync setter
passport.owner(18);
test.equal(passport.owner(), 18);
test.done();
});
2016-04-01 11:48:17 +00:00
it('should query one record', function(test) {
2014-01-24 17:09:53 +00:00
test.expect(4);
2016-04-01 11:48:17 +00:00
Post.findOne(function(err, post) {
2014-01-24 17:09:53 +00:00
test.ok(post && post.id);
2016-08-19 17:46:59 +00:00
Post.findOne({where: {title: 'hey'}}, function(err, post) {
2014-01-24 17:09:53 +00:00
if (err) {
console.log(err);
return test.done();
}
test.equal(post && post.constructor.modelName, 'Post');
test.equal(post && post.title, 'hey');
2016-08-19 17:46:59 +00:00
Post.findOne({where: {title: 'not exists'}}, function(err, post) {
2014-01-24 17:09:53 +00:00
test.ok(post === null);
test.done();
});
2014-01-24 17:09:53 +00:00
});
});
2014-01-24 17:09:53 +00:00
});
// if (
// !dataSource.name.match(/redis/) &&
// dataSource.name !== 'memory' &&
// dataSource.name !== 'neo4j' &&
// dataSource.name !== 'cradle' &&
// dataSource.name !== 'nano'
// )
// it('belongsTo should be cached', function (test) {
// User.findOne(function(err, user) {
// var passport = new Passport({ownerId: user.id});
// var passport2 = new Passport({ownerId: null});
// // There can't be any concurrency because we are counting requests
// // We are first testing cases when passport has an owner
// passport.owner(function(err, data) {
// var nbInitialRequests = nbSchemaRequests;
// passport.owner(function(err, data2) {
// test.equal(data.id, data2.id, 'The value should remain the same');
// requestsAreCounted && test.equal(nbInitialRequests, nbSchemaRequests, 'There should not be any request because value is cached.');
// // We are now testing cases when passport has not an owner
// passport2.owner(function(err, data) {
// var nbInitialRequests2 = nbSchemaRequests;
// passport2.owner(function(err, data2) {
// test.equal(data, null, 'The value should be null since there is no owner');
// test.equal(data, data2, 'The value should remain the same (null)');
// requestsAreCounted && test.equal(nbInitialRequests2, nbSchemaRequests, 'There should not be any request because value is cached.');
// passport2.owner(user.id);
// passport2.owner(function(err, data3) {
// test.equal(data3.id, user.id, 'Owner should now be the user.');
// requestsAreCounted && test.equal(nbInitialRequests2 + 1, nbSchemaRequests, 'If we changed owner id, there should be one more request.');
// passport2.owner(true, function(err, data4) {
// test.equal(data3.id, data3.id, 'The value should remain the same');
// requestsAreCounted && test.equal(nbInitialRequests2 + 2, nbSchemaRequests, 'If we forced refreshing, there should be one more request.');
// test.done();
// });
// });
// });
// });
// });
// });
// });
// });
if (dataSource.name !== 'mongoose' && dataSource.name !== 'neo4j')
2016-04-01 11:48:17 +00:00
it('should update or create record', function(test) {
2018-12-07 14:54:29 +00:00
const newData = {
2014-01-24 17:09:53 +00:00
id: 1,
title: 'New title (really new)',
2016-04-01 11:48:17 +00:00
content: 'Some example content (updated)',
2014-01-24 17:09:53 +00:00
};
2016-04-01 11:48:17 +00:00
Post.updateOrCreate(newData, function(err, updatedPost) {
2014-01-24 17:09:53 +00:00
if (err) throw err;
test.ok(updatedPost);
if (!updatedPost) throw Error('No post!');
if (dataSource.name !== 'mongodb') {
test.equal(newData.id, updatedPost.toObject().id);
}
test.equal(newData.title, updatedPost.toObject().title);
test.equal(newData.content, updatedPost.toObject().content);
2016-04-01 11:48:17 +00:00
Post.findById(updatedPost.id, function(err, post) {
2014-01-24 17:09:53 +00:00
if (err) throw err;
if (!post) throw Error('No post!');
if (dataSource.name !== 'mongodb') {
test.equal(newData.id, post.toObject().id);
}
test.equal(newData.title, post.toObject().title);
test.equal(newData.content, post.toObject().content);
2016-08-19 17:46:59 +00:00
Post.updateOrCreate({id: 100001, title: 'hey'}, function(err, post) {
2014-01-24 17:09:53 +00:00
if (dataSource.name !== 'mongodb') test.equal(post.id, 100001);
test.equal(post.title, 'hey');
2016-04-01 11:48:17 +00:00
Post.findById(post.id, function(err, post) {
2014-01-24 17:09:53 +00:00
if (!post) throw Error('No post!');
test.done();
2012-03-22 19:46:16 +00:00
});
2014-01-24 17:09:53 +00:00
});
2012-03-22 19:46:16 +00:00
});
2014-01-24 17:09:53 +00:00
});
2012-03-22 19:46:16 +00:00
});
2016-04-01 11:48:17 +00:00
it('should work with custom setters and getters', function(test) {
2014-01-24 17:09:53 +00:00
User.dataSource.defineForeignKey('User', 'passwd');
2016-04-01 11:48:17 +00:00
User.setter.passwd = function(pass) {
2014-01-24 17:09:53 +00:00
this._passwd = pass + 'salt';
};
2018-12-07 14:54:29 +00:00
const u = new User({passwd: 'qwerty'});
2014-01-24 17:09:53 +00:00
test.equal(u.passwd, 'qwertysalt');
2016-04-01 11:48:17 +00:00
u.save(function(err, user) {
User.findById(user.id, function(err, user) {
2014-01-24 17:09:53 +00:00
test.ok(user !== u);
test.equal(user.passwd, 'qwertysalt');
2016-08-19 17:46:59 +00:00
User.all({where: {passwd: 'qwertysalt'}}, function(err, users) {
2014-01-24 17:09:53 +00:00
test.ok(users[0] !== user);
test.equal(users[0].passwd, 'qwertysalt');
2016-08-19 17:46:59 +00:00
User.create({passwd: 'asalat'}, function(err, usr) {
2014-01-24 17:09:53 +00:00
test.equal(usr.passwd, 'asalatsalt');
2016-08-19 17:46:59 +00:00
User.upsert({passwd: 'heyman'}, function(err, us) {
2014-01-24 17:09:53 +00:00
test.equal(us.passwd, 'heymansalt');
2016-04-01 11:48:17 +00:00
User.findById(us.id, function(err, user) {
2014-01-24 17:09:53 +00:00
test.equal(user.passwd, 'heymansalt');
test.done();
});
2012-04-19 15:01:40 +00:00
});
2014-01-24 17:09:53 +00:00
});
2012-04-19 15:01:40 +00:00
});
2014-01-24 17:09:53 +00:00
});
2012-04-19 15:01:40 +00:00
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should work with typed and untyped nested collections', function(test) {
2018-12-07 14:54:29 +00:00
const post = new Post;
const like = post.likes.push({foo: 'bar'});
2014-01-24 17:09:53 +00:00
test.equal(like.constructor.name, 'ListItem');
2018-12-07 14:54:29 +00:00
const related = post.related.push({hello: 'world'});
2014-01-24 17:09:53 +00:00
test.ok(related.someMethod);
2016-04-01 11:48:17 +00:00
post.save(function(err, p) {
2014-01-24 17:09:53 +00:00
test.equal(p.likes.nextid, 2);
2016-08-19 17:46:59 +00:00
p.likes.push({second: 2});
p.likes.push({third: 3});
2016-04-01 11:48:17 +00:00
p.save(function(err) {
Post.findById(p.id, function(err, pp) {
2014-01-24 17:09:53 +00:00
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]);
2016-04-01 11:48:17 +00:00
pp.save(function() {
Post.findById(p.id, function(err, pp) {
2014-01-24 17:09:53 +00:00
test.equal(pp.likes.length, 1);
test.ok(!pp.likes[1]);
test.ok(pp.likes[3]);
test.done();
2012-09-11 16:51:31 +00:00
});
2014-01-24 17:09:53 +00:00
});
2012-09-11 16:51:31 +00:00
});
2014-01-24 17:09:53 +00:00
});
2012-09-10 15:57:21 +00:00
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should find or create', function(test) {
2018-12-07 14:54:29 +00:00
const email = 'some email ' + Math.random();
2016-08-19 17:46:59 +00:00
User.findOrCreate({where: {email: email}}, function(err, u, created) {
2014-01-24 17:09:53 +00:00
test.ok(u);
test.ok(!u.age);
test.ok(created);
2016-08-19 17:46:59 +00:00
User.findOrCreate({where: {email: email}}, {age: 21}, function(err, u2, created) {
2014-01-24 17:09:53 +00:00
test.equals(u.id.toString(), u2.id.toString(), 'Same user ids');
test.ok(!u2.age);
test.ok(!created);
2014-01-24 17:09:53 +00:00
test.done();
});
2013-01-21 18:21:43 +00:00
});
2014-01-24 17:09:53 +00:00
});
2011-10-03 17:18:44 +00:00
}