2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 2013,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
|
|
|
|
|
2013-04-06 10:57:12 +00:00
|
|
|
// This test written in mocha+should.js
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
2013-04-06 10:57:12 +00:00
|
|
|
var should = require('./init.js');
|
2014-09-04 15:31:53 +00:00
|
|
|
var jdb = require('../');
|
2014-09-04 16:01:04 +00:00
|
|
|
var DataSource = jdb.DataSource;
|
2015-02-18 05:04:31 +00:00
|
|
|
var createPromiseCallback = require('../lib/utils.js').createPromiseCallback;
|
2013-04-06 10:57:12 +00:00
|
|
|
|
2014-08-30 08:53:10 +00:00
|
|
|
var db, tmp, Book, Chapter, Author, Reader;
|
2014-08-26 05:17:40 +00:00
|
|
|
var Category, Job;
|
2014-07-26 12:54:54 +00:00
|
|
|
var Picture, PictureLink;
|
2014-07-27 14:30:45 +00:00
|
|
|
var Person, Address;
|
2014-07-29 08:51:33 +00:00
|
|
|
var Link;
|
2013-03-26 00:39:47 +00:00
|
|
|
|
2014-09-04 15:56:26 +00:00
|
|
|
var getTransientDataSource = function(settings) {
|
2016-04-01 11:48:17 +00:00
|
|
|
return new DataSource('transient', settings, db.modelBuilder);
|
2014-09-04 13:33:12 +00:00
|
|
|
};
|
|
|
|
|
2014-09-07 10:59:47 +00:00
|
|
|
var getMemoryDataSource = function(settings) {
|
2016-04-01 11:48:17 +00:00
|
|
|
return new DataSource('memory', settings, db.modelBuilder);
|
2014-09-07 10:59:47 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('relations', function() {
|
2014-07-15 23:10:37 +00:00
|
|
|
|
2015-02-21 00:10:25 +00:00
|
|
|
before(function() {
|
|
|
|
db = getSchema();
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasMany', function() {
|
|
|
|
before(function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Book = db.define('Book', {name: String, type: String});
|
|
|
|
Chapter = db.define('Chapter', {name: {type: String, index: true},
|
|
|
|
bookType: String});
|
|
|
|
Author = db.define('Author', {name: String});
|
|
|
|
Reader = db.define('Reader', {name: String});
|
2014-07-15 23:10:37 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Book', 'Chapter', 'Author', 'Reader'], done);
|
2013-03-26 00:39:47 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared in different ways', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
Book.hasMany(Chapter);
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.hasMany(Reader, {as: 'users'});
|
|
|
|
Book.hasMany(Author, {foreignKey: 'projectId'});
|
2014-01-24 17:09:53 +00:00
|
|
|
var b = new Book;
|
|
|
|
b.chapters.should.be.an.instanceOf(Function);
|
|
|
|
b.users.should.be.an.instanceOf(Function);
|
|
|
|
b.authors.should.be.an.instanceOf(Function);
|
2015-02-03 10:37:43 +00:00
|
|
|
Object.keys((new Chapter).toObject()).should.containEql('bookId');
|
|
|
|
Object.keys((new Author).toObject()).should.containEql('projectId');
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Book', 'Chapter', 'Author', 'Reader'], done);
|
2013-03-26 00:39:47 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared in short form', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
Author.hasMany('readers');
|
|
|
|
(new Author).readers.should.be.an.instanceOf(Function);
|
2015-02-03 10:37:43 +00:00
|
|
|
Object.keys((new Reader).toObject()).should.containEql('authorId');
|
2013-03-26 00:39:47 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.autoupdate(['Author', 'Reader'], done);
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 20:48:14 +00:00
|
|
|
|
2014-11-07 23:21:15 +00:00
|
|
|
describe('with scope', function() {
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2014-11-07 23:21:15 +00:00
|
|
|
Book.hasMany(Chapter);
|
|
|
|
done();
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 20:48:14 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should build record on scope', function(done) {
|
|
|
|
Book.create(function(err, book) {
|
2014-11-07 23:21:15 +00:00
|
|
|
var c = book.chapters.build();
|
2015-04-24 23:50:15 +00:00
|
|
|
c.bookId.should.eql(book.id);
|
2014-11-07 23:21:15 +00:00
|
|
|
c.save(done);
|
2013-03-26 20:48:14 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 20:48:14 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope', function(done) {
|
|
|
|
Book.create(function(err, book) {
|
|
|
|
book.chapters.create(function(err, c) {
|
2014-11-07 23:21:15 +00:00
|
|
|
should.not.exist(err);
|
2014-01-24 17:09:53 +00:00
|
|
|
should.exist(c);
|
2015-04-24 23:50:15 +00:00
|
|
|
c.bookId.should.eql(book.id);
|
2014-01-24 17:09:53 +00:00
|
|
|
done();
|
|
|
|
});
|
2013-03-26 20:48:14 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-04-03 20:46:41 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Book.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then (function(book) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return book.chapters.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then (function(c) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(c);
|
2015-04-24 23:50:15 +00:00
|
|
|
c.bookId.should.eql(book.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create a batch of records on scope', function(done) {
|
2014-11-08 02:01:18 +00:00
|
|
|
var chapters = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{name: 'a'},
|
|
|
|
{name: 'z'},
|
|
|
|
{name: 'c'},
|
2014-11-08 02:01:18 +00:00
|
|
|
];
|
2016-04-01 11:48:17 +00:00
|
|
|
Book.create(function(err, book) {
|
|
|
|
book.chapters.create(chapters, function(err, chs) {
|
2014-11-08 02:01:18 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(chs);
|
|
|
|
chs.should.have.lengthOf(chapters.length);
|
|
|
|
chs.forEach(function(c) {
|
2015-04-24 23:50:15 +00:00
|
|
|
c.bookId.should.eql(book.id);
|
2014-11-08 02:01:18 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
done();
|
|
|
|
});
|
2013-03-26 00:39:47 +00:00
|
|
|
});
|
2014-11-08 02:01:18 +00:00
|
|
|
});
|
2013-04-03 20:46:41 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create a batch of records on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var chapters = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{name: 'a'},
|
|
|
|
{name: 'z'},
|
|
|
|
{name: 'c'},
|
2015-02-18 05:04:31 +00:00
|
|
|
];
|
2016-04-01 11:48:17 +00:00
|
|
|
Book.create(function(err, book) {
|
2015-02-18 05:04:31 +00:00
|
|
|
book.chapters.create(chapters)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(chs) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(chs);
|
|
|
|
chs.should.have.lengthOf(chapters.length);
|
|
|
|
chs.forEach(function(c) {
|
2015-04-24 23:50:15 +00:00
|
|
|
c.bookId.should.eql(book.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
});
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should fetch all scoped instances', function(done) {
|
|
|
|
Book.create(function(err, book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'a'}, function() {
|
|
|
|
book.chapters.create({name: 'z'}, function() {
|
|
|
|
book.chapters.create({name: 'c'}, function() {
|
2014-11-07 23:21:15 +00:00
|
|
|
verify(book);
|
|
|
|
});
|
2013-04-03 20:46:41 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-04-03 20:46:41 +00:00
|
|
|
});
|
2014-11-07 23:21:15 +00:00
|
|
|
function verify(book) {
|
2016-04-01 11:48:17 +00:00
|
|
|
book.chapters(function(err, ch) {
|
2014-11-07 23:21:15 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(3);
|
|
|
|
|
|
|
|
var chapters = book.chapters();
|
|
|
|
chapters.should.eql(ch);
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters({order: 'name DESC'}, function(e, c) {
|
2014-11-07 23:21:15 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(c);
|
|
|
|
|
|
|
|
c.shift().name.should.equal('z');
|
|
|
|
c.pop().name.should.equal('a');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should fetch all scoped instances with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Book.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'z'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'c'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
return verify(book);
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
|
|
|
|
function verify(book) {
|
|
|
|
return book.chapters.getAsync()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(3);
|
|
|
|
|
|
|
|
var chapters = book.chapters();
|
|
|
|
chapters.should.eql(ch);
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.getAsync({order: 'name DESC'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(c) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(c);
|
|
|
|
|
|
|
|
c.shift().name.should.equal('z');
|
|
|
|
c.pop().name.should.equal('a');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should fetch all scoped instances with getAsync with callback and condition', function(done) {
|
|
|
|
Book.create(function(err, book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'a'}, function() {
|
|
|
|
book.chapters.create({name: 'z'}, function() {
|
|
|
|
book.chapters.create({name: 'c'}, function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
verify(book);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function verify(book) {
|
2016-04-01 11:48:17 +00:00
|
|
|
book.chapters(function(err, ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(3);
|
|
|
|
|
|
|
|
var chapters = book.chapters();
|
|
|
|
chapters.should.eql(ch);
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.getAsync({order: 'name DESC'}, function(e, c) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(c);
|
|
|
|
|
|
|
|
c.shift().name.should.equal('z');
|
|
|
|
c.pop().name.should.equal('a');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should fetch all scoped instances with getAsync with callback and no condition', function(done) {
|
|
|
|
Book.create(function(err, book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'a'}, function() {
|
|
|
|
book.chapters.create({name: 'z'}, function() {
|
|
|
|
book.chapters.create({name: 'c'}, function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
verify(book);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function verify(book) {
|
2016-04-01 11:48:17 +00:00
|
|
|
book.chapters(function(err, ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(3);
|
|
|
|
|
|
|
|
var chapters = book.chapters();
|
|
|
|
chapters.should.eql(ch);
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
book.chapters.getAsync(function(e, c) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(c);
|
|
|
|
should.exist(c.length);
|
|
|
|
c.shift().name.should.equal('a');
|
|
|
|
c.pop().name.should.equal('c');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find scoped record', function(done) {
|
2014-11-07 23:21:15 +00:00
|
|
|
var id;
|
2016-04-01 11:48:17 +00:00
|
|
|
Book.create(function(err, book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'a'}, function(err, ch) {
|
2014-11-07 23:21:15 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'z'}, function() {
|
|
|
|
book.chapters.create({name: 'c'}, function() {
|
2014-11-07 23:21:15 +00:00
|
|
|
verify(book);
|
|
|
|
});
|
2014-08-26 12:54:19 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-07 23:21:15 +00:00
|
|
|
function verify(book) {
|
2016-04-01 11:48:17 +00:00
|
|
|
book.chapters.findById(id, function(err, ch) {
|
2014-08-26 12:54:19 +00:00
|
|
|
should.not.exist(err);
|
2014-11-07 23:21:15 +00:00
|
|
|
should.exist(ch);
|
|
|
|
ch.id.should.eql(id);
|
2014-08-26 12:54:19 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-11-07 23:21:15 +00:00
|
|
|
}
|
|
|
|
});
|
2014-07-15 15:50:34 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find scoped record with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var id;
|
|
|
|
Book.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'z'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'c'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
return verify(book);
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
}).catch(done);
|
|
|
|
|
|
|
|
function verify(book) {
|
|
|
|
return book.chapters.findById(id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(ch);
|
|
|
|
ch.id.should.eql(id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should count scoped records - all and filtered', function(done) {
|
|
|
|
Book.create(function(err, book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'a'}, function(err, ch) {
|
|
|
|
book.chapters.create({name: 'b'}, function() {
|
|
|
|
book.chapters.create({name: 'c'}, function() {
|
2014-11-07 23:21:15 +00:00
|
|
|
verify(book);
|
|
|
|
});
|
|
|
|
});
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-07 23:21:15 +00:00
|
|
|
|
|
|
|
function verify(book) {
|
2016-04-01 11:48:17 +00:00
|
|
|
book.chapters.count(function(err, count) {
|
2014-11-07 23:21:15 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
count.should.equal(3);
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.count({name: 'b'}, function(err, count) {
|
2014-11-07 23:21:15 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
count.should.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should count scoped records - all and filtered with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Book.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'b'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'c'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
return verify(book);
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
|
|
|
|
function verify(book) {
|
|
|
|
return book.chapters.count()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(count) {
|
2015-02-18 05:04:31 +00:00
|
|
|
count.should.equal(3);
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.count({name: 'b'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(count) {
|
2015-02-18 05:04:31 +00:00
|
|
|
count.should.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-11-07 23:21:15 +00:00
|
|
|
it('should set targetClass on scope property', function() {
|
|
|
|
should.equal(Book.prototype.chapters._targetClass, 'Chapter');
|
|
|
|
});
|
2014-07-15 15:50:34 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should update scoped record', function(done) {
|
2014-11-07 23:21:15 +00:00
|
|
|
var id;
|
2016-04-01 11:48:17 +00:00
|
|
|
Book.create(function(err, book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'a'}, function(err, ch) {
|
2014-11-07 23:21:15 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.updateById(id, {name: 'aa'}, function(err, ch) {
|
2014-11-07 23:21:15 +00:00
|
|
|
verify(book);
|
|
|
|
});
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-07 23:21:15 +00:00
|
|
|
|
|
|
|
function verify(book) {
|
2016-04-01 11:48:17 +00:00
|
|
|
book.chapters.findById(id, function(err, ch) {
|
2014-11-07 23:21:15 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.id.should.eql(id);
|
|
|
|
ch.name.should.equal('aa');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should update scoped record with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var id;
|
|
|
|
Book.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.updateById(id, {name: 'aa'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
|
|
|
.then(function(ch) {
|
|
|
|
return verify(book);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
|
|
|
|
function verify(book) {
|
|
|
|
return book.chapters.findById(id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(ch);
|
|
|
|
ch.id.should.eql(id);
|
|
|
|
ch.name.should.equal('aa');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should destroy scoped record', function(done) {
|
2014-11-07 23:21:15 +00:00
|
|
|
var id;
|
2016-04-01 11:48:17 +00:00
|
|
|
Book.create(function(err, book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'a'}, function(err, ch) {
|
2014-11-07 23:21:15 +00:00
|
|
|
id = ch.id;
|
|
|
|
book.chapters.destroy(id, function(err, ch) {
|
|
|
|
verify(book);
|
|
|
|
});
|
|
|
|
});
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
|
|
|
|
2014-11-07 23:21:15 +00:00
|
|
|
function verify(book) {
|
2016-04-01 11:48:17 +00:00
|
|
|
book.chapters.findById(id, function(err, ch) {
|
2014-11-07 23:21:15 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should destroy scoped record with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var id;
|
|
|
|
Book.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
id = ch.id;
|
2016-04-01 11:48:17 +00:00
|
|
|
return book.chapters.destroy(id);
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
|
|
|
.then(function(ch) {
|
|
|
|
return verify(book);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
|
|
|
|
function verify(book) {
|
|
|
|
return book.chapters.findById(id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.catch(function(err) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check existence of a scoped record', function(done) {
|
2014-11-07 23:21:15 +00:00
|
|
|
var id;
|
2016-04-01 11:48:17 +00:00
|
|
|
Book.create(function(err, book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'a'}, function(err, ch) {
|
2014-11-07 23:21:15 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
book.chapters.create({name: 'z'}, function() {
|
|
|
|
book.chapters.create({name: 'c'}, function() {
|
2014-11-07 23:21:15 +00:00
|
|
|
verify(book);
|
|
|
|
});
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-07 23:21:15 +00:00
|
|
|
|
|
|
|
function verify(book) {
|
2016-04-01 11:48:17 +00:00
|
|
|
book.chapters.exists(id, function(err, flag) {
|
2014-11-07 23:21:15 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
flag.should.be.eql(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check existence of a scoped record with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var id;
|
|
|
|
Book.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(book) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'z'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return book.chapters.create({name: 'c'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
return verify(book);
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
|
|
|
|
function verify(book) {
|
|
|
|
return book.chapters.exists(id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(flag) {
|
2015-02-18 05:04:31 +00:00
|
|
|
flag.should.be.eql(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check ignore related data on creation - array', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.create({chapters: []}, function(err, book) {
|
2014-07-15 15:50:34 +00:00
|
|
|
should.not.exist(err);
|
2014-11-07 23:21:15 +00:00
|
|
|
book.chapters.should.be.a.function;
|
|
|
|
var obj = book.toObject();
|
|
|
|
should.not.exist(obj.chapters);
|
2014-07-15 15:50:34 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-08-21 08:44:55 +00:00
|
|
|
});
|
2014-11-07 23:21:15 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check ignore related data on creation with promises - array', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.create({chapters: []})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(book) {
|
2015-02-18 05:04:31 +00:00
|
|
|
book.chapters.should.be.a.function;
|
|
|
|
var obj = book.toObject();
|
|
|
|
should.not.exist(obj.chapters);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check ignore related data on creation - object', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.create({chapters: {}}, function(err, book) {
|
2014-11-07 23:21:15 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
book.chapters.should.be.a.function;
|
|
|
|
var obj = book.toObject();
|
|
|
|
should.not.exist(obj.chapters);
|
|
|
|
done();
|
|
|
|
});
|
2014-08-21 08:44:55 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check ignore related data on creation with promises - object', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.create({chapters: {}})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(book) {
|
2015-02-18 05:04:31 +00:00
|
|
|
book.chapters.should.be.a.function;
|
|
|
|
var obj = book.toObject();
|
|
|
|
should.not.exist(obj.chapters);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2014-08-21 08:44:55 +00:00
|
|
|
});
|
2014-11-07 23:21:15 +00:00
|
|
|
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasMany through', function() {
|
2014-08-20 12:03:38 +00:00
|
|
|
var Physician, Patient, Appointment, Address;
|
2014-07-15 15:50:34 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician = db.define('Physician', {name: String});
|
|
|
|
Patient = db.define('Patient', {name: String});
|
|
|
|
Appointment = db.define('Appointment', {date: {type: Date,
|
2016-04-01 11:48:17 +00:00
|
|
|
default: function() {
|
2014-07-15 15:50:34 +00:00
|
|
|
return new Date();
|
2016-08-19 17:46:59 +00:00
|
|
|
}}});
|
|
|
|
Address = db.define('Address', {name: String});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician.hasMany(Patient, {through: Appointment});
|
|
|
|
Patient.hasMany(Physician, {through: Appointment});
|
2014-08-20 12:03:38 +00:00
|
|
|
Patient.belongsTo(Address);
|
2014-07-15 15:50:34 +00:00
|
|
|
Appointment.belongsTo(Patient);
|
|
|
|
Appointment.belongsTo(Physician);
|
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Physician', 'Patient', 'Appointment', 'Address'], done);
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should build record on scope', function(done) {
|
|
|
|
Physician.create(function(err, physician) {
|
2014-07-15 15:50:34 +00:00
|
|
|
var patient = physician.patients.build();
|
2015-04-24 23:50:15 +00:00
|
|
|
patient.physicianId.should.eql(physician.id);
|
2014-07-15 15:50:34 +00:00
|
|
|
patient.save(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope', function(done) {
|
|
|
|
Physician.create(function(err, physician) {
|
|
|
|
physician.patients.create(function(err, patient) {
|
2014-07-15 15:50:34 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(patient);
|
2016-08-19 17:46:59 +00:00
|
|
|
Appointment.find({where: {physicianId: physician.id, patientId: patient.id}},
|
2014-07-15 15:50:34 +00:00
|
|
|
function(err, apps) {
|
|
|
|
should.not.exist(err);
|
|
|
|
apps.should.have.lengthOf(1);
|
|
|
|
done();
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2014-07-15 15:50:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Physician.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return physician.patients.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(patient) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(patient);
|
2016-08-19 17:46:59 +00:00
|
|
|
return Appointment.find({where: {physicianId: physician.id, patientId: patient.id}})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(apps) {
|
|
|
|
apps.should.have.lengthOf(1);
|
|
|
|
done();
|
2015-02-18 05:04:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create multiple records on scope', function(done) {
|
2014-11-11 21:36:49 +00:00
|
|
|
var async = require('async');
|
2016-04-01 11:48:17 +00:00
|
|
|
Physician.create(function(err, physician) {
|
|
|
|
physician.patients.create([{}, {}], function(err, patients) {
|
2014-11-11 21:36:49 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(patients);
|
|
|
|
patients.should.have.lengthOf(2);
|
|
|
|
function verifyPatient(patient, next) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Appointment.find({where: {
|
2014-11-11 21:36:49 +00:00
|
|
|
physicianId: physician.id,
|
2016-04-01 11:48:17 +00:00
|
|
|
patientId: patient.id,
|
2014-11-11 21:36:49 +00:00
|
|
|
}},
|
|
|
|
function(err, apps) {
|
|
|
|
should.not.exist(err);
|
|
|
|
apps.should.have.lengthOf(1);
|
|
|
|
next();
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2014-11-11 21:36:49 +00:00
|
|
|
}
|
|
|
|
async.forEach(patients, verifyPatient, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create multiple records on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var async = require('async');
|
|
|
|
Physician.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return physician.patients.create([{}, {}])
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(patients) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(patients);
|
|
|
|
patients.should.have.lengthOf(2);
|
|
|
|
function verifyPatient(patient, next) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Appointment.find({where: {
|
2015-02-18 05:04:31 +00:00
|
|
|
physicianId: physician.id,
|
2016-04-01 11:48:17 +00:00
|
|
|
patientId: patient.id,
|
2015-02-18 05:04:31 +00:00
|
|
|
}})
|
|
|
|
.then(function(apps) {
|
2016-04-01 11:48:17 +00:00
|
|
|
apps.should.have.lengthOf(1);
|
|
|
|
next();
|
2015-02-18 05:04:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
async.forEach(patients, verifyPatient, done);
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should fetch all scoped instances', function(done) {
|
|
|
|
Physician.create(function(err, physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.create({name: 'a'}, function() {
|
|
|
|
physician.patients.create({name: 'z'}, function() {
|
|
|
|
physician.patients.create({name: 'c'}, function() {
|
2014-07-15 15:50:34 +00:00
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function verify(physician) {
|
2016-04-01 11:48:17 +00:00
|
|
|
physician.patients(function(err, ch) {
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-04 19:25:26 +00:00
|
|
|
var patients = physician.patients();
|
|
|
|
patients.should.eql(ch);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-15 15:50:34 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should fetch all scoped instances with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Physician.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'z'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'c'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
return verify(physician);
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
}).catch(done);
|
|
|
|
function verify(physician) {
|
|
|
|
return physician.patients.getAsync()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
|
|
|
|
var patients = physician.patients();
|
|
|
|
patients.should.eql(ch);
|
|
|
|
|
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should fetch scoped instances with paging filters', function(done) {
|
|
|
|
Physician.create(function(err, physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.create({name: 'a'}, function() {
|
|
|
|
physician.patients.create({name: 'z'}, function() {
|
|
|
|
physician.patients.create({name: 'c'}, function() {
|
2015-05-13 05:18:50 +00:00
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function verify(physician) {
|
2015-05-14 01:55:54 +00:00
|
|
|
//limit plus skip
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients({limit: 1, skip: 1}, function(err, ch) {
|
2015-05-13 05:18:50 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(1);
|
|
|
|
ch[0].name.should.eql('z');
|
2015-05-14 01:55:54 +00:00
|
|
|
//offset plus skip
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients({limit: 1, offset: 1}, function(err1, ch1) {
|
2015-05-14 01:55:54 +00:00
|
|
|
should.not.exist(err1);
|
|
|
|
should.exist(ch1);
|
|
|
|
ch1.should.have.lengthOf(1);
|
|
|
|
ch1[0].name.should.eql('z');
|
|
|
|
//order
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients({order: 'patientId DESC'}, function(err2, ch2) {
|
2015-05-14 01:55:54 +00:00
|
|
|
should.not.exist(err2);
|
|
|
|
should.exist(ch2);
|
|
|
|
ch2.should.have.lengthOf(3);
|
|
|
|
ch2[0].name.should.eql('c');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-05-13 05:18:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find scoped record', function(done) {
|
2014-07-15 15:50:34 +00:00
|
|
|
var id;
|
2016-04-01 11:48:17 +00:00
|
|
|
Physician.create(function(err, physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.create({name: 'a'}, function(err, ch) {
|
2014-07-15 15:50:34 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.create({name: 'z'}, function() {
|
|
|
|
physician.patients.create({name: 'c'}, function() {
|
2014-07-15 15:50:34 +00:00
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(physician) {
|
2016-04-01 11:48:17 +00:00
|
|
|
physician.patients.findById(id, function(err, ch) {
|
2014-07-15 15:50:34 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
2014-09-06 12:38:57 +00:00
|
|
|
ch.id.should.eql(id);
|
2014-07-15 15:50:34 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find scoped record with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var id;
|
|
|
|
Physician.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'z'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'c'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
return verify(physician);
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
|
|
|
|
function verify(physician) {
|
2016-04-01 11:48:17 +00:00
|
|
|
return physician.patients.findById(id, function(err, ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.id.should.eql(id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to use include syntax on related data', function(done) {
|
|
|
|
Physician.create(function(err, physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.create({name: 'a'}, function(err, patient) {
|
|
|
|
Address.create({name: 'z'}, function(err, address) {
|
2014-08-20 12:03:38 +00:00
|
|
|
should.not.exist(err);
|
2014-07-29 09:05:57 +00:00
|
|
|
patient.address(address);
|
|
|
|
patient.save(function() {
|
2014-08-08 16:39:36 +00:00
|
|
|
verify(physician, address.id);
|
2014-07-29 09:05:57 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-08-08 16:39:36 +00:00
|
|
|
function verify(physician, addressId) {
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients({include: 'address'}, function(err, ch) {
|
2014-07-29 09:05:57 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(1);
|
2014-08-08 16:39:36 +00:00
|
|
|
ch[0].addressId.should.eql(addressId);
|
2014-07-29 09:05:57 +00:00
|
|
|
var address = ch[0].address();
|
|
|
|
should.exist(address);
|
|
|
|
address.should.be.an.instanceof(Address);
|
|
|
|
address.name.should.equal('z');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to use include syntax on related data with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Physician.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(patient) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return Address.create({name: 'z'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(address) {
|
2015-02-18 05:04:31 +00:00
|
|
|
patient.address(address);
|
|
|
|
return patient.save()
|
|
|
|
.then(function() {
|
|
|
|
return verify(physician, address.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
|
|
|
|
function verify(physician, addressId) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.getAsync({include: 'address'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(1);
|
|
|
|
ch[0].addressId.should.eql(addressId);
|
|
|
|
var address = ch[0].address();
|
|
|
|
should.exist(address);
|
|
|
|
address.should.be.an.instanceof(Address);
|
|
|
|
address.name.should.equal('z');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-07-15 15:50:34 +00:00
|
|
|
it('should set targetClass on scope property', function() {
|
|
|
|
should.equal(Physician.prototype.patients._targetClass, 'Patient');
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should update scoped record', function(done) {
|
2014-07-15 15:50:34 +00:00
|
|
|
var id;
|
2016-04-01 11:48:17 +00:00
|
|
|
Physician.create(function(err, physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.create({name: 'a'}, function(err, ch) {
|
2014-07-15 15:50:34 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.updateById(id, {name: 'aa'}, function(err, ch) {
|
2014-07-15 15:50:34 +00:00
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(physician) {
|
2016-04-01 11:48:17 +00:00
|
|
|
physician.patients.findById(id, function(err, ch) {
|
2014-07-15 15:50:34 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
2014-09-06 12:38:57 +00:00
|
|
|
ch.id.should.eql(id);
|
2014-07-15 15:50:34 +00:00
|
|
|
ch.name.should.equal('aa');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should update scoped record with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var id;
|
|
|
|
Physician.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.updateById(id, {name: 'aa'})
|
2015-02-18 05:04:31 +00:00
|
|
|
.then(function(ch) {
|
|
|
|
return verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
|
|
|
|
function verify(physician) {
|
|
|
|
return physician.patients.findById(id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(ch);
|
|
|
|
ch.id.should.eql(id);
|
|
|
|
ch.name.should.equal('aa');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should destroy scoped record', function(done) {
|
2014-07-15 15:50:34 +00:00
|
|
|
var id;
|
2016-04-01 11:48:17 +00:00
|
|
|
Physician.create(function(err, physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.create({name: 'a'}, function(err, ch) {
|
2014-07-15 15:50:34 +00:00
|
|
|
id = ch.id;
|
|
|
|
physician.patients.destroy(id, function(err, ch) {
|
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(physician) {
|
2016-04-01 11:48:17 +00:00
|
|
|
physician.patients.findById(id, function(err, ch) {
|
2014-07-15 15:50:34 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should destroy scoped record with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var id;
|
|
|
|
Physician.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
id = ch.id;
|
|
|
|
return physician.patients.destroy(id)
|
|
|
|
.then(function(ch) {
|
|
|
|
return verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
|
|
|
|
function verify(physician) {
|
|
|
|
return physician.patients.findById(id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.not.exist(ch);
|
|
|
|
done();
|
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.catch(function(err) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check existence of a scoped record', function(done) {
|
2014-07-15 15:50:34 +00:00
|
|
|
var id;
|
2016-04-01 11:48:17 +00:00
|
|
|
Physician.create(function(err, physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.create({name: 'a'}, function(err, ch) {
|
2014-08-20 12:03:38 +00:00
|
|
|
should.not.exist(err);
|
2014-07-15 15:50:34 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.create({name: 'z'}, function() {
|
|
|
|
physician.patients.create({name: 'c'}, function() {
|
2014-07-15 15:50:34 +00:00
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(physician) {
|
2016-04-01 11:48:17 +00:00
|
|
|
physician.patients.exists(id, function(err, flag) {
|
2014-07-15 15:50:34 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
flag.should.be.eql(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check existence of a scoped record with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var id;
|
|
|
|
Physician.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
id = ch.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'z'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'c'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
return verify(physician);
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
|
|
|
|
function verify(physician) {
|
|
|
|
return physician.patients.exists(id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(flag) {
|
2015-02-18 05:04:31 +00:00
|
|
|
flag.should.be.eql(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to add connection with instance', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician.create({name: 'ph1'}, function(e, physician) {
|
|
|
|
Patient.create({name: 'pa1'}, function(e, patient) {
|
2016-04-01 11:48:17 +00:00
|
|
|
physician.patients.add(patient, function(e, app) {
|
2014-07-15 15:50:34 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(app);
|
|
|
|
app.should.be.an.instanceOf(Appointment);
|
2015-04-24 23:50:15 +00:00
|
|
|
app.physicianId.should.eql(physician.id);
|
|
|
|
app.patientId.should.eql(patient.id);
|
2014-07-15 15:50:34 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to add connection with instance with promises', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician.create({name: 'ph1'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return Patient.create({name: 'pa1'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(patient) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return physician.patients.add(patient)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(app) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(app);
|
|
|
|
app.should.be.an.instanceOf(Appointment);
|
2015-04-24 23:50:15 +00:00
|
|
|
app.physicianId.should.eql(physician.id);
|
|
|
|
app.patientId.should.eql(patient.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to add connection with through data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician.create({name: 'ph1'}, function(e, physician) {
|
|
|
|
Patient.create({name: 'pa1'}, function(e, patient) {
|
2014-08-31 13:40:18 +00:00
|
|
|
var now = Date.now();
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.add(patient, {date: new Date(now)}, function(e, app) {
|
2014-08-31 13:40:18 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(app);
|
|
|
|
app.should.be.an.instanceOf(Appointment);
|
2015-04-24 23:50:15 +00:00
|
|
|
app.physicianId.should.eql(physician.id);
|
|
|
|
app.patientId.should.eql(patient.id);
|
|
|
|
app.patientId.should.eql(patient.id);
|
2014-08-31 13:40:18 +00:00
|
|
|
app.date.getTime().should.equal(now);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to add connection with through data with promises', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician.create({name: 'ph1'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return Patient.create({name: 'pa1'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(patient) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var now = Date.now();
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.add(patient, {date: new Date(now)})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(app) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(app);
|
|
|
|
app.should.be.an.instanceOf(Appointment);
|
2015-04-24 23:50:15 +00:00
|
|
|
app.physicianId.should.eql(physician.id);
|
|
|
|
app.patientId.should.eql(patient.id);
|
|
|
|
app.patientId.should.eql(patient.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
app.date.getTime().should.equal(now);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to remove connection with instance', function(done) {
|
2014-07-15 15:50:34 +00:00
|
|
|
var id;
|
2016-04-01 11:48:17 +00:00
|
|
|
Physician.create(function(err, physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
physician.patients.create({name: 'a'}, function(err, patient) {
|
2014-07-15 15:50:34 +00:00
|
|
|
id = patient.id;
|
2016-04-01 11:48:17 +00:00
|
|
|
physician.patients.remove(id, function(err, ch) {
|
2014-07-15 15:50:34 +00:00
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(physician) {
|
2016-04-01 11:48:17 +00:00
|
|
|
physician.patients.exists(id, function(err, flag) {
|
2014-07-15 15:50:34 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
flag.should.be.eql(false);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to remove connection with instance with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var id;
|
|
|
|
Physician.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(physician) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return physician.patients.create({name: 'a'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(patient) {
|
2015-02-18 05:04:31 +00:00
|
|
|
id = patient.id;
|
|
|
|
return physician.patients.remove(id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ch) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
|
|
|
|
function verify(physician) {
|
|
|
|
return physician.patients.exists(id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(flag) {
|
2015-02-18 05:04:31 +00:00
|
|
|
flag.should.be.eql(false);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
beforeEach(function(done) {
|
|
|
|
Appointment.destroyAll(function(err) {
|
|
|
|
Physician.destroyAll(function(err) {
|
2014-07-15 15:50:34 +00:00
|
|
|
Patient.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-09-03 03:04:58 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasMany through - collect', function() {
|
2014-09-03 03:04:58 +00:00
|
|
|
var Physician, Patient, Appointment, Address;
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
beforeEach(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician = db.define('Physician', {name: String});
|
|
|
|
Patient = db.define('Patient', {name: String});
|
|
|
|
Appointment = db.define('Appointment', {date: {type: Date,
|
2016-04-01 11:48:17 +00:00
|
|
|
default: function() {
|
2014-09-03 11:58:39 +00:00
|
|
|
return new Date();
|
2016-08-19 17:46:59 +00:00
|
|
|
}}});
|
|
|
|
Address = db.define('Address', {name: String});
|
2014-09-03 03:04:58 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Physician', 'Patient', 'Appointment', 'Address'], done);
|
2014-09-03 11:58:39 +00:00
|
|
|
});
|
2014-09-03 03:04:58 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('with default options', function() {
|
|
|
|
it('can determine the collect by modelTo\'s name as default', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician.hasMany(Patient, {through: Appointment});
|
|
|
|
Patient.hasMany(Physician, {through: Appointment, as: 'yyy'});
|
2014-09-03 11:58:39 +00:00
|
|
|
Patient.belongsTo(Address);
|
|
|
|
Appointment.belongsTo(Physician);
|
|
|
|
Appointment.belongsTo(Patient);
|
2016-08-19 17:46:59 +00:00
|
|
|
var physician = new Physician({id: 1});
|
2014-09-03 13:51:21 +00:00
|
|
|
var scope1 = physician.patients._scope;
|
2014-09-03 11:58:39 +00:00
|
|
|
scope1.should.have.property('collect', 'patient');
|
|
|
|
scope1.should.have.property('include', 'patient');
|
2016-08-19 17:46:59 +00:00
|
|
|
var patient = new Patient({id: 1});
|
2014-09-03 11:58:39 +00:00
|
|
|
var scope2 = patient.yyy._scope;
|
|
|
|
scope2.should.have.property('collect', 'physician');
|
|
|
|
scope2.should.have.property('include', 'physician');
|
2014-09-03 03:04:58 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('when custom reverse belongsTo names for both sides', function() {
|
|
|
|
it('can determine the collect via keyThrough', function() {
|
2016-04-01 13:23:42 +00:00
|
|
|
Physician.hasMany(Patient, {
|
|
|
|
through: Appointment, foreignKey: 'fooId', keyThrough: 'barId',
|
|
|
|
});
|
|
|
|
Patient.hasMany(Physician, {
|
|
|
|
through: Appointment, foreignKey: 'barId', keyThrough: 'fooId', as: 'yyy',
|
|
|
|
});
|
2016-08-19 17:46:59 +00:00
|
|
|
Appointment.belongsTo(Physician, {as: 'foo'});
|
|
|
|
Appointment.belongsTo(Patient, {as: 'bar'});
|
2014-09-03 11:58:39 +00:00
|
|
|
Patient.belongsTo(Address); // jam.
|
2016-08-19 17:46:59 +00:00
|
|
|
Appointment.belongsTo(Patient, {as: 'car'}); // jam. Should we complain in this case???
|
2014-09-03 11:58:39 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
var physician = new Physician({id: 1});
|
2014-09-03 13:51:21 +00:00
|
|
|
var scope1 = physician.patients._scope;
|
2014-09-03 11:58:39 +00:00
|
|
|
scope1.should.have.property('collect', 'bar');
|
|
|
|
scope1.should.have.property('include', 'bar');
|
2016-08-19 17:46:59 +00:00
|
|
|
var patient = new Patient({id: 1});
|
2014-09-03 11:58:39 +00:00
|
|
|
var scope2 = patient.yyy._scope;
|
|
|
|
scope2.should.have.property('collect', 'foo');
|
|
|
|
scope2.should.have.property('include', 'foo');
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can determine the collect via modelTo name', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician.hasMany(Patient, {through: Appointment});
|
|
|
|
Patient.hasMany(Physician, {through: Appointment, as: 'yyy'});
|
|
|
|
Appointment.belongsTo(Physician, {as: 'foo', foreignKey: 'physicianId'});
|
|
|
|
Appointment.belongsTo(Patient, {as: 'bar', foreignKey: 'patientId'});
|
2014-09-03 11:58:39 +00:00
|
|
|
Patient.belongsTo(Address); // jam.
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
var physician = new Physician({id: 1});
|
2014-09-03 11:58:39 +00:00
|
|
|
var scope1 = physician.patients._scope;
|
|
|
|
scope1.should.have.property('collect', 'bar');
|
|
|
|
scope1.should.have.property('include', 'bar');
|
2016-08-19 17:46:59 +00:00
|
|
|
var patient = new Patient({id: 1});
|
2014-09-03 13:51:21 +00:00
|
|
|
var scope2 = patient.yyy._scope;
|
2014-09-03 11:58:39 +00:00
|
|
|
scope2.should.have.property('collect', 'foo');
|
|
|
|
scope2.should.have.property('include', 'foo');
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can determine the collect via modelTo name (with jams)', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician.hasMany(Patient, {through: Appointment});
|
|
|
|
Patient.hasMany(Physician, {through: Appointment, as: 'yyy'});
|
|
|
|
Appointment.belongsTo(Physician, {as: 'foo', foreignKey: 'physicianId'});
|
|
|
|
Appointment.belongsTo(Patient, {as: 'bar', foreignKey: 'patientId'});
|
2014-09-03 11:58:39 +00:00
|
|
|
Patient.belongsTo(Address); // jam.
|
2016-08-19 17:46:59 +00:00
|
|
|
Appointment.belongsTo(Physician, {as: 'goo', foreignKey: 'physicianId'}); // jam. Should we complain in this case???
|
|
|
|
Appointment.belongsTo(Patient, {as: 'car', foreignKey: 'patientId'}); // jam. Should we complain in this case???
|
2014-09-03 11:58:39 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
var physician = new Physician({id: 1});
|
2014-09-03 11:58:39 +00:00
|
|
|
var scope1 = physician.patients._scope;
|
|
|
|
scope1.should.have.property('collect', 'bar');
|
|
|
|
scope1.should.have.property('include', 'bar');
|
2016-08-19 17:46:59 +00:00
|
|
|
var patient = new Patient({id: 1});
|
2014-09-03 13:51:21 +00:00
|
|
|
var scope2 = patient.yyy._scope;
|
|
|
|
scope2.should.have.property('collect', 'foo'); // first matched relation
|
|
|
|
scope2.should.have.property('include', 'foo'); // first matched relation
|
2014-09-03 03:04:58 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('when custom reverse belongsTo name for one side only', function() {
|
2014-09-03 03:04:58 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
beforeEach(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Physician.hasMany(Patient, {as: 'xxx', through: Appointment, foreignKey: 'fooId'});
|
|
|
|
Patient.hasMany(Physician, {as: 'yyy', through: Appointment, keyThrough: 'fooId'});
|
|
|
|
Appointment.belongsTo(Physician, {as: 'foo'});
|
2014-09-03 11:58:39 +00:00
|
|
|
Appointment.belongsTo(Patient);
|
|
|
|
Patient.belongsTo(Address); // jam.
|
2016-08-19 17:46:59 +00:00
|
|
|
Appointment.belongsTo(Physician, {as: 'bar'}); // jam. Should we complain in this case???
|
2014-09-03 03:04:58 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can determine the collect via model name', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
var physician = new Physician({id: 1});
|
2014-09-03 11:58:39 +00:00
|
|
|
var scope1 = physician.xxx._scope;
|
|
|
|
scope1.should.have.property('collect', 'patient');
|
|
|
|
scope1.should.have.property('include', 'patient');
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can determine the collect via keyThrough', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
var patient = new Patient({id: 1});
|
2014-09-03 11:58:39 +00:00
|
|
|
var scope2 = patient.yyy._scope;
|
|
|
|
scope2.should.have.property('collect', 'foo');
|
|
|
|
scope2.should.have.property('include', 'foo');
|
2014-09-03 03:04:58 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasMany through bi-directional relations on the same model', function() {
|
2015-04-24 20:02:33 +00:00
|
|
|
var User, Follow, Address;
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
User = db.define('User', {name: String});
|
|
|
|
Follow = db.define('Follow', {date: {type: Date,
|
2016-04-01 11:48:17 +00:00
|
|
|
default: function() {
|
2015-04-24 20:02:33 +00:00
|
|
|
return new Date();
|
2016-08-19 17:46:59 +00:00
|
|
|
}}});
|
|
|
|
Address = db.define('Address', {name: String});
|
2015-04-24 20:02:33 +00:00
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
User.hasMany(User, {
|
|
|
|
as: 'followers', foreignKey: 'followeeId', keyThrough: 'followerId', through: Follow,
|
|
|
|
});
|
|
|
|
User.hasMany(User, {
|
|
|
|
as: 'following', foreignKey: 'followerId', keyThrough: 'followeeId', through: Follow,
|
|
|
|
});
|
2015-04-24 20:02:33 +00:00
|
|
|
User.belongsTo(Address);
|
2016-08-19 17:46:59 +00:00
|
|
|
Follow.belongsTo(User, {as: 'follower'});
|
|
|
|
Follow.belongsTo(User, {as: 'followee'});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['User', 'Follow'], done);
|
2015-04-24 20:02:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should set foreignKeys of through model correctly in first relation',
|
2016-04-01 11:48:17 +00:00
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var follower = new User({id: 1});
|
|
|
|
var followee = new User({id: 2});
|
2016-04-01 11:48:17 +00:00
|
|
|
followee.followers.add(follower, function(err, throughInst) {
|
2015-04-24 20:02:33 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(throughInst);
|
2015-04-24 23:50:15 +00:00
|
|
|
throughInst.followerId.should.eql(follower.id);
|
|
|
|
throughInst.followeeId.should.eql(followee.id);
|
2015-04-24 20:02:33 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set foreignKeys of through model correctly in second relation',
|
2016-04-01 11:48:17 +00:00
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var follower = new User({id: 3});
|
|
|
|
var followee = new User({id: 4});
|
2016-04-01 11:48:17 +00:00
|
|
|
follower.following.add(followee, function(err, throughInst) {
|
2015-04-24 20:02:33 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(throughInst);
|
2015-04-24 23:50:15 +00:00
|
|
|
throughInst.followeeId.should.eql(followee.id);
|
|
|
|
throughInst.followerId.should.eql(follower.id);
|
2015-04-24 20:02:33 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasMany through - between same models', function() {
|
2014-09-03 03:29:32 +00:00
|
|
|
var User, Follow, Address;
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
User = db.define('User', {name: String});
|
|
|
|
Follow = db.define('Follow', {date: {type: Date,
|
2016-04-01 11:48:17 +00:00
|
|
|
default: function() {
|
2014-09-03 03:29:32 +00:00
|
|
|
return new Date();
|
2016-08-19 17:46:59 +00:00
|
|
|
}}});
|
|
|
|
Address = db.define('Address', {name: String});
|
2014-09-03 03:29:32 +00:00
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
User.hasMany(User, {
|
|
|
|
as: 'followers', foreignKey: 'followeeId', keyThrough: 'followerId', through: Follow,
|
|
|
|
});
|
|
|
|
User.hasMany(User, {
|
|
|
|
as: 'following', foreignKey: 'followerId', keyThrough: 'followeeId', through: Follow,
|
|
|
|
});
|
2014-09-03 03:29:32 +00:00
|
|
|
User.belongsTo(Address);
|
2016-08-19 17:46:59 +00:00
|
|
|
Follow.belongsTo(User, {as: 'follower'});
|
|
|
|
Follow.belongsTo(User, {as: 'followee'});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['User', 'Follow', 'Address'], done);
|
2014-09-03 03:29:32 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should set the keyThrough and the foreignKey', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var user = new User({id: 1});
|
|
|
|
var user2 = new User({id: 2});
|
2016-04-01 11:48:17 +00:00
|
|
|
user.following.add(user2, function(err, f) {
|
2015-02-16 23:36:51 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(f);
|
2015-04-24 23:50:15 +00:00
|
|
|
f.followeeId.should.eql(user2.id);
|
|
|
|
f.followerId.should.eql(user.id);
|
2015-02-16 23:36:51 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can determine the collect via keyThrough for each side', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
var user = new User({id: 1});
|
2014-09-03 11:58:39 +00:00
|
|
|
var scope1 = user.followers._scope;
|
|
|
|
scope1.should.have.property('collect', 'follower');
|
|
|
|
scope1.should.have.property('include', 'follower');
|
|
|
|
var scope2 = user.following._scope;
|
|
|
|
scope2.should.have.property('collect', 'followee');
|
|
|
|
scope2.should.have.property('include', 'followee');
|
2014-09-03 03:29:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasMany with properties', function() {
|
|
|
|
it('can be declared with properties', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.hasMany(Chapter, {properties: {type: 'bookType'}});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Book', 'Chapter'], done);
|
2014-07-11 13:29:47 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.create({type: 'fiction'}, function(err, book) {
|
2016-04-01 11:48:17 +00:00
|
|
|
book.chapters.create(function(err, c) {
|
2014-07-11 13:29:47 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(c);
|
2015-04-24 23:50:15 +00:00
|
|
|
c.bookId.should.eql(book.id);
|
2014-07-11 13:29:47 +00:00
|
|
|
c.bookType.should.equal('fiction');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope with promises', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.create({type: 'fiction'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(book) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return book.chapters.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(c) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(c);
|
2015-04-24 23:50:15 +00:00
|
|
|
c.bookId.should.eql(book.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
c.bookType.should.equal('fiction');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2014-07-11 13:29:47 +00:00
|
|
|
});
|
2014-07-15 23:10:37 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasMany with scope and properties', function() {
|
|
|
|
it('can be declared with properties', function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Category = db.define('Category', {name: String, jobType: String});
|
|
|
|
Job = db.define('Job', {name: String, type: String});
|
2014-07-15 23:10:37 +00:00
|
|
|
|
2014-08-26 05:17:40 +00:00
|
|
|
Category.hasMany(Job, {
|
2015-01-26 18:09:29 +00:00
|
|
|
properties: function(inst, target) {
|
2014-08-26 05:17:40 +00:00
|
|
|
if (!inst.jobType) return; // skip
|
2016-08-19 17:46:59 +00:00
|
|
|
return {type: inst.jobType};
|
2014-07-11 13:29:47 +00:00
|
|
|
},
|
|
|
|
scope: function(inst, filter) {
|
2014-07-11 22:02:16 +00:00
|
|
|
var m = this.properties(inst); // re-use properties
|
2016-08-19 17:46:59 +00:00
|
|
|
if (m) return {where: m};
|
2016-04-01 11:48:17 +00:00
|
|
|
},
|
2014-07-11 13:29:47 +00:00
|
|
|
});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Category', 'Job'], done);
|
2014-07-11 13:29:47 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope', function(done) {
|
|
|
|
Category.create(function(err, c) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
c.jobs.create({type: 'book'}, function(err, p) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2015-04-24 23:50:15 +00:00
|
|
|
p.categoryId.should.eql(c.id);
|
2014-07-11 13:29:47 +00:00
|
|
|
p.type.should.equal('book');
|
2016-08-19 17:46:59 +00:00
|
|
|
c.jobs.create({type: 'widget'}, function(err, p) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2015-04-24 23:50:15 +00:00
|
|
|
p.categoryId.should.eql(c.id);
|
2014-07-11 13:29:47 +00:00
|
|
|
p.type.should.equal('widget');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(c) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return c.jobs.create({type: 'book'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-04-24 23:50:15 +00:00
|
|
|
p.categoryId.should.eql(c.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
p.type.should.equal('book');
|
2016-08-19 17:46:59 +00:00
|
|
|
return c.jobs.create({type: 'widget'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-04-24 23:50:15 +00:00
|
|
|
p.categoryId.should.eql(c.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
p.type.should.equal('widget');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find records on scope', function(done) {
|
|
|
|
Category.findOne(function(err, c) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
c.jobs(function(err, jobs) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
jobs.should.have.length(2);
|
2014-07-11 13:29:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find records on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(c) {
|
|
|
|
return c.jobs.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
|
|
|
.then(function(jobs) {
|
|
|
|
jobs.should.have.length(2);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find record on scope - filtered', function(done) {
|
|
|
|
Category.findOne(function(err, c) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
c.jobs({where: {type: 'book'}}, function(err, jobs) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
jobs.should.have.length(1);
|
|
|
|
jobs[0].type.should.equal('book');
|
2014-07-11 13:29:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find record on scope with promises - filtered', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(c) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return c.jobs.getAsync({where: {type: 'book'}});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
|
|
|
.then(function(jobs) {
|
|
|
|
jobs.should.have.length(1);
|
|
|
|
jobs[0].type.should.equal('book');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2014-07-11 13:29:47 +00:00
|
|
|
// So why not just do the above? In LoopBack, the context
|
|
|
|
// that gets passed into a beforeRemote handler contains
|
|
|
|
// a reference to the parent scope/instance: ctx.instance
|
|
|
|
// in order to enforce a (dynamic scope) at runtime
|
|
|
|
// a temporary property can be set in the beforeRemoting
|
2014-07-11 22:02:16 +00:00
|
|
|
// handler. Optionally,properties dynamic properties can be declared.
|
2014-07-11 13:29:47 +00:00
|
|
|
//
|
|
|
|
// The code below simulates this.
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope - properties', function(done) {
|
|
|
|
Category.findOne(function(err, c) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
c.jobType = 'tool'; // temporary
|
|
|
|
c.jobs.create(function(err, p) {
|
2015-04-24 23:50:15 +00:00
|
|
|
p.categoryId.should.eql(c.id);
|
2014-07-11 13:29:47 +00:00
|
|
|
p.type.should.equal('tool');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find records on scope', function(done) {
|
|
|
|
Category.findOne(function(err, c) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
c.jobs(function(err, jobs) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
jobs.should.have.length(3);
|
2014-07-24 13:55:00 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find record on scope - scoped', function(done) {
|
|
|
|
Category.findOne(function(err, c) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
c.jobType = 'book'; // temporary, for scoping
|
|
|
|
c.jobs(function(err, jobs) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
jobs.should.have.length(1);
|
|
|
|
jobs[0].type.should.equal('book');
|
2014-07-11 13:29:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find record on scope - scoped', function(done) {
|
|
|
|
Category.findOne(function(err, c) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
c.jobType = 'tool'; // temporary, for scoping
|
|
|
|
c.jobs(function(err, jobs) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
jobs.should.have.length(1);
|
|
|
|
jobs[0].type.should.equal('tool');
|
2014-07-11 13:29:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-08-12 12:44:33 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find count of records on scope - scoped', function(done) {
|
|
|
|
Category.findOne(function(err, c) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
c.jobType = 'tool'; // temporary, for scoping
|
|
|
|
c.jobs.count(function(err, count) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-12 12:44:33 +00:00
|
|
|
count.should.equal(1);
|
|
|
|
done();
|
2014-07-11 13:29:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should delete records on scope - scoped', function(done) {
|
|
|
|
Category.findOne(function(err, c) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
c.jobType = 'tool'; // temporary, for scoping
|
|
|
|
c.jobs.destroyAll(function(err, result) {
|
2015-02-21 05:14:48 +00:00
|
|
|
done(err);
|
2014-07-24 13:55:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find record on scope - verify', function(done) {
|
|
|
|
Category.findOne(function(err, c) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
c.jobs(function(err, jobs) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
jobs.should.have.length(2);
|
2014-07-24 13:55:00 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-11 13:29:47 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('polymorphic hasOne', function() {
|
|
|
|
before(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture = db.define('Picture', {name: String});
|
|
|
|
Author = db.define('Author', {name: String});
|
|
|
|
Reader = db.define('Reader', {name: String});
|
2014-07-26 13:20:46 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Picture', 'Author', 'Reader'], done);
|
2014-07-26 13:20:46 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.hasOne(Picture, {as: 'avatar', polymorphic: 'imageable'});
|
|
|
|
Reader.hasOne(Picture, {as: 'mugshot', polymorphic: 'imageable'});
|
|
|
|
Picture.belongsTo('imageable', {polymorphic: true});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Picture', 'Author', 'Reader'], done);
|
2014-07-26 13:20:46 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.create({name: 'Author 1'}, function(err, author) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
author.avatar.create({name: 'Avatar'}, function(err, p) {
|
2014-07-26 13:20:46 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p);
|
2015-04-24 23:50:15 +00:00
|
|
|
p.imageableId.should.eql(author.id);
|
2014-07-26 13:20:46 +00:00
|
|
|
p.imageableType.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic relation with promises - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.create({name: 'Author 1'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(author) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return author.avatar.create({name: 'Avatar'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(p);
|
2015-04-24 23:50:15 +00:00
|
|
|
p.imageableId.should.eql(author.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
p.imageableType.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic relation - reader', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Reader.create({name: 'Reader 1'}, function(err, reader) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
reader.mugshot.create({name: 'Mugshot'}, function(err, p) {
|
2014-07-26 13:20:46 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p);
|
2015-04-24 23:50:15 +00:00
|
|
|
p.imageableId.should.eql(reader.id);
|
2014-07-26 13:20:46 +00:00
|
|
|
p.imageableType.should.equal('Reader');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic relation - author', function(done) {
|
|
|
|
Author.findOne(function(err, author) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-04-01 11:48:17 +00:00
|
|
|
author.avatar(function(err, p) {
|
2014-07-26 13:20:46 +00:00
|
|
|
should.not.exist(err);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-04 19:27:40 +00:00
|
|
|
var avatar = author.avatar();
|
|
|
|
avatar.should.equal(p);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-26 13:20:46 +00:00
|
|
|
p.name.should.equal('Avatar');
|
2014-07-28 23:15:37 +00:00
|
|
|
p.imageableId.should.eql(author.id);
|
2014-07-26 13:20:46 +00:00
|
|
|
p.imageableType.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic relation - reader', function(done) {
|
|
|
|
Reader.findOne(function(err, reader) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-04-01 11:48:17 +00:00
|
|
|
reader.mugshot(function(err, p) {
|
2014-07-26 13:20:46 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.name.should.equal('Mugshot');
|
2014-07-28 23:15:37 +00:00
|
|
|
p.imageableId.should.eql(reader.id);
|
2014-07-26 13:20:46 +00:00
|
|
|
p.imageableType.should.equal('Reader');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should include polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.findOne({include: 'avatar'}, function(err, author) {
|
2015-06-11 18:59:36 +00:00
|
|
|
should.not.exists(err);
|
|
|
|
var avatar = author.avatar();
|
|
|
|
should.exist(avatar);
|
|
|
|
avatar.name.should.equal('Avatar');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic relation with promises - reader', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Reader.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(reader) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return reader.mugshot.getAsync()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
p.name.should.equal('Mugshot');
|
|
|
|
p.imageableId.should.eql(reader.id);
|
|
|
|
p.imageableType.should.equal('Reader');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find inverse polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.findOne({where: {name: 'Avatar'}}, function(err, p) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-04-01 11:48:17 +00:00
|
|
|
p.imageable(function(err, imageable) {
|
2014-07-26 13:20:46 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
imageable.should.be.instanceof(Author);
|
|
|
|
imageable.name.should.equal('Author 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should include inverse polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.findOne({where: {name: 'Avatar'}, include: 'imageable'},
|
2016-04-01 11:48:17 +00:00
|
|
|
function(err, p) {
|
2015-06-11 18:59:36 +00:00
|
|
|
should.not.exists(err);
|
|
|
|
var imageable = p.imageable();
|
|
|
|
should.exist(imageable);
|
|
|
|
imageable.should.be.instanceof(Author);
|
|
|
|
imageable.name.should.equal('Author 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find inverse polymorphic relation - reader', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.findOne({where: {name: 'Mugshot'}}, function(err, p) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-04-01 11:48:17 +00:00
|
|
|
p.imageable(function(err, imageable) {
|
2014-07-26 13:20:46 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
imageable.should.be.instanceof(Reader);
|
|
|
|
imageable.name.should.equal('Reader 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-26 13:20:46 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('polymorphic hasOne with non standard ids', function() {
|
|
|
|
before(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture = db.define('Picture', {name: String});
|
2014-10-17 16:18:55 +00:00
|
|
|
Author = db.define('Author', {
|
2016-08-19 17:46:59 +00:00
|
|
|
username: {type: String, id: true, generated: true},
|
2016-04-01 11:48:17 +00:00
|
|
|
name: String,
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
|
|
|
Reader = db.define('Reader', {
|
2016-08-19 17:46:59 +00:00
|
|
|
username: {type: String, id: true, generated: true},
|
2016-04-01 11:48:17 +00:00
|
|
|
name: String,
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Picture', 'Author', 'Reader'], done);
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared with non standard foreign key', function(done) {
|
2014-10-17 16:18:55 +00:00
|
|
|
Author.hasOne(Picture, {
|
|
|
|
as: 'avatar',
|
|
|
|
polymorphic: {
|
|
|
|
foreignKey: 'oid',
|
2016-04-01 11:48:17 +00:00
|
|
|
discriminator: 'type',
|
|
|
|
},
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
|
|
|
Reader.hasOne(Picture, {
|
|
|
|
as: 'mugshot',
|
|
|
|
polymorphic: {
|
|
|
|
foreignKey: 'oid',
|
2016-04-01 11:48:17 +00:00
|
|
|
discriminator: 'type',
|
|
|
|
},
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
|
|
|
Picture.belongsTo('owner', {
|
|
|
|
idName: 'username',
|
|
|
|
polymorphic: {
|
2015-02-21 00:39:48 +00:00
|
|
|
idType: Author.definition.properties.username.type,
|
2014-10-17 16:18:55 +00:00
|
|
|
foreignKey: 'oid',
|
2016-04-01 11:48:17 +00:00
|
|
|
discriminator: 'type',
|
|
|
|
},
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Picture', 'Author', 'Reader'], done);
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.create({name: 'Author 1'}, function(err, author) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
author.avatar.create({name: 'Avatar'}, function(err, p) {
|
2014-10-17 16:18:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p);
|
2014-12-09 00:18:42 +00:00
|
|
|
p.oid.toString().should.equal(author.username.toString());
|
2014-10-17 16:18:55 +00:00
|
|
|
p.type.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic relation with promises - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.create({name: 'Author 1'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(author) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return author.avatar.create({name: 'Avatar'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(p);
|
|
|
|
p.oid.toString().should.equal(author.username.toString());
|
|
|
|
p.type.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic relation - reader', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Reader.create({name: 'Reader 1'}, function(err, reader) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
reader.mugshot.create({name: 'Mugshot'}, function(err, p) {
|
2014-10-17 16:18:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p);
|
2014-12-09 00:18:42 +00:00
|
|
|
p.oid.toString().should.equal(reader.username.toString());
|
2014-10-17 16:18:55 +00:00
|
|
|
p.type.should.equal('Reader');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic relation - author', function(done) {
|
|
|
|
Author.findOne(function(err, author) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-04-01 11:48:17 +00:00
|
|
|
author.avatar(function(err, p) {
|
2014-10-17 16:18:55 +00:00
|
|
|
should.not.exist(err);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-10-17 16:18:55 +00:00
|
|
|
var avatar = author.avatar();
|
|
|
|
avatar.should.equal(p);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-10-17 16:18:55 +00:00
|
|
|
p.name.should.equal('Avatar');
|
2015-02-21 00:10:25 +00:00
|
|
|
p.oid.toString().should.equal(author.username.toString());
|
2014-10-17 16:18:55 +00:00
|
|
|
p.type.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic relation - reader', function(done) {
|
|
|
|
Reader.findOne(function(err, reader) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-04-01 11:48:17 +00:00
|
|
|
reader.mugshot(function(err, p) {
|
2014-10-17 16:18:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.name.should.equal('Mugshot');
|
2015-02-21 00:10:25 +00:00
|
|
|
p.oid.toString().should.equal(reader.username.toString());
|
2014-10-17 16:18:55 +00:00
|
|
|
p.type.should.equal('Reader');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find inverse polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.findOne({where: {name: 'Avatar'}}, function(err, p) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-04-01 11:48:17 +00:00
|
|
|
p.owner(function(err, owner) {
|
2014-10-17 16:18:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
owner.should.be.instanceof(Author);
|
|
|
|
owner.name.should.equal('Author 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find inverse polymorphic relation - reader', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.findOne({where: {name: 'Mugshot'}}, function(err, p) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-04-01 11:48:17 +00:00
|
|
|
p.owner(function(err, owner) {
|
2014-10-17 16:18:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
owner.should.be.instanceof(Reader);
|
|
|
|
owner.name.should.equal('Reader 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should include polymorphic relation - reader', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Reader.findOne({include: 'mugshot'},
|
2016-04-01 11:48:17 +00:00
|
|
|
function(err, reader) {
|
2015-06-11 18:59:36 +00:00
|
|
|
should.not.exists(err);
|
|
|
|
var mugshot = reader.mugshot();
|
|
|
|
should.exist(mugshot);
|
|
|
|
mugshot.name.should.equal('Mugshot');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should include inverse polymorphic relation - reader', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.findOne({where: {name: 'Mugshot'}, include: 'owner'},
|
2016-04-01 11:48:17 +00:00
|
|
|
function(err, p) {
|
2015-06-11 18:59:36 +00:00
|
|
|
should.not.exists(err);
|
|
|
|
var owner = p.owner();
|
|
|
|
should.exist(owner);
|
|
|
|
owner.should.be.instanceof(Reader);
|
|
|
|
owner.name.should.equal('Reader 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('polymorphic hasMany', function() {
|
|
|
|
before(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture = db.define('Picture', {name: String});
|
|
|
|
Author = db.define('Author', {name: String});
|
|
|
|
Reader = db.define('Reader', {name: String});
|
2014-07-26 10:47:55 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Picture', 'Author', 'Reader'], done);
|
2014-07-26 10:47:55 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.hasMany(Picture, {polymorphic: 'imageable'});
|
|
|
|
Reader.hasMany(Picture, {polymorphic: { // alt syntax
|
2014-07-28 08:44:26 +00:00
|
|
|
as: 'imageable', foreignKey: 'imageableId',
|
2016-04-01 11:48:17 +00:00
|
|
|
discriminator: 'imageableType',
|
|
|
|
}});
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.belongsTo('imageable', {polymorphic: true});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-16 08:23:32 +00:00
|
|
|
Author.relations['pictures'].toJSON().should.eql({
|
|
|
|
name: 'pictures',
|
|
|
|
type: 'hasMany',
|
|
|
|
modelFrom: 'Author',
|
|
|
|
keyFrom: 'id',
|
|
|
|
modelTo: 'Picture',
|
|
|
|
keyTo: 'imageableId',
|
|
|
|
multiple: true,
|
2015-02-03 10:37:43 +00:00
|
|
|
polymorphic: {
|
2014-08-16 08:23:32 +00:00
|
|
|
as: 'imageable',
|
|
|
|
foreignKey: 'imageableId',
|
2016-04-01 11:48:17 +00:00
|
|
|
discriminator: 'imageableType',
|
|
|
|
},
|
2014-08-16 08:23:32 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-16 08:23:32 +00:00
|
|
|
Picture.relations['imageable'].toJSON().should.eql({
|
|
|
|
name: 'imageable',
|
|
|
|
type: 'belongsTo',
|
|
|
|
modelFrom: 'Picture',
|
|
|
|
keyFrom: 'imageableId',
|
|
|
|
modelTo: '<polymorphic>',
|
|
|
|
keyTo: 'id',
|
|
|
|
multiple: false,
|
2015-02-03 10:37:43 +00:00
|
|
|
polymorphic: {
|
2014-08-16 08:23:32 +00:00
|
|
|
as: 'imageable',
|
|
|
|
foreignKey: 'imageableId',
|
2016-04-01 11:48:17 +00:00
|
|
|
discriminator: 'imageableType',
|
|
|
|
},
|
2014-08-16 08:23:32 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Picture', 'Author', 'Reader'], done);
|
2014-07-26 10:47:55 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.create({name: 'Author 1'}, function(err, author) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
author.pictures.create({name: 'Author Pic'}, function(err, p) {
|
2014-07-26 10:47:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p);
|
2014-07-28 23:15:37 +00:00
|
|
|
p.imageableId.should.eql(author.id);
|
2014-07-26 10:47:55 +00:00
|
|
|
p.imageableType.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic relation - reader', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Reader.create({name: 'Reader 1'}, function(err, reader) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
reader.pictures.create({name: 'Reader Pic'}, function(err, p) {
|
2014-07-26 10:47:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p);
|
2014-07-28 23:15:37 +00:00
|
|
|
p.imageableId.should.eql(reader.id);
|
2014-07-26 10:47:55 +00:00
|
|
|
p.imageableType.should.equal('Reader');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic items - author', function(done) {
|
|
|
|
Author.findOne(function(err, author) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!author) return done();
|
2016-04-01 11:48:17 +00:00
|
|
|
author.pictures(function(err, pics) {
|
2014-07-26 10:47:55 +00:00
|
|
|
should.not.exist(err);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-04 19:29:21 +00:00
|
|
|
var pictures = author.pictures();
|
|
|
|
pictures.should.eql(pics);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-26 10:47:55 +00:00
|
|
|
pics.should.have.length(1);
|
|
|
|
pics[0].name.should.equal('Author Pic');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic items - reader', function(done) {
|
|
|
|
Reader.findOne(function(err, reader) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-04-01 11:48:17 +00:00
|
|
|
reader.pictures(function(err, pics) {
|
2014-07-26 10:47:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
pics.should.have.length(1);
|
|
|
|
pics[0].name.should.equal('Reader Pic');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find the inverse of polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.findOne({where: {name: 'Author Pic'}}, function(err, p) {
|
2014-07-26 10:47:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.imageableType.should.equal('Author');
|
|
|
|
p.imageable(function(err, imageable) {
|
|
|
|
should.not.exist(err);
|
|
|
|
imageable.should.be.instanceof(Author);
|
|
|
|
imageable.name.should.equal('Author 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find the inverse of polymorphic relation - reader', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.findOne({where: {name: 'Reader Pic'}}, function(err, p) {
|
2014-07-26 10:47:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.imageableType.should.equal('Reader');
|
|
|
|
p.imageable(function(err, imageable) {
|
|
|
|
should.not.exist(err);
|
|
|
|
imageable.should.be.instanceof(Reader);
|
|
|
|
imageable.name.should.equal('Reader 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should include the inverse of polymorphic relation', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.find({include: 'imageable'}, function(err, pics) {
|
2014-07-26 10:47:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
pics.should.have.length(2);
|
|
|
|
pics[0].name.should.equal('Author Pic');
|
|
|
|
pics[0].imageable().name.should.equal('Author 1');
|
|
|
|
pics[1].name.should.equal('Reader Pic');
|
|
|
|
pics[1].imageable().name.should.equal('Reader 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-26 10:47:55 +00:00
|
|
|
it('should assign a polymorphic relation', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.create({name: 'Author 2'}, function(err, author) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
var p = new Picture({name: 'Sample'});
|
2014-07-26 10:47:55 +00:00
|
|
|
p.imageable(author); // assign
|
2014-07-28 23:15:37 +00:00
|
|
|
p.imageableId.should.eql(author.id);
|
2014-07-26 10:47:55 +00:00
|
|
|
p.imageableType.should.equal('Author');
|
|
|
|
p.save(done);
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic items - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.findOne({where: {name: 'Author 2'}}, function(err, author) {
|
2015-02-21 05:14:48 +00:00
|
|
|
should.not.exists(err);
|
2016-04-01 11:48:17 +00:00
|
|
|
author.pictures(function(err, pics) {
|
2014-07-26 10:47:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
pics.should.have.length(1);
|
|
|
|
pics[0].name.should.equal('Sample');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find the inverse of polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.findOne({where: {name: 'Sample'}}, function(err, p) {
|
2014-07-26 10:47:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.imageableType.should.equal('Author');
|
|
|
|
p.imageable(function(err, imageable) {
|
|
|
|
should.not.exist(err);
|
|
|
|
imageable.should.be.instanceof(Author);
|
|
|
|
imageable.name.should.equal('Author 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-06-11 18:59:36 +00:00
|
|
|
it('should include the inverse of polymorphic relation - author',
|
2016-04-01 11:48:17 +00:00
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.findOne({where: {name: 'Sample'}, include: 'imageable'},
|
2016-04-01 11:48:17 +00:00
|
|
|
function(err, p) {
|
2015-06-11 18:59:36 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
var imageable = p.imageable();
|
|
|
|
should.exist(imageable);
|
|
|
|
imageable.should.be.instanceof(Author);
|
|
|
|
imageable.name.should.equal('Author 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-26 10:47:55 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('polymorphic hasAndBelongsToMany through', function() {
|
|
|
|
before(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture = db.define('Picture', {name: String});
|
|
|
|
Author = db.define('Author', {name: String});
|
|
|
|
Reader = db.define('Reader', {name: String});
|
2014-07-26 12:54:54 +00:00
|
|
|
PictureLink = db.define('PictureLink', {});
|
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Picture', 'Author', 'Reader', 'PictureLink'], done);
|
2014-07-26 12:54:54 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.hasAndBelongsToMany(Picture, {through: PictureLink, polymorphic: 'imageable'});
|
|
|
|
Reader.hasAndBelongsToMany(Picture, {through: PictureLink, polymorphic: 'imageable'});
|
2014-07-26 19:32:24 +00:00
|
|
|
// Optionally, define inverse relations:
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.hasMany(Author, {through: PictureLink, polymorphic: 'imageable', invert: true});
|
|
|
|
Picture.hasMany(Reader, {through: PictureLink, polymorphic: 'imageable', invert: true});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Picture', 'Author', 'Reader', 'PictureLink'], done);
|
2014-07-26 12:54:54 +00:00
|
|
|
});
|
2014-07-28 21:22:45 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can determine the collect via modelTo name', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.hasAndBelongsToMany(Picture, {through: PictureLink, polymorphic: 'imageable'});
|
|
|
|
Reader.hasAndBelongsToMany(Picture, {through: PictureLink, polymorphic: 'imageable'});
|
2014-09-03 11:58:39 +00:00
|
|
|
// Optionally, define inverse relations:
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.hasMany(Author, {through: PictureLink, polymorphic: 'imageable', invert: true});
|
|
|
|
Picture.hasMany(Reader, {through: PictureLink, polymorphic: 'imageable', invert: true});
|
|
|
|
var author = new Author({id: 1});
|
2014-09-03 11:58:39 +00:00
|
|
|
var scope1 = author.pictures._scope;
|
|
|
|
scope1.should.have.property('collect', 'picture');
|
|
|
|
scope1.should.have.property('include', 'picture');
|
2016-08-19 17:46:59 +00:00
|
|
|
var reader = new Reader({id: 1});
|
2014-09-03 11:58:39 +00:00
|
|
|
var scope2 = reader.pictures._scope;
|
|
|
|
scope2.should.have.property('collect', 'picture');
|
|
|
|
scope2.should.have.property('include', 'picture');
|
2016-08-19 17:46:59 +00:00
|
|
|
var picture = new Picture({id: 1});
|
2014-09-03 11:58:39 +00:00
|
|
|
var scope3 = picture.authors._scope;
|
|
|
|
scope3.should.have.property('collect', 'imageable');
|
|
|
|
scope3.should.have.property('include', 'imageable');
|
|
|
|
var scope4 = picture.readers._scope;
|
|
|
|
scope4.should.have.property('collect', 'imageable');
|
|
|
|
scope4.should.have.property('include', 'imageable');
|
|
|
|
});
|
|
|
|
|
2014-07-28 21:22:45 +00:00
|
|
|
var author, reader, pictures = [];
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.create({name: 'Author 1'}, function(err, a) {
|
2014-07-28 21:22:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
author = a;
|
2016-08-19 17:46:59 +00:00
|
|
|
author.pictures.create({name: 'Author Pic 1'}, function(err, p) {
|
2014-07-26 12:54:54 +00:00
|
|
|
should.not.exist(err);
|
2014-07-28 21:22:45 +00:00
|
|
|
pictures.push(p);
|
2016-08-19 17:46:59 +00:00
|
|
|
author.pictures.create({name: 'Author Pic 2'}, function(err, p) {
|
2014-07-26 12:54:54 +00:00
|
|
|
should.not.exist(err);
|
2014-07-28 21:22:45 +00:00
|
|
|
pictures.push(p);
|
2014-07-26 12:54:54 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic relation - reader', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Reader.create({name: 'Reader 1'}, function(err, r) {
|
2014-07-28 21:22:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
reader = r;
|
2016-08-19 17:46:59 +00:00
|
|
|
reader.pictures.create({name: 'Reader Pic 1'}, function(err, p) {
|
2014-07-26 12:54:54 +00:00
|
|
|
should.not.exist(err);
|
2014-07-28 21:22:45 +00:00
|
|
|
pictures.push(p);
|
2014-07-26 12:54:54 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic through model', function(done) {
|
2014-07-26 12:54:54 +00:00
|
|
|
PictureLink.findOne(function(err, link) {
|
|
|
|
should.not.exist(err);
|
2014-07-28 21:22:45 +00:00
|
|
|
link.pictureId.should.eql(pictures[0].id); // eql for mongo ObjectId
|
|
|
|
link.imageableId.should.eql(author.id);
|
2014-07-26 12:54:54 +00:00
|
|
|
link.imageableType.should.equal('Author');
|
2014-07-26 19:11:25 +00:00
|
|
|
link.imageable(function(err, imageable) {
|
|
|
|
imageable.should.be.instanceof(Author);
|
2014-07-28 21:22:45 +00:00
|
|
|
imageable.id.should.eql(author.id);
|
2014-07-26 19:11:25 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-07-26 12:54:54 +00:00
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should get polymorphic relation through model - author', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!author) return done();
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.findById(author.id, function(err, author) {
|
2014-07-26 12:54:54 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
author.name.should.equal('Author 1');
|
|
|
|
author.pictures(function(err, pics) {
|
|
|
|
should.not.exist(err);
|
|
|
|
pics.should.have.length(2);
|
|
|
|
pics[0].name.should.equal('Author Pic 1');
|
|
|
|
pics[1].name.should.equal('Author Pic 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should get polymorphic relation through model - reader', function(done) {
|
2014-07-28 21:22:45 +00:00
|
|
|
Reader.findById(reader.id, function(err, reader) {
|
2014-07-26 12:54:54 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
reader.name.should.equal('Reader 1');
|
|
|
|
reader.pictures(function(err, pics) {
|
|
|
|
should.not.exist(err);
|
|
|
|
pics.should.have.length(1);
|
|
|
|
pics[0].name.should.equal('Reader Pic 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should include polymorphic items', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.find({include: 'pictures'}, function(err, authors) {
|
2014-07-26 15:20:25 +00:00
|
|
|
authors.should.have.length(1);
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!authors) return done();
|
2014-07-26 15:20:25 +00:00
|
|
|
authors[0].pictures(function(err, pics) {
|
2014-07-26 19:11:25 +00:00
|
|
|
pics.should.have.length(2);
|
2014-07-26 15:20:25 +00:00
|
|
|
pics[0].name.should.equal('Author Pic 1');
|
|
|
|
pics[1].name.should.equal('Author Pic 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-28 21:22:45 +00:00
|
|
|
|
|
|
|
var anotherPicture;
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should add to a polymorphic relation - author', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!author) return done();
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.findById(author.id, function(err, author) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Picture.create({name: 'Example'}, function(err, p) {
|
2014-07-28 21:22:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
pictures.push(p);
|
|
|
|
anotherPicture = p;
|
2014-07-26 15:20:25 +00:00
|
|
|
author.pictures.add(p, function(err, link) {
|
|
|
|
link.should.be.instanceof(PictureLink);
|
2014-07-28 21:22:45 +00:00
|
|
|
link.pictureId.should.eql(p.id);
|
|
|
|
link.imageableId.should.eql(author.id);
|
2014-07-26 15:20:25 +00:00
|
|
|
link.imageableType.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic through model', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!anotherPicture) return done();
|
2016-08-19 17:46:59 +00:00
|
|
|
PictureLink.findOne({where: {pictureId: anotherPicture.id, imageableType: 'Author'}},
|
2016-04-01 13:23:42 +00:00
|
|
|
function(err, link) {
|
2014-07-26 15:20:25 +00:00
|
|
|
should.not.exist(err);
|
2014-07-28 21:22:45 +00:00
|
|
|
link.pictureId.should.eql(anotherPicture.id);
|
|
|
|
link.imageableId.should.eql(author.id);
|
2014-07-26 15:20:25 +00:00
|
|
|
link.imageableType.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-07-28 21:22:45 +00:00
|
|
|
|
|
|
|
var anotherAuthor, anotherReader;
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should add to a polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.create({name: 'Author 2'}, function(err, author) {
|
2014-07-28 21:22:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
anotherAuthor = author;
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!anotherPicture) return done();
|
2016-04-01 11:48:17 +00:00
|
|
|
author.pictures.add(anotherPicture.id, function(err, p) {
|
2014-07-26 19:11:25 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should add to a polymorphic relation - author', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Reader.create({name: 'Reader 2'}, function(err, reader) {
|
2014-07-28 21:22:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
anotherReader = reader;
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!anotherPicture) return done();
|
2016-04-01 11:48:17 +00:00
|
|
|
reader.pictures.add(anotherPicture.id, function(err, p) {
|
2014-07-26 19:11:25 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should get the inverse polymorphic relation - author', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!anotherPicture) return done();
|
2014-07-28 21:22:45 +00:00
|
|
|
Picture.findById(anotherPicture.id, function(err, p) {
|
2014-07-26 19:11:25 +00:00
|
|
|
p.authors(function(err, authors) {
|
|
|
|
authors.should.have.length(2);
|
|
|
|
authors[0].name.should.equal('Author 1');
|
|
|
|
authors[1].name.should.equal('Author 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should get the inverse polymorphic relation - reader', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!anotherPicture) return done();
|
2014-07-28 21:22:45 +00:00
|
|
|
Picture.findById(anotherPicture.id, function(err, p) {
|
2014-07-26 19:11:25 +00:00
|
|
|
p.readers(function(err, readers) {
|
|
|
|
readers.should.have.length(1);
|
|
|
|
readers[0].name.should.equal('Reader 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic items - author', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!author) return done();
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.findById(author.id, function(err, author) {
|
2014-07-26 19:11:25 +00:00
|
|
|
author.pictures(function(err, pics) {
|
|
|
|
pics.should.have.length(3);
|
|
|
|
pics[0].name.should.equal('Author Pic 1');
|
|
|
|
pics[1].name.should.equal('Author Pic 2');
|
|
|
|
pics[2].name.should.equal('Example');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check if polymorphic relation exists - author', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!author) return done();
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.findById(author.id, function(err, author) {
|
|
|
|
author.pictures.exists(anotherPicture.id, function(err, exists) {
|
2014-07-26 15:20:25 +00:00
|
|
|
exists.should.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should remove from a polymorphic relation - author', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!author || !anotherPicture) return done();
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.findById(author.id, function(err, author) {
|
|
|
|
author.pictures.remove(anotherPicture.id, function(err) {
|
2014-07-26 15:20:25 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic items - author', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!author) return done();
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.findById(author.id, function(err, author) {
|
2014-07-26 19:11:25 +00:00
|
|
|
author.pictures(function(err, pics) {
|
|
|
|
pics.should.have.length(2);
|
|
|
|
pics[0].name.should.equal('Author Pic 1');
|
|
|
|
pics[1].name.should.equal('Author Pic 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check if polymorphic relation exists - author', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!author) return done();
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.findById(author.id, function(err, author) {
|
2014-07-26 15:20:25 +00:00
|
|
|
author.pictures.exists(7, function(err, exists) {
|
|
|
|
exists.should.be.false;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic item through relation scope', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!anotherPicture) return done();
|
2014-08-15 10:55:10 +00:00
|
|
|
Picture.findById(anotherPicture.id, function(err, p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.authors.create({name: 'Author 3'}, function(err, a) {
|
2014-08-15 10:55:10 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
author = a;
|
|
|
|
author.name.should.equal('Author 3');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create polymorphic through model - new author', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!author || !anotherPicture) return done();
|
2016-08-19 17:46:59 +00:00
|
|
|
PictureLink.findOne({where: {
|
2016-04-01 11:48:17 +00:00
|
|
|
pictureId: anotherPicture.id, imageableId: author.id, imageableType: 'Author',
|
|
|
|
}}, function(err, link) {
|
2014-08-15 10:55:10 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
link.pictureId.should.eql(anotherPicture.id);
|
|
|
|
link.imageableId.should.eql(author.id);
|
|
|
|
link.imageableType.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find polymorphic items - new author', function(done) {
|
2016-09-27 00:27:33 +00:00
|
|
|
if (!author) return done();
|
2014-08-15 10:55:10 +00:00
|
|
|
Author.findById(author.id, function(err, author) {
|
|
|
|
author.pictures(function(err, pics) {
|
|
|
|
pics.should.have.length(1);
|
|
|
|
pics[0].id.should.eql(anotherPicture.id);
|
|
|
|
pics[0].name.should.equal('Example');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-26 12:54:54 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('belongsTo', function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
var List, Item, Fear, Mind;
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-20 14:46:54 +00:00
|
|
|
var listId, itemId;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared in different ways', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
List = db.define('List', {name: String});
|
|
|
|
Item = db.define('Item', {name: String});
|
2014-01-24 17:09:53 +00:00
|
|
|
Fear = db.define('Fear');
|
|
|
|
Mind = db.define('Mind');
|
|
|
|
|
|
|
|
// syntax 1 (old)
|
|
|
|
Item.belongsTo(List);
|
2015-02-03 10:37:43 +00:00
|
|
|
Object.keys((new Item).toObject()).should.containEql('listId');
|
2014-01-24 17:09:53 +00:00
|
|
|
(new Item).list.should.be.an.instanceOf(Function);
|
|
|
|
|
|
|
|
// syntax 2 (new)
|
2015-03-19 15:50:26 +00:00
|
|
|
Fear.belongsTo('mind', {
|
2016-08-19 17:46:59 +00:00
|
|
|
methods: {check: function() { return true; }},
|
2015-03-19 15:50:26 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2015-02-03 10:37:43 +00:00
|
|
|
Object.keys((new Fear).toObject()).should.containEql('mindId');
|
2014-01-24 17:09:53 +00:00
|
|
|
(new Fear).mind.should.be.an.instanceOf(Function);
|
|
|
|
// (new Fear).mind.build().should.be.an.instanceOf(Mind);
|
|
|
|
});
|
2013-03-26 20:48:14 +00:00
|
|
|
|
2015-03-19 15:50:26 +00:00
|
|
|
it('should setup a custom method on accessor', function() {
|
|
|
|
var rel = Fear.relations['mind'];
|
|
|
|
rel.defineMethod('other', function() {
|
|
|
|
return true;
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-03-19 15:50:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should have setup a custom method on accessor', function() {
|
|
|
|
var f = new Fear();
|
|
|
|
f.mind.check.should.be.a.function;
|
|
|
|
f.mind.check().should.be.true;
|
|
|
|
f.mind.other.should.be.a.function;
|
|
|
|
f.mind.other().should.be.true;
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
List.hasMany('todos', {model: Item});
|
2016-04-01 11:48:17 +00:00
|
|
|
db.automigrate(['List', 'Item', 'Fear', 'Mind'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
List.create({name: 'List 1'}, function(e, list) {
|
2014-08-20 14:46:54 +00:00
|
|
|
listId = list.id;
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(list);
|
2016-08-19 17:46:59 +00:00
|
|
|
list.todos.create({name: 'Item 1'}, function(err, todo) {
|
2014-08-20 14:46:54 +00:00
|
|
|
itemId = todo.id;
|
2016-04-01 11:48:17 +00:00
|
|
|
todo.list(function(e, l) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(l);
|
|
|
|
l.should.be.an.instanceOf(List);
|
2014-06-27 06:38:04 +00:00
|
|
|
todo.list().id.should.equal(l.id);
|
2014-08-20 14:46:54 +00:00
|
|
|
todo.list().name.should.equal('List 1');
|
2014-01-24 17:09:53 +00:00
|
|
|
done();
|
2013-03-27 17:53:07 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-27 17:53:07 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
2013-04-15 05:13:31 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data with getAsync with callback', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
List.hasMany('todos', {model: Item});
|
2016-04-01 11:48:17 +00:00
|
|
|
db.automigrate(['List', 'Item', 'Fear', 'Find'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
List.create({name: 'List 1'}, function(e, list) {
|
2015-02-18 05:04:31 +00:00
|
|
|
listId = list.id;
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(list);
|
2016-08-19 17:46:59 +00:00
|
|
|
list.todos.create({name: 'Item 1'}, function(err, todo) {
|
2015-02-18 05:04:31 +00:00
|
|
|
itemId = todo.id;
|
2016-04-01 11:48:17 +00:00
|
|
|
todo.list.getAsync(function(e, l) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(l);
|
|
|
|
l.should.be.an.instanceOf(List);
|
|
|
|
todo.list().id.should.equal(l.id);
|
|
|
|
todo.list().name.should.equal('List 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data with promises', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
List.hasMany('todos', {model: Item});
|
2016-04-01 11:48:17 +00:00
|
|
|
db.automigrate(['List', 'Item', 'Fear', 'Find'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
List.create({name: 'List 1'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(list) {
|
2015-02-18 05:04:31 +00:00
|
|
|
listId = list.id;
|
|
|
|
should.exist(list);
|
2016-08-19 17:46:59 +00:00
|
|
|
return list.todos.create({name: 'Item 1'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(todo) {
|
2015-02-18 05:04:31 +00:00
|
|
|
itemId = todo.id;
|
|
|
|
return todo.list.getAsync()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(l) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(l);
|
|
|
|
l.should.be.an.instanceOf(List);
|
|
|
|
todo.list().id.should.equal(l.id);
|
|
|
|
todo.list().name.should.equal('List 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('could accept objects when creating on scope', function(done) {
|
|
|
|
List.create(function(e, list) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(list);
|
2016-08-19 17:46:59 +00:00
|
|
|
Item.create({list: list}, function(err, item) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(item);
|
|
|
|
should.exist(item.listId);
|
|
|
|
item.listId.should.equal(list.id);
|
|
|
|
item.__cachedRelations.list.should.equal(list);
|
|
|
|
done();
|
2013-04-18 09:05:11 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 00:39:47 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-20 14:46:54 +00:00
|
|
|
it('should update related item on scope', function(done) {
|
2016-04-01 11:48:17 +00:00
|
|
|
Item.findById(itemId, function(e, todo) {
|
2016-08-19 17:46:59 +00:00
|
|
|
todo.list.update({name: 'List A'}, function(err, list) {
|
2014-08-20 14:46:54 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(list);
|
|
|
|
list.name.should.equal('List A');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-20 14:46:54 +00:00
|
|
|
it('should get related item on scope', function(done) {
|
2016-04-01 11:48:17 +00:00
|
|
|
Item.findById(itemId, function(e, todo) {
|
2014-08-20 14:46:54 +00:00
|
|
|
todo.list(function(err, list) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(list);
|
|
|
|
list.name.should.equal('List A');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-20 14:46:54 +00:00
|
|
|
it('should destroy related item on scope', function(done) {
|
2016-04-01 11:48:17 +00:00
|
|
|
Item.findById(itemId, function(e, todo) {
|
2014-08-20 14:46:54 +00:00
|
|
|
todo.list.destroy(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-20 14:46:54 +00:00
|
|
|
it('should get related item on scope - verify', function(done) {
|
2016-04-01 11:48:17 +00:00
|
|
|
Item.findById(itemId, function(e, todo) {
|
2014-08-20 14:46:54 +00:00
|
|
|
todo.list(function(err, list) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.not.exist(list);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-20 14:46:54 +00:00
|
|
|
it('should not have deleted related item', function(done) {
|
2016-04-01 11:48:17 +00:00
|
|
|
List.findById(listId, function(e, list) {
|
2014-08-20 14:46:54 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(list);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-03-26 00:39:47 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to create belongsTo model in beforeCreate hook', function(done) {
|
2014-11-24 11:20:38 +00:00
|
|
|
var mind;
|
2016-04-01 11:48:17 +00:00
|
|
|
Fear.beforeCreate = function(next) {
|
|
|
|
this.mind.create(function(err, m) {
|
2014-11-24 11:20:38 +00:00
|
|
|
mind = m;
|
|
|
|
if (err) next(err); else next();
|
|
|
|
});
|
|
|
|
};
|
2016-04-01 11:48:17 +00:00
|
|
|
Fear.create(function(err, fear) {
|
2014-11-24 11:20:38 +00:00
|
|
|
should.not.exists(err);
|
|
|
|
should.exists(fear);
|
|
|
|
fear.mindId.should.be.equal(mind.id);
|
|
|
|
should.exists(fear.mind());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to create belongsTo model in beforeCreate hook with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var mind;
|
2016-04-01 11:48:17 +00:00
|
|
|
Fear.beforeCreate = function(next) {
|
2015-02-18 05:04:31 +00:00
|
|
|
this.mind.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(m) {
|
2015-02-18 05:04:31 +00:00
|
|
|
mind = m;
|
|
|
|
next();
|
|
|
|
}).catch(next);
|
|
|
|
};
|
|
|
|
Fear.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(fear) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exists(fear);
|
|
|
|
fear.mindId.should.be.equal(mind.id);
|
|
|
|
should.exists(fear.mind());
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('belongsTo with scope', function() {
|
2014-07-23 09:10:44 +00:00
|
|
|
var Person, Passport;
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared with scope and properties', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person = db.define('Person', {name: String, age: Number, passportNotes: String});
|
|
|
|
Passport = db.define('Passport', {name: String, notes: String});
|
2014-07-23 09:10:44 +00:00
|
|
|
Passport.belongsTo(Person, {
|
2016-08-19 17:46:59 +00:00
|
|
|
properties: {notes: 'passportNotes'},
|
|
|
|
scope: {fields: {id: true, name: true}},
|
2014-07-23 09:10:44 +00:00
|
|
|
});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person', 'Passport'], done);
|
2014-07-23 09:10:44 +00:00
|
|
|
});
|
2014-07-28 21:22:45 +00:00
|
|
|
|
|
|
|
var personCreated;
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var p = new Passport({name: 'Passport', notes: 'Some notes...'});
|
|
|
|
p.person.create({name: 'Fred', age: 36}, function(err, person) {
|
2014-07-28 21:22:45 +00:00
|
|
|
personCreated = person;
|
2014-07-23 09:10:44 +00:00
|
|
|
p.personId.should.equal(person.id);
|
2014-08-15 09:28:25 +00:00
|
|
|
person.name.should.equal('Fred');
|
|
|
|
person.passportNotes.should.equal('Some notes...');
|
2016-04-01 11:48:17 +00:00
|
|
|
p.save(function(err, passport) {
|
2014-11-24 11:20:38 +00:00
|
|
|
should.not.exists(err);
|
|
|
|
done();
|
|
|
|
});
|
2014-07-23 09:10:44 +00:00
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find record on scope', function(done) {
|
|
|
|
Passport.findOne(function(err, p) {
|
2014-07-28 23:15:37 +00:00
|
|
|
p.personId.should.eql(personCreated.id);
|
2014-07-23 09:10:44 +00:00
|
|
|
p.person(function(err, person) {
|
|
|
|
person.name.should.equal('Fred');
|
2015-02-03 10:37:43 +00:00
|
|
|
person.should.have.property('age', undefined);
|
|
|
|
person.should.have.property('passportNotes', undefined);
|
2014-07-23 09:10:44 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope with promises', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var p = new Passport({name: 'Passport', notes: 'Some notes...'});
|
|
|
|
p.person.create({name: 'Fred', age: 36})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(person) {
|
2015-02-18 05:04:31 +00:00
|
|
|
p.personId.should.equal(person.id);
|
|
|
|
person.name.should.equal('Fred');
|
|
|
|
person.passportNotes.should.equal('Some notes...');
|
2016-04-01 11:48:17 +00:00
|
|
|
return p.save();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(passport) {
|
2015-02-18 05:04:31 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find record on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Passport.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
p.personId.should.eql(personCreated.id);
|
2016-04-01 11:48:17 +00:00
|
|
|
return p.person.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(person) {
|
2015-02-18 05:04:31 +00:00
|
|
|
person.name.should.equal('Fred');
|
|
|
|
person.should.have.property('age', undefined);
|
|
|
|
person.should.have.property('passportNotes', undefined);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2014-07-23 09:10:44 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2015-02-21 00:10:25 +00:00
|
|
|
// Disable the tests until the issue in
|
|
|
|
// https://github.com/strongloop/loopback-datasource-juggler/pull/399
|
|
|
|
// is fixed
|
2016-04-01 11:48:17 +00:00
|
|
|
describe.skip('belongsTo with embed', function() {
|
2015-01-19 14:49:20 +00:00
|
|
|
var Person, Passport;
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared with embed and properties', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person = db.define('Person', {name: String, age: Number});
|
|
|
|
Passport = db.define('Passport', {name: String, notes: String});
|
2015-01-19 14:49:20 +00:00
|
|
|
Passport.belongsTo(Person, {
|
|
|
|
properties: ['name'],
|
2016-08-19 17:46:59 +00:00
|
|
|
options: {embedsProperties: true, invertProperties: true},
|
2015-01-19 14:49:20 +00:00
|
|
|
});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person', 'Passport'], done);
|
2015-01-19 14:49:20 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record with embedded data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Fred', age: 36}, function(err, person) {
|
|
|
|
var p = new Passport({name: 'Passport', notes: 'Some notes...'});
|
2015-01-19 14:49:20 +00:00
|
|
|
p.person(person);
|
|
|
|
p.personId.should.equal(person.id);
|
|
|
|
var data = p.toObject(true);
|
|
|
|
data.person.id.should.equal(person.id);
|
|
|
|
data.person.name.should.equal('Fred');
|
2016-04-01 11:48:17 +00:00
|
|
|
p.save(function(err) {
|
2015-01-19 14:49:20 +00:00
|
|
|
should.not.exists(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find record with embedded data', function(done) {
|
|
|
|
Passport.findOne(function(err, p) {
|
2015-01-19 14:49:20 +00:00
|
|
|
should.not.exists(err);
|
|
|
|
var data = p.toObject(true);
|
|
|
|
data.person.id.should.equal(p.personId);
|
|
|
|
data.person.name.should.equal('Fred');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find record with embedded data with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Passport.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var data = p.toObject(true);
|
|
|
|
data.person.id.should.equal(p.personId);
|
|
|
|
data.person.name.should.equal('Fred');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2015-01-19 14:49:20 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasOne', function() {
|
2014-06-16 08:17:37 +00:00
|
|
|
var Supplier, Account;
|
2014-08-20 14:46:54 +00:00
|
|
|
var supplierId, accountId;
|
2014-06-16 08:17:37 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function() {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Supplier = db.define('Supplier', {name: String});
|
|
|
|
Account = db.define('Account', {accountNo: String, supplierName: String});
|
2014-06-16 08:17:37 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared using hasOne method', function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
Supplier.hasOne(Account, {
|
2016-08-19 17:46:59 +00:00
|
|
|
properties: {name: 'supplierName'},
|
|
|
|
methods: {check: function() { return true; }},
|
2015-03-19 15:50:26 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
Object.keys((new Account()).toObject()).should.containEql('supplierId');
|
2014-06-16 08:17:37 +00:00
|
|
|
(new Supplier()).account.should.be.an.instanceOf(Function);
|
|
|
|
});
|
|
|
|
|
2015-03-19 15:50:26 +00:00
|
|
|
it('should setup a custom method on accessor', function() {
|
|
|
|
var rel = Supplier.relations['account'];
|
|
|
|
rel.defineMethod('other', function() {
|
|
|
|
return true;
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-03-19 15:50:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should have setup a custom method on accessor', function() {
|
|
|
|
var s = new Supplier();
|
|
|
|
s.account.check.should.be.a.function;
|
|
|
|
s.account.check().should.be.true;
|
|
|
|
s.account.other.should.be.a.function;
|
|
|
|
s.account.other().should.be.true;
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data', function(done) {
|
|
|
|
db.automigrate(['Supplier', 'Account'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Supplier.create({name: 'Supplier 1'}, function(e, supplier) {
|
2014-08-20 13:54:47 +00:00
|
|
|
supplierId = supplier.id;
|
2014-06-16 08:17:37 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
2016-08-19 17:46:59 +00:00
|
|
|
supplier.account.create({accountNo: 'a01'}, function(err, account) {
|
2016-04-01 11:48:17 +00:00
|
|
|
supplier.account(function(e, act) {
|
2014-08-20 14:46:54 +00:00
|
|
|
accountId = act.id;
|
2014-06-16 08:17:37 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(act);
|
|
|
|
act.should.be.an.instanceOf(Account);
|
2014-06-27 06:38:04 +00:00
|
|
|
supplier.account().id.should.equal(act.id);
|
2014-07-11 13:29:47 +00:00
|
|
|
act.supplierName.should.equal(supplier.name);
|
2014-06-16 08:17:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data with getAsync with callback', function(done) {
|
|
|
|
db.automigrate(['Supplier', 'Account'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Supplier.create({name: 'Supplier 1'}, function(e, supplier) {
|
2015-02-18 05:04:31 +00:00
|
|
|
supplierId = supplier.id;
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
2016-08-19 17:46:59 +00:00
|
|
|
supplier.account.create({accountNo: 'a01'}, function(err, account) {
|
2016-04-01 11:48:17 +00:00
|
|
|
supplier.account.getAsync(function(e, act) {
|
2015-02-18 05:04:31 +00:00
|
|
|
accountId = act.id;
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(act);
|
|
|
|
act.should.be.an.instanceOf(Account);
|
|
|
|
supplier.account().id.should.equal(act.id);
|
|
|
|
act.supplierName.should.equal(supplier.name);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data with promises', function(done) {
|
|
|
|
db.automigrate(['Supplier', 'Account'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Supplier.create({name: 'Supplier 1'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(supplier) {
|
2015-02-18 05:04:31 +00:00
|
|
|
supplierId = supplier.id;
|
|
|
|
should.exist(supplier);
|
2016-08-19 17:46:59 +00:00
|
|
|
return supplier.account.create({accountNo: 'a01'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(account) {
|
|
|
|
return supplier.account.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(act) {
|
2015-02-18 05:04:31 +00:00
|
|
|
accountId = act.id;
|
|
|
|
should.exist(act);
|
|
|
|
act.should.be.an.instanceOf(Account);
|
|
|
|
supplier.account().id.should.equal(act.id);
|
|
|
|
act.supplierName.should.equal(supplier.name);
|
|
|
|
done();
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-24 17:52:55 +00:00
|
|
|
it('should set targetClass on scope property', function() {
|
|
|
|
should.equal(Supplier.prototype.account._targetClass, 'Account');
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-20 13:54:47 +00:00
|
|
|
it('should update the related item on scope', function(done) {
|
|
|
|
Supplier.findById(supplierId, function(e, supplier) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
2016-08-19 17:46:59 +00:00
|
|
|
supplier.account.update({supplierName: 'Supplier A'}, function(err, act) {
|
2014-08-20 13:54:47 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
act.supplierName.should.equal('Supplier A');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-01-30 07:26:11 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
it('should update the related item on scope with promises', function(done) {
|
|
|
|
Supplier.findById(supplierId)
|
|
|
|
.then(function(supplier) {
|
|
|
|
should.exist(supplier);
|
2016-08-19 17:46:59 +00:00
|
|
|
return supplier.account.update({supplierName: 'Supplier B'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
|
|
|
.then(function(act) {
|
|
|
|
act.supplierName.should.equal('Supplier B');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2015-01-30 07:26:11 +00:00
|
|
|
it('should ignore the foreign key in the update', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Supplier.create({name: 'Supplier 2'}, function(e, supplier) {
|
2015-01-30 07:26:11 +00:00
|
|
|
var sid = supplier.id;
|
|
|
|
Supplier.findById(supplierId, function(e, supplier) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
2016-08-19 17:46:59 +00:00
|
|
|
supplier.account.update({supplierName: 'Supplier A',
|
|
|
|
supplierId: sid},
|
2015-01-30 07:26:11 +00:00
|
|
|
function(err, act) {
|
|
|
|
should.not.exist(e);
|
|
|
|
act.supplierName.should.equal('Supplier A');
|
2015-02-02 18:51:31 +00:00
|
|
|
act.supplierId.should.eql(supplierId);
|
2015-01-30 07:26:11 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-20 13:54:47 +00:00
|
|
|
it('should get the related item on scope', function(done) {
|
|
|
|
Supplier.findById(supplierId, function(e, supplier) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
|
|
|
supplier.account(function(err, act) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(act);
|
|
|
|
act.supplierName.should.equal('Supplier A');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
it('should get the related item on scope with promises', function(done) {
|
|
|
|
Supplier.findById(supplierId)
|
|
|
|
.then(function(supplier) {
|
|
|
|
should.exist(supplier);
|
2016-04-01 11:48:17 +00:00
|
|
|
return supplier.account.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(act) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(act);
|
|
|
|
act.supplierName.should.equal('Supplier A');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2014-08-20 13:58:45 +00:00
|
|
|
it('should destroy the related item on scope', function(done) {
|
|
|
|
Supplier.findById(supplierId, function(e, supplier) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
|
|
|
supplier.account.destroy(function(err) {
|
|
|
|
should.not.exist(e);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
it('should destroy the related item on scope with promises', function(done) {
|
|
|
|
Supplier.findById(supplierId)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(supplier) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(supplier);
|
2016-08-19 17:46:59 +00:00
|
|
|
return supplier.account.create({accountNo: 'a01'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(account) {
|
|
|
|
return supplier.account.destroy();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(err) {
|
2015-02-18 05:04:31 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2014-08-20 13:58:45 +00:00
|
|
|
it('should get the related item on scope - verify', function(done) {
|
|
|
|
Supplier.findById(supplierId, function(e, supplier) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
|
|
|
supplier.account(function(err, act) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.not.exist(act);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
it('should get the related item on scope with promises - verify', function(done) {
|
|
|
|
Supplier.findById(supplierId)
|
|
|
|
.then(function(supplier) {
|
|
|
|
should.exist(supplier);
|
2016-04-01 11:48:17 +00:00
|
|
|
return supplier.account.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
|
|
|
.then(function(act) {
|
|
|
|
should.not.exist(act);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2014-08-20 14:46:54 +00:00
|
|
|
it('should have deleted related item', function(done) {
|
2016-04-01 11:48:17 +00:00
|
|
|
Supplier.findById(supplierId, function(e, supplier) {
|
2014-08-20 14:46:54 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-20 11:27:04 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasOne with scope', function() {
|
2014-11-20 11:27:04 +00:00
|
|
|
|
|
|
|
var Supplier, Account;
|
|
|
|
var supplierId, accountId;
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function() {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Supplier = db.define('Supplier', {name: String});
|
|
|
|
Account = db.define('Account', {accountNo: String, supplierName: String, block: Boolean});
|
|
|
|
Supplier.hasOne(Account, {scope: {where: {block: false}}, properties: {name: 'supplierName'}});
|
2014-11-20 11:27:04 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data', function(done) {
|
|
|
|
db.automigrate(['Supplier', 'Account'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Supplier.create({name: 'Supplier 1'}, function(e, supplier) {
|
2014-11-20 11:27:04 +00:00
|
|
|
supplierId = supplier.id;
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
2016-08-19 17:46:59 +00:00
|
|
|
supplier.account.create({accountNo: 'a01', block: false}, function(err, account) {
|
2016-04-01 11:48:17 +00:00
|
|
|
supplier.account(function(e, act) {
|
2014-11-20 11:27:04 +00:00
|
|
|
accountId = act.id;
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(act);
|
|
|
|
act.should.be.an.instanceOf(Account);
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(act.block);
|
|
|
|
act.block.should.be.false;
|
2014-11-20 11:27:04 +00:00
|
|
|
supplier.account().id.should.equal(act.id);
|
|
|
|
act.supplierName.should.equal(supplier.name);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-16 18:49:02 +00:00
|
|
|
it('should include record that matches scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Supplier.findById(supplierId, {include: 'account'}, function(err, supplier) {
|
2015-05-16 18:49:02 +00:00
|
|
|
should.exists(supplier.toJSON().account);
|
|
|
|
supplier.account(function(err, account) {
|
|
|
|
should.exists(account);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should not find record that does not match scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Account.updateAll({block: true}, function(err) {
|
2016-04-01 11:48:17 +00:00
|
|
|
Supplier.findById(supplierId, function(err, supplier) {
|
|
|
|
supplier.account(function(err, account) {
|
2014-11-20 11:27:04 +00:00
|
|
|
should.not.exists(account);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should not include record that does not match scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Account.updateAll({block: true}, function(err) {
|
|
|
|
Supplier.findById(supplierId, {include: 'account'}, function(err, supplier) {
|
2015-05-16 18:49:02 +00:00
|
|
|
should.not.exists(supplier.toJSON().account);
|
2016-04-01 11:48:17 +00:00
|
|
|
supplier.account(function(err, account) {
|
2015-05-16 18:49:02 +00:00
|
|
|
should.not.exists(account);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data with promises', function(done) {
|
|
|
|
db.automigrate(['Supplier', 'Account'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Supplier.create({name: 'Supplier 1'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(supplier) {
|
2015-02-18 05:04:31 +00:00
|
|
|
supplierId = supplier.id;
|
|
|
|
should.exist(supplier);
|
2016-08-19 17:46:59 +00:00
|
|
|
return supplier.account.create({accountNo: 'a01', block: false})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(account) {
|
|
|
|
return supplier.account.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(act) {
|
2015-02-18 05:04:31 +00:00
|
|
|
accountId = act.id;
|
|
|
|
should.exist(act);
|
|
|
|
act.should.be.an.instanceOf(Account);
|
|
|
|
should.exist(act.block);
|
|
|
|
act.block.should.be.false;
|
|
|
|
supplier.account().id.should.equal(act.id);
|
|
|
|
act.supplierName.should.equal(supplier.name);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find record that match scope with promises', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Account.updateAll({block: true})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
|
|
|
return Supplier.findById(supplierId);
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(supplier) {
|
|
|
|
return supplier.account.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(account) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.not.exist(account);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2014-06-16 08:17:37 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasOne with non standard id', function() {
|
2014-10-17 16:18:55 +00:00
|
|
|
var Supplier, Account;
|
|
|
|
var supplierId, accountId;
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function() {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2014-10-17 16:18:55 +00:00
|
|
|
Supplier = db.define('Supplier', {
|
|
|
|
sid: {
|
|
|
|
type: String,
|
2015-02-21 00:10:25 +00:00
|
|
|
id: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
generated: true,
|
2014-10-17 16:18:55 +00:00
|
|
|
},
|
2016-04-01 11:48:17 +00:00
|
|
|
name: String,
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
|
|
|
Account = db.define('Account', {
|
|
|
|
accid: {
|
|
|
|
type: String,
|
|
|
|
id: true,
|
2016-04-01 11:48:17 +00:00
|
|
|
generated: false,
|
2014-10-17 16:18:55 +00:00
|
|
|
},
|
2016-04-01 11:48:17 +00:00
|
|
|
supplierName: String,
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared with non standard foreignKey', function() {
|
2014-10-17 16:18:55 +00:00
|
|
|
Supplier.hasOne(Account, {
|
2016-08-19 17:46:59 +00:00
|
|
|
properties: {name: 'supplierName'},
|
2016-04-01 11:48:17 +00:00
|
|
|
foreignKey: 'sid',
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
Object.keys((new Account()).toObject()).should.containEql('sid');
|
2014-10-17 16:18:55 +00:00
|
|
|
(new Supplier()).account.should.be.an.instanceOf(Function);
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data', function(done) {
|
|
|
|
db.automigrate(['Supplier', 'Account'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Supplier.create({name: 'Supplier 1'}, function(e, supplier) {
|
2014-10-17 16:18:55 +00:00
|
|
|
supplierId = supplier.sid;
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
2016-08-19 17:46:59 +00:00
|
|
|
supplier.account.create({accid: 'a01'}, function(err, account) {
|
2016-04-01 11:48:17 +00:00
|
|
|
supplier.account(function(e, act) {
|
2014-10-17 16:18:55 +00:00
|
|
|
accountId = act.accid;
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(act);
|
|
|
|
act.should.be.an.instanceOf(Account);
|
|
|
|
supplier.account().accid.should.equal(act.accid);
|
|
|
|
act.supplierName.should.equal(supplier.name);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-10-17 16:18:55 +00:00
|
|
|
it('should destroy the related item on scope', function(done) {
|
|
|
|
Supplier.findById(supplierId, function(e, supplier) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
|
|
|
supplier.account.destroy(function(err) {
|
|
|
|
should.not.exist(e);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-10-17 16:18:55 +00:00
|
|
|
it('should get the related item on scope - verify', function(done) {
|
|
|
|
Supplier.findById(supplierId, function(e, supplier) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
|
|
|
supplier.account(function(err, act) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.not.exist(act);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-10-17 16:18:55 +00:00
|
|
|
it('should have deleted related item', function(done) {
|
2016-04-01 11:48:17 +00:00
|
|
|
Supplier.findById(supplierId, function(e, supplier) {
|
2014-10-17 16:18:55 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-10-17 16:18:55 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasOne with primaryKey different from model PK', function() {
|
2015-08-11 20:30:15 +00:00
|
|
|
var CompanyBoard, Boss;
|
|
|
|
var companyBoardId, bossId;
|
|
|
|
|
2015-09-11 18:15:00 +00:00
|
|
|
before(function() {
|
2015-08-11 20:30:15 +00:00
|
|
|
db = getSchema();
|
2015-09-11 18:15:00 +00:00
|
|
|
CompanyBoard = db.define('CompanyBoard', {
|
|
|
|
membersNumber: Number,
|
2016-04-01 11:48:17 +00:00
|
|
|
companyId: String,
|
2015-09-11 18:15:00 +00:00
|
|
|
});
|
|
|
|
Boss = db.define('Boss', {
|
2016-08-19 17:46:59 +00:00
|
|
|
id: {type: String, id: true, generated: false},
|
2015-09-11 18:15:00 +00:00
|
|
|
boardMembersNumber: Number,
|
2016-04-01 11:48:17 +00:00
|
|
|
companyId: String,
|
2015-09-11 18:15:00 +00:00
|
|
|
});
|
2015-08-11 20:30:15 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('relation can be declared with primaryKey', function() {
|
2015-08-11 20:30:15 +00:00
|
|
|
CompanyBoard.hasOne(Boss, {
|
2016-08-19 17:46:59 +00:00
|
|
|
properties: {membersNumber: 'boardMembersNumber'},
|
2015-08-11 20:30:15 +00:00
|
|
|
primaryKey: 'companyId',
|
2016-04-01 11:48:17 +00:00
|
|
|
foreignKey: 'companyId',
|
2015-08-11 20:30:15 +00:00
|
|
|
});
|
|
|
|
Object.keys((new Boss()).toObject()).should.containEql('companyId');
|
|
|
|
(new CompanyBoard()).boss.should.be.an.instanceOf(Function);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data', function(done) {
|
|
|
|
db.automigrate(['CompanyBoard', 'Boss'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
CompanyBoard.create({membersNumber: 7, companyId: 'Company1'}, function(e, companyBoard) {
|
2015-08-11 20:30:15 +00:00
|
|
|
companyBoardId = companyBoard.id;
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(companyBoard);
|
2016-08-19 17:46:59 +00:00
|
|
|
companyBoard.boss.create({id: 'a01'}, function(err, account) {
|
2016-04-01 11:48:17 +00:00
|
|
|
companyBoard.boss(function(e, boss) {
|
2015-08-11 20:30:15 +00:00
|
|
|
bossId = boss.id;
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(boss);
|
|
|
|
boss.should.be.an.instanceOf(Boss);
|
2015-09-08 16:53:02 +00:00
|
|
|
companyBoard.boss().id.should.eql(boss.id);
|
|
|
|
boss.boardMembersNumber.should.eql(companyBoard.membersNumber);
|
|
|
|
boss.companyId.should.eql(companyBoard.companyId);
|
2015-08-11 20:30:15 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should destroy the related item on scope', function(done) {
|
|
|
|
CompanyBoard.findById(companyBoardId, function(e, companyBoard) {
|
2015-08-11 20:30:15 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(companyBoard);
|
2016-04-01 11:48:17 +00:00
|
|
|
companyBoard.boss.destroy(function(err) {
|
2015-08-11 20:30:15 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should get the related item on scope - verify', function(done) {
|
|
|
|
CompanyBoard.findById(companyBoardId, function(e, companyBoard) {
|
2015-08-11 20:30:15 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(companyBoard);
|
2016-04-01 11:48:17 +00:00
|
|
|
companyBoard.boss(function(err, act) {
|
2015-08-11 20:30:15 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.not.exist(act);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasMany with primaryKey different from model PK', function() {
|
2015-08-31 20:24:47 +00:00
|
|
|
var Employee, Boss;
|
2016-04-01 11:48:17 +00:00
|
|
|
var COMPANY_ID = 'Company1';
|
2015-08-31 20:24:47 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function() {
|
2015-08-31 20:24:47 +00:00
|
|
|
db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Employee = db.define('Employee', {name: String, companyId: String});
|
|
|
|
Boss = db.define('Boss', {address: String, companyId: String});
|
2015-08-31 20:24:47 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('relation can be declared with primaryKey', function() {
|
2015-08-31 20:24:47 +00:00
|
|
|
Boss.hasMany(Employee, {
|
|
|
|
primaryKey: 'companyId',
|
2016-04-01 11:48:17 +00:00
|
|
|
foreignKey: 'companyId',
|
2015-08-31 20:24:47 +00:00
|
|
|
});
|
|
|
|
(new Boss()).employees.should.be.an.instanceOf(Function);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query employees for boss', function() {
|
|
|
|
return db.automigrate(['Employee', 'Boss']).then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return Boss.create({address: 'testAddress', companyId: COMPANY_ID})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(boss) {
|
2015-08-31 20:24:47 +00:00
|
|
|
should.exist(boss);
|
|
|
|
should.exist(boss.employees);
|
2016-08-19 17:46:59 +00:00
|
|
|
return boss.employees.create([{name: 'a01'}, {name: 'a02'}])
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(employees) {
|
2015-08-31 20:24:47 +00:00
|
|
|
should.exists(employees);
|
|
|
|
return boss.employees();
|
2016-04-01 11:48:17 +00:00
|
|
|
}).then(function(employees) {
|
2015-08-31 20:24:47 +00:00
|
|
|
var employee = employees[0];
|
|
|
|
should.exist(employee);
|
|
|
|
employees.length.should.equal(2);
|
|
|
|
employee.should.be.an.instanceOf(Employee);
|
2015-09-08 16:53:02 +00:00
|
|
|
employee.companyId.should.be.eql(boss.companyId);
|
2015-08-31 20:24:47 +00:00
|
|
|
return employees;
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-08-31 20:24:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query employees for boss2', function() {
|
|
|
|
return db.automigrate(['Employee', 'Boss']).then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return Boss.create({address: 'testAddress', companyId: COMPANY_ID})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(boss) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return Employee.create({name: 'a01', companyId: COMPANY_ID})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(employee) {
|
2015-08-31 20:24:47 +00:00
|
|
|
should.exist(employee);
|
|
|
|
return boss.employees.getAsync();
|
2016-04-01 11:48:17 +00:00
|
|
|
}).then(function(employees) {
|
2015-08-31 20:24:47 +00:00
|
|
|
should.exists(employees);
|
|
|
|
employees.length.should.equal(1);
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-08-31 20:24:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('belongsTo with primaryKey different from model PK', function() {
|
2015-08-31 20:24:47 +00:00
|
|
|
var Employee, Boss;
|
2016-04-01 11:48:17 +00:00
|
|
|
var COMPANY_ID = 'Company1';
|
2015-08-31 20:24:47 +00:00
|
|
|
var bossId;
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function() {
|
2015-08-31 20:24:47 +00:00
|
|
|
db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Employee = db.define('Employee', {name: String, companyId: String});
|
|
|
|
Boss = db.define('Boss', {address: String, companyId: String});
|
2015-08-31 20:24:47 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('relation can be declared with primaryKey', function() {
|
2015-08-31 20:24:47 +00:00
|
|
|
Employee.belongsTo(Boss, {
|
|
|
|
primaryKey: 'companyId',
|
2016-04-01 11:48:17 +00:00
|
|
|
foreignKey: 'companyId',
|
2015-08-31 20:24:47 +00:00
|
|
|
});
|
|
|
|
(new Employee()).boss.should.be.an.instanceOf(Function);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be used to query data', function() {
|
|
|
|
return db.automigrate(['Employee', 'Boss']).then(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return Boss.create({address: 'testAddress', companyId: COMPANY_ID})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(boss) {
|
2015-08-31 20:24:47 +00:00
|
|
|
bossId = boss.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
return Employee.create({name: 'a', companyId: COMPANY_ID});
|
2015-08-31 20:24:47 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(employee) {
|
2015-08-31 20:24:47 +00:00
|
|
|
should.exists(employee);
|
|
|
|
return employee.boss.getAsync();
|
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(boss) {
|
2015-08-31 20:24:47 +00:00
|
|
|
should.exists(boss);
|
2015-09-08 16:53:02 +00:00
|
|
|
boss.id.should.eql(bossId);
|
2015-08-31 20:24:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('hasAndBelongsToMany', function() {
|
2014-09-17 15:28:40 +00:00
|
|
|
var Article, TagName, ArticleTag;
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Article = db.define('Article', {title: String});
|
|
|
|
TagName = db.define('TagName', {name: String, flag: String});
|
2014-09-17 15:28:40 +00:00
|
|
|
Article.hasAndBelongsToMany('tagNames');
|
|
|
|
ArticleTag = db.models.ArticleTagName;
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Article', 'TagName', 'ArticleTagName'], done);
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-04-12 21:35:06 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to create instances on scope', function(done) {
|
|
|
|
Article.create(function(e, article) {
|
2016-08-19 17:46:59 +00:00
|
|
|
article.tagNames.create({name: 'popular'}, function(e, t) {
|
2014-09-17 15:28:40 +00:00
|
|
|
t.should.be.an.instanceOf(TagName);
|
2016-04-01 11:48:17 +00:00
|
|
|
ArticleTag.findOne(function(e, at) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.exist(at);
|
2014-09-17 15:28:40 +00:00
|
|
|
at.tagNameId.toString().should.equal(t.id.toString());
|
2014-01-24 17:09:53 +00:00
|
|
|
at.articleId.toString().should.equal(article.id.toString());
|
|
|
|
done();
|
|
|
|
});
|
2013-04-12 21:35:06 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
2013-04-12 21:35:06 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to fetch scoped instances', function(done) {
|
|
|
|
Article.findOne(function(e, article) {
|
|
|
|
article.tagNames(function(e, tags) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(tags);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-10-09 16:18:04 +00:00
|
|
|
article.tagNames().should.eql(tags);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
done();
|
2013-04-12 21:35:06 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
2013-04-12 21:35:06 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to add connection with instance', function(done) {
|
|
|
|
Article.findOne(function(e, article) {
|
2016-08-19 17:46:59 +00:00
|
|
|
TagName.create({name: 'awesome'}, function(e, tag) {
|
2016-04-01 11:48:17 +00:00
|
|
|
article.tagNames.add(tag, function(e, at) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(at);
|
|
|
|
at.should.be.an.instanceOf(ArticleTag);
|
2015-09-08 16:53:02 +00:00
|
|
|
at.tagNameId.should.eql(tag.id);
|
|
|
|
at.articleId.should.eql(article.id);
|
2014-01-24 17:09:53 +00:00
|
|
|
done();
|
|
|
|
});
|
2013-04-12 21:35:06 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
2013-04-12 21:35:06 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to remove connection with instance', function(done) {
|
|
|
|
Article.findOne(function(e, article) {
|
|
|
|
article.tagNames(function(e, tags) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var len = tags.length;
|
|
|
|
tags.should.not.be.empty;
|
2016-04-01 11:48:17 +00:00
|
|
|
article.tagNames.remove(tags[0], function(e) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(e);
|
2016-04-01 11:48:17 +00:00
|
|
|
article.tagNames(true, function(e, tags) {
|
2014-01-24 17:09:53 +00:00
|
|
|
tags.should.have.lengthOf(len - 1);
|
|
|
|
done();
|
2013-04-12 21:35:06 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-04-12 21:35:06 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 00:39:47 +00:00
|
|
|
});
|
2013-04-11 23:23:34 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to create instances on scope with promises', function(done) {
|
|
|
|
db.automigrate(['Article', 'TagName', 'ArticleTagName'], function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
Article.create()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(article) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return article.tagNames.create({name: 'popular'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(t) {
|
2015-02-18 05:04:31 +00:00
|
|
|
t.should.be.an.instanceOf(TagName);
|
|
|
|
return ArticleTag.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(at) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(at);
|
|
|
|
at.tagNameId.toString().should.equal(t.id.toString());
|
|
|
|
at.articleId.toString().should.equal(article.id.toString());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to fetch scoped instances with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Article.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(article) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return article.tagNames.getAsync()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(tags) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(tags);
|
|
|
|
article.tagNames().should.eql(tags);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to add connection with instance with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Article.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(article) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return TagName.create({name: 'awesome'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(tag) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return article.tagNames.add(tag)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(at) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(at);
|
|
|
|
at.should.be.an.instanceOf(ArticleTag);
|
2015-09-08 16:53:02 +00:00
|
|
|
at.tagNameId.should.eql(tag.id);
|
|
|
|
at.articleId.should.eql(article.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
done();
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to remove connection with instance with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Article.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(article) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return article.tagNames.getAsync()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(tags) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var len = tags.length;
|
|
|
|
tags.should.not.be.empty;
|
|
|
|
return article.tagNames.remove(tags[0])
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
|
|
|
return article.tagNames.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(tags) {
|
2015-02-18 05:04:31 +00:00
|
|
|
tags.should.have.lengthOf(len - 1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2014-04-02 17:32:37 +00:00
|
|
|
it('should set targetClass on scope property', function() {
|
2014-09-17 15:28:40 +00:00
|
|
|
should.equal(Article.prototype.tagNames._targetClass, 'TagName');
|
2014-04-02 17:32:37 +00:00
|
|
|
});
|
2015-02-28 18:53:18 +00:00
|
|
|
|
|
|
|
it('should apply inclusion fields to the target model', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Article.create({title: 'a1'}, function(e, article) {
|
2015-02-28 18:53:18 +00:00
|
|
|
should.not.exist(e);
|
2016-08-19 17:46:59 +00:00
|
|
|
article.tagNames.create({name: 't1', flag: '1'}, function(e, t) {
|
2015-02-28 18:53:18 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
Article.find({
|
2016-08-19 17:46:59 +00:00
|
|
|
where: {id: article.id},
|
|
|
|
include: {relation: 'tagNames', scope: {fields: ['name']}}},
|
2015-02-28 18:53:18 +00:00
|
|
|
function(e, articles) {
|
|
|
|
should.not.exist(e);
|
|
|
|
articles.should.have.property('length', 1);
|
|
|
|
var a = articles[0].toJSON();
|
|
|
|
a.should.have.property('title', 'a1');
|
|
|
|
a.should.have.property('tagNames');
|
|
|
|
a.tagNames.should.have.property('length', 1);
|
|
|
|
var n = a.tagNames[0];
|
|
|
|
n.should.have.property('name', 't1');
|
|
|
|
n.should.have.property('flag', undefined);
|
|
|
|
n.id.should.eql(t.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should apply inclusion where to the target model', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Article.create({title: 'a2'}, function(e, article) {
|
2015-02-28 18:53:18 +00:00
|
|
|
should.not.exist(e);
|
2016-08-19 17:46:59 +00:00
|
|
|
article.tagNames.create({name: 't2', flag: '2'}, function(e, t2) {
|
2015-02-28 18:53:18 +00:00
|
|
|
should.not.exist(e);
|
2016-08-19 17:46:59 +00:00
|
|
|
article.tagNames.create({name: 't3', flag: '3'}, function(e, t3) {
|
2015-02-28 18:53:18 +00:00
|
|
|
Article.find({
|
2016-08-19 17:46:59 +00:00
|
|
|
where: {id: article.id},
|
|
|
|
include: {relation: 'tagNames', scope: {where: {flag: '2'}}}},
|
2015-02-28 18:53:18 +00:00
|
|
|
function(e, articles) {
|
|
|
|
should.not.exist(e);
|
|
|
|
articles.should.have.property('length', 1);
|
|
|
|
var a = articles[0].toJSON();
|
|
|
|
a.should.have.property('title', 'a2');
|
|
|
|
a.should.have.property('tagNames');
|
|
|
|
a.tagNames.should.have.property('length', 1);
|
|
|
|
var n = a.tagNames[0];
|
|
|
|
n.should.have.property('name', 't2');
|
|
|
|
n.should.have.property('flag', '2');
|
|
|
|
n.id.should.eql(t2.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('embedsOne', function() {
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
var person;
|
2014-11-28 05:45:47 +00:00
|
|
|
var Passport;
|
2014-08-19 20:10:35 +00:00
|
|
|
var Other;
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function() {
|
2014-09-04 15:56:26 +00:00
|
|
|
tmp = getTransientDataSource();
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Person = db.define('Person', {name: String});
|
2014-08-30 09:43:36 +00:00
|
|
|
Passport = tmp.define('Passport',
|
2016-08-19 17:46:59 +00:00
|
|
|
{name: {type: 'string', required: true}},
|
|
|
|
{idInjection: false}
|
2014-08-19 20:10:35 +00:00
|
|
|
);
|
2016-08-19 17:46:59 +00:00
|
|
|
Address = tmp.define('Address', {street: String}, {idInjection: false});
|
|
|
|
Other = db.define('Other', {name: String});
|
2014-08-19 20:10:35 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared using embedsOne method', function(done) {
|
2014-08-19 20:10:35 +00:00
|
|
|
Person.embedsOne(Passport, {
|
2016-08-19 17:46:59 +00:00
|
|
|
default: {name: 'Anonymous'}, // a bit contrived
|
|
|
|
methods: {check: function() { return true; }},
|
2014-08-19 20:10:35 +00:00
|
|
|
});
|
2014-11-28 05:45:47 +00:00
|
|
|
Person.embedsOne(Address); // all by default
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person'], done);
|
2014-08-19 20:10:35 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
it('should have setup a property and accessor', function() {
|
|
|
|
var p = new Person();
|
|
|
|
p.passport.should.be.an.object; // because of default
|
|
|
|
p.passportItem.should.be.a.function;
|
|
|
|
p.passportItem.create.should.be.a.function;
|
|
|
|
p.passportItem.build.should.be.a.function;
|
|
|
|
p.passportItem.destroy.should.be.a.function;
|
|
|
|
});
|
2014-11-28 05:45:47 +00:00
|
|
|
|
2015-03-19 15:50:26 +00:00
|
|
|
it('should setup a custom method on accessor', function() {
|
|
|
|
var rel = Person.relations['passportItem'];
|
|
|
|
rel.defineMethod('other', function() {
|
|
|
|
return true;
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-03-19 15:50:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should have setup a custom method on accessor', function() {
|
|
|
|
var p = new Person();
|
|
|
|
p.passportItem.check.should.be.a.function;
|
|
|
|
p.passportItem.check().should.be.true;
|
|
|
|
p.passportItem.other.should.be.a.function;
|
|
|
|
p.passportItem.other().should.be.true;
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should behave properly without default or being set', function(done) {
|
2014-11-28 05:45:47 +00:00
|
|
|
var p = new Person();
|
|
|
|
should.not.exist(p.address);
|
|
|
|
var a = p.addressItem();
|
|
|
|
should.not.exist(a);
|
2016-04-01 11:48:17 +00:00
|
|
|
Person.create({}, function(err, p) {
|
2014-11-28 05:45:47 +00:00
|
|
|
should.not.exist(p.address);
|
|
|
|
var a = p.addressItem();
|
|
|
|
should.not.exist(a);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
it('should return an instance with default values', function() {
|
|
|
|
var p = new Person();
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passport.toObject().should.eql({name: 'Anonymous'});
|
2014-08-19 20:10:35 +00:00
|
|
|
p.passportItem().should.equal(p.passport);
|
|
|
|
p.passportItem(function(err, passport) {
|
|
|
|
should.not.exist(err);
|
|
|
|
passport.should.equal(p.passport);
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
it('should embed a model instance', function() {
|
|
|
|
var p = new Person();
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passportItem(new Passport({name: 'Fred'}));
|
|
|
|
p.passport.toObject().should.eql({name: 'Fred'});
|
2014-08-19 20:10:35 +00:00
|
|
|
p.passport.should.be.an.instanceOf(Passport);
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
it('should not embed an invalid model type', function() {
|
|
|
|
var p = new Person();
|
|
|
|
p.passportItem(new Other());
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passport.toObject().should.eql({name: 'Anonymous'});
|
2014-08-19 20:10:35 +00:00
|
|
|
p.passport.should.be.an.instanceOf(Passport);
|
|
|
|
});
|
2014-08-19 22:23:37 +00:00
|
|
|
|
|
|
|
var personId;
|
2014-08-19 20:10:35 +00:00
|
|
|
it('should create an embedded item on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Fred'}, function(err, p) {
|
2014-08-19 20:10:35 +00:00
|
|
|
should.not.exist(err);
|
2014-08-19 22:23:37 +00:00
|
|
|
personId = p.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passportItem.create({name: 'Fredric'}, function(err, passport) {
|
2014-08-19 20:10:35 +00:00
|
|
|
should.not.exist(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passport.toObject().should.eql({name: 'Fredric'});
|
2014-08-19 20:10:35 +00:00
|
|
|
p.passport.should.be.an.instanceOf(Passport);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
it('should get an embedded item on scope', function(done) {
|
2014-08-19 22:23:37 +00:00
|
|
|
Person.findById(personId, function(err, p) {
|
2014-08-19 20:10:35 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
var passport = p.passportItem();
|
2016-08-19 17:46:59 +00:00
|
|
|
passport.toObject().should.eql({name: 'Fredric'});
|
2014-08-19 20:10:35 +00:00
|
|
|
passport.should.be.an.instanceOf(Passport);
|
|
|
|
passport.should.equal(p.passport);
|
2014-09-04 19:51:59 +00:00
|
|
|
passport.should.equal(p.passportItem.value());
|
2014-08-19 20:10:35 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
it('should validate an embedded item on scope - on creation', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var p = new Person({name: 'Fred'});
|
2014-08-19 20:10:35 +00:00
|
|
|
p.passportItem.create({}, function(err, passport) {
|
|
|
|
should.exist(err);
|
|
|
|
err.name.should.equal('ValidationError');
|
2015-01-12 13:52:54 +00:00
|
|
|
err.details.messages.name.should.eql(['can\'t be blank']);
|
2014-08-19 20:10:35 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
it('should validate an embedded item on scope - on update', function(done) {
|
2014-08-19 22:23:37 +00:00
|
|
|
Person.findById(personId, function(err, p) {
|
2014-08-19 20:10:35 +00:00
|
|
|
var passport = p.passportItem();
|
|
|
|
passport.name = null;
|
|
|
|
p.save(function(err) {
|
|
|
|
should.exist(err);
|
|
|
|
err.name.should.equal('ValidationError');
|
2015-01-12 13:52:54 +00:00
|
|
|
err.details.messages.passportItem
|
|
|
|
.should.eql(['is invalid: `name` can\'t be blank']);
|
2014-08-19 20:10:35 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-20 13:37:40 +00:00
|
|
|
it('should update an embedded item on scope', function(done) {
|
|
|
|
Person.findById(personId, function(err, p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passportItem.update({name: 'Freddy'}, function(err, passport) {
|
2014-08-20 13:37:40 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
var passport = p.passportItem();
|
2016-08-19 17:46:59 +00:00
|
|
|
passport.toObject().should.eql({name: 'Freddy'});
|
2014-08-20 13:37:40 +00:00
|
|
|
passport.should.be.an.instanceOf(Passport);
|
|
|
|
passport.should.equal(p.passport);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-20 13:37:40 +00:00
|
|
|
it('should get an embedded item on scope - verify', function(done) {
|
|
|
|
Person.findById(personId, function(err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
var passport = p.passportItem();
|
2016-08-19 17:46:59 +00:00
|
|
|
passport.toObject().should.eql({name: 'Freddy'});
|
2014-08-20 13:37:40 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
it('should destroy an embedded item on scope', function(done) {
|
2014-08-19 22:23:37 +00:00
|
|
|
Person.findById(personId, function(err, p) {
|
2014-08-19 20:10:35 +00:00
|
|
|
p.passportItem.destroy(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.equal(p.passport, null);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
it('should get an embedded item on scope - verify', function(done) {
|
2014-08-19 22:23:37 +00:00
|
|
|
Person.findById(personId, function(err, p) {
|
2014-08-19 20:10:35 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.equal(p.passport, null);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-04-01 14:59:21 +00:00
|
|
|
it('should save an unsaved model', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var p = new Person({name: 'Fred'});
|
2015-04-01 14:59:21 +00:00
|
|
|
p.isNewRecord().should.be.true;
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passportItem.create({name: 'Fredric'}, function(err, passport) {
|
2015-04-01 14:59:21 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.passport.should.equal(passport);
|
|
|
|
p.isNewRecord().should.be.false;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
it('should create an embedded item on scope with promises', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Fred'})
|
2015-02-18 05:04:31 +00:00
|
|
|
.then(function(p) {
|
|
|
|
personId = p.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passportItem.create({name: 'Fredric'})
|
2015-02-18 05:04:31 +00:00
|
|
|
.then(function(passport) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passport.toObject().should.eql({name: 'Fredric'});
|
2015-02-18 05:04:31 +00:00
|
|
|
p.passport.should.be.an.instanceOf(Passport);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get an embedded item on scope with promises', function(done) {
|
|
|
|
Person.findById(personId)
|
|
|
|
.then(function(p) {
|
|
|
|
var passport = p.passportItem();
|
2016-08-19 17:46:59 +00:00
|
|
|
passport.toObject().should.eql({name: 'Fredric'});
|
2015-02-18 05:04:31 +00:00
|
|
|
passport.should.be.an.instanceOf(Passport);
|
|
|
|
passport.should.equal(p.passport);
|
|
|
|
passport.should.equal(p.passportItem.value());
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate an embedded item on scope with promises - on creation', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var p = new Person({name: 'Fred'});
|
2015-02-18 05:04:31 +00:00
|
|
|
p.passportItem.create({})
|
|
|
|
.then(function(passport) {
|
|
|
|
should.not.exist(passport);
|
|
|
|
done();
|
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.catch(function(err) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(err);
|
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
err.details.messages.name.should.eql(['can\'t be blank']);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate an embedded item on scope with promises - on update', function(done) {
|
|
|
|
Person.findById(personId)
|
|
|
|
.then(function(p) {
|
|
|
|
var passport = p.passportItem();
|
|
|
|
passport.name = null;
|
|
|
|
return p.save()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.not.exist(p);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
should.exist(err);
|
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
err.details.messages.passportItem
|
|
|
|
.should.eql(['is invalid: `name` can\'t be blank']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update an embedded item on scope with promises', function(done) {
|
|
|
|
Person.findById(personId)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return p.passportItem.update({name: 'Jason'})
|
2015-02-18 05:04:31 +00:00
|
|
|
.then(function(passport) {
|
|
|
|
var passport = p.passportItem();
|
2016-08-19 17:46:59 +00:00
|
|
|
passport.toObject().should.eql({name: 'Jason'});
|
2015-02-18 05:04:31 +00:00
|
|
|
passport.should.be.an.instanceOf(Passport);
|
|
|
|
passport.should.equal(p.passport);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get an embedded item on scope with promises - verify', function(done) {
|
|
|
|
Person.findById(personId)
|
|
|
|
.then(function(p) {
|
|
|
|
var passport = p.passportItem();
|
2016-08-19 17:46:59 +00:00
|
|
|
passport.toObject().should.eql({name: 'Jason'});
|
2015-02-18 05:04:31 +00:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should destroy an embedded item on scope with promises', function(done) {
|
|
|
|
Person.findById(personId)
|
|
|
|
.then(function(p) {
|
|
|
|
return p.passportItem.destroy()
|
|
|
|
.then(function() {
|
|
|
|
should.equal(p.passport, null);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get an embedded item on scope with promises - verify', function(done) {
|
|
|
|
Person.findById(personId)
|
|
|
|
.then(function(p) {
|
|
|
|
should.equal(p.passport, null);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2016-05-31 07:44:17 +00:00
|
|
|
|
|
|
|
it('should also save changes when directly saving the embedded model', function(done) {
|
|
|
|
// Passport should normally have an id for the direct save to work. For now override the check
|
|
|
|
var originalHasPK = Passport.definition.hasPK;
|
|
|
|
Passport.definition.hasPK = function() { return true; };
|
|
|
|
Person.findById(personId)
|
|
|
|
.then(function(p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return p.passportItem.create({name: 'Mitsos'});
|
2016-05-31 07:44:17 +00:00
|
|
|
})
|
|
|
|
.then(function(passport) {
|
|
|
|
passport.name = 'Jim';
|
|
|
|
return passport.save();
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
return Person.findById(personId);
|
|
|
|
})
|
|
|
|
.then(function(person) {
|
2016-08-19 17:46:59 +00:00
|
|
|
person.passportItem().toObject().should.eql({name: 'Jim'});
|
2016-05-31 07:44:17 +00:00
|
|
|
// restore original hasPk
|
|
|
|
Passport.definition.hasPK = originalHasPK;
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
Passport.definition.hasPK = originalHasPK;
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete the embedded document and also update parent', function(done) {
|
|
|
|
var originalHasPK = Passport.definition.hasPK;
|
|
|
|
Passport.definition.hasPK = function() { return true; };
|
|
|
|
Person.findById(personId)
|
|
|
|
.then(function(p) {
|
|
|
|
return p.passportItem().destroy();
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
return Person.findById(personId);
|
|
|
|
})
|
|
|
|
.then(function(person) {
|
|
|
|
person.should.have.property('passport', null);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
Passport.definition.hasPK = originalHasPK;
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2014-08-19 20:10:35 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('embedsOne - persisted model', function() {
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 11:10:23 +00:00
|
|
|
// This test spefically uses the Memory connector
|
|
|
|
// in order to test the use of the auto-generated
|
|
|
|
// id, in the sequence of the related model.
|
2016-08-22 19:55:22 +00:00
|
|
|
var Passport;
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function() {
|
2014-09-07 11:10:23 +00:00
|
|
|
db = getMemoryDataSource();
|
2016-08-19 17:46:59 +00:00
|
|
|
Person = db.define('Person', {name: String});
|
2014-09-07 11:10:23 +00:00
|
|
|
Passport = db.define('Passport',
|
2016-08-19 17:46:59 +00:00
|
|
|
{name: {type: 'string', required: true}}
|
2014-09-07 11:10:23 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared using embedsOne method', function(done) {
|
2014-09-07 11:10:23 +00:00
|
|
|
Person.embedsOne(Passport, {
|
2016-08-19 17:46:59 +00:00
|
|
|
options: {persistent: true},
|
2014-09-07 11:10:23 +00:00
|
|
|
});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person', 'Passport'], done);
|
2014-09-07 11:10:23 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 11:10:23 +00:00
|
|
|
it('should create an item - to offset id', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Passport.create({name: 'Wilma'}, function(err, p) {
|
2014-09-07 11:10:23 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.id.should.equal(1);
|
|
|
|
p.name.should.equal('Wilma');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 11:10:23 +00:00
|
|
|
it('should create an embedded item on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Fred'}, function(err, p) {
|
2014-09-07 11:10:23 +00:00
|
|
|
should.not.exist(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passportItem.create({name: 'Fredric'}, function(err, passport) {
|
2014-09-07 11:10:23 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.passport.id.should.eql(2);
|
|
|
|
p.passport.name.should.equal('Fredric');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
it('should create an embedded item on scope with promises', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Barney'})
|
2015-02-18 05:04:31 +00:00
|
|
|
.then(function(p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
return p.passportItem.create({name: 'Barnabus'})
|
2015-02-18 05:04:31 +00:00
|
|
|
.then(function(passport) {
|
|
|
|
p.passport.id.should.eql(3);
|
|
|
|
p.passport.name.should.equal('Barnabus');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2014-09-07 11:10:23 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('embedsOne - generated id', function() {
|
2016-08-22 19:55:22 +00:00
|
|
|
var Passport;
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function() {
|
2014-09-07 11:24:05 +00:00
|
|
|
tmp = getTransientDataSource();
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Person = db.define('Person', {name: String});
|
2014-09-07 11:24:05 +00:00
|
|
|
Passport = tmp.define('Passport',
|
2016-05-27 22:21:53 +00:00
|
|
|
{
|
2016-08-19 17:46:59 +00:00
|
|
|
id: {type: 'string', id: true, generated: true},
|
|
|
|
name: {type: 'string', required: true},
|
2016-05-27 22:21:53 +00:00
|
|
|
}
|
2014-09-07 11:24:05 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared using embedsOne method', function(done) {
|
2014-09-07 11:24:05 +00:00
|
|
|
Person.embedsOne(Passport);
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person'], done);
|
2014-09-07 11:24:05 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 11:24:05 +00:00
|
|
|
it('should create an embedded item on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Fred'}, function(err, p) {
|
2014-09-07 11:24:05 +00:00
|
|
|
should.not.exist(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
p.passportItem.create({name: 'Fredric'}, function(err, passport) {
|
2014-09-07 11:24:05 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
passport.id.should.match(/^[0-9a-fA-F]{24}$/);
|
|
|
|
p.passport.name.should.equal('Fredric');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 11:24:05 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('embedsMany', function() {
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
var address1, address2;
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
tmp = getTransientDataSource({defaultIdType: Number});
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Person = db.define('Person', {name: String});
|
|
|
|
Address = tmp.define('Address', {street: String});
|
2014-07-27 14:30:45 +00:00
|
|
|
Address.validatesPresenceOf('street');
|
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person'], done);
|
2014-07-27 14:30:45 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2014-07-27 14:30:45 +00:00
|
|
|
Person.embedsMany(Address);
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person'], done);
|
2014-07-27 14:30:45 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should have setup embedded accessor/scope', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
var p = new Person({name: 'Fred'});
|
2014-07-27 14:30:45 +00:00
|
|
|
p.addresses.should.be.an.array;
|
|
|
|
p.addresses.should.have.length(0);
|
|
|
|
p.addressList.should.be.a.function;
|
|
|
|
p.addressList.findById.should.be.a.function;
|
|
|
|
p.addressList.updateById.should.be.a.function;
|
|
|
|
p.addressList.destroy.should.be.a.function;
|
|
|
|
p.addressList.exists.should.be.a.function;
|
|
|
|
p.addressList.create.should.be.a.function;
|
|
|
|
p.addressList.build.should.be.a.function;
|
|
|
|
});
|
2014-07-28 23:15:37 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should create embedded items on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Fred'}, function(err, p) {
|
|
|
|
p.addressList.create({street: 'Street 1'}, function(err, address) {
|
2014-07-27 14:30:45 +00:00
|
|
|
should.not.exist(err);
|
2014-07-30 13:01:55 +00:00
|
|
|
address1 = address;
|
2014-07-28 23:15:37 +00:00
|
|
|
should.exist(address1.id);
|
2014-07-30 13:01:55 +00:00
|
|
|
address1.street.should.equal('Street 1');
|
2014-07-27 14:30:45 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should create embedded items on scope', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.create({street: 'Street 2'}, function(err, address) {
|
2014-07-27 14:30:45 +00:00
|
|
|
should.not.exist(err);
|
2014-07-30 13:01:55 +00:00
|
|
|
address2 = address;
|
2014-07-28 23:15:37 +00:00
|
|
|
should.exist(address2.id);
|
|
|
|
address2.street.should.equal('Street 2');
|
2014-07-27 14:30:45 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should return embedded items from scope', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addressList(function(err, addresses) {
|
|
|
|
should.not.exist(err);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-04 19:51:59 +00:00
|
|
|
var list = p.addressList();
|
|
|
|
list.should.equal(addresses);
|
|
|
|
list.should.equal(p.addresses);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-04 19:57:18 +00:00
|
|
|
p.addressList.value().should.equal(list);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
addresses.should.have.length(2);
|
2014-07-28 23:15:37 +00:00
|
|
|
addresses[0].id.should.eql(address1.id);
|
2014-07-27 14:30:45 +00:00
|
|
|
addresses[0].street.should.equal('Street 1');
|
2014-07-28 23:15:37 +00:00
|
|
|
addresses[1].id.should.eql(address2.id);
|
2014-07-27 14:30:45 +00:00
|
|
|
addresses[1].street.should.equal('Street 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should filter embedded items on scope', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList({where: {street: 'Street 2'}}, function(err, addresses) {
|
2014-07-27 14:30:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
addresses.should.have.length(1);
|
2014-07-28 23:15:37 +00:00
|
|
|
addresses[0].id.should.eql(address2.id);
|
2014-07-27 14:30:45 +00:00
|
|
|
addresses[0].street.should.equal('Street 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should validate embedded items', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2014-07-30 13:01:55 +00:00
|
|
|
p.addressList.create({}, function(err, address) {
|
2014-07-27 14:30:45 +00:00
|
|
|
should.exist(err);
|
2014-07-30 13:01:55 +00:00
|
|
|
should.not.exist(address);
|
2014-07-27 14:30:45 +00:00
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
err.details.codes.street.should.eql(['presence']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should find embedded items by id', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2014-07-28 23:15:37 +00:00
|
|
|
p.addressList.findById(address2.id, function(err, address) {
|
2014-07-27 14:30:45 +00:00
|
|
|
address.should.be.instanceof(Address);
|
2014-07-28 23:15:37 +00:00
|
|
|
address.id.should.eql(address2.id);
|
2014-07-27 14:30:45 +00:00
|
|
|
address.street.should.equal('Street 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should check if item exists', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2014-07-28 23:15:37 +00:00
|
|
|
p.addressList.exists(address2.id, function(err, exists) {
|
2014-07-27 14:30:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
exists.should.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should update embedded items by id', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.updateById(address2.id, {street: 'New Street'}, function(err, address) {
|
2014-07-27 14:30:45 +00:00
|
|
|
address.should.be.instanceof(Address);
|
2014-07-28 23:15:37 +00:00
|
|
|
address.id.should.eql(address2.id);
|
2014-07-27 14:30:45 +00:00
|
|
|
address.street.should.equal('New Street');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should validate the update of embedded items', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.updateById(address2.id, {street: null}, function(err, address) {
|
2014-07-27 14:30:45 +00:00
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
err.details.codes.street.should.eql(['presence']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should find embedded items by id - verify', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2014-07-28 23:15:37 +00:00
|
|
|
p.addressList.findById(address2.id, function(err, address) {
|
2014-07-27 14:30:45 +00:00
|
|
|
address.should.be.instanceof(Address);
|
2014-07-28 23:15:37 +00:00
|
|
|
address.id.should.eql(address2.id);
|
2014-07-27 14:30:45 +00:00
|
|
|
address.street.should.equal('New Street');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:51:33 +00:00
|
|
|
it('should have accessors: at, get, set', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addressList.at(0).id.should.equal(address1.id);
|
|
|
|
p.addressList.get(address1.id).id.should.equal(address1.id);
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.set(address1.id, {street: 'Changed 1'});
|
2014-07-29 08:51:33 +00:00
|
|
|
p.addresses[0].street.should.equal('Changed 1');
|
|
|
|
p.addressList.at(1).id.should.equal(address2.id);
|
|
|
|
p.addressList.get(address2.id).id.should.equal(address2.id);
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.set(address2.id, {street: 'Changed 2'});
|
2014-07-29 08:51:33 +00:00
|
|
|
p.addresses[1].street.should.equal('Changed 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should remove embedded items by id', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addresses.should.have.length(2);
|
2014-07-28 23:15:37 +00:00
|
|
|
p.addressList.destroy(address1.id, function(err) {
|
2014-07-27 14:30:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.addresses.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-03-24 14:35:56 +00:00
|
|
|
it('should have removed embedded items - verify', function(done) {
|
2014-07-27 14:30:45 +00:00
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addresses.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-03-24 14:35:56 +00:00
|
|
|
it('should create embedded items on scope', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.create({street: 'Street 3'}, function(err, address) {
|
2015-03-24 14:35:56 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
address.street.should.equal('Street 3');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove embedded items - filtered', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addresses.should.have.length(2);
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.destroyAll({street: 'Street 3'}, function(err) {
|
2015-03-24 14:35:56 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.addresses.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove all embedded items', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addresses.should.have.length(1);
|
|
|
|
p.addressList.destroyAll(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.addresses.should.have.length(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have removed all embedded items - verify', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addresses.should.have.length(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
|
2015-04-01 14:59:21 +00:00
|
|
|
it('should save an unsaved model', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var p = new Person({name: 'Fred'});
|
2015-04-01 14:59:21 +00:00
|
|
|
p.isNewRecord().should.be.true;
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.create({street: 'Street 4'}, function(err, address) {
|
2015-04-01 14:59:21 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
address.street.should.equal('Street 4');
|
|
|
|
p.isNewRecord().should.be.false;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-24 14:35:56 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('embedsMany - numeric ids + forceId', function() {
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2014-09-07 18:54:11 +00:00
|
|
|
tmp = getTransientDataSource();
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Person = db.define('Person', {name: String});
|
2014-09-07 18:54:11 +00:00
|
|
|
Address = tmp.define('Address', {
|
2016-08-19 17:46:59 +00:00
|
|
|
id: {type: Number, id: true},
|
2016-04-01 11:48:17 +00:00
|
|
|
street: String,
|
2014-09-07 18:54:11 +00:00
|
|
|
});
|
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person'], done);
|
2014-09-07 18:54:11 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.embedsMany(Address, {options: {forceId: true}});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person'], done);
|
2014-09-07 18:54:11 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 18:54:11 +00:00
|
|
|
it('should create embedded items on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Fred'}, function(err, p) {
|
|
|
|
p.addressList.create({street: 'Street 1'}, function(err, address) {
|
2014-09-07 18:54:11 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
address.id.should.equal(1);
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.create({street: 'Street 2'}, function(err, address) {
|
2014-09-07 18:54:11 +00:00
|
|
|
address.id.should.equal(2);
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.create({id: 12345, street: 'Street 3'}, function(err, address) {
|
2014-09-07 18:54:11 +00:00
|
|
|
address.id.should.equal(3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 18:54:11 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('embedsMany - explicit ids', function() {
|
|
|
|
before(function(done) {
|
2014-09-04 15:56:26 +00:00
|
|
|
tmp = getTransientDataSource();
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Person = db.define('Person', {name: String});
|
|
|
|
Address = tmp.define('Address', {street: String}, {forceId: false});
|
2014-07-27 14:30:45 +00:00
|
|
|
Address.validatesPresenceOf('street');
|
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person'], done);
|
2014-07-27 14:30:45 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2014-08-30 09:43:36 +00:00
|
|
|
Person.embedsMany(Address);
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person'], done);
|
2014-07-27 14:30:45 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should create embedded items on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Fred'}, function(err, p) {
|
|
|
|
p.addressList.create({id: 'home', street: 'Street 1'}, function(err, address) {
|
2014-07-27 14:30:45 +00:00
|
|
|
should.not.exist(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.create({id: 'work', street: 'Work Street 2'}, function(err, address) {
|
2014-07-30 13:01:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
address.id.should.equal('work');
|
|
|
|
address.street.should.equal('Work Street 2');
|
2014-07-27 14:30:45 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should find embedded items by id', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addressList.findById('work', function(err, address) {
|
|
|
|
address.should.be.instanceof(Address);
|
|
|
|
address.id.should.equal('work');
|
|
|
|
address.street.should.equal('Work Street 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should check for duplicate ids', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.create({id: 'home', street: 'Invalid'}, function(err, addresses) {
|
2014-07-27 14:30:45 +00:00
|
|
|
should.exist(err);
|
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
err.details.codes.addresses.should.eql(['uniqueness']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should update embedded items by id', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.updateById('home', {street: 'New Street'}, function(err, address) {
|
2014-07-27 14:30:45 +00:00
|
|
|
address.should.be.instanceof(Address);
|
|
|
|
address.id.should.equal('home');
|
|
|
|
address.street.should.equal('New Street');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should remove embedded items by id', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addresses.should.have.length(2);
|
|
|
|
p.addressList.destroy('home', function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.addresses.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
it('should have embedded items - verify', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addresses.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:54:01 +00:00
|
|
|
it('should validate all embedded items', function(done) {
|
|
|
|
var addresses = [];
|
2016-08-19 17:46:59 +00:00
|
|
|
addresses.push({id: 'home', street: 'Home Street'});
|
|
|
|
addresses.push({id: 'work', street: ''});
|
|
|
|
Person.create({name: 'Wilma', addresses: addresses}, function(err, p) {
|
2014-07-27 14:54:01 +00:00
|
|
|
err.name.should.equal('ValidationError');
|
2015-01-12 13:52:54 +00:00
|
|
|
err.details.messages.addresses.should.eql([
|
2016-04-01 11:48:17 +00:00
|
|
|
'contains invalid item: `work` (`street` can\'t be blank)',
|
2015-01-12 13:52:54 +00:00
|
|
|
]);
|
2014-07-27 14:54:01 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 15:16:25 +00:00
|
|
|
it('should build embedded items', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Wilma'}, function(err, p) {
|
|
|
|
p.addressList.build({id: 'home', street: 'Home'});
|
|
|
|
p.addressList.build({id: 'work', street: 'Work'});
|
2014-07-27 15:16:25 +00:00
|
|
|
p.addresses.should.have.length(2);
|
|
|
|
p.save(function(err, p) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 15:16:25 +00:00
|
|
|
it('should have embedded items - verify', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.findOne({where: {name: 'Wilma'}}, function(err, p) {
|
2014-07-27 15:16:25 +00:00
|
|
|
p.name.should.equal('Wilma');
|
|
|
|
p.addresses.should.have.length(2);
|
|
|
|
p.addresses[0].id.should.equal('home');
|
|
|
|
p.addresses[0].street.should.equal('Home');
|
|
|
|
p.addresses[1].id.should.equal('work');
|
|
|
|
p.addresses[1].street.should.equal('Work');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:51:33 +00:00
|
|
|
it('should have accessors: at, get, set', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.findOne({where: {name: 'Wilma'}}, function(err, p) {
|
2014-07-29 08:51:33 +00:00
|
|
|
p.name.should.equal('Wilma');
|
|
|
|
p.addresses.should.have.length(2);
|
|
|
|
p.addressList.at(0).id.should.equal('home');
|
|
|
|
p.addressList.get('home').id.should.equal('home');
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.set('home', {id: 'den'}).id.should.equal('den');
|
2014-07-29 08:51:33 +00:00
|
|
|
p.addressList.at(1).id.should.equal('work');
|
|
|
|
p.addressList.get('work').id.should.equal('work');
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.set('work', {id: 'factory'}).id.should.equal('factory');
|
2014-07-29 08:51:33 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-21 08:44:55 +00:00
|
|
|
it('should create embedded from attributes - property name', function(done) {
|
|
|
|
var addresses = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{id: 'home', street: 'Home Street'},
|
|
|
|
{id: 'work', street: 'Work Street'},
|
2014-08-21 08:44:55 +00:00
|
|
|
];
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Wilma', addresses: addresses}, function(err, p) {
|
2014-08-21 08:44:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.addressList.at(0).id.should.equal('home');
|
|
|
|
p.addressList.at(1).id.should.equal('work');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-21 08:44:55 +00:00
|
|
|
it('should not create embedded from attributes - relation name', function(done) {
|
|
|
|
var addresses = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{id: 'home', street: 'Home Street'},
|
|
|
|
{id: 'work', street: 'Work Street'},
|
2014-08-21 08:44:55 +00:00
|
|
|
];
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Wilma', addressList: addresses}, function(err, p) {
|
2014-08-21 08:44:55 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.addresses.should.have.length(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-30 09:43:36 +00:00
|
|
|
it('should create embedded items with auto-generated id', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Wilma'}, function(err, p) {
|
|
|
|
p.addressList.create({street: 'Home Street 1'}, function(err, address) {
|
2014-08-30 09:43:36 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
address.id.should.match(/^[0-9a-fA-F]{24}$/);
|
|
|
|
address.street.should.equal('Home Street 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('embedsMany - persisted model', function() {
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:17:42 +00:00
|
|
|
var address0, address1, address2;
|
2014-09-07 10:44:45 +00:00
|
|
|
var person;
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:59:47 +00:00
|
|
|
// This test spefically uses the Memory connector
|
|
|
|
// in order to test the use of the auto-generated
|
|
|
|
// id, in the sequence of the related model.
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2014-09-07 10:59:47 +00:00
|
|
|
db = getMemoryDataSource();
|
2016-08-19 17:46:59 +00:00
|
|
|
Person = db.define('Person', {name: String});
|
|
|
|
Address = db.define('Address', {street: String});
|
2014-09-07 10:17:42 +00:00
|
|
|
Address.validatesPresenceOf('street');
|
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person', 'Address'], done);
|
2014-09-07 10:17:42 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2014-09-07 10:59:47 +00:00
|
|
|
// to save related model itself, set
|
|
|
|
// persistent: true
|
|
|
|
Person.embedsMany(Address, {
|
2016-08-19 17:46:59 +00:00
|
|
|
scope: {order: 'street'},
|
|
|
|
options: {persistent: true},
|
2014-09-07 10:59:47 +00:00
|
|
|
});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Person', 'Address'], done);
|
2014-09-07 10:17:42 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:17:42 +00:00
|
|
|
it('should create individual items (0)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Address.create({street: 'Street 0'}, function(err, inst) {
|
2014-09-07 10:59:47 +00:00
|
|
|
inst.id.should.equal(1); // offset sequence
|
2014-09-07 10:17:42 +00:00
|
|
|
address0 = inst;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:17:42 +00:00
|
|
|
it('should create individual items (1)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Address.create({street: 'Street 1'}, function(err, inst) {
|
2014-09-07 10:59:47 +00:00
|
|
|
inst.id.should.equal(2);
|
2014-09-07 10:17:42 +00:00
|
|
|
address1 = inst;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:17:42 +00:00
|
|
|
it('should create individual items (2)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Address.create({street: 'Street 2'}, function(err, inst) {
|
2014-09-07 10:59:47 +00:00
|
|
|
inst.id.should.equal(3);
|
2014-09-07 10:17:42 +00:00
|
|
|
address2 = inst;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:59:47 +00:00
|
|
|
it('should create individual items (3)', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Address.create({street: 'Street 3'}, function(err, inst) {
|
2014-09-07 10:59:47 +00:00
|
|
|
inst.id.should.equal(4); // offset sequence
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:44:45 +00:00
|
|
|
it('should add embedded items on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Fred'}, function(err, p) {
|
2014-09-07 10:44:45 +00:00
|
|
|
person = p;
|
2014-09-07 10:17:42 +00:00
|
|
|
p.addressList.create(address1.toObject(), function(err, address) {
|
|
|
|
should.not.exist(err);
|
2014-09-07 10:59:47 +00:00
|
|
|
address.id.should.eql(2);
|
2014-09-07 10:44:45 +00:00
|
|
|
address.street.should.equal('Street 1');
|
2014-09-07 10:17:42 +00:00
|
|
|
p.addressList.create(address2.toObject(), function(err, address) {
|
|
|
|
should.not.exist(err);
|
2014-09-07 10:59:47 +00:00
|
|
|
address.id.should.eql(3);
|
2014-09-07 10:44:45 +00:00
|
|
|
address.street.should.equal('Street 2');
|
2014-09-07 10:17:42 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:44:45 +00:00
|
|
|
it('should create embedded items on scope', function(done) {
|
|
|
|
Person.findById(person.id, function(err, p) {
|
2016-08-19 17:46:59 +00:00
|
|
|
p.addressList.create({street: 'Street 4'}, function(err, address) {
|
2014-09-07 10:44:45 +00:00
|
|
|
should.not.exist(err);
|
2014-09-07 10:59:47 +00:00
|
|
|
address.id.should.equal(5); // in Address sequence, correct offset
|
|
|
|
address.street.should.equal('Street 4');
|
2014-09-07 10:44:45 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:44:45 +00:00
|
|
|
it('should have embedded items on scope', function(done) {
|
|
|
|
Person.findById(person.id, function(err, p) {
|
|
|
|
p.addressList(function(err, addresses) {
|
|
|
|
should.not.exist(err);
|
|
|
|
addresses.should.have.length(3);
|
|
|
|
addresses[0].street.should.equal('Street 1');
|
|
|
|
addresses[1].street.should.equal('Street 2');
|
2014-09-07 10:59:47 +00:00
|
|
|
addresses[2].street.should.equal('Street 4');
|
2014-09-07 10:44:45 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:17:42 +00:00
|
|
|
it('should validate embedded items on scope - id', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Wilma'}, function(err, p) {
|
|
|
|
p.addressList.create({id: null, street: 'Street 1'}, function(err, address) {
|
2014-09-07 10:59:47 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
address.street.should.equal('Street 1');
|
2014-09-07 10:17:42 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:17:42 +00:00
|
|
|
it('should validate embedded items on scope - street', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Person.create({name: 'Wilma'}, function(err, p) {
|
|
|
|
p.addressList.create({id: 1234}, function(err, address) {
|
2014-09-07 10:17:42 +00:00
|
|
|
should.exist(err);
|
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
err.details.codes.street.should.eql(['presence']);
|
|
|
|
var expected = 'The `Address` instance is not valid. ';
|
2015-01-12 13:52:54 +00:00
|
|
|
expected += 'Details: `street` can\'t be blank (value: undefined).';
|
2014-09-07 10:17:42 +00:00
|
|
|
err.message.should.equal(expected);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-07 10:17:42 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('embedsMany - relations, scope and properties', function() {
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-26 05:17:40 +00:00
|
|
|
var category, job1, job2, job3;
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function() {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Category = db.define('Category', {name: String});
|
|
|
|
Job = db.define('Job', {name: String});
|
|
|
|
Link = db.define('Link', {name: String, notes: String}, {forceId: false});
|
2014-07-29 08:54:28 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2015-02-03 10:37:43 +00:00
|
|
|
Category.embedsMany(Link, {
|
2014-07-29 08:54:28 +00:00
|
|
|
as: 'items', // rename
|
2016-08-19 17:46:59 +00:00
|
|
|
scope: {include: 'job'}, // always include
|
|
|
|
options: {belongsTo: 'job'}, // optional, for add()/remove()
|
2014-07-29 08:54:28 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
Link.belongsTo(Job, {
|
2014-08-26 05:17:40 +00:00
|
|
|
foreignKey: 'id', // re-use the actual job id
|
2016-08-19 17:46:59 +00:00
|
|
|
properties: {id: 'id', name: 'name'}, // denormalize, transfer id
|
|
|
|
options: {invertProperties: true},
|
2014-07-29 08:54:28 +00:00
|
|
|
});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Category', 'Job', 'Link'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Job.create({name: 'Job 0'}, done); // offset ids for tests
|
2014-07-29 15:43:30 +00:00
|
|
|
});
|
2014-07-29 08:54:28 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
it('should setup related items', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Job.create({name: 'Job 1'}, function(err, p) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
job1 = p;
|
2016-08-19 17:46:59 +00:00
|
|
|
Job.create({name: 'Job 2'}, function(err, p) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
job2 = p;
|
2016-08-19 17:46:59 +00:00
|
|
|
Job.create({name: 'Job 3'}, function(err, p) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
job3 = p;
|
2014-07-29 15:43:30 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-07-29 08:54:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-15 09:28:25 +00:00
|
|
|
it('should associate items on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Category.create({name: 'Category A'}, function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 08:54:28 +00:00
|
|
|
var link = cat.items.build();
|
2014-08-26 05:17:40 +00:00
|
|
|
link.job(job1);
|
2014-07-29 08:54:28 +00:00
|
|
|
var link = cat.items.build();
|
2014-08-26 05:17:40 +00:00
|
|
|
link.job(job2);
|
2014-07-29 08:54:28 +00:00
|
|
|
cat.save(function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
var job = cat.items.at(0);
|
|
|
|
job.should.be.instanceof(Link);
|
|
|
|
job.should.not.have.property('jobId');
|
|
|
|
job.id.should.eql(job1.id);
|
|
|
|
job.name.should.equal(job1.name);
|
|
|
|
var job = cat.items.at(1);
|
|
|
|
job.id.should.eql(job2.id);
|
|
|
|
job.name.should.equal(job2.name);
|
2014-07-29 08:54:28 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
it('should include related items on scope', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 08:54:28 +00:00
|
|
|
cat.links.should.have.length(2);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
// denormalized properties:
|
2014-07-29 15:43:30 +00:00
|
|
|
cat.items.at(0).should.be.instanceof(Link);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.items.at(0).id.should.eql(job1.id);
|
|
|
|
cat.items.at(0).name.should.equal(job1.name);
|
|
|
|
cat.items.at(1).id.should.eql(job2.id);
|
|
|
|
cat.items.at(1).name.should.equal(job2.name);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
// lazy-loaded relations
|
2014-08-26 05:17:40 +00:00
|
|
|
should.not.exist(cat.items.at(0).job());
|
|
|
|
should.not.exist(cat.items.at(1).job());
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
cat.items(function(err, items) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.items.at(0).job().should.be.instanceof(Job);
|
|
|
|
cat.items.at(1).job().should.be.instanceof(Job);
|
|
|
|
cat.items.at(1).job().name.should.equal('Job 2');
|
2014-07-29 08:54:28 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
it('should remove embedded items by id', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 08:54:28 +00:00
|
|
|
cat.links.should.have.length(2);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.items.destroy(job1.id, function(err) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 08:54:28 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
cat.links.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
it('should find items on scope', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 08:54:28 +00:00
|
|
|
cat.links.should.have.length(1);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.items.at(0).id.should.eql(job2.id);
|
|
|
|
cat.items.at(0).name.should.equal(job2.name);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
// lazy-loaded relations
|
2014-08-26 05:17:40 +00:00
|
|
|
should.not.exist(cat.items.at(0).job());
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
cat.items(function(err, items) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.items.at(0).job().should.be.instanceof(Job);
|
|
|
|
cat.items.at(0).job().name.should.equal('Job 2');
|
2014-07-29 08:54:28 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
it('should add related items to scope', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 15:43:30 +00:00
|
|
|
cat.links.should.have.length(1);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.items.add(job3, function(err, link) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 15:43:30 +00:00
|
|
|
link.should.be.instanceof(Link);
|
2014-08-26 05:17:40 +00:00
|
|
|
link.id.should.eql(job3.id);
|
|
|
|
link.name.should.equal('Job 3');
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
cat.links.should.have.length(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
it('should find items on scope', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 15:43:30 +00:00
|
|
|
cat.links.should.have.length(2);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
cat.items.at(0).should.be.instanceof(Link);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.items.at(0).id.should.eql(job2.id);
|
|
|
|
cat.items.at(0).name.should.equal(job2.name);
|
|
|
|
cat.items.at(1).id.should.eql(job3.id);
|
|
|
|
cat.items.at(1).name.should.equal(job3.name);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
it('should remove embedded items by reference id', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 15:43:30 +00:00
|
|
|
cat.links.should.have.length(2);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.items.remove(job2.id, function(err) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 15:43:30 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
cat.links.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-15 09:28:25 +00:00
|
|
|
it('should have removed embedded items by reference id', function(done) {
|
2014-07-29 15:43:30 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-07-29 15:43:30 +00:00
|
|
|
cat.links.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-08-15 20:48:38 +00:00
|
|
|
|
2014-08-26 05:17:40 +00:00
|
|
|
var jobId;
|
2014-08-15 20:48:38 +00:00
|
|
|
|
2014-08-15 09:28:25 +00:00
|
|
|
it('should create items on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Category.create({name: 'Category B'}, function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-15 09:28:25 +00:00
|
|
|
category = cat;
|
2016-08-19 17:46:59 +00:00
|
|
|
var link = cat.items.build({notes: 'Some notes...'});
|
|
|
|
link.job.create({name: 'Job 1'}, function(err, p) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
jobId = p.id;
|
2014-08-15 13:12:02 +00:00
|
|
|
cat.links[0].id.should.eql(p.id);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.links[0].name.should.equal('Job 1'); // denormalized
|
2014-08-15 13:12:02 +00:00
|
|
|
cat.links[0].notes.should.equal('Some notes...');
|
|
|
|
cat.items.at(0).should.equal(cat.links[0]);
|
|
|
|
done();
|
|
|
|
});
|
2014-08-15 09:28:25 +00:00
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-15 09:28:25 +00:00
|
|
|
it('should find items on scope', function(done) {
|
|
|
|
Category.findById(category.id, function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-15 09:28:25 +00:00
|
|
|
cat.name.should.equal('Category B');
|
|
|
|
cat.links.toObject().should.eql([
|
2016-08-19 17:46:59 +00:00
|
|
|
{id: jobId, name: 'Job 1', notes: 'Some notes...'},
|
2014-08-15 09:28:25 +00:00
|
|
|
]);
|
|
|
|
cat.items.at(0).should.equal(cat.links[0]);
|
|
|
|
cat.items(function(err, items) { // alternative access
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-15 09:28:25 +00:00
|
|
|
items.should.be.an.array;
|
|
|
|
items.should.have.length(1);
|
2014-08-26 05:17:40 +00:00
|
|
|
items[0].job(function(err, p) {
|
|
|
|
p.name.should.equal('Job 1'); // actual value
|
2014-08-15 09:28:25 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-15 13:12:02 +00:00
|
|
|
it('should update items on scope - and save parent', function(done) {
|
|
|
|
Category.findById(category.id, function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-15 13:12:02 +00:00
|
|
|
var link = cat.items.at(0);
|
2016-08-19 17:46:59 +00:00
|
|
|
link.updateAttributes({notes: 'Updated notes...'}, function(err, link) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-15 13:12:02 +00:00
|
|
|
link.notes.should.equal('Updated notes...');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-15 13:12:02 +00:00
|
|
|
it('should find items on scope - verify update', function(done) {
|
|
|
|
Category.findById(category.id, function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-15 13:12:02 +00:00
|
|
|
cat.name.should.equal('Category B');
|
|
|
|
cat.links.toObject().should.eql([
|
2016-08-19 17:46:59 +00:00
|
|
|
{id: jobId, name: 'Job 1', notes: 'Updated notes...'},
|
2014-08-15 13:12:02 +00:00
|
|
|
]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-15 13:24:00 +00:00
|
|
|
it('should remove items from scope - and save parent', function(done) {
|
|
|
|
Category.findById(category.id, function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-15 13:24:00 +00:00
|
|
|
cat.items.at(0).destroy(function(err, link) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-15 13:24:00 +00:00
|
|
|
cat.links.should.eql([]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-15 13:24:00 +00:00
|
|
|
it('should find items on scope - verify destroy', function(done) {
|
|
|
|
Category.findById(category.id, function(err, cat) {
|
2016-07-26 10:23:08 +00:00
|
|
|
if (err) return done(err);
|
2014-08-15 13:24:00 +00:00
|
|
|
cat.name.should.equal('Category B');
|
|
|
|
cat.links.should.eql([]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('embedsMany - polymorphic relations', function() {
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
var person1, person2;
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2014-09-04 15:56:26 +00:00
|
|
|
tmp = getTransientDataSource();
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
Book = db.define('Book', {name: String});
|
|
|
|
Author = db.define('Author', {name: String});
|
|
|
|
Reader = db.define('Reader', {name: String});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-09-04 15:31:53 +00:00
|
|
|
Link = tmp.define('Link', {
|
2016-08-19 17:46:59 +00:00
|
|
|
id: {type: Number, id: true},
|
2016-04-01 11:48:17 +00:00
|
|
|
name: String, notes: String,
|
2014-09-04 15:31:53 +00:00
|
|
|
}); // generic model
|
2014-07-29 11:57:49 +00:00
|
|
|
Link.validatesPresenceOf('linkedId');
|
|
|
|
Link.validatesPresenceOf('linkedType');
|
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Book', 'Author', 'Reader'], done);
|
2014-07-29 11:57:49 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2014-09-04 15:31:53 +00:00
|
|
|
var idType = db.connector.getDefaultIdType();
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.embedsMany(Link, {as: 'people',
|
2014-07-29 11:57:49 +00:00
|
|
|
polymorphic: 'linked',
|
2016-08-19 17:46:59 +00:00
|
|
|
scope: {include: 'linked'},
|
2015-02-03 10:37:43 +00:00
|
|
|
});
|
2014-07-29 11:57:49 +00:00
|
|
|
Link.belongsTo('linked', {
|
2016-08-19 17:46:59 +00:00
|
|
|
polymorphic: {idType: idType}, // native type
|
|
|
|
properties: {name: 'name'}, // denormalized
|
|
|
|
options: {invertProperties: true},
|
2014-07-29 11:57:49 +00:00
|
|
|
});
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Book', 'Author', 'Reader'], done);
|
2014-07-29 11:57:49 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
it('should setup related items', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Author.create({name: 'Author 1'}, function(err, p) {
|
2014-07-29 11:57:49 +00:00
|
|
|
person1 = p;
|
2016-08-19 17:46:59 +00:00
|
|
|
Reader.create({name: 'Reader 1'}, function(err, p) {
|
2014-07-29 11:57:49 +00:00
|
|
|
person2 = p;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
it('should create items on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.create({name: 'Book'}, function(err, book) {
|
|
|
|
var link = book.people.build({notes: 'Something ...'});
|
2014-07-29 11:57:49 +00:00
|
|
|
link.linked(person1);
|
|
|
|
var link = book.people.build();
|
|
|
|
link.linked(person2);
|
|
|
|
book.save(function(err, book) {
|
|
|
|
should.not.exist(err);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
var link = book.people.at(0);
|
|
|
|
link.should.be.instanceof(Link);
|
|
|
|
link.id.should.equal(1);
|
|
|
|
link.linkedId.should.eql(person1.id);
|
|
|
|
link.linkedType.should.equal('Author');
|
|
|
|
link.name.should.equal('Author 1');
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
var link = book.people.at(1);
|
|
|
|
link.should.be.instanceof(Link);
|
|
|
|
link.id.should.equal(2);
|
|
|
|
link.linkedId.should.eql(person2.id);
|
|
|
|
link.linkedType.should.equal('Reader');
|
|
|
|
link.name.should.equal('Reader 1');
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
it('should include related items on scope', function(done) {
|
|
|
|
Book.findOne(function(err, book) {
|
|
|
|
book.links.should.have.length(2);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
var link = book.people.at(0);
|
|
|
|
link.should.be.instanceof(Link);
|
|
|
|
link.id.should.equal(1);
|
|
|
|
link.linkedId.should.eql(person1.id);
|
|
|
|
link.linkedType.should.equal('Author');
|
|
|
|
link.notes.should.equal('Something ...');
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
var link = book.people.at(1);
|
|
|
|
link.should.be.instanceof(Link);
|
|
|
|
link.id.should.equal(2);
|
|
|
|
link.linkedId.should.eql(person2.id);
|
|
|
|
link.linkedType.should.equal('Reader');
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
// lazy-loaded relations
|
|
|
|
should.not.exist(book.people.at(0).linked());
|
|
|
|
should.not.exist(book.people.at(1).linked());
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
book.people(function(err, people) {
|
|
|
|
people[0].linked().should.be.instanceof(Author);
|
|
|
|
people[0].linked().name.should.equal('Author 1');
|
|
|
|
people[1].linked().should.be.instanceof(Reader);
|
|
|
|
people[1].linked().name.should.equal('Reader 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
it('should include nested related items on scope', function(done) {
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 12:05:18 +00:00
|
|
|
// There's some date duplication going on, so it might
|
|
|
|
// make sense to override toObject on a case-by-case basis
|
|
|
|
// to sort this out (delete links, keep people).
|
|
|
|
// In loopback, an afterRemote filter could do this as well.
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
Book.find({include: 'people'}, function(err, books) {
|
2014-07-29 11:57:49 +00:00
|
|
|
var obj = books[0].toObject();
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
obj.should.have.property('links');
|
|
|
|
obj.should.have.property('people');
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
obj.links.should.have.length(2);
|
|
|
|
obj.links[0].name.should.equal('Author 1');
|
|
|
|
obj.links[1].name.should.equal('Reader 1');
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
obj.people.should.have.length(2);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 12:05:18 +00:00
|
|
|
obj.people[0].name.should.equal('Author 1');
|
|
|
|
obj.people[0].notes.should.equal('Something ...');
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
obj.people[0].linked.name.should.equal('Author 1');
|
|
|
|
obj.people[1].linked.name.should.equal('Reader 1');
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('referencesMany', function() {
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-26 05:17:40 +00:00
|
|
|
var job1, job2, job3;
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Category = db.define('Category', {name: String});
|
|
|
|
Job = db.define('Job', {name: String});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Job', 'Category'], done);
|
2014-07-29 13:01:47 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2014-07-30 14:46:05 +00:00
|
|
|
var reverse = function(cb) {
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || createPromiseCallback();
|
2014-07-30 14:46:05 +00:00
|
|
|
var modelInstance = this.modelInstance;
|
|
|
|
var fk = this.definition.keyFrom;
|
|
|
|
var ids = modelInstance[fk] || [];
|
|
|
|
modelInstance.updateAttribute(fk, ids.reverse(), function(err, inst) {
|
|
|
|
cb(err, inst[fk] || []);
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-07-30 14:46:05 +00:00
|
|
|
};
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-30 14:46:05 +00:00
|
|
|
reverse.shared = true; // remoting
|
2016-08-19 17:46:59 +00:00
|
|
|
reverse.http = {verb: 'put', path: '/jobs/reverse'};
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
Category.referencesMany(Job, {scopeMethods: {
|
2016-04-01 11:48:17 +00:00
|
|
|
reverse: reverse,
|
|
|
|
}});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-26 05:17:40 +00:00
|
|
|
Category.prototype['__reverse__jobs'].should.be.a.function;
|
|
|
|
should.exist(Category.prototype['__reverse__jobs'].shared);
|
|
|
|
Category.prototype['__reverse__jobs'].http.should.eql(reverse.http);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Job', 'Category'], done);
|
2014-07-29 13:01:47 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should setup test records', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Job.create({name: 'Job 1'}, function(err, p) {
|
2014-08-26 05:17:40 +00:00
|
|
|
job1 = p;
|
2016-08-19 17:46:59 +00:00
|
|
|
Job.create({name: 'Job 3'}, function(err, p) {
|
2014-08-26 05:17:40 +00:00
|
|
|
job3 = p;
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Category.create({name: 'Category A'}, function(err, cat) {
|
2015-10-16 16:25:23 +00:00
|
|
|
cat.jobIds.should.be.an.array;
|
|
|
|
cat.jobIds.should.have.length(0);
|
2016-08-19 17:46:59 +00:00
|
|
|
cat.jobs.create({name: 'Job 2'}, function(err, p) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2015-10-16 16:25:23 +00:00
|
|
|
cat.jobIds.should.have.length(1);
|
|
|
|
cat.jobIds.should.eql([p.id]);
|
2014-08-26 05:17:40 +00:00
|
|
|
p.name.should.equal('Job 2');
|
|
|
|
job2 = p;
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should not allow duplicate record on scope', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobIds = [job2.id, job2.id];
|
2014-07-29 19:46:12 +00:00
|
|
|
cat.save(function(err, p) {
|
|
|
|
should.exist(err);
|
|
|
|
err.name.should.equal('ValidationError');
|
2014-08-26 05:17:40 +00:00
|
|
|
err.details.codes.jobs.should.eql(['uniqueness']);
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobIds.should.eql([job2.id]);
|
|
|
|
cat.jobs(function(err, jobs) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
var p = jobs[0];
|
|
|
|
p.id.should.eql(job2.id);
|
|
|
|
p.name.should.equal('Job 2');
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope - findById', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobIds.should.eql([job2.id]);
|
|
|
|
cat.jobs.findById(job2.id, function(err, p) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
p.should.be.instanceof(Job);
|
|
|
|
p.id.should.eql(job2.id);
|
|
|
|
p.name.should.equal('Job 2');
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check if a record exists on scope', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.exists(job2.id, function(err, exists) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(exists);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should update a record on scope', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var attrs = {name: 'Job 2 - edit'};
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.updateById(job2.id, attrs, function(err, p) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
p.name.should.equal(attrs.name);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should get a record by index - at', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.at(0, function(err, p) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
p.should.be.instanceof(Job);
|
|
|
|
p.id.should.eql(job2.id);
|
|
|
|
p.name.should.equal('Job 2 - edit');
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should add a record to scope - object', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.add(job1, function(err, prod) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobIds.should.eql([job2.id, job1.id]);
|
|
|
|
prod.id.should.eql(job1.id);
|
2014-07-30 13:01:55 +00:00
|
|
|
prod.should.have.property('name');
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should add a record to scope - object', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.add(job3.id, function(err, prod) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
var expected = [job2.id, job1.id, job3.id];
|
|
|
|
cat.jobIds.should.eql(expected);
|
|
|
|
prod.id.should.eql(job3.id);
|
2014-07-30 13:01:55 +00:00
|
|
|
prod.should.have.property('name');
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope - findById', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.findById(job3.id, function(err, p) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
p.id.should.eql(job3.id);
|
|
|
|
p.name.should.equal('Job 3');
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope - filter', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var filter = {where: {name: 'Job 1'}};
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs(filter, function(err, jobs) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
jobs.should.have.length(1);
|
|
|
|
var p = jobs[0];
|
|
|
|
p.id.should.eql(job1.id);
|
|
|
|
p.name.should.equal('Job 1');
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should remove items from scope', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.remove(job1.id, function(err, ids) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
var expected = [job2.id, job3.id];
|
|
|
|
cat.jobIds.should.eql(expected);
|
|
|
|
ids.should.eql(cat.jobIds);
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope - verify', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
var expected = [job2.id, job3.id];
|
|
|
|
cat.jobIds.should.eql(expected);
|
|
|
|
cat.jobs(function(err, jobs) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
jobs.should.have.length(2);
|
|
|
|
jobs[0].id.should.eql(job2.id);
|
|
|
|
jobs[1].id.should.eql(job3.id);
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-10-16 16:21:04 +00:00
|
|
|
it('should find items on scope and ordered them by name DESC', function(done) {
|
|
|
|
Category.find(function(err, categories) {
|
|
|
|
categories.should.have.length(1);
|
2016-08-19 17:46:59 +00:00
|
|
|
categories[0].jobs({order: 'name DESC'}, function(err, jobs) {
|
2015-10-16 16:21:04 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
jobs.should.have.length(2);
|
2016-04-01 11:48:17 +00:00
|
|
|
jobs[0].id.should.eql(job3.id);
|
|
|
|
jobs[1].id.should.eql(job2.id);
|
2015-10-16 16:21:04 +00:00
|
|
|
done();
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-10-16 16:21:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-29 20:59:44 +00:00
|
|
|
it('should allow custom scope methods - reverse', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.reverse(function(err, ids) {
|
|
|
|
var expected = [job3.id, job2.id];
|
2014-07-29 20:59:44 +00:00
|
|
|
ids.should.eql(expected);
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobIds.should.eql(expected);
|
2014-07-29 20:59:44 +00:00
|
|
|
done();
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2014-07-29 20:59:44 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
it('should include related items from scope', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Category.find({include: 'jobs'}, function(err, categories) {
|
2014-07-29 19:46:12 +00:00
|
|
|
categories.should.have.length(1);
|
|
|
|
var cat = categories[0].toObject();
|
|
|
|
cat.name.should.equal('Category A');
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.should.have.length(2);
|
|
|
|
cat.jobs[0].id.should.eql(job3.id);
|
|
|
|
cat.jobs[1].id.should.eql(job2.id);
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should destroy items from scope - destroyById', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.destroy(job2.id, function(err) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
var expected = [job3.id];
|
|
|
|
cat.jobIds.should.eql(expected);
|
|
|
|
Job.exists(job2.id, function(err, exists) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(exists);
|
2015-02-18 05:04:31 +00:00
|
|
|
exists.should.be.false;
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope - verify', function(done) {
|
2014-07-29 19:46:12 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
var expected = [job3.id];
|
|
|
|
cat.jobIds.should.eql(expected);
|
|
|
|
cat.jobs(function(err, jobs) {
|
2014-07-29 19:46:12 +00:00
|
|
|
should.not.exist(err);
|
2014-08-26 05:17:40 +00:00
|
|
|
jobs.should.have.length(1);
|
|
|
|
jobs[0].id.should.eql(job3.id);
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should setup test records with promises', function(done) {
|
|
|
|
db.automigrate(['Job', 'Category'], function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return Job.create({name: 'Job 1'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
job1 = p;
|
2016-08-19 17:46:59 +00:00
|
|
|
return Job.create({name: 'Job 3'});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
|
|
|
job3 = p;
|
|
|
|
done();
|
2015-02-18 05:04:31 +00:00
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should create record on scope with promises', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Category.create({name: 'Category A'})
|
2015-02-18 05:04:31 +00:00
|
|
|
.then(function(cat) {
|
|
|
|
cat.jobIds.should.be.an.array;
|
|
|
|
cat.jobIds.should.have.length(0);
|
2016-08-19 17:46:59 +00:00
|
|
|
return cat.jobs.create({name: 'Job 2'})
|
2015-02-18 05:04:31 +00:00
|
|
|
.then(function(p) {
|
|
|
|
cat.jobIds.should.have.length(1);
|
|
|
|
cat.jobIds.should.eql([p.id]);
|
|
|
|
p.name.should.equal('Job 2');
|
2015-04-24 23:50:15 +00:00
|
|
|
job2 = p;
|
2015-02-18 05:04:31 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should not allow duplicate record on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-05-19 19:47:31 +00:00
|
|
|
.then(function(cat) {
|
|
|
|
cat.jobIds = [job2.id, job2.id];
|
|
|
|
return cat.save();
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
function(p) { done(new Error('save() should have failed')); },
|
|
|
|
function(err) {
|
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
err.details.codes.jobs.should.eql(['uniqueness']);
|
|
|
|
done();
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2015-02-18 05:04:31 +00:00
|
|
|
cat.jobIds.should.eql([job2.id]);
|
2016-04-01 11:48:17 +00:00
|
|
|
return cat.jobs.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(jobs) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var p = jobs[0];
|
|
|
|
p.id.should.eql(job2.id);
|
|
|
|
p.name.should.equal('Job 2');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope with promises - findById', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2015-02-18 05:04:31 +00:00
|
|
|
cat.jobIds.should.eql([job2.id]);
|
2016-04-01 11:48:17 +00:00
|
|
|
return cat.jobs.findById(job2.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
p.should.be.instanceof(Job);
|
|
|
|
p.id.should.eql(job2.id);
|
|
|
|
p.name.should.equal('Job 2');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should check if a record exists on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return cat.jobs.exists(job2.id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(exists) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(exists);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should update a record on scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var attrs = {name: 'Job 2 - edit'};
|
2015-02-18 05:04:31 +00:00
|
|
|
return cat.jobs.updateById(job2.id, attrs)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
p.name.should.equal(attrs.name);
|
|
|
|
done();
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should get a record by index with promises - at', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
|
|
|
return cat.jobs.at(0);
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
p.should.be.instanceof(Job);
|
|
|
|
p.id.should.eql(job2.id);
|
|
|
|
p.name.should.equal('Job 2 - edit');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should add a record to scope with promises - object', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return cat.jobs.add(job1)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(prod) {
|
2015-02-18 05:04:31 +00:00
|
|
|
cat.jobIds.should.eql([job2.id, job1.id]);
|
|
|
|
prod.id.should.eql(job1.id);
|
|
|
|
prod.should.have.property('name');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should add a record to scope with promises - object', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return cat.jobs.add(job3.id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(prod) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var expected = [job2.id, job1.id, job3.id];
|
|
|
|
cat.jobIds.should.eql(expected);
|
|
|
|
prod.id.should.eql(job3.id);
|
|
|
|
prod.should.have.property('name');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope with promises - findById', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
|
|
|
return cat.jobs.findById(job3.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(p) {
|
2015-02-18 05:04:31 +00:00
|
|
|
p.id.should.eql(job3.id);
|
|
|
|
p.name.should.equal('Job 3');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope with promises - filter', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2016-08-19 17:46:59 +00:00
|
|
|
var filter = {where: {name: 'Job 1'}};
|
2016-04-01 11:48:17 +00:00
|
|
|
return cat.jobs.getAsync(filter);
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(jobs) {
|
2015-02-18 05:04:31 +00:00
|
|
|
jobs.should.have.length(1);
|
|
|
|
var p = jobs[0];
|
|
|
|
p.id.should.eql(job1.id);
|
|
|
|
p.name.should.equal('Job 1');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should remove items from scope with promises', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return cat.jobs.remove(job1.id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ids) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var expected = [job2.id, job3.id];
|
|
|
|
cat.jobIds.should.eql(expected);
|
|
|
|
ids.should.eql(cat.jobIds);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope with promises - verify', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var expected = [job2.id, job3.id];
|
|
|
|
cat.jobIds.should.eql(expected);
|
2016-04-01 11:48:17 +00:00
|
|
|
return cat.jobs.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(jobs) {
|
2015-02-18 05:04:31 +00:00
|
|
|
jobs.should.have.length(2);
|
|
|
|
jobs[0].id.should.eql(job2.id);
|
|
|
|
jobs[1].id.should.eql(job3.id);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope and ordered them by name DESC', function(done) {
|
2015-10-16 16:21:04 +00:00
|
|
|
Category.find()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(categories) {
|
2015-10-16 16:21:04 +00:00
|
|
|
categories.should.have.length(1);
|
2016-08-19 17:46:59 +00:00
|
|
|
return categories[0].jobs.getAsync({order: 'name DESC'});
|
2015-10-16 16:21:04 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(jobs) {
|
2015-10-16 16:21:04 +00:00
|
|
|
jobs.should.have.length(2);
|
|
|
|
jobs[0].id.should.eql(job3.id);
|
|
|
|
jobs[1].id.should.eql(job2.id);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
it('should allow custom scope methods with promises - reverse', function(done) {
|
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return cat.jobs.reverse()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(ids) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var expected = [job3.id, job2.id];
|
|
|
|
ids.should.eql(expected);
|
|
|
|
cat.jobIds.should.eql(expected);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should include related items from scope with promises', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Category.find({include: 'jobs'})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(categories) {
|
2015-02-18 05:04:31 +00:00
|
|
|
categories.should.have.length(1);
|
|
|
|
var cat = categories[0].toObject();
|
|
|
|
cat.name.should.equal('Category A');
|
|
|
|
cat.jobs.should.have.length(2);
|
|
|
|
cat.jobs[0].id.should.eql(job3.id);
|
|
|
|
cat.jobs[1].id.should.eql(job2.id);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should destroy items from scope with promises - destroyById', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2015-02-18 05:04:31 +00:00
|
|
|
return cat.jobs.destroy(job2.id)
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function() {
|
2015-02-18 05:04:31 +00:00
|
|
|
var expected = [job3.id];
|
|
|
|
cat.jobIds.should.eql(expected);
|
2016-04-01 11:48:17 +00:00
|
|
|
return Job.exists(job2.id);
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(exists) {
|
2015-02-18 05:04:31 +00:00
|
|
|
should.exist(exists);
|
|
|
|
exists.should.be.false;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should find items on scope with promises - verify', function(done) {
|
2015-02-18 05:04:31 +00:00
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var expected = [job3.id];
|
|
|
|
cat.jobIds.should.eql(expected);
|
2016-04-01 11:48:17 +00:00
|
|
|
return cat.jobs.getAsync();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(jobs) {
|
2015-02-18 05:04:31 +00:00
|
|
|
jobs.should.have.length(1);
|
|
|
|
jobs[0].id.should.eql(job3.id);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
2014-07-29 13:01:47 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('custom relation/scope methods', function() {
|
2014-08-15 20:48:38 +00:00
|
|
|
var categoryId;
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2015-08-27 22:59:58 +00:00
|
|
|
// db = getSchema();
|
2016-08-19 17:46:59 +00:00
|
|
|
Category = db.define('Category', {name: String});
|
|
|
|
Job = db.define('Job', {name: String});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Job', 'Category'], done);
|
2014-08-13 09:28:23 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('can be declared', function(done) {
|
2014-08-26 05:17:40 +00:00
|
|
|
var relation = Category.hasMany(Job);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
var summarize = function(cb) {
|
2015-02-18 05:04:31 +00:00
|
|
|
cb = cb || createPromiseCallback();
|
2014-08-13 09:28:23 +00:00
|
|
|
var modelInstance = this.modelInstance;
|
|
|
|
this.fetch(function(err, items) {
|
|
|
|
if (err) return cb(err, []);
|
|
|
|
var summary = items.map(function(item) {
|
|
|
|
var obj = item.toObject();
|
|
|
|
obj.categoryName = modelInstance.name;
|
|
|
|
return obj;
|
|
|
|
});
|
|
|
|
cb(null, summary);
|
|
|
|
});
|
2015-02-18 05:04:31 +00:00
|
|
|
return cb.promise;
|
2014-08-13 09:28:23 +00:00
|
|
|
};
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
summarize.shared = true; // remoting
|
2016-08-19 17:46:59 +00:00
|
|
|
summarize.http = {verb: 'get', path: '/jobs/summary'};
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
relation.defineMethod('summarize', summarize);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-26 05:17:40 +00:00
|
|
|
Category.prototype['__summarize__jobs'].should.be.a.function;
|
|
|
|
should.exist(Category.prototype['__summarize__jobs'].shared);
|
|
|
|
Category.prototype['__summarize__jobs'].http.should.eql(summarize.http);
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['Job', 'Category'], done);
|
2014-08-13 09:28:23 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should setup test records', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Category.create({name: 'Category A'}, function(err, cat) {
|
2014-08-15 20:48:38 +00:00
|
|
|
categoryId = cat.id;
|
2016-08-19 17:46:59 +00:00
|
|
|
cat.jobs.create({name: 'Job 1'}, function(err, p) {
|
|
|
|
cat.jobs.create({name: 'Job 2'}, function(err, p) {
|
2014-08-13 09:28:23 +00:00
|
|
|
done();
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2014-08-13 09:28:23 +00:00
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
it('should allow custom scope methods - summarize', function(done) {
|
|
|
|
var expected = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{name: 'Job 1', categoryId: categoryId, categoryName: 'Category A'},
|
|
|
|
{name: 'Job 2', categoryId: categoryId, categoryName: 'Category A'},
|
2014-08-13 09:28:23 +00:00
|
|
|
];
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
Category.findOne(function(err, cat) {
|
2014-08-26 05:17:40 +00:00
|
|
|
cat.jobs.summarize(function(err, summary) {
|
2014-08-13 09:28:23 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
var result = summary.map(function(item) {
|
|
|
|
delete item.id;
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
result.should.eql(expected);
|
|
|
|
done();
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2014-08-13 09:28:23 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-02-18 05:04:31 +00:00
|
|
|
it('should allow custom scope methods with promises - summarize', function(done) {
|
|
|
|
var expected = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{name: 'Job 1', categoryId: categoryId, categoryName: 'Category A'},
|
|
|
|
{name: 'Job 2', categoryId: categoryId, categoryName: 'Category A'},
|
2015-02-18 05:04:31 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
Category.findOne()
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(cat) {
|
|
|
|
return cat.jobs.summarize();
|
2015-02-18 05:04:31 +00:00
|
|
|
})
|
2016-04-01 11:48:17 +00:00
|
|
|
.then(function(summary) {
|
2015-02-18 05:04:31 +00:00
|
|
|
var result = summary.map(function(item) {
|
|
|
|
delete item.id;
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
result.should.eql(expected);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2014-08-13 09:28:23 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-11-11 21:36:49 +00:00
|
|
|
});
|