2013-04-06 10:57:12 +00:00
|
|
|
// This test written in mocha+should.js
|
|
|
|
var should = require('./init.js');
|
|
|
|
|
2014-07-11 13:29:47 +00:00
|
|
|
var db, Book, Chapter, Author, Reader;
|
|
|
|
var Category, Product;
|
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-01-24 17:09:53 +00:00
|
|
|
describe('relations', function () {
|
2014-07-15 23:10:37 +00:00
|
|
|
|
|
|
|
describe('hasMany', function () {
|
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
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});
|
|
|
|
|
|
|
|
db.automigrate(function () {
|
|
|
|
Book.destroyAll(function () {
|
|
|
|
Chapter.destroyAll(function () {
|
|
|
|
Author.destroyAll(function () {
|
|
|
|
Reader.destroyAll(done);
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 20:48:14 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 00:39:47 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
it('can be declared in different ways', function (done) {
|
|
|
|
Book.hasMany(Chapter);
|
|
|
|
Book.hasMany(Reader, {as: 'users'});
|
|
|
|
Book.hasMany(Author, {foreignKey: 'projectId'});
|
|
|
|
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);
|
|
|
|
Object.keys((new Chapter).toObject()).should.include('bookId');
|
|
|
|
Object.keys((new Author).toObject()).should.include('projectId');
|
|
|
|
|
|
|
|
db.automigrate(done);
|
2013-03-26 00:39:47 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('can be declared in short form', function (done) {
|
|
|
|
Author.hasMany('readers');
|
|
|
|
(new Author).readers.should.be.an.instanceOf(Function);
|
|
|
|
Object.keys((new Reader).toObject()).should.include('authorId');
|
2013-03-26 00:39:47 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
db.autoupdate(done);
|
|
|
|
});
|
2013-03-26 20:48:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should build record on scope', function (done) {
|
|
|
|
Book.create(function (err, book) {
|
|
|
|
var c = book.chapters.build();
|
|
|
|
c.bookId.should.equal(book.id);
|
|
|
|
c.save(done);
|
|
|
|
});
|
|
|
|
});
|
2013-03-26 20:48:14 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should create record on scope', function (done) {
|
|
|
|
Book.create(function (err, book) {
|
|
|
|
book.chapters.create(function (err, c) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(c);
|
|
|
|
c.bookId.should.equal(book.id);
|
|
|
|
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
|
|
|
|
2014-04-02 17:35:14 +00:00
|
|
|
it('should fetch all scoped instances', function (done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
Book.create(function (err, book) {
|
|
|
|
book.chapters.create({name: 'a'}, function () {
|
|
|
|
book.chapters.create({name: 'z'}, function () {
|
|
|
|
book.chapters.create({name: 'c'}, function () {
|
2014-07-15 15:50:34 +00:00
|
|
|
verify(book);
|
2013-03-26 20:48:14 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 20:48:14 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-07-15 15:50:34 +00:00
|
|
|
function verify(book) {
|
2014-01-24 17:09:53 +00:00
|
|
|
book.chapters(function (err, ch) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(3);
|
|
|
|
|
|
|
|
book.chapters({order: 'name DESC'}, function (e, c) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(c);
|
|
|
|
c.shift().name.should.equal('z');
|
|
|
|
c.pop().name.should.equal('a');
|
|
|
|
done();
|
|
|
|
});
|
2013-03-26 00:39:47 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
});
|
2013-04-03 20:46:41 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should find scoped record', function (done) {
|
|
|
|
var id;
|
|
|
|
Book.create(function (err, book) {
|
|
|
|
book.chapters.create({name: 'a'}, function (err, ch) {
|
|
|
|
id = ch.id;
|
|
|
|
book.chapters.create({name: 'z'}, function () {
|
|
|
|
book.chapters.create({name: 'c'}, function () {
|
2014-07-15 15:50:34 +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-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
|
2014-07-15 15:50:34 +00:00
|
|
|
function verify(book) {
|
2014-01-24 17:09:53 +00:00
|
|
|
book.chapters.findById(id, function (err, ch) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
2014-07-15 23:10:37 +00:00
|
|
|
ch.id.should.eql(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
|
|
|
}
|
|
|
|
});
|
2014-04-02 17:32:37 +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
|
|
|
|
|
|
|
it('should update scoped record', function (done) {
|
|
|
|
var id;
|
|
|
|
Book.create(function (err, book) {
|
|
|
|
book.chapters.create({name: 'a'}, function (err, ch) {
|
|
|
|
id = ch.id;
|
|
|
|
book.chapters.updateById(id, {name: 'aa'}, function(err, ch) {
|
|
|
|
verify(book);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(book) {
|
|
|
|
book.chapters.findById(id, function (err, ch) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
2014-07-15 23:10:37 +00:00
|
|
|
ch.id.should.eql(id);
|
2014-07-15 15:50:34 +00:00
|
|
|
ch.name.should.equal('aa');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should destroy scoped record', function (done) {
|
|
|
|
var id;
|
|
|
|
Book.create(function (err, book) {
|
|
|
|
book.chapters.create({name: 'a'}, function (err, ch) {
|
|
|
|
id = ch.id;
|
|
|
|
book.chapters.destroy(id, function(err, ch) {
|
|
|
|
verify(book);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(book) {
|
|
|
|
book.chapters.findById(id, function (err, ch) {
|
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should check existence of a scoped record', function (done) {
|
|
|
|
var id;
|
|
|
|
Book.create(function (err, book) {
|
|
|
|
book.chapters.create({name: 'a'}, function (err, ch) {
|
|
|
|
id = ch.id;
|
|
|
|
book.chapters.create({name: 'z'}, function () {
|
|
|
|
book.chapters.create({name: 'c'}, function () {
|
|
|
|
verify(book);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(book) {
|
|
|
|
book.chapters.exists(id, function (err, flag) {
|
|
|
|
should.not.exist(err);
|
|
|
|
flag.should.be.eql(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hasMany through', function () {
|
|
|
|
var Physician, Patient, Appointment;
|
|
|
|
|
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
Physician = db.define('Physician', {name: String});
|
|
|
|
Patient = db.define('Patient', {name: String});
|
|
|
|
Appointment = db.define('Appointment', {date: {type: Date,
|
|
|
|
default: function () {
|
|
|
|
return new Date();
|
|
|
|
}}});
|
|
|
|
|
|
|
|
Physician.hasMany(Patient, {through: Appointment});
|
|
|
|
Patient.hasMany(Physician, {through: Appointment});
|
|
|
|
Appointment.belongsTo(Patient);
|
|
|
|
Appointment.belongsTo(Physician);
|
|
|
|
|
|
|
|
db.automigrate(['Physician', 'Patient', 'Appointment'], function (err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should build record on scope', function (done) {
|
|
|
|
Physician.create(function (err, physician) {
|
|
|
|
var patient = physician.patients.build();
|
|
|
|
patient.physicianId.should.equal(physician.id);
|
|
|
|
patient.save(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create record on scope', function (done) {
|
|
|
|
Physician.create(function (err, physician) {
|
|
|
|
physician.patients.create(function (err, patient) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(patient);
|
|
|
|
Appointment.find({where: {physicianId: physician.id, patientId: patient.id}},
|
|
|
|
function(err, apps) {
|
|
|
|
should.not.exist(err);
|
|
|
|
apps.should.have.lengthOf(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fetch all scoped instances', function (done) {
|
|
|
|
Physician.create(function (err, physician) {
|
|
|
|
physician.patients.create({name: 'a'}, function () {
|
|
|
|
physician.patients.create({name: 'z'}, function () {
|
|
|
|
physician.patients.create({name: 'c'}, function () {
|
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function verify(physician) {
|
|
|
|
physician.patients(function (err, ch) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.should.have.lengthOf(3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find scoped record', function (done) {
|
|
|
|
var id;
|
|
|
|
Physician.create(function (err, physician) {
|
|
|
|
physician.patients.create({name: 'a'}, function (err, ch) {
|
|
|
|
id = ch.id;
|
|
|
|
physician.patients.create({name: 'z'}, function () {
|
|
|
|
physician.patients.create({name: 'c'}, function () {
|
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(physician) {
|
|
|
|
physician.patients.findById(id, function (err, ch) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.id.should.equal(id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set targetClass on scope property', function() {
|
|
|
|
should.equal(Physician.prototype.patients._targetClass, 'Patient');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update scoped record', function (done) {
|
|
|
|
var id;
|
|
|
|
Physician.create(function (err, physician) {
|
|
|
|
physician.patients.create({name: 'a'}, function (err, ch) {
|
|
|
|
id = ch.id;
|
|
|
|
physician.patients.updateById(id, {name: 'aa'}, function(err, ch) {
|
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(physician) {
|
|
|
|
physician.patients.findById(id, function (err, ch) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(ch);
|
|
|
|
ch.id.should.equal(id);
|
|
|
|
ch.name.should.equal('aa');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should destroy scoped record', function (done) {
|
|
|
|
var id;
|
|
|
|
Physician.create(function (err, physician) {
|
|
|
|
physician.patients.create({name: 'a'}, function (err, ch) {
|
|
|
|
id = ch.id;
|
|
|
|
physician.patients.destroy(id, function(err, ch) {
|
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(physician) {
|
|
|
|
physician.patients.findById(id, function (err, ch) {
|
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should check existence of a scoped record', function (done) {
|
|
|
|
var id;
|
|
|
|
Physician.create(function (err, physician) {
|
|
|
|
physician.patients.create({name: 'a'}, function (err, ch) {
|
|
|
|
id = ch.id;
|
|
|
|
physician.patients.create({name: 'z'}, function () {
|
|
|
|
physician.patients.create({name: 'c'}, function () {
|
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(physician) {
|
|
|
|
physician.patients.exists(id, function (err, flag) {
|
|
|
|
should.not.exist(err);
|
|
|
|
flag.should.be.eql(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow to add connection with instance', function (done) {
|
|
|
|
Physician.create({name: 'ph1'}, function (e, physician) {
|
|
|
|
Patient.create({name: 'pa1'}, function (e, patient) {
|
|
|
|
physician.patients.add(patient, function (e, app) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(app);
|
|
|
|
app.should.be.an.instanceOf(Appointment);
|
|
|
|
app.physicianId.should.equal(physician.id);
|
|
|
|
app.patientId.should.equal(patient.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow to remove connection with instance', function (done) {
|
|
|
|
var id;
|
|
|
|
Physician.create(function (err, physician) {
|
|
|
|
physician.patients.create({name: 'a'}, function (err, patient) {
|
|
|
|
id = patient.id;
|
|
|
|
physician.patients.remove(id, function (err, ch) {
|
|
|
|
verify(physician);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function verify(physician) {
|
|
|
|
physician.patients.exists(id, function (err, flag) {
|
|
|
|
should.not.exist(err);
|
|
|
|
flag.should.be.eql(false);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function (done) {
|
|
|
|
Appointment.destroyAll(function (err) {
|
|
|
|
Physician.destroyAll(function (err) {
|
|
|
|
Patient.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-07-11 13:29:47 +00:00
|
|
|
|
2014-07-11 22:02:16 +00:00
|
|
|
describe('hasMany with properties', function () {
|
|
|
|
it('can be declared with properties', function (done) {
|
|
|
|
Book.hasMany(Chapter, { properties: { type: 'bookType' } });
|
2014-07-11 13:29:47 +00:00
|
|
|
db.automigrate(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create record on scope', function (done) {
|
|
|
|
Book.create({ type: 'fiction' }, function (err, book) {
|
|
|
|
book.chapters.create(function (err, c) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(c);
|
|
|
|
c.bookId.should.equal(book.id);
|
|
|
|
c.bookType.should.equal('fiction');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-15 23:10:37 +00:00
|
|
|
|
2014-07-23 09:10:44 +00:00
|
|
|
describe('hasMany with scope and properties', function () {
|
2014-07-11 22:02:16 +00:00
|
|
|
it('can be declared with properties', function (done) {
|
2014-07-15 23:10:37 +00:00
|
|
|
db = getSchema();
|
|
|
|
Category = db.define('Category', {name: String, productType: String});
|
|
|
|
Product = db.define('Product', {name: String, type: String});
|
|
|
|
|
2014-07-11 13:29:47 +00:00
|
|
|
Category.hasMany(Product, {
|
2014-07-11 22:02:16 +00:00
|
|
|
properties: function(inst) {
|
2014-07-11 13:29:47 +00:00
|
|
|
if (!inst.productType) return; // skip
|
|
|
|
return { type: inst.productType };
|
|
|
|
},
|
|
|
|
scope: function(inst, filter) {
|
2014-07-11 22:02:16 +00:00
|
|
|
var m = this.properties(inst); // re-use properties
|
2014-07-11 13:29:47 +00:00
|
|
|
if (m) return { where: m };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
db.automigrate(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create record on scope', function (done) {
|
|
|
|
Category.create(function (err, c) {
|
|
|
|
c.products.create({ type: 'book' }, function(err, p) {
|
|
|
|
p.categoryId.should.equal(c.id);
|
|
|
|
p.type.should.equal('book');
|
|
|
|
c.products.create({ type: 'widget' }, function(err, p) {
|
|
|
|
p.categoryId.should.equal(c.id);
|
|
|
|
p.type.should.equal('widget');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-24 13:55:00 +00:00
|
|
|
it('should find records on scope', function (done) {
|
2014-07-11 13:29:47 +00:00
|
|
|
Category.findOne(function (err, c) {
|
|
|
|
c.products(function(err, products) {
|
|
|
|
products.should.have.length(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find record on scope - filtered', function (done) {
|
|
|
|
Category.findOne(function (err, c) {
|
|
|
|
c.products({ where: { type: 'book' } }, function(err, products) {
|
|
|
|
products.should.have.length(1);
|
|
|
|
products[0].type.should.equal('book');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2014-07-11 22:02:16 +00:00
|
|
|
it('should create record on scope - properties', function (done) {
|
2014-07-11 13:29:47 +00:00
|
|
|
Category.findOne(function (err, c) {
|
2014-07-11 22:02:16 +00:00
|
|
|
c.productType = 'tool'; // temporary
|
2014-07-11 13:29:47 +00:00
|
|
|
c.products.create(function(err, p) {
|
|
|
|
p.categoryId.should.equal(c.id);
|
|
|
|
p.type.should.equal('tool');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-24 13:55:00 +00:00
|
|
|
it('should find records on scope', function (done) {
|
|
|
|
Category.findOne(function (err, c) {
|
|
|
|
c.products(function(err, products) {
|
|
|
|
products.should.have.length(3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-11 13:29:47 +00:00
|
|
|
it('should find record on scope - scoped', function (done) {
|
|
|
|
Category.findOne(function (err, c) {
|
|
|
|
c.productType = 'book'; // temporary, for scoping
|
|
|
|
c.products(function(err, products) {
|
|
|
|
products.should.have.length(1);
|
|
|
|
products[0].type.should.equal('book');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find record on scope - scoped', function (done) {
|
|
|
|
Category.findOne(function (err, c) {
|
|
|
|
c.productType = 'tool'; // temporary, for scoping
|
|
|
|
c.products(function(err, products) {
|
|
|
|
products.should.have.length(1);
|
|
|
|
products[0].type.should.equal('tool');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-24 13:55:00 +00:00
|
|
|
|
|
|
|
it('should delete records on scope - scoped', function (done) {
|
|
|
|
Category.findOne(function (err, c) {
|
|
|
|
c.productType = 'tool'; // temporary, for scoping
|
|
|
|
c.products.destroyAll(function(err, result) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find record on scope - verify', function (done) {
|
|
|
|
Category.findOne(function (err, c) {
|
|
|
|
c.products(function(err, products) {
|
|
|
|
products.should.have.length(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-11 13:29:47 +00:00
|
|
|
|
|
|
|
});
|
2014-07-26 10:47:55 +00:00
|
|
|
|
2014-07-26 13:20:46 +00:00
|
|
|
describe('polymorphic hasOne', function () {
|
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
Picture = db.define('Picture', {name: String});
|
|
|
|
Author = db.define('Author', {name: String});
|
|
|
|
Reader = db.define('Reader', {name: String});
|
|
|
|
|
|
|
|
db.automigrate(function () {
|
|
|
|
Picture.destroyAll(function () {
|
|
|
|
Author.destroyAll(function () {
|
|
|
|
Reader.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be declared', function (done) {
|
|
|
|
Author.hasOne(Picture, { as: 'avatar', polymorphic: 'imageable' });
|
|
|
|
Reader.hasOne(Picture, { as: 'mugshot', polymorphic: 'imageable' });
|
|
|
|
Picture.belongsTo('imageable', { polymorphic: true });
|
|
|
|
db.automigrate(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create polymorphic relation - author', function (done) {
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.create({name: 'Author 1' }, function (err, author) {
|
2014-07-26 13:20:46 +00:00
|
|
|
author.avatar.create({ name: 'Avatar' }, function (err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p);
|
|
|
|
p.imageableId.should.equal(author.id);
|
|
|
|
p.imageableType.should.equal('Author');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create polymorphic relation - reader', function (done) {
|
2014-07-28 21:22:45 +00:00
|
|
|
Reader.create({name: 'Reader 1' }, function (err, reader) {
|
2014-07-26 13:20:46 +00:00
|
|
|
reader.mugshot.create({ name: 'Mugshot' }, function (err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p);
|
|
|
|
p.imageableId.should.equal(reader.id);
|
|
|
|
p.imageableType.should.equal('Reader');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find polymorphic relation - author', function (done) {
|
|
|
|
Author.findOne(function (err, author) {
|
|
|
|
author.avatar(function (err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find polymorphic relation - reader', function (done) {
|
|
|
|
Reader.findOne(function (err, reader) {
|
|
|
|
reader.mugshot(function (err, p) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find inverse polymorphic relation - author', function (done) {
|
|
|
|
Picture.findOne({ where: { name: 'Avatar' } }, function (err, p) {
|
|
|
|
p.imageable(function (err, imageable) {
|
|
|
|
should.not.exist(err);
|
|
|
|
imageable.should.be.instanceof(Author);
|
|
|
|
imageable.name.should.equal('Author 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find inverse polymorphic relation - reader', function (done) {
|
|
|
|
Picture.findOne({ where: { name: 'Mugshot' } }, function (err, p) {
|
|
|
|
p.imageable(function (err, imageable) {
|
|
|
|
should.not.exist(err);
|
|
|
|
imageable.should.be.instanceof(Reader);
|
|
|
|
imageable.name.should.equal('Reader 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-07-26 10:47:55 +00:00
|
|
|
describe('polymorphic hasMany', function () {
|
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
Picture = db.define('Picture', {name: String});
|
|
|
|
Author = db.define('Author', {name: String});
|
|
|
|
Reader = db.define('Reader', {name: String});
|
|
|
|
|
|
|
|
db.automigrate(function () {
|
|
|
|
Picture.destroyAll(function () {
|
|
|
|
Author.destroyAll(function () {
|
|
|
|
Reader.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be declared', function (done) {
|
|
|
|
Author.hasMany(Picture, { polymorphic: 'imageable' });
|
2014-07-28 08:44:26 +00:00
|
|
|
Reader.hasMany(Picture, { polymorphic: { // alt syntax
|
|
|
|
as: 'imageable', foreignKey: 'imageableId',
|
|
|
|
discriminator: 'imageableType'
|
|
|
|
} });
|
2014-07-26 10:47:55 +00:00
|
|
|
Picture.belongsTo('imageable', { polymorphic: true });
|
|
|
|
db.automigrate(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create polymorphic relation - author', function (done) {
|
|
|
|
Author.create({ name: 'Author 1' }, function (err, author) {
|
|
|
|
author.pictures.create({ name: 'Author Pic' }, function (err, p) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create polymorphic relation - reader', function (done) {
|
|
|
|
Reader.create({ name: 'Reader 1' }, function (err, reader) {
|
|
|
|
reader.pictures.create({ name: 'Reader Pic' }, function (err, p) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find polymorphic items - author', function (done) {
|
|
|
|
Author.findOne(function (err, author) {
|
|
|
|
author.pictures(function (err, pics) {
|
|
|
|
should.not.exist(err);
|
|
|
|
pics.should.have.length(1);
|
|
|
|
pics[0].name.should.equal('Author Pic');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find polymorphic items - reader', function (done) {
|
|
|
|
Reader.findOne(function (err, reader) {
|
|
|
|
reader.pictures(function (err, pics) {
|
|
|
|
should.not.exist(err);
|
|
|
|
pics.should.have.length(1);
|
|
|
|
pics[0].name.should.equal('Reader Pic');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find the inverse of polymorphic relation - author', function (done) {
|
|
|
|
Picture.findOne({ where: { name: 'Author Pic' } }, function (err, p) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find the inverse of polymorphic relation - reader', function (done) {
|
|
|
|
Picture.findOne({ where: { name: 'Reader Pic' } }, function (err, p) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should include the inverse of polymorphic relation', function (done) {
|
|
|
|
Picture.find({ include: 'imageable' }, function (err, pics) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should assign a polymorphic relation', function(done) {
|
|
|
|
Author.create({ name: 'Author 2' }, function(err, author) {
|
|
|
|
var p = new Picture({ name: 'Sample' });
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find polymorphic items - author', function (done) {
|
|
|
|
Author.findOne({ where: { name: 'Author 2' } }, function (err, author) {
|
|
|
|
author.pictures(function (err, pics) {
|
|
|
|
should.not.exist(err);
|
|
|
|
pics.should.have.length(1);
|
|
|
|
pics[0].name.should.equal('Sample');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find the inverse of polymorphic relation - author', function (done) {
|
|
|
|
Picture.findOne({ where: { name: 'Sample' } }, function (err, p) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2014-07-26 12:54:54 +00:00
|
|
|
|
|
|
|
describe('polymorphic hasAndBelongsToMany through', function () {
|
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
Picture = db.define('Picture', {name: String});
|
|
|
|
Author = db.define('Author', {name: String});
|
|
|
|
Reader = db.define('Reader', {name: String});
|
|
|
|
PictureLink = db.define('PictureLink', {});
|
|
|
|
|
|
|
|
db.automigrate(function () {
|
|
|
|
Picture.destroyAll(function () {
|
|
|
|
PictureLink.destroyAll(function () {
|
|
|
|
Author.destroyAll(function () {
|
|
|
|
Reader.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be declared', function (done) {
|
|
|
|
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:
|
2014-07-26 19:11:25 +00:00
|
|
|
Picture.hasMany(Author, { through: PictureLink, polymorphic: 'imageable', invert: true });
|
|
|
|
Picture.hasMany(Reader, { through: PictureLink, polymorphic: 'imageable', invert: true });
|
2014-07-26 12:54:54 +00:00
|
|
|
db.automigrate(done);
|
|
|
|
});
|
2014-07-28 21:22:45 +00:00
|
|
|
|
|
|
|
var author, reader, pictures = [];
|
2014-07-26 12:54:54 +00:00
|
|
|
it('should create polymorphic relation - author', function (done) {
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.create({ name: 'Author 1' }, function (err, a) {
|
|
|
|
should.not.exist(err);
|
|
|
|
author = a;
|
2014-07-26 12:54:54 +00:00
|
|
|
author.pictures.create({ name: 'Author Pic 1' }, function (err, p) {
|
|
|
|
should.not.exist(err);
|
2014-07-28 21:22:45 +00:00
|
|
|
pictures.push(p);
|
2014-07-26 12:54:54 +00:00
|
|
|
author.pictures.create({ name: 'Author Pic 2' }, function (err, p) {
|
|
|
|
should.not.exist(err);
|
2014-07-28 21:22:45 +00:00
|
|
|
pictures.push(p);
|
2014-07-26 12:54:54 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create polymorphic relation - reader', function (done) {
|
2014-07-28 21:22:45 +00:00
|
|
|
Reader.create({ name: 'Reader 1' }, function (err, r) {
|
|
|
|
should.not.exist(err);
|
|
|
|
reader = r;
|
2014-07-26 12:54:54 +00:00
|
|
|
reader.pictures.create({ name: 'Reader Pic 1' }, function (err, p) {
|
|
|
|
should.not.exist(err);
|
2014-07-28 21:22:45 +00:00
|
|
|
pictures.push(p);
|
2014-07-26 12:54:54 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create polymorphic through model', function (done) {
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get polymorphic relation through model - author', function (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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-26 15:20:25 +00:00
|
|
|
|
|
|
|
it('should include polymorphic items', function (done) {
|
|
|
|
Author.find({ include: 'pictures' }, function(err, authors) {
|
|
|
|
authors.should.have.length(1);
|
|
|
|
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;
|
2014-07-26 15:20:25 +00:00
|
|
|
it('should add to a polymorphic relation - author', function (done) {
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.findById(author.id, function(err, author) {
|
|
|
|
Picture.create({name: 'Example' }, function(err, p) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create polymorphic through model', function (done) {
|
2014-07-28 21:22:45 +00:00
|
|
|
PictureLink.findOne({ where: { pictureId: anotherPicture.id, imageableType: 'Author' } }, 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;
|
2014-07-26 19:11:25 +00:00
|
|
|
it('should add to a polymorphic relation - author', function (done) {
|
2014-07-28 21:22:45 +00:00
|
|
|
Author.create({ name: 'Author 2' }, function (err, author) {
|
|
|
|
should.not.exist(err);
|
|
|
|
anotherAuthor = author;
|
|
|
|
author.pictures.add(anotherPicture.id, function (err, p) {
|
2014-07-26 19:11:25 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add to a polymorphic relation - author', function (done) {
|
2014-07-28 21:22:45 +00:00
|
|
|
Reader.create({name: 'Reader 2' }, function (err, reader) {
|
|
|
|
should.not.exist(err);
|
|
|
|
anotherReader = reader;
|
|
|
|
reader.pictures.add(anotherPicture.id, function (err, p) {
|
2014-07-26 19:11:25 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get the inverse polymorphic relation - author', function (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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get the inverse polymorphic relation - reader', function (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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find polymorphic items - author', function (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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-26 15:20:25 +00:00
|
|
|
it('should check if polymorphic relation exists - author', function (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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove from a polymorphic relation - author', function (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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-26 19:11:25 +00:00
|
|
|
it('should find polymorphic items - author', function (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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-26 15:20:25 +00:00
|
|
|
it('should check if polymorphic relation exists - author', function (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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-26 12:54:54 +00:00
|
|
|
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
describe('belongsTo', function () {
|
|
|
|
var List, Item, Fear, Mind;
|
|
|
|
|
|
|
|
it('can be declared in different ways', function () {
|
|
|
|
List = db.define('List', {name: String});
|
|
|
|
Item = db.define('Item', {name: String});
|
|
|
|
Fear = db.define('Fear');
|
|
|
|
Mind = db.define('Mind');
|
|
|
|
|
|
|
|
// syntax 1 (old)
|
|
|
|
Item.belongsTo(List);
|
|
|
|
Object.keys((new Item).toObject()).should.include('listId');
|
|
|
|
(new Item).list.should.be.an.instanceOf(Function);
|
|
|
|
|
|
|
|
// syntax 2 (new)
|
|
|
|
Fear.belongsTo('mind');
|
|
|
|
Object.keys((new Fear).toObject()).should.include('mindId');
|
|
|
|
(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
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('can be used to query data', function (done) {
|
|
|
|
List.hasMany('todos', {model: Item});
|
|
|
|
db.automigrate(function () {
|
|
|
|
List.create(function (e, list) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(list);
|
|
|
|
list.todos.create(function (err, todo) {
|
|
|
|
todo.list(function (e, l) {
|
|
|
|
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-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
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('could accept objects when creating on scope', function (done) {
|
|
|
|
List.create(function (e, list) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(list);
|
|
|
|
Item.create({list: list}, function (err, item) {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-07-23 09:10:44 +00:00
|
|
|
|
|
|
|
describe('belongsTo with scope', function () {
|
|
|
|
var Person, Passport;
|
|
|
|
|
|
|
|
it('can be declared with scope and properties', function (done) {
|
|
|
|
Person = db.define('Person', {name: String, age: Number});
|
|
|
|
Passport = db.define('Passport', {name: String, notes: String});
|
|
|
|
Passport.belongsTo(Person, {
|
|
|
|
properties: { notes: 'passportNotes' },
|
|
|
|
scope: { fields: { id: true, name: true } }
|
|
|
|
});
|
|
|
|
db.automigrate(done);
|
|
|
|
});
|
2014-07-28 21:22:45 +00:00
|
|
|
|
|
|
|
var personCreated;
|
2014-07-23 09:10:44 +00:00
|
|
|
it('should create record on scope', function (done) {
|
|
|
|
var p = new Passport({ name: 'Passport', notes: 'Some notes...' });
|
2014-07-28 21:22:45 +00:00
|
|
|
p.person.create({name: 'Fred', age: 36 }, function(err, person) {
|
|
|
|
personCreated = person;
|
2014-07-23 09:10:44 +00:00
|
|
|
p.personId.should.equal(person.id);
|
|
|
|
p.save(function (err, p) {
|
|
|
|
person.name.should.equal('Fred');
|
|
|
|
person.passportNotes.should.equal('Some notes...');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
|
|
|
person.should.not.have.property('age');
|
|
|
|
person.should.not.have.property('passportNotes');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-06-16 08:17:37 +00:00
|
|
|
describe('hasOne', function () {
|
|
|
|
var Supplier, Account;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
db = getSchema();
|
|
|
|
Supplier = db.define('Supplier', {name: String});
|
2014-07-15 23:10:37 +00:00
|
|
|
Account = db.define('Account', {accountNo: String, supplierName: String});
|
2014-06-16 08:17:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can be declared using hasOne method', function () {
|
2014-07-11 22:02:16 +00:00
|
|
|
Supplier.hasOne(Account, { properties: { name: 'supplierName' } });
|
2014-06-16 08:17:37 +00:00
|
|
|
Object.keys((new Account()).toObject()).should.include('supplierId');
|
|
|
|
(new Supplier()).account.should.be.an.instanceOf(Function);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be used to query data', function (done) {
|
|
|
|
// Supplier.hasOne(Account);
|
|
|
|
db.automigrate(function () {
|
|
|
|
Supplier.create({name: 'Supplier 1'}, function (e, supplier) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(supplier);
|
|
|
|
supplier.account.create({accountNo: 'a01'}, function (err, account) {
|
|
|
|
supplier.account(function (e, act) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-24 17:52:55 +00:00
|
|
|
it('should set targetClass on scope property', function() {
|
|
|
|
should.equal(Supplier.prototype.account._targetClass, 'Account');
|
|
|
|
});
|
2014-06-16 08:17:37 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('hasAndBelongsToMany', function () {
|
|
|
|
var Article, Tag, ArticleTag;
|
|
|
|
it('can be declared', function (done) {
|
|
|
|
Article = db.define('Article', {title: String});
|
|
|
|
Tag = db.define('Tag', {name: String});
|
|
|
|
Article.hasAndBelongsToMany('tags');
|
|
|
|
ArticleTag = db.models.ArticleTag;
|
|
|
|
db.automigrate(function () {
|
|
|
|
Article.destroyAll(function () {
|
|
|
|
Tag.destroyAll(function () {
|
|
|
|
ArticleTag.destroyAll(done)
|
|
|
|
});
|
2013-04-11 23:23:34 +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
|
|
|
it('should allow to create instances on scope', function (done) {
|
|
|
|
Article.create(function (e, article) {
|
|
|
|
article.tags.create({name: 'popular'}, function (e, t) {
|
|
|
|
t.should.be.an.instanceOf(Tag);
|
|
|
|
// console.log(t);
|
|
|
|
ArticleTag.findOne(function (e, at) {
|
|
|
|
should.exist(at);
|
|
|
|
at.tagId.toString().should.equal(t.id.toString());
|
|
|
|
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
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should allow to fetch scoped instances', function (done) {
|
|
|
|
Article.findOne(function (e, article) {
|
|
|
|
article.tags(function (e, tags) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(tags);
|
|
|
|
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
|
|
|
it('should allow to add connection with instance', function (done) {
|
|
|
|
Article.findOne(function (e, article) {
|
|
|
|
Tag.create({name: 'awesome'}, function (e, tag) {
|
|
|
|
article.tags.add(tag, function (e, at) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(at);
|
|
|
|
at.should.be.an.instanceOf(ArticleTag);
|
|
|
|
at.tagId.should.equal(tag.id);
|
|
|
|
at.articleId.should.equal(article.id);
|
|
|
|
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
|
|
|
it('should allow to remove connection with instance', function (done) {
|
|
|
|
Article.findOne(function (e, article) {
|
|
|
|
article.tags(function (e, tags) {
|
|
|
|
var len = tags.length;
|
|
|
|
tags.should.not.be.empty;
|
|
|
|
article.tags.remove(tags[0], function (e) {
|
|
|
|
should.not.exist(e);
|
|
|
|
article.tags(true, function (e, tags) {
|
|
|
|
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
|
|
|
|
2014-04-02 17:32:37 +00:00
|
|
|
it('should set targetClass on scope property', function() {
|
|
|
|
should.equal(Article.prototype.tags._targetClass, 'Tag');
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-07-27 14:30:45 +00:00
|
|
|
|
|
|
|
describe('embedsMany', function () {
|
2014-07-29 08:54:28 +00:00
|
|
|
|
|
|
|
var address1, address2;
|
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
Person = db.define('Person', {name: String});
|
|
|
|
Address = db.define('Address', {street: String});
|
|
|
|
Address.validatesPresenceOf('street');
|
|
|
|
|
|
|
|
db.automigrate(function () {
|
|
|
|
Person.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be declared', function (done) {
|
|
|
|
Person.embedsMany(Address);
|
|
|
|
db.automigrate(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have setup embedded accessor/scope', function() {
|
|
|
|
var p = new Person({ name: 'Fred' });
|
|
|
|
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) {
|
|
|
|
Person.create({ name: 'Fred' }, function(err, p) {
|
|
|
|
p.addressList.create({ street: 'Street 1' }, function(err, addresses) {
|
|
|
|
should.not.exist(err);
|
|
|
|
addresses.should.have.length(1);
|
2014-07-28 23:15:37 +00:00
|
|
|
address1 = addresses[0];
|
|
|
|
should.exist(address1.id);
|
2014-07-27 14:30:45 +00:00
|
|
|
addresses[0].street.should.equal('Street 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create embedded items on scope', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addressList.create({ street: 'Street 2' }, function(err, addresses) {
|
|
|
|
should.not.exist(err);
|
|
|
|
addresses.should.have.length(2);
|
2014-07-28 23:15:37 +00:00
|
|
|
address1 = addresses[0];
|
|
|
|
address2 = addresses[1];
|
|
|
|
should.exist(address1.id);
|
|
|
|
address1.street.should.equal('Street 1');
|
|
|
|
should.exist(address2.id);
|
|
|
|
address2.street.should.equal('Street 2');
|
2014-07-27 14:30:45 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return embedded items from scope', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addressList(function(err, addresses) {
|
|
|
|
should.not.exist(err);
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should filter embedded items on scope', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addressList({ where: { street: 'Street 2' } }, function(err, addresses) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate embedded items', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addressList.create({}, function(err, addresses) {
|
|
|
|
should.exist(err);
|
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
err.details.codes.street.should.eql(['presence']);
|
|
|
|
addresses.should.have.length(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update embedded items by id', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2014-07-28 23:15:37 +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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate the update of embedded items', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
2014-07-28 23:15:37 +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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
p.addressList.set(address1.id, { street: 'Changed 1' });
|
|
|
|
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);
|
|
|
|
p.addressList.set(address2.id, { street: 'Changed 2' });
|
|
|
|
p.addresses[1].street.should.equal('Changed 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have embedded items - verify', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addresses.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('embedsMany - explicit ids', function () {
|
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
Person = db.define('Person', {name: String});
|
|
|
|
Address = db.define('Address', {id: { type: String, id: true }, street: String});
|
|
|
|
Address.validatesPresenceOf('street');
|
|
|
|
|
|
|
|
db.automigrate(function () {
|
|
|
|
Person.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be declared', function (done) {
|
2014-07-27 14:54:01 +00:00
|
|
|
Person.embedsMany(Address, { options: { autoId: false, validate: true } });
|
2014-07-27 14:30:45 +00:00
|
|
|
db.automigrate(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create embedded items on scope', function(done) {
|
|
|
|
Person.create({ name: 'Fred' }, function(err, p) {
|
|
|
|
p.addressList.create({ id: 'home', street: 'Street 1' }, function(err, addresses) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.addressList.create({ id: 'work', street: 'Work Street 2' }, function(err, addresses) {
|
|
|
|
addresses.should.have.length(2);
|
|
|
|
addresses[0].id.should.equal('home');
|
|
|
|
addresses[0].street.should.equal('Street 1');
|
|
|
|
addresses[1].id.should.equal('work');
|
|
|
|
addresses[1].street.should.equal('Work Street 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should check for duplicate ids', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addressList.create({ id: 'home', street: 'Invalid' }, function(err, addresses) {
|
|
|
|
should.exist(err);
|
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
err.details.codes.addresses.should.eql(['uniqueness']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update embedded items by id', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addressList.updateById('home', { street: 'New Street' }, function(err, address) {
|
|
|
|
address.should.be.instanceof(Address);
|
|
|
|
address.id.should.equal('home');
|
|
|
|
address.street.should.equal('New Street');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have embedded items - verify', function(done) {
|
|
|
|
Person.findOne(function(err, p) {
|
|
|
|
p.addresses.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-27 14:54:01 +00:00
|
|
|
it('should validate all embedded items', function(done) {
|
|
|
|
var addresses = [];
|
|
|
|
addresses.push({ id: 'home', street: 'Home Street' });
|
|
|
|
addresses.push({ id: 'work', street: '' });
|
|
|
|
Person.create({ name: 'Wilma', addresses: addresses }, function(err, p) {
|
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
var expected = 'The `Person` instance is not valid. ';
|
|
|
|
expected += 'Details: `addresses` contains invalid item: `work` (street can\'t be blank).';
|
|
|
|
err.message.should.equal(expected);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-27 15:16:25 +00:00
|
|
|
it('should build embedded items', function(done) {
|
|
|
|
Person.create({ name: 'Wilma' }, function(err, p) {
|
|
|
|
p.addressList.build({ id: 'home', street: 'Home' });
|
|
|
|
p.addressList.build({ id: 'work', street: 'Work' });
|
|
|
|
p.addresses.should.have.length(2);
|
|
|
|
p.save(function(err, p) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have embedded items - verify', function(done) {
|
|
|
|
Person.findOne({ where: { name: 'Wilma' } }, function(err, p) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-29 08:51:33 +00:00
|
|
|
it('should have accessors: at, get, set', function(done) {
|
|
|
|
Person.findOne({ where: { name: 'Wilma' } }, function(err, p) {
|
|
|
|
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');
|
|
|
|
p.addressList.set('home', { id: 'den' }).id.should.equal('den');
|
|
|
|
p.addressList.at(1).id.should.equal('work');
|
|
|
|
p.addressList.get('work').id.should.equal('work');
|
|
|
|
p.addressList.set('work', { id: 'factory' }).id.should.equal('factory');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-27 14:30:45 +00:00
|
|
|
});
|
2014-07-29 08:54:28 +00:00
|
|
|
|
|
|
|
describe('embedsMany - relations, scope and properties', function () {
|
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
var product1, product2, product3;
|
2014-07-29 08:54:28 +00:00
|
|
|
|
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
Category = db.define('Category', {name: String});
|
|
|
|
Product = db.define('Product', {name: String});
|
2014-07-29 15:43:30 +00:00
|
|
|
Link = db.define('Link', {name: String});
|
2014-07-29 08:54:28 +00:00
|
|
|
|
|
|
|
db.automigrate(function () {
|
|
|
|
Person.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be declared', function (done) {
|
|
|
|
Category.embedsMany(Link, {
|
|
|
|
as: 'items', // rename
|
2014-07-29 15:43:30 +00:00
|
|
|
scope: { include: 'product' }, // always include
|
2014-07-29 19:56:59 +00:00
|
|
|
options: { belongsTo: 'product' } // optional, for add()/remove()
|
2014-07-29 08:54:28 +00:00
|
|
|
});
|
|
|
|
Link.belongsTo(Product, {
|
|
|
|
foreignKey: 'id', // re-use the actual product id
|
|
|
|
properties: { id: 'id', name: 'name' }, // denormalize, transfer id
|
|
|
|
});
|
2014-07-29 15:43:30 +00:00
|
|
|
db.automigrate(function() {
|
|
|
|
Product.create({ name: 'Product 0' }, done); // offset ids for tests
|
|
|
|
});
|
2014-07-29 08:54:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should setup related items', function(done) {
|
|
|
|
Product.create({ name: 'Product 1' }, function(err, p) {
|
|
|
|
product1 = p;
|
|
|
|
Product.create({ name: 'Product 2' }, function(err, p) {
|
|
|
|
product2 = p;
|
2014-07-29 15:43:30 +00:00
|
|
|
Product.create({ name: 'Product 3' }, function(err, p) {
|
|
|
|
product3 = p;
|
|
|
|
done();
|
|
|
|
});
|
2014-07-29 08:54:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
it('should create items on scope', function(done) {
|
2014-07-29 08:54:28 +00:00
|
|
|
Category.create({ name: 'Category A' }, function(err, cat) {
|
|
|
|
var link = cat.items.build();
|
|
|
|
link.product(product1);
|
|
|
|
var link = cat.items.build();
|
|
|
|
link.product(product2);
|
|
|
|
cat.save(function(err, cat) {
|
|
|
|
var product = cat.items.at(0);
|
2014-07-29 15:43:30 +00:00
|
|
|
product.should.be.instanceof(Link);
|
2014-07-29 12:05:18 +00:00
|
|
|
product.should.not.have.property('productId');
|
2014-07-29 11:57:49 +00:00
|
|
|
product.id.should.eql(product1.id);
|
2014-07-29 08:54:28 +00:00
|
|
|
product.name.should.equal(product1.name);
|
|
|
|
var product = cat.items.at(1);
|
2014-07-29 11:57:49 +00:00
|
|
|
product.id.should.eql(product2.id);
|
2014-07-29 08:54:28 +00:00
|
|
|
product.name.should.equal(product2.name);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should include related items on scope', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.links.should.have.length(2);
|
|
|
|
|
|
|
|
// denormalized properties:
|
2014-07-29 15:43:30 +00:00
|
|
|
cat.items.at(0).should.be.instanceof(Link);
|
2014-07-29 11:57:49 +00:00
|
|
|
cat.items.at(0).id.should.eql(product1.id);
|
2014-07-29 08:54:28 +00:00
|
|
|
cat.items.at(0).name.should.equal(product1.name);
|
2014-07-29 11:57:49 +00:00
|
|
|
cat.items.at(1).id.should.eql(product2.id);
|
2014-07-29 08:54:28 +00:00
|
|
|
cat.items.at(1).name.should.equal(product2.name);
|
|
|
|
|
|
|
|
// lazy-loaded relations
|
|
|
|
should.not.exist(cat.items.at(0).product());
|
|
|
|
should.not.exist(cat.items.at(1).product());
|
|
|
|
|
|
|
|
cat.items(function(err, items) {
|
|
|
|
cat.items.at(0).product().should.be.instanceof(Product);
|
|
|
|
cat.items.at(1).product().should.be.instanceof(Product);
|
|
|
|
cat.items.at(1).product().name.should.equal('Product 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove embedded items by id', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.links.should.have.length(2);
|
|
|
|
cat.items.destroy(product1.id, function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
cat.links.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find items on scope', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.links.should.have.length(1);
|
2014-07-29 11:57:49 +00:00
|
|
|
cat.items.at(0).id.should.eql(product2.id);
|
2014-07-29 08:54:28 +00:00
|
|
|
cat.items.at(0).name.should.equal(product2.name);
|
|
|
|
|
|
|
|
// lazy-loaded relations
|
|
|
|
should.not.exist(cat.items.at(0).product());
|
|
|
|
|
|
|
|
cat.items(function(err, items) {
|
|
|
|
cat.items.at(0).product().should.be.instanceof(Product);
|
|
|
|
cat.items.at(0).product().name.should.equal('Product 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-29 15:43:30 +00:00
|
|
|
it('should add related items to scope', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.links.should.have.length(1);
|
|
|
|
cat.items.add(product3, function(err, link) {
|
|
|
|
link.should.be.instanceof(Link);
|
|
|
|
link.id.should.equal(product3.id);
|
|
|
|
link.name.should.equal('Product 3');
|
|
|
|
|
|
|
|
cat.links.should.have.length(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find items on scope', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.links.should.have.length(2);
|
|
|
|
|
|
|
|
cat.items.at(0).should.be.instanceof(Link);
|
|
|
|
cat.items.at(0).id.should.eql(product2.id);
|
|
|
|
cat.items.at(0).name.should.equal(product2.name);
|
|
|
|
cat.items.at(1).id.should.eql(product3.id);
|
|
|
|
cat.items.at(1).name.should.equal(product3.name);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove embedded items by reference id', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.links.should.have.length(2);
|
|
|
|
cat.items.remove(product2.id, function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
cat.links.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove embedded items by reference id', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.links.should.have.length(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-29 08:54:28 +00:00
|
|
|
});
|
2014-07-29 11:57:49 +00:00
|
|
|
|
|
|
|
describe('embedsMany - polymorphic relations', function () {
|
|
|
|
|
|
|
|
var person1, person2;
|
|
|
|
|
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
Book = db.define('Book', {name: String});
|
|
|
|
Author = db.define('Author', {name: String});
|
|
|
|
Reader = db.define('Reader', {name: String});
|
|
|
|
|
|
|
|
Link = db.define('Link'); // generic model
|
|
|
|
Link.validatesPresenceOf('linkedId');
|
|
|
|
Link.validatesPresenceOf('linkedType');
|
|
|
|
|
|
|
|
db.automigrate(function () {
|
|
|
|
Book.destroyAll(function() {
|
|
|
|
Author.destroyAll(function() {
|
|
|
|
Reader.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be declared', function (done) {
|
|
|
|
Book.embedsMany(Link, { as: 'people',
|
|
|
|
polymorphic: 'linked',
|
|
|
|
scope: { include: 'linked' }
|
|
|
|
});
|
|
|
|
Link.belongsTo('linked', {
|
2014-07-29 13:01:47 +00:00
|
|
|
polymorphic: true, // needs unique auto-id
|
2014-07-29 11:57:49 +00:00
|
|
|
properties: { name: 'name' } // denormalized
|
|
|
|
});
|
|
|
|
db.automigrate(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should setup related items', function(done) {
|
|
|
|
Author.create({ name: 'Author 1' }, function(err, p) {
|
|
|
|
person1 = p;
|
|
|
|
Reader.create({ name: 'Reader 1' }, function(err, p) {
|
|
|
|
person2 = p;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create items on scope', function(done) {
|
|
|
|
Book.create({ name: 'Book' }, function(err, book) {
|
|
|
|
var link = book.people.build({ notes: 'Something ...' });
|
|
|
|
link.linked(person1);
|
|
|
|
var link = book.people.build();
|
|
|
|
link.linked(person2);
|
|
|
|
book.save(function(err, book) {
|
|
|
|
should.not.exist(err);
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should include related items on scope', function(done) {
|
|
|
|
Book.findOne(function(err, book) {
|
|
|
|
book.links.should.have.length(2);
|
|
|
|
|
|
|
|
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 ...');
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
// lazy-loaded relations
|
|
|
|
should.not.exist(book.people.at(0).linked());
|
|
|
|
should.not.exist(book.people.at(1).linked());
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should include nested related items on scope', function(done) {
|
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.
|
|
|
|
|
2014-07-29 11:57:49 +00:00
|
|
|
Book.find({ include: 'people' }, function(err, books) {
|
|
|
|
var obj = books[0].toObject();
|
|
|
|
|
|
|
|
obj.should.have.property('links');
|
|
|
|
obj.should.have.property('people');
|
|
|
|
|
|
|
|
obj.links.should.have.length(2);
|
|
|
|
obj.links[0].name.should.equal('Author 1');
|
|
|
|
obj.links[1].name.should.equal('Reader 1');
|
|
|
|
|
|
|
|
obj.people.should.have.length(2);
|
2014-07-29 12:05:18 +00:00
|
|
|
|
|
|
|
obj.people[0].name.should.equal('Author 1');
|
|
|
|
obj.people[0].notes.should.equal('Something ...');
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2014-07-29 13:01:47 +00:00
|
|
|
|
|
|
|
describe('referencesMany', function () {
|
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
var product1, product2, product3;
|
|
|
|
|
2014-07-29 13:01:47 +00:00
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
Category = db.define('Category', {name: String});
|
|
|
|
Product = db.define('Product', {name: String});
|
|
|
|
|
|
|
|
db.automigrate(function () {
|
|
|
|
Category.destroyAll(function() {
|
|
|
|
Product.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be declared', function (done) {
|
2014-07-29 20:59:44 +00:00
|
|
|
Category.referencesMany(Product, { scopeMethods: {
|
|
|
|
reverse: function(cb) {
|
|
|
|
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] || []);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} });
|
2014-07-29 13:01:47 +00:00
|
|
|
db.automigrate(done);
|
|
|
|
});
|
2014-07-29 19:46:12 +00:00
|
|
|
|
|
|
|
it('should setup test records', function (done) {
|
|
|
|
Product.create({ name: 'Product 1' }, function(err, p) {
|
|
|
|
product1 = p;
|
|
|
|
Product.create({ name: 'Product 3' }, function(err, p) {
|
|
|
|
product3 = p;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create record on scope', function (done) {
|
|
|
|
Category.create({ name: 'Category A' }, function(err, cat) {
|
|
|
|
cat.productIds.should.be.an.array;
|
|
|
|
cat.productIds.should.have.length(0);
|
|
|
|
cat.products.create({ name: 'Product 2' }, function(err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
cat.productIds.should.have.length(1);
|
|
|
|
cat.productIds.should.eql([p.id]);
|
|
|
|
p.name.should.equal('Product 2');
|
|
|
|
product2 = p;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-29 20:59:44 +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) {
|
|
|
|
cat.productIds = [product2.id, product2.id];
|
|
|
|
cat.save(function(err, p) {
|
|
|
|
should.exist(err);
|
|
|
|
err.name.should.equal('ValidationError');
|
|
|
|
err.details.codes.products.should.eql(['uniqueness']);
|
|
|
|
var expected = 'The `Category` instance is not valid. ';
|
|
|
|
expected += 'Details: `products` Contains duplicate `Product` instance.';
|
|
|
|
err.message.should.equal(expected);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find items on scope', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.productIds.should.eql([product2.id]);
|
|
|
|
cat.products(function(err, products) {
|
|
|
|
should.not.exist(err);
|
|
|
|
var p = products[0];
|
|
|
|
p.id.should.eql(product2.id);
|
|
|
|
p.name.should.equal('Product 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find items on scope - findById', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.productIds.should.eql([product2.id]);
|
|
|
|
cat.products.findById(product2.id, function(err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.should.be.instanceof(Product);
|
|
|
|
p.id.should.eql(product2.id);
|
|
|
|
p.name.should.equal('Product 2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should check if a record exists on scope', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.products.exists(product2.id, function(err, exists) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(exists);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update a record on scope', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
var attrs = { name: 'Product 2 - edit' };
|
|
|
|
cat.products.updateById(product2.id, attrs, function(err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.name.should.equal(attrs.name);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get a record by index - at', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.products.at(0, function(err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.should.be.instanceof(Product);
|
|
|
|
p.id.should.eql(product2.id);
|
|
|
|
p.name.should.equal('Product 2 - edit');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add a record to scope - object', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.products.add(product1, function(err, ids) {
|
|
|
|
should.not.exist(err);
|
|
|
|
cat.productIds.should.eql([product2.id, product1.id]);
|
|
|
|
ids.should.eql(cat.productIds);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add a record to scope - object', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.products.add(product3.id, function(err, ids) {
|
|
|
|
should.not.exist(err);
|
|
|
|
var expected = [product2.id, product1.id, product3.id];
|
|
|
|
cat.productIds.should.eql(expected);
|
|
|
|
ids.should.eql(cat.productIds);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find items on scope - findById', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.products.findById(product3.id, function(err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.id.should.eql(product3.id);
|
|
|
|
p.name.should.equal('Product 3');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find items on scope - filter', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
var filter = { where: { name: 'Product 1' } };
|
|
|
|
cat.products(filter, function(err, products) {
|
|
|
|
should.not.exist(err);
|
|
|
|
products.should.have.length(1);
|
|
|
|
var p = products[0];
|
|
|
|
p.id.should.eql(product1.id);
|
|
|
|
p.name.should.equal('Product 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove items from scope', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.products.remove(product1.id, function(err, ids) {
|
|
|
|
should.not.exist(err);
|
|
|
|
var expected = [product2.id, product3.id];
|
|
|
|
cat.productIds.should.eql(expected);
|
|
|
|
ids.should.eql(cat.productIds);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find items on scope - verify', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
var expected = [product2.id, product3.id];
|
|
|
|
cat.productIds.should.eql(expected);
|
|
|
|
cat.products(function(err, products) {
|
|
|
|
should.not.exist(err);
|
|
|
|
products.should.have.length(2);
|
|
|
|
products[0].id.should.eql(product2.id);
|
|
|
|
products[1].id.should.eql(product3.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-29 20:59:44 +00:00
|
|
|
it('should allow custom scope methods - reverse', function(done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.products.reverse(function(err, ids) {
|
|
|
|
var expected = [product3.id, product2.id];
|
|
|
|
ids.should.eql(expected);
|
|
|
|
cat.productIds.should.eql(expected);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2014-07-29 19:46:12 +00:00
|
|
|
it('should include related items from scope', function(done) {
|
|
|
|
Category.find({ include: 'products' }, function(err, categories) {
|
|
|
|
categories.should.have.length(1);
|
|
|
|
var cat = categories[0].toObject();
|
|
|
|
cat.name.should.equal('Category A');
|
|
|
|
cat.products.should.have.length(2);
|
2014-07-29 20:59:44 +00:00
|
|
|
cat.products[0].id.should.eql(product3.id);
|
|
|
|
cat.products[1].id.should.eql(product2.id);
|
2014-07-29 19:46:12 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should destroy items from scope - destroyById', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
cat.products.destroy(product2.id, function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
var expected = [product3.id];
|
|
|
|
cat.productIds.should.eql(expected);
|
|
|
|
Product.exists(product2.id, function(err, exists) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(exists);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find items on scope - verify', function (done) {
|
|
|
|
Category.findOne(function(err, cat) {
|
|
|
|
var expected = [product3.id];
|
|
|
|
cat.productIds.should.eql(expected);
|
|
|
|
cat.products(function(err, products) {
|
|
|
|
should.not.exist(err);
|
|
|
|
products.should.have.length(1);
|
|
|
|
products[0].id.should.eql(product3.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-29 13:01:47 +00:00
|
|
|
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-03-26 00:39:47 +00:00
|
|
|
});
|