2014-01-29 20:04:09 +00:00
|
|
|
var jdb = require('../');
|
|
|
|
var DataSource = jdb.DataSource;
|
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
var assert = require('assert');
|
|
|
|
var async = require('async');
|
2014-06-18 06:19:28 +00:00
|
|
|
var should = require('./init.js');
|
2015-01-21 16:57:47 +00:00
|
|
|
var Memory = require('../lib/connectors/memory').Memory;
|
2014-01-29 20:04:09 +00:00
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
describe('Memory connector', function() {
|
2014-01-29 20:04:09 +00:00
|
|
|
var file = path.join(__dirname, 'memory.json');
|
|
|
|
|
|
|
|
function readModels(done) {
|
2015-02-02 16:44:36 +00:00
|
|
|
fs.readFile(file, function(err, data) {
|
2014-01-29 20:04:09 +00:00
|
|
|
var json = JSON.parse(data.toString());
|
|
|
|
assert(json.models);
|
|
|
|
assert(json.ids.User);
|
|
|
|
done(err, json);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
before(function(done) {
|
|
|
|
fs.unlink(file, function(err) {
|
2014-01-29 20:04:09 +00:00
|
|
|
if (!err || err.code === 'ENOENT') {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
describe('with file', function() {
|
|
|
|
function createUserModel() {
|
|
|
|
var ds = new DataSource({
|
|
|
|
connector: 'memory',
|
|
|
|
file: file
|
|
|
|
});
|
2014-01-29 20:04:09 +00:00
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
var User = ds.createModel('User', {
|
|
|
|
id: {
|
|
|
|
type: Number,
|
|
|
|
id: true,
|
|
|
|
generated: true
|
|
|
|
},
|
|
|
|
name: String,
|
|
|
|
bio: String,
|
|
|
|
approved: Boolean,
|
|
|
|
joinedAt: Date,
|
|
|
|
age: Number
|
|
|
|
});
|
|
|
|
return User;
|
|
|
|
}
|
2014-01-29 20:04:09 +00:00
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
var User;
|
2014-01-29 21:41:42 +00:00
|
|
|
var ids = [];
|
2015-02-02 16:44:36 +00:00
|
|
|
|
|
|
|
before(function() {
|
|
|
|
User = createUserModel();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should persist create', function(done) {
|
|
|
|
var count = 0;
|
|
|
|
async.eachSeries(['John1', 'John2', 'John3'], function(item, cb) {
|
|
|
|
User.create({name: item}, function(err, result) {
|
|
|
|
ids.push(result.id);
|
|
|
|
count++;
|
|
|
|
readModels(function(err, json) {
|
|
|
|
assert.equal(Object.keys(json.models.User).length, count);
|
|
|
|
cb(err);
|
|
|
|
});
|
2014-01-29 20:04:09 +00:00
|
|
|
});
|
2015-02-02 16:44:36 +00:00
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should persist delete', function(done) {
|
2014-01-29 20:04:09 +00:00
|
|
|
// Now try to delete one
|
2015-02-02 16:44:36 +00:00
|
|
|
User.deleteById(ids[0], function(err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
readModels(function(err, json) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-01-29 20:04:09 +00:00
|
|
|
assert.equal(Object.keys(json.models.User).length, 2);
|
2015-02-02 16:44:36 +00:00
|
|
|
done();
|
2014-01-29 20:04:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should persist upsert', function(done) {
|
|
|
|
User.upsert({id: ids[1], name: 'John'}, function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
readModels(function(err, json) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
assert.equal(Object.keys(json.models.User).length, 2);
|
|
|
|
var user = JSON.parse(json.models.User[ids[1]]);
|
|
|
|
assert.equal(user.name, 'John');
|
|
|
|
assert(user.id === ids[1]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-29 20:04:09 +00:00
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should persist update', function(done) {
|
|
|
|
User.update({id: ids[1]}, {name: 'John1'},
|
|
|
|
function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
readModels(function(err, json) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
assert.equal(Object.keys(json.models.User).length, 2);
|
|
|
|
var user = JSON.parse(json.models.User[ids[1]]);
|
|
|
|
assert.equal(user.name, 'John1');
|
|
|
|
assert(user.id === ids[1]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-29 20:04:09 +00:00
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
// The saved memory.json from previous test should be loaded
|
|
|
|
it('should load from the json file', function(done) {
|
|
|
|
User.find(function(err, users) {
|
|
|
|
// There should be 2 records
|
|
|
|
assert.equal(users.length, 2);
|
|
|
|
done(err);
|
|
|
|
});
|
2014-01-29 20:04:09 +00:00
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
});
|
2014-01-29 20:04:09 +00:00
|
|
|
});
|
2014-06-18 06:19:28 +00:00
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
describe('Query for memory connector', function() {
|
2014-06-18 06:19:28 +00:00
|
|
|
var ds = new DataSource({
|
|
|
|
connector: 'memory'
|
|
|
|
});
|
|
|
|
|
|
|
|
var User = ds.define('User', {
|
|
|
|
seq: {type: Number, index: true},
|
|
|
|
name: {type: String, index: true, sort: true},
|
|
|
|
email: {type: String, index: true},
|
|
|
|
birthday: {type: Date, index: true},
|
|
|
|
role: {type: String, index: true},
|
|
|
|
order: {type: Number, index: true, sort: true},
|
|
|
|
vip: {type: Boolean}
|
|
|
|
});
|
|
|
|
|
|
|
|
before(seed);
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should allow to find using like', function(done) {
|
|
|
|
User.find({where: {name: {like: '%St%'}}}, function(err, posts) {
|
2014-06-18 06:19:28 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
posts.should.have.property('length', 2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should support like for no match', function(done) {
|
|
|
|
User.find({where: {name: {like: 'M%XY'}}}, function(err, posts) {
|
2014-06-18 06:19:28 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
posts.should.have.property('length', 0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should allow to find using nlike', function(done) {
|
|
|
|
User.find({where: {name: {nlike: '%St%'}}}, function(err, posts) {
|
2014-06-18 06:19:28 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
posts.should.have.property('length', 4);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should support nlike for no match', function(done) {
|
|
|
|
User.find({where: {name: {nlike: 'M%XY'}}}, function(err, posts) {
|
2014-06-18 06:19:28 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
posts.should.have.property('length', 6);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should throw if the like value is not string or regexp', function(done) {
|
|
|
|
User.find({where: {name: {like: 123}}}, function(err, posts) {
|
2014-06-27 06:40:20 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should throw if the nlike value is not string or regexp', function(done) {
|
|
|
|
User.find({where: {name: {nlike: 123}}}, function(err, posts) {
|
2014-06-27 06:40:20 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should throw if the inq value is not an array', function(done) {
|
|
|
|
User.find({where: {name: {inq: '12'}}}, function(err, posts) {
|
2014-06-27 06:40:20 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should throw if the nin value is not an array', function(done) {
|
|
|
|
User.find({where: {name: {nin: '12'}}}, function(err, posts) {
|
2014-06-27 06:40:20 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should throw if the between value is not an array', function(done) {
|
|
|
|
User.find({where: {name: {between: '12'}}}, function(err, posts) {
|
2014-06-27 06:40:20 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should throw if the between value is not an array of length 2', function(done) {
|
|
|
|
User.find({where: {name: {between: ['12']}}}, function(err, posts) {
|
2014-06-27 06:40:20 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-16 17:26:33 +00:00
|
|
|
it('should successfully extract 5 users from the db', function(done) {
|
|
|
|
User.find({where: {seq: {between: [1,5]}}}, function(err, users) {
|
|
|
|
should(users.length).be.equal(5);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully extract 1 user (Lennon) from the db', function(done) {
|
|
|
|
User.find({where: {birthday: {between: [new Date(1970,0),new Date(1990,0)]}}},
|
|
|
|
function(err, users) {
|
|
|
|
should(users.length).be.equal(1);
|
|
|
|
should(users[0].name).be.equal('John Lennon');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully extract 2 users from the db', function(done) {
|
|
|
|
User.find({where: {birthday: {between: [new Date(1940,0),new Date(1990,0)]}}},
|
|
|
|
function(err, users) {
|
|
|
|
should(users.length).be.equal(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully extract 0 user from the db', function(done) {
|
|
|
|
User.find({where: {birthday: {between: [new Date(1990,0), Date.now()]}}},
|
|
|
|
function(err, users) {
|
|
|
|
should(users.length).be.equal(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should support order with multiple fields', function(done) {
|
|
|
|
User.find({order: 'vip ASC, seq DESC'}, function(err, posts) {
|
2014-06-27 06:40:20 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
posts[0].seq.should.be.eql(4);
|
|
|
|
posts[1].seq.should.be.eql(3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should sort undefined values to the end when ordered DESC', function(done) {
|
|
|
|
User.find({order: 'vip ASC, order DESC'}, function(err, posts) {
|
2015-01-05 20:56:17 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
|
|
|
|
posts[4].seq.should.be.eql(1);
|
|
|
|
posts[5].seq.should.be.eql(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should throw if order has wrong direction', function(done) {
|
|
|
|
User.find({order: 'seq ABC'}, function(err, posts) {
|
2014-06-27 06:40:20 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should support neq operator for number', function(done) {
|
|
|
|
User.find({where: {seq: {neq: 4}}}, function(err, users) {
|
2014-08-29 15:45:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
users.length.should.be.equal(5);
|
|
|
|
for (var i = 0; i < users.length; i++) {
|
2015-01-05 20:56:17 +00:00
|
|
|
users[i].seq.should.not.be.equal(4);
|
2014-08-29 15:45:45 +00:00
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should support neq operator for string', function(done) {
|
|
|
|
User.find({where: {role: {neq: 'lead'}}}, function(err, users) {
|
2014-08-29 15:45:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
users.length.should.be.equal(4);
|
|
|
|
for (var i = 0; i < users.length; i++) {
|
|
|
|
if (users[i].role) {
|
|
|
|
users[i].role.not.be.equal('lead');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should support neq operator for null', function(done) {
|
|
|
|
User.find({where: {role: {neq: null}}}, function(err, users) {
|
2014-09-02 15:36:37 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
users.length.should.be.equal(2);
|
|
|
|
for (var i = 0; i < users.length; i++) {
|
|
|
|
should.exist(users[i].role);
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-06-18 06:19:28 +00:00
|
|
|
function seed(done) {
|
|
|
|
var beatles = [
|
|
|
|
{
|
|
|
|
seq: 0,
|
|
|
|
name: 'John Lennon',
|
|
|
|
email: 'john@b3atl3s.co.uk',
|
|
|
|
role: 'lead',
|
|
|
|
birthday: new Date('1980-12-08'),
|
|
|
|
vip: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
seq: 1,
|
|
|
|
name: 'Paul McCartney',
|
|
|
|
email: 'paul@b3atl3s.co.uk',
|
|
|
|
role: 'lead',
|
|
|
|
birthday: new Date('1942-06-18'),
|
|
|
|
order: 1,
|
|
|
|
vip: true
|
|
|
|
},
|
|
|
|
{seq: 2, name: 'George Harrison', order: 5, vip: false},
|
|
|
|
{seq: 3, name: 'Ringo Starr', order: 6, vip: false},
|
|
|
|
{seq: 4, name: 'Pete Best', order: 4},
|
|
|
|
{seq: 5, name: 'Stuart Sutcliffe', order: 3, vip: true}
|
|
|
|
];
|
|
|
|
|
2014-06-20 19:05:42 +00:00
|
|
|
async.series([
|
|
|
|
User.destroyAll.bind(User),
|
|
|
|
function(cb) {
|
|
|
|
async.each(beatles, User.create.bind(User), cb);
|
2014-06-18 06:19:28 +00:00
|
|
|
}
|
2014-06-20 19:05:42 +00:00
|
|
|
], done);
|
2014-06-18 06:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2015-01-21 16:57:47 +00:00
|
|
|
|
2015-02-02 16:44:36 +00:00
|
|
|
it('should use collection setting', function(done) {
|
2014-09-06 12:09:30 +00:00
|
|
|
var ds = new DataSource({
|
|
|
|
connector: 'memory'
|
|
|
|
});
|
2015-01-21 16:57:47 +00:00
|
|
|
|
2014-09-06 12:09:30 +00:00
|
|
|
var Product = ds.createModel('Product', {
|
|
|
|
name: String
|
|
|
|
});
|
2015-01-21 16:57:47 +00:00
|
|
|
|
2014-09-06 12:09:30 +00:00
|
|
|
var Tool = ds.createModel('Tool', {
|
|
|
|
name: String
|
|
|
|
}, {memory: {collection: 'Product'}});
|
2015-01-21 16:57:47 +00:00
|
|
|
|
2014-09-06 12:09:30 +00:00
|
|
|
var Widget = ds.createModel('Widget', {
|
|
|
|
name: String
|
|
|
|
}, {memory: {collection: 'Product'}});
|
2015-01-21 16:57:47 +00:00
|
|
|
|
2014-09-06 12:09:30 +00:00
|
|
|
ds.connector.getCollection('Tool').should.equal('Product');
|
|
|
|
ds.connector.getCollection('Widget').should.equal('Product');
|
2015-01-21 16:57:47 +00:00
|
|
|
|
2014-09-06 12:09:30 +00:00
|
|
|
async.series([
|
|
|
|
function(next) {
|
|
|
|
Tool.create({ name: 'Tool A' }, next);
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Tool.create({ name: 'Tool B' }, next);
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
Widget.create({ name: 'Widget A' }, next);
|
|
|
|
}
|
|
|
|
], function(err) {
|
|
|
|
Product.find(function(err, products) {
|
|
|
|
should.not.exist(err);
|
|
|
|
products.should.have.length(3);
|
|
|
|
products[0].toObject().should.eql({ name: 'Tool A', id: 1 });
|
|
|
|
products[1].toObject().should.eql({ name: 'Tool B', id: 2 });
|
|
|
|
products[2].toObject().should.eql({ name: 'Widget A', id: 3 });
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-06-18 06:19:28 +00:00
|
|
|
|
2014-10-16 19:04:16 +00:00
|
|
|
describe('automigrate', function() {
|
|
|
|
var ds;
|
|
|
|
beforeEach(function() {
|
|
|
|
ds = new DataSource({
|
|
|
|
connector: 'memory'
|
|
|
|
});
|
|
|
|
|
|
|
|
ds.createModel('m1', {
|
|
|
|
name: String
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('automigrate all models', function(done) {
|
|
|
|
ds.automigrate(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('automigrate one model', function(done) {
|
|
|
|
ds.automigrate('m1', function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('automigrate one or more models in an array', function(done) {
|
|
|
|
ds.automigrate(['m1'], function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('automigrate reports errors for models not attached', function(done) {
|
|
|
|
ds.automigrate(['m1', 'm2'], function(err) {
|
|
|
|
err.should.be.an.instanceOf(Error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 07:34:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Optimized connector', function() {
|
|
|
|
var ds = new DataSource({ connector: Memory });
|
2014-10-16 19:04:16 +00:00
|
|
|
|
2015-02-03 07:34:49 +00:00
|
|
|
// optimized methods
|
|
|
|
ds.connector.findOrCreate = function (model, query, data, callback) {
|
|
|
|
this.all(model, query, function (err, list) {
|
|
|
|
if (err || (list && list[0])) return callback(err, list && list[0], false);
|
|
|
|
this.create(model, data, function (err) {
|
|
|
|
callback(err, data, true);
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
require('./persistence-hooks.suite')(ds, should);
|
2015-01-21 16:57:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Unoptimized connector', function() {
|
|
|
|
var ds = new DataSource({ connector: Memory });
|
|
|
|
// disable optimized methods
|
|
|
|
ds.connector.updateOrCreate = false;
|
2015-02-03 07:34:49 +00:00
|
|
|
ds.connector.findOrCreate = false;
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
require('./persistence-hooks.suite')(ds, should);
|
2014-01-29 20:04:09 +00:00
|
|
|
});
|
|
|
|
|
2014-06-18 06:19:28 +00:00
|
|
|
|
|
|
|
|