2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2015-02-11 07:57:05 +00:00
|
|
|
// This test written in mocha+should.js
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-12-05 14:14:09 +00:00
|
|
|
/* global getSchema:false */
|
2015-02-11 07:57:05 +00:00
|
|
|
var should = require('./init.js');
|
|
|
|
var async = require('async');
|
2015-04-16 16:06:53 +00:00
|
|
|
var db, User, options, filter;
|
2015-02-11 07:57:05 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('crud-with-options', function() {
|
|
|
|
before(function(done) {
|
2015-02-11 07:57:05 +00:00
|
|
|
db = getSchema();
|
|
|
|
User = db.define('User', {
|
2016-08-19 17:46:59 +00:00
|
|
|
id: {type: Number, id: true},
|
|
|
|
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},
|
2018-09-06 17:34:12 +00:00
|
|
|
address: {type: {city: String, area: String}},
|
2015-02-11 07:57:05 +00:00
|
|
|
});
|
|
|
|
options = {};
|
2016-08-19 17:46:59 +00:00
|
|
|
filter = {fields: ['name', 'id']};
|
2015-02-11 07:57:05 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['User'], done);
|
2015-02-11 07:57:05 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('findById', function() {
|
|
|
|
before(function(done) {
|
2015-02-11 07:57:05 +00:00
|
|
|
User.destroyAll(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow findById(id, options, cb)', function(done) {
|
|
|
|
User.findById(1, options, function(err, u) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(u);
|
|
|
|
should.not.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow findById(id, filter, cb)', function(done) {
|
|
|
|
User.findById(1, filter, function(err, u) {
|
2015-04-16 16:06:53 +00:00
|
|
|
should.not.exist(u);
|
|
|
|
should.not.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow findById(id)', function() {
|
2015-02-11 07:57:05 +00:00
|
|
|
User.findById(1);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow findById(id, filter)', function() {
|
2015-04-16 16:06:53 +00:00
|
|
|
User.findById(1, filter);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow findById(id, options)', function() {
|
2015-02-11 07:57:05 +00:00
|
|
|
User.findById(1, options);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow findById(id, filter, options)', function() {
|
2015-04-16 16:06:53 +00:00
|
|
|
User.findById(1, filter, options);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when invalid filter are provided for findById',
|
2015-02-11 07:57:05 +00:00
|
|
|
function(done) {
|
|
|
|
(function() {
|
|
|
|
User.findById(1, '123', function(err, u) {
|
|
|
|
});
|
2015-04-16 16:06:53 +00:00
|
|
|
}).should.throw('The filter argument must be an object');
|
2016-04-01 11:48:17 +00:00
|
|
|
done();
|
2015-02-11 07:57:05 +00:00
|
|
|
});
|
|
|
|
|
2015-04-16 16:06:53 +00:00
|
|
|
it('should throw when invalid options are provided for findById',
|
|
|
|
function(done) {
|
|
|
|
(function() {
|
|
|
|
User.findById(1, filter, '123', function(err, u) {
|
|
|
|
});
|
|
|
|
}).should.throw('The options argument must be an object');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2015-02-11 07:57:05 +00:00
|
|
|
it('should report an invalid id via callback for findById',
|
|
|
|
function(done) {
|
|
|
|
User.findById(undefined, {}, function(err, u) {
|
|
|
|
err.should.be.eql(
|
2018-07-16 06:46:25 +00:00
|
|
|
new Error('Model::findById requires the id argument')
|
|
|
|
);
|
2015-02-11 07:57:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-16 16:06:53 +00:00
|
|
|
it('should allow findById(id, filter, cb) for a matching id',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create({name: 'x', email: 'x@y.com'}, function(err, u) {
|
2015-04-16 16:06:53 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(u.id);
|
|
|
|
User.findById(u.id, filter, function(err, u) {
|
|
|
|
should.exist(u);
|
|
|
|
should.not.exist(err);
|
|
|
|
u.should.be.an.instanceOf(User);
|
|
|
|
u.should.have.property('name', 'x');
|
|
|
|
u.should.have.property('email', undefined);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-11 07:57:05 +00:00
|
|
|
it('should allow findById(id, options, cb) for a matching id',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create({name: 'y', email: 'y@y.com'}, function(err, u) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(u.id);
|
|
|
|
User.findById(u.id, options, function(err, u) {
|
|
|
|
should.exist(u);
|
|
|
|
should.not.exist(err);
|
|
|
|
u.should.be.an.instanceOf(User);
|
2015-04-16 16:06:53 +00:00
|
|
|
u.should.have.property('name', 'y');
|
|
|
|
u.should.have.property('email', 'y@y.com');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow findById(id, filter, options, cb) for a matching id',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create({name: 'z', email: 'z@y.com'}, function(err, u) {
|
2015-04-16 16:06:53 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(u.id);
|
|
|
|
User.findById(u.id, filter, options, function(err, u) {
|
|
|
|
should.exist(u);
|
|
|
|
should.not.exist(err);
|
|
|
|
u.should.be.an.instanceOf(User);
|
|
|
|
u.should.have.property('name', 'z');
|
|
|
|
u.should.have.property('email', undefined);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow promise-style findById',
|
|
|
|
function(done) {
|
2018-09-06 17:34:12 +00:00
|
|
|
User.create({id: 15, name: 'w', email: 'w@y.com'}).then(function(u) {
|
2015-04-16 16:06:53 +00:00
|
|
|
should.exist(u.id);
|
|
|
|
return User.findById(u.id).then(function(u) {
|
|
|
|
should.exist(u);
|
|
|
|
u.should.be.an.instanceOf(User);
|
|
|
|
u.should.have.property('name', 'w');
|
|
|
|
u.should.have.property('email', 'w@y.com');
|
|
|
|
return u;
|
|
|
|
});
|
|
|
|
}).then(function(u) {
|
|
|
|
should.exist(u);
|
|
|
|
should.exist(u.id);
|
|
|
|
return User.findById(u.id, filter).then(function(u) {
|
|
|
|
should.exist(u);
|
|
|
|
u.should.be.an.instanceOf(User);
|
|
|
|
u.should.have.property('name', 'w');
|
|
|
|
u.should.have.property('email', undefined);
|
|
|
|
return u;
|
|
|
|
});
|
|
|
|
}).then(function(u) {
|
|
|
|
should.exist(u);
|
|
|
|
should.exist(u.id);
|
|
|
|
return User.findById(u.id, options).then(function(u) {
|
|
|
|
should.exist(u);
|
|
|
|
u.should.be.an.instanceOf(User);
|
|
|
|
u.should.have.property('name', 'w');
|
|
|
|
u.should.have.property('email', 'w@y.com');
|
|
|
|
return u;
|
|
|
|
});
|
|
|
|
}).then(function(u) {
|
|
|
|
should.exist(u);
|
|
|
|
should.exist(u.id);
|
|
|
|
return User.findById(u.id, filter, options).then(function(u) {
|
|
|
|
should.exist(u);
|
|
|
|
u.should.be.an.instanceOf(User);
|
|
|
|
u.should.have.property('name', 'w');
|
|
|
|
u.should.have.property('email', undefined);
|
2015-02-11 07:57:05 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-04-16 16:06:53 +00:00
|
|
|
}).catch(function(err) {
|
|
|
|
done(err);
|
2015-02-11 07:57:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-13 17:34:40 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('findByIds', function() {
|
2015-02-11 07:57:05 +00:00
|
|
|
before(function(done) {
|
|
|
|
var people = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{id: 1, name: 'a', vip: true},
|
|
|
|
{id: 2, name: 'b'},
|
|
|
|
{id: 3, name: 'c'},
|
|
|
|
{id: 4, name: 'd', vip: true},
|
|
|
|
{id: 5, name: 'e'},
|
|
|
|
{id: 6, name: 'f'},
|
2015-02-11 07:57:05 +00:00
|
|
|
];
|
|
|
|
// Use automigrate so that serial keys are 1-6
|
|
|
|
db.automigrate(['User'], function(err) {
|
|
|
|
User.create(people, options, function(err, users) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow findByIds(ids, cb)', function(done) {
|
|
|
|
User.findByIds([3, 2, 1], function(err, users) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.exist(users);
|
|
|
|
should.not.exist(err);
|
|
|
|
var names = users.map(function(u) { return u.name; });
|
|
|
|
names.should.eql(['c', 'b', 'a']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow findByIds(ids, filter, options, cb)',
|
|
|
|
function(done) {
|
|
|
|
User.findByIds([4, 3, 2, 1],
|
2016-08-19 17:46:59 +00:00
|
|
|
{where: {vip: true}}, options, function(err, users) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.exist(users);
|
|
|
|
should.not.exist(err);
|
|
|
|
var names = users.map(function(u) {
|
|
|
|
return u.name;
|
|
|
|
});
|
|
|
|
names.should.eql(['d', 'a']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('find', function() {
|
2015-02-11 07:57:05 +00:00
|
|
|
before(seed);
|
|
|
|
|
|
|
|
it('should allow find(cb)', function(done) {
|
|
|
|
User.find(function(err, users) {
|
|
|
|
should.exists(users);
|
|
|
|
should.not.exists(err);
|
|
|
|
users.should.have.lengthOf(6);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow find(filter, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({limit: 3}, function(err, users) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.exists(users);
|
|
|
|
should.not.exists(err);
|
|
|
|
users.should.have.lengthOf(3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow find(filter, options, cb)', function(done) {
|
|
|
|
User.find({}, options, function(err, users) {
|
|
|
|
should.exists(users);
|
|
|
|
should.not.exists(err);
|
|
|
|
users.should.have.lengthOf(6);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow find(filter, options)', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({limit: 3}, options);
|
2015-02-11 07:57:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow find(filter)', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({limit: 3});
|
2015-02-11 07:57:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip trailing undefined args', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({limit: 3}, function(err, users) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.exists(users);
|
|
|
|
should.not.exists(err);
|
|
|
|
users.should.have.lengthOf(3);
|
|
|
|
done();
|
|
|
|
}, undefined, undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw on an invalid query arg', function() {
|
|
|
|
(function() {
|
|
|
|
User.find('invalid query', function(err, users) {
|
|
|
|
// noop
|
|
|
|
});
|
|
|
|
}).should.throw('The query argument must be an object');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw on an invalid options arg', function() {
|
|
|
|
(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({limit: 3}, 'invalid option', function(err, users) {
|
2015-02-11 07:57:05 +00:00
|
|
|
// noop
|
|
|
|
});
|
|
|
|
}).should.throw('The options argument must be an object');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw on an invalid cb arg', function() {
|
|
|
|
(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({limit: 3}, {}, 'invalid cb');
|
2015-02-11 07:57:05 +00:00
|
|
|
}).should.throw('The cb argument must be a function');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('count', function() {
|
2015-02-11 07:57:05 +00:00
|
|
|
before(seed);
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow count(cb)', function(done) {
|
|
|
|
User.count(function(err, n) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(n);
|
|
|
|
n.should.equal(6);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow count(where, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.count({role: 'lead'}, function(err, n) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(n);
|
|
|
|
n.should.equal(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow count(where, options, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.count({role: 'lead'}, options, function(err, n) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(n);
|
|
|
|
n.should.equal(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('findOne', function() {
|
2015-02-11 07:57:05 +00:00
|
|
|
before(seed);
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow findOne(cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({order: 'id'}, function(err, users) {
|
2016-04-01 11:48:17 +00:00
|
|
|
User.findOne(function(e, u) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(u);
|
|
|
|
u.id.toString().should.equal(users[0].id.toString());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow findOne(filter, options, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOne({order: 'order'}, options, function(e, u) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(u);
|
|
|
|
u.order.should.equal(1);
|
|
|
|
u.name.should.equal('Paul McCartney');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow findOne(filter, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOne({order: 'order'}, function(e, u) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(u);
|
|
|
|
u.order.should.equal(1);
|
|
|
|
u.name.should.equal('Paul McCartney');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow trailing undefined args', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOne({order: 'order'}, function(e, u) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(u);
|
|
|
|
u.order.should.equal(1);
|
|
|
|
u.name.should.equal('Paul McCartney');
|
|
|
|
done();
|
|
|
|
}, undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('exists', function() {
|
2015-02-11 07:57:05 +00:00
|
|
|
before(seed);
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow exists(id, cb)', function(done) {
|
|
|
|
User.findOne(function(e, u) {
|
|
|
|
User.exists(u.id, function(err, exists) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(exists);
|
|
|
|
exists.should.be.ok;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow exists(id, options, cb)', function(done) {
|
|
|
|
User.destroyAll(function() {
|
|
|
|
User.exists(42, options, function(err, exists) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
exists.should.not.be.ok;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('save', function() {
|
|
|
|
it('should allow save(options, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var options = {foo: 'bar'};
|
2015-05-10 08:44:22 +00:00
|
|
|
var opts;
|
2015-03-20 16:49:32 +00:00
|
|
|
|
2015-05-10 08:44:22 +00:00
|
|
|
User.observe('after save', function(ctx, next) {
|
|
|
|
opts = ctx.options;
|
|
|
|
next();
|
|
|
|
});
|
2015-03-20 16:49:32 +00:00
|
|
|
|
2015-05-10 08:44:22 +00:00
|
|
|
var u = new User();
|
|
|
|
u.save(options, function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
options.should.equal(opts);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('destroyAll with options', function() {
|
2015-02-11 07:57:05 +00:00
|
|
|
beforeEach(seed);
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow destroyAll(where, options, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.destroyAll({name: 'John Lennon'}, options, function(err) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Lennon'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(0);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'Paul McCartney'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow destroyAll(where, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.destroyAll({name: 'John Lennon'}, function(err) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Lennon'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(0);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'Paul McCartney'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow destroyAll(cb)', function(done) {
|
|
|
|
User.destroyAll(function(err) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Lennon'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(0);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'Paul McCartney'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('updateAll ', function() {
|
2015-02-11 07:57:05 +00:00
|
|
|
beforeEach(seed);
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow updateAll(where, data, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.update({name: 'John Lennon'}, {name: 'John Smith'}, function(err) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Lennon'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(0);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Smith'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow updateAll(where, data, options, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.update({name: 'John Lennon'}, {name: 'John Smith'}, options,
|
2015-02-11 07:57:05 +00:00
|
|
|
function(err) {
|
|
|
|
should.not.exist(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Lennon'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(0);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Smith'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow updateAll(data, cb)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.update({name: 'John Smith'}, function() {
|
|
|
|
User.find({where: {name: 'John Lennon'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(0);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Smith'}}, function(err, data) {
|
2015-02-11 07:57:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
data.length.should.equal(6);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-06 17:34:12 +00:00
|
|
|
|
|
|
|
describe('updateAttributes', function() {
|
|
|
|
beforeEach(seed);
|
|
|
|
it('preserves document properties not modified by the patch', function() {
|
|
|
|
return User.findOne({where: {name: 'John Lennon'}})
|
|
|
|
.then(function(user) {
|
|
|
|
return user.updateAttributes({address: {city: 'Volos'}});
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
return User.findOne({where: {name: 'John Lennon'}}); // retrieve the user again from the db
|
|
|
|
})
|
|
|
|
.then(function(updatedUser) {
|
|
|
|
updatedUser.address.city.should.equal('Volos');
|
|
|
|
should(updatedUser.address.area).not.be.exactly(null);
|
|
|
|
should(updatedUser.address.area).be.undefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-11 07:57:05 +00:00
|
|
|
});
|
|
|
|
|
2016-08-16 12:36:01 +00:00
|
|
|
describe('upsertWithWhere', function() {
|
|
|
|
beforeEach(seed);
|
|
|
|
it('rejects upsertWithWhere (options,cb)', function(done) {
|
|
|
|
try {
|
|
|
|
User.upsertWithWhere({}, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
} catch (ex) {
|
|
|
|
ex.message.should.equal('The data argument must be an object');
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects upsertWithWhere (cb)', function(done) {
|
|
|
|
try {
|
|
|
|
User.upsertWithWhere(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
} catch (ex) {
|
|
|
|
ex.message.should.equal('The where argument must be an object');
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows upsertWithWhere by accepting where,data and cb as arguments', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.upsertWithWhere({name: 'John Lennon'}, {name: 'John Smith'}, function(err) {
|
2016-08-16 12:36:01 +00:00
|
|
|
if (err) return done(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Lennon'}}, function(err, data) {
|
2016-08-16 12:36:01 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
data.length.should.equal(0);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Smith'}}, function(err, data) {
|
2016-08-16 12:36:01 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
data.length.should.equal(1);
|
|
|
|
data[0].name.should.equal('John Smith');
|
|
|
|
data[0].email.should.equal('john@b3atl3s.co.uk');
|
|
|
|
data[0].role.should.equal('lead');
|
|
|
|
data[0].order.should.equal(2);
|
|
|
|
data[0].vip.should.equal(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows upsertWithWhere by accepting where, data, options, and cb as arguments', function(done) {
|
|
|
|
options = {};
|
2016-08-19 17:46:59 +00:00
|
|
|
User.upsertWithWhere({name: 'John Lennon'}, {name: 'John Smith'}, options, function(err) {
|
2016-08-16 12:36:01 +00:00
|
|
|
if (err) return done(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.find({where: {name: 'John Smith'}}, function(err, data) {
|
2016-08-16 12:36:01 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
data.length.should.equal(1);
|
|
|
|
data[0].name.should.equal('John Smith');
|
|
|
|
data[0].seq.should.equal(0);
|
|
|
|
data[0].email.should.equal('john@b3atl3s.co.uk');
|
|
|
|
data[0].role.should.equal('lead');
|
|
|
|
data[0].order.should.equal(2);
|
|
|
|
data[0].vip.should.equal(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-11 07:57:05 +00:00
|
|
|
function seed(done) {
|
|
|
|
var beatles = [
|
|
|
|
{
|
2018-09-06 17:34:12 +00:00
|
|
|
id: 0,
|
2015-02-11 07:57:05 +00:00
|
|
|
seq: 0,
|
|
|
|
name: 'John Lennon',
|
|
|
|
email: 'john@b3atl3s.co.uk',
|
|
|
|
role: 'lead',
|
|
|
|
birthday: new Date('1980-12-08'),
|
|
|
|
order: 2,
|
2016-04-01 11:48:17 +00:00
|
|
|
vip: true,
|
2015-02-11 07:57:05 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-06 17:34:12 +00:00
|
|
|
id: 1,
|
2015-02-11 07:57:05 +00:00
|
|
|
seq: 1,
|
|
|
|
name: 'Paul McCartney',
|
|
|
|
email: 'paul@b3atl3s.co.uk',
|
|
|
|
role: 'lead',
|
|
|
|
birthday: new Date('1942-06-18'),
|
|
|
|
order: 1,
|
2016-04-01 11:48:17 +00:00
|
|
|
vip: true,
|
2015-02-11 07:57:05 +00:00
|
|
|
},
|
2018-09-06 17:34:12 +00:00
|
|
|
{id: 2, seq: 2, name: 'George Harrison', order: 5, vip: false},
|
|
|
|
{id: 3, seq: 3, name: 'Ringo Starr', order: 6, vip: false},
|
|
|
|
{id: 4, seq: 4, name: 'Pete Best', order: 4},
|
|
|
|
{id: 5, seq: 5, name: 'Stuart Sutcliffe', order: 3, vip: true},
|
2015-02-11 07:57:05 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
User.destroyAll.bind(User),
|
|
|
|
function(cb) {
|
|
|
|
async.each(beatles, User.create.bind(User), cb);
|
2016-04-01 11:48:17 +00:00
|
|
|
},
|
2015-02-11 07:57:05 +00:00
|
|
|
], done);
|
|
|
|
}
|