Tweak the model names used by tests to avoid mssql conflicts

This commit is contained in:
Raymond Feng 2014-08-25 22:17:40 -07:00
parent 4470c02bbc
commit 6bc2a53afb
2 changed files with 185 additions and 181 deletions

View File

@ -59,9 +59,10 @@ describe('basic-querying', function () {
}); });
describe('findById', function () { describe('findByIds', function () {
before(function(done) { before(function(done) {
// FIXME: [rfeng] The autogenerated ids are not always 1-6
var people = [ var people = [
{ id: 1, name: 'a', vip: true }, { id: 1, name: 'a', vip: true },
{ id: 2, name: 'b' }, { id: 2, name: 'b' },
@ -70,8 +71,11 @@ describe('basic-querying', function () {
{ id: 5, name: 'e' }, { id: 5, name: 'e' },
{ id: 6, name: 'f' } { id: 6, name: 'f' }
]; ];
User.destroyAll(function() { // Use automigrate so that serial keys are 1-6
User.create(people, done); db.automigrate(['User'], function(err) {
User.create(people, function(err, users) {
done();
});
}); });
}); });

View File

@ -2,7 +2,7 @@
var should = require('./init.js'); var should = require('./init.js');
var db, Book, Chapter, Author, Reader; var db, Book, Chapter, Author, Reader;
var Category, Product; var Category, Job;
var Picture, PictureLink; var Picture, PictureLink;
var Person, Address; var Person, Address;
var Link; var Link;
@ -461,13 +461,13 @@ describe('relations', function () {
describe('hasMany with scope and properties', function () { describe('hasMany with scope and properties', function () {
it('can be declared with properties', function (done) { it('can be declared with properties', function (done) {
db = getSchema(); db = getSchema();
Category = db.define('Category', {name: String, productType: String}); Category = db.define('Category', {name: String, jobType: String});
Product = db.define('Product', {name: String, type: String}); Job = db.define('Job', {name: String, type: String});
Category.hasMany(Product, { Category.hasMany(Job, {
properties: function(inst) { properties: function(inst) {
if (!inst.productType) return; // skip if (!inst.jobType) return; // skip
return { type: inst.productType }; return { type: inst.jobType };
}, },
scope: function(inst, filter) { scope: function(inst, filter) {
var m = this.properties(inst); // re-use properties var m = this.properties(inst); // re-use properties
@ -479,10 +479,10 @@ describe('relations', function () {
it('should create record on scope', function (done) { it('should create record on scope', function (done) {
Category.create(function (err, c) { Category.create(function (err, c) {
c.products.create({ type: 'book' }, function(err, p) { c.jobs.create({ type: 'book' }, function(err, p) {
p.categoryId.should.equal(c.id); p.categoryId.should.equal(c.id);
p.type.should.equal('book'); p.type.should.equal('book');
c.products.create({ type: 'widget' }, function(err, p) { c.jobs.create({ type: 'widget' }, function(err, p) {
p.categoryId.should.equal(c.id); p.categoryId.should.equal(c.id);
p.type.should.equal('widget'); p.type.should.equal('widget');
done(); done();
@ -493,8 +493,8 @@ describe('relations', function () {
it('should find records on scope', function (done) { it('should find records on scope', function (done) {
Category.findOne(function (err, c) { Category.findOne(function (err, c) {
c.products(function(err, products) { c.jobs(function(err, jobs) {
products.should.have.length(2); jobs.should.have.length(2);
done(); done();
}); });
}); });
@ -502,9 +502,9 @@ describe('relations', function () {
it('should find record on scope - filtered', function (done) { it('should find record on scope - filtered', function (done) {
Category.findOne(function (err, c) { Category.findOne(function (err, c) {
c.products({ where: { type: 'book' } }, function(err, products) { c.jobs({ where: { type: 'book' } }, function(err, jobs) {
products.should.have.length(1); jobs.should.have.length(1);
products[0].type.should.equal('book'); jobs[0].type.should.equal('book');
done(); done();
}); });
}); });
@ -521,8 +521,8 @@ describe('relations', function () {
it('should create record on scope - properties', function (done) { it('should create record on scope - properties', function (done) {
Category.findOne(function (err, c) { Category.findOne(function (err, c) {
c.productType = 'tool'; // temporary c.jobType = 'tool'; // temporary
c.products.create(function(err, p) { c.jobs.create(function(err, p) {
p.categoryId.should.equal(c.id); p.categoryId.should.equal(c.id);
p.type.should.equal('tool'); p.type.should.equal('tool');
done(); done();
@ -532,8 +532,8 @@ describe('relations', function () {
it('should find records on scope', function (done) { it('should find records on scope', function (done) {
Category.findOne(function (err, c) { Category.findOne(function (err, c) {
c.products(function(err, products) { c.jobs(function(err, jobs) {
products.should.have.length(3); jobs.should.have.length(3);
done(); done();
}); });
}); });
@ -541,10 +541,10 @@ describe('relations', function () {
it('should find record on scope - scoped', function (done) { it('should find record on scope - scoped', function (done) {
Category.findOne(function (err, c) { Category.findOne(function (err, c) {
c.productType = 'book'; // temporary, for scoping c.jobType = 'book'; // temporary, for scoping
c.products(function(err, products) { c.jobs(function(err, jobs) {
products.should.have.length(1); jobs.should.have.length(1);
products[0].type.should.equal('book'); jobs[0].type.should.equal('book');
done(); done();
}); });
}); });
@ -552,10 +552,10 @@ describe('relations', function () {
it('should find record on scope - scoped', function (done) { it('should find record on scope - scoped', function (done) {
Category.findOne(function (err, c) { Category.findOne(function (err, c) {
c.productType = 'tool'; // temporary, for scoping c.jobType = 'tool'; // temporary, for scoping
c.products(function(err, products) { c.jobs(function(err, jobs) {
products.should.have.length(1); jobs.should.have.length(1);
products[0].type.should.equal('tool'); jobs[0].type.should.equal('tool');
done(); done();
}); });
}); });
@ -563,8 +563,8 @@ describe('relations', function () {
it('should find count of records on scope - scoped', function (done) { it('should find count of records on scope - scoped', function (done) {
Category.findOne(function (err, c) { Category.findOne(function (err, c) {
c.productType = 'tool'; // temporary, for scoping c.jobType = 'tool'; // temporary, for scoping
c.products.count(function(err, count) { c.jobs.count(function(err, count) {
count.should.equal(1); count.should.equal(1);
done(); done();
}); });
@ -573,8 +573,8 @@ describe('relations', function () {
it('should delete records on scope - scoped', function (done) { it('should delete records on scope - scoped', function (done) {
Category.findOne(function (err, c) { Category.findOne(function (err, c) {
c.productType = 'tool'; // temporary, for scoping c.jobType = 'tool'; // temporary, for scoping
c.products.destroyAll(function(err, result) { c.jobs.destroyAll(function(err, result) {
done(); done();
}); });
}); });
@ -582,8 +582,8 @@ describe('relations', function () {
it('should find record on scope - verify', function (done) { it('should find record on scope - verify', function (done) {
Category.findOne(function (err, c) { Category.findOne(function (err, c) {
c.products(function(err, products) { c.jobs(function(err, jobs) {
products.should.have.length(2); jobs.should.have.length(2);
done(); done();
}); });
}); });
@ -1948,38 +1948,38 @@ describe('relations', function () {
describe('embedsMany - relations, scope and properties', function () { describe('embedsMany - relations, scope and properties', function () {
var category, product1, product2, product3; var category, job1, job2, job3;
before(function () { before(function () {
db = getSchema(); db = getSchema();
Category = db.define('Category', {name: String}); Category = db.define('Category', {name: String});
Product = db.define('Product', {name: String}); Job = db.define('Job', {name: String});
Link = db.define('Link', {name: String, notes: String}); Link = db.define('Link', {name: String, notes: String});
}); });
it('can be declared', function (done) { it('can be declared', function (done) {
Category.embedsMany(Link, { Category.embedsMany(Link, {
as: 'items', // rename as: 'items', // rename
scope: { include: 'product' }, // always include scope: { include: 'job' }, // always include
options: { belongsTo: 'product' } // optional, for add()/remove() options: { belongsTo: 'job' } // optional, for add()/remove()
}); });
Link.belongsTo(Product, { Link.belongsTo(Job, {
foreignKey: 'id', // re-use the actual product id foreignKey: 'id', // re-use the actual job id
properties: { id: 'id', name: 'name' }, // denormalize, transfer id properties: { id: 'id', name: 'name' }, // denormalize, transfer id
options: { invertProperties: true } options: { invertProperties: true }
}); });
db.automigrate(function() { db.automigrate(function() {
Product.create({ name: 'Product 0' }, done); // offset ids for tests Job.create({ name: 'Job 0' }, done); // offset ids for tests
}); });
}); });
it('should setup related items', function(done) { it('should setup related items', function(done) {
Product.create({ name: 'Product 1' }, function(err, p) { Job.create({ name: 'Job 1' }, function(err, p) {
product1 = p; job1 = p;
Product.create({ name: 'Product 2' }, function(err, p) { Job.create({ name: 'Job 2' }, function(err, p) {
product2 = p; job2 = p;
Product.create({ name: 'Product 3' }, function(err, p) { Job.create({ name: 'Job 3' }, function(err, p) {
product3 = p; job3 = p;
done(); done();
}); });
}); });
@ -1989,18 +1989,18 @@ describe('relations', function () {
it('should associate items on scope', function(done) { it('should associate items on scope', function(done) {
Category.create({ name: 'Category A' }, function(err, cat) { Category.create({ name: 'Category A' }, function(err, cat) {
var link = cat.items.build(); var link = cat.items.build();
link.product(product1); link.job(job1);
var link = cat.items.build(); var link = cat.items.build();
link.product(product2); link.job(job2);
cat.save(function(err, cat) { cat.save(function(err, cat) {
var product = cat.items.at(0); var job = cat.items.at(0);
product.should.be.instanceof(Link); job.should.be.instanceof(Link);
product.should.not.have.property('productId'); job.should.not.have.property('jobId');
product.id.should.eql(product1.id); job.id.should.eql(job1.id);
product.name.should.equal(product1.name); job.name.should.equal(job1.name);
var product = cat.items.at(1); var job = cat.items.at(1);
product.id.should.eql(product2.id); job.id.should.eql(job2.id);
product.name.should.equal(product2.name); job.name.should.equal(job2.name);
done(); done();
}); });
}); });
@ -2012,19 +2012,19 @@ describe('relations', function () {
// denormalized properties: // denormalized properties:
cat.items.at(0).should.be.instanceof(Link); cat.items.at(0).should.be.instanceof(Link);
cat.items.at(0).id.should.eql(product1.id); cat.items.at(0).id.should.eql(job1.id);
cat.items.at(0).name.should.equal(product1.name); cat.items.at(0).name.should.equal(job1.name);
cat.items.at(1).id.should.eql(product2.id); cat.items.at(1).id.should.eql(job2.id);
cat.items.at(1).name.should.equal(product2.name); cat.items.at(1).name.should.equal(job2.name);
// lazy-loaded relations // lazy-loaded relations
should.not.exist(cat.items.at(0).product()); should.not.exist(cat.items.at(0).job());
should.not.exist(cat.items.at(1).product()); should.not.exist(cat.items.at(1).job());
cat.items(function(err, items) { cat.items(function(err, items) {
cat.items.at(0).product().should.be.instanceof(Product); cat.items.at(0).job().should.be.instanceof(Job);
cat.items.at(1).product().should.be.instanceof(Product); cat.items.at(1).job().should.be.instanceof(Job);
cat.items.at(1).product().name.should.equal('Product 2'); cat.items.at(1).job().name.should.equal('Job 2');
done(); done();
}); });
}); });
@ -2033,7 +2033,7 @@ describe('relations', function () {
it('should remove embedded items by id', function(done) { it('should remove embedded items by id', function(done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.links.should.have.length(2); cat.links.should.have.length(2);
cat.items.destroy(product1.id, function(err) { cat.items.destroy(job1.id, function(err) {
should.not.exist(err); should.not.exist(err);
cat.links.should.have.length(1); cat.links.should.have.length(1);
done(); done();
@ -2044,15 +2044,15 @@ describe('relations', function () {
it('should find items on scope', function(done) { it('should find items on scope', function(done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.links.should.have.length(1); cat.links.should.have.length(1);
cat.items.at(0).id.should.eql(product2.id); cat.items.at(0).id.should.eql(job2.id);
cat.items.at(0).name.should.equal(product2.name); cat.items.at(0).name.should.equal(job2.name);
// lazy-loaded relations // lazy-loaded relations
should.not.exist(cat.items.at(0).product()); should.not.exist(cat.items.at(0).job());
cat.items(function(err, items) { cat.items(function(err, items) {
cat.items.at(0).product().should.be.instanceof(Product); cat.items.at(0).job().should.be.instanceof(Job);
cat.items.at(0).product().name.should.equal('Product 2'); cat.items.at(0).job().name.should.equal('Job 2');
done(); done();
}); });
}); });
@ -2061,10 +2061,10 @@ describe('relations', function () {
it('should add related items to scope', function(done) { it('should add related items to scope', function(done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.links.should.have.length(1); cat.links.should.have.length(1);
cat.items.add(product3, function(err, link) { cat.items.add(job3, function(err, link) {
link.should.be.instanceof(Link); link.should.be.instanceof(Link);
link.id.should.eql(product3.id); link.id.should.eql(job3.id);
link.name.should.equal('Product 3'); link.name.should.equal('Job 3');
cat.links.should.have.length(2); cat.links.should.have.length(2);
done(); done();
@ -2077,10 +2077,10 @@ describe('relations', function () {
cat.links.should.have.length(2); cat.links.should.have.length(2);
cat.items.at(0).should.be.instanceof(Link); cat.items.at(0).should.be.instanceof(Link);
cat.items.at(0).id.should.eql(product2.id); cat.items.at(0).id.should.eql(job2.id);
cat.items.at(0).name.should.equal(product2.name); cat.items.at(0).name.should.equal(job2.name);
cat.items.at(1).id.should.eql(product3.id); cat.items.at(1).id.should.eql(job3.id);
cat.items.at(1).name.should.equal(product3.name); cat.items.at(1).name.should.equal(job3.name);
done(); done();
}); });
@ -2089,7 +2089,7 @@ describe('relations', function () {
it('should remove embedded items by reference id', function(done) { it('should remove embedded items by reference id', function(done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.links.should.have.length(2); cat.links.should.have.length(2);
cat.items.remove(product2.id, function(err) { cat.items.remove(job2.id, function(err) {
should.not.exist(err); should.not.exist(err);
cat.links.should.have.length(1); cat.links.should.have.length(1);
done(); done();
@ -2104,16 +2104,16 @@ describe('relations', function () {
}); });
}); });
var productId; var jobId;
it('should create items on scope', function(done) { it('should create items on scope', function(done) {
Category.create({ name: 'Category B' }, function(err, cat) { Category.create({ name: 'Category B' }, function(err, cat) {
category = cat; category = cat;
var link = cat.items.build({ notes: 'Some notes...' }); var link = cat.items.build({ notes: 'Some notes...' });
link.product.create({ name: 'Product 1' }, function(err, p) { link.job.create({ name: 'Job 1' }, function(err, p) {
productId = p.id; jobId = p.id;
cat.links[0].id.should.eql(p.id); cat.links[0].id.should.eql(p.id);
cat.links[0].name.should.equal('Product 1'); // denormalized cat.links[0].name.should.equal('Job 1'); // denormalized
cat.links[0].notes.should.equal('Some notes...'); cat.links[0].notes.should.equal('Some notes...');
cat.items.at(0).should.equal(cat.links[0]); cat.items.at(0).should.equal(cat.links[0]);
done(); done();
@ -2125,14 +2125,14 @@ describe('relations', function () {
Category.findById(category.id, function(err, cat) { Category.findById(category.id, function(err, cat) {
cat.name.should.equal('Category B'); cat.name.should.equal('Category B');
cat.links.toObject().should.eql([ cat.links.toObject().should.eql([
{id: productId, name: 'Product 1', notes: 'Some notes...'} {id: jobId, name: 'Job 1', notes: 'Some notes...'}
]); ]);
cat.items.at(0).should.equal(cat.links[0]); cat.items.at(0).should.equal(cat.links[0]);
cat.items(function(err, items) { // alternative access cat.items(function(err, items) { // alternative access
items.should.be.an.array; items.should.be.an.array;
items.should.have.length(1); items.should.have.length(1);
items[0].product(function(err, p) { items[0].job(function(err, p) {
p.name.should.equal('Product 1'); // actual value p.name.should.equal('Job 1'); // actual value
done(); done();
}); });
}); });
@ -2153,7 +2153,7 @@ describe('relations', function () {
Category.findById(category.id, function(err, cat) { Category.findById(category.id, function(err, cat) {
cat.name.should.equal('Category B'); cat.name.should.equal('Category B');
cat.links.toObject().should.eql([ cat.links.toObject().should.eql([
{id: productId, name: 'Product 1', notes: 'Updated notes...'} {id: jobId, name: 'Job 1', notes: 'Updated notes...'}
]); ]);
done(); done();
}); });
@ -2316,16 +2316,16 @@ describe('relations', function () {
describe('referencesMany', function () { describe('referencesMany', function () {
var product1, product2, product3; var job1, job2, job3;
before(function (done) { before(function (done) {
db = getSchema(); db = getSchema();
Category = db.define('Category', {name: String}); Category = db.define('Category', {name: String});
Product = db.define('Product', {name: String}); Job = db.define('Job', {name: String});
db.automigrate(function () { db.automigrate(function () {
Category.destroyAll(function() { Category.destroyAll(function() {
Product.destroyAll(done); Job.destroyAll(done);
}); });
}); });
}); });
@ -2341,24 +2341,24 @@ describe('relations', function () {
}; };
reverse.shared = true; // remoting reverse.shared = true; // remoting
reverse.http = { verb: 'put', path: '/products/reverse' }; reverse.http = { verb: 'put', path: '/jobs/reverse' };
Category.referencesMany(Product, { scopeMethods: { Category.referencesMany(Job, { scopeMethods: {
reverse: reverse reverse: reverse
} }); } });
Category.prototype['__reverse__products'].should.be.a.function; Category.prototype['__reverse__jobs'].should.be.a.function;
should.exist(Category.prototype['__reverse__products'].shared); should.exist(Category.prototype['__reverse__jobs'].shared);
Category.prototype['__reverse__products'].http.should.eql(reverse.http); Category.prototype['__reverse__jobs'].http.should.eql(reverse.http);
db.automigrate(done); db.automigrate(done);
}); });
it('should setup test records', function (done) { it('should setup test records', function (done) {
Product.create({ name: 'Product 1' }, function(err, p) { Job.create({ name: 'Job 1' }, function(err, p) {
product1 = p; job1 = p;
Product.create({ name: 'Product 3' }, function(err, p) { Job.create({ name: 'Job 3' }, function(err, p) {
product3 = p; job3 = p;
done(); done();
}); });
}); });
@ -2366,14 +2366,14 @@ describe('relations', function () {
it('should create record on scope', function (done) { it('should create record on scope', function (done) {
Category.create({ name: 'Category A' }, function(err, cat) { Category.create({ name: 'Category A' }, function(err, cat) {
cat.productIds.should.be.an.array; cat.jobIds.should.be.an.array;
cat.productIds.should.have.length(0); cat.jobIds.should.have.length(0);
cat.products.create({ name: 'Product 2' }, function(err, p) { cat.jobs.create({ name: 'Job 2' }, function(err, p) {
should.not.exist(err); should.not.exist(err);
cat.productIds.should.have.length(1); cat.jobIds.should.have.length(1);
cat.productIds.should.eql([p.id]); cat.jobIds.should.eql([p.id]);
p.name.should.equal('Product 2'); p.name.should.equal('Job 2');
product2 = p; job2 = p;
done(); done();
}); });
}); });
@ -2381,13 +2381,13 @@ describe('relations', function () {
it('should not allow duplicate record on scope', function (done) { it('should not allow duplicate record on scope', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.productIds = [product2.id, product2.id]; cat.jobIds = [job2.id, job2.id];
cat.save(function(err, p) { cat.save(function(err, p) {
should.exist(err); should.exist(err);
err.name.should.equal('ValidationError'); err.name.should.equal('ValidationError');
err.details.codes.products.should.eql(['uniqueness']); err.details.codes.jobs.should.eql(['uniqueness']);
var expected = 'The `Category` instance is not valid. '; var expected = 'The `Category` instance is not valid. ';
expected += 'Details: `products` Contains duplicate `Product` instance.'; expected += 'Details: `jobs` Contains duplicate `Job` instance.';
err.message.should.equal(expected); err.message.should.equal(expected);
done(); done();
}); });
@ -2396,12 +2396,12 @@ describe('relations', function () {
it('should find items on scope', function (done) { it('should find items on scope', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.productIds.should.eql([product2.id]); cat.jobIds.should.eql([job2.id]);
cat.products(function(err, products) { cat.jobs(function(err, jobs) {
should.not.exist(err); should.not.exist(err);
var p = products[0]; var p = jobs[0];
p.id.should.eql(product2.id); p.id.should.eql(job2.id);
p.name.should.equal('Product 2'); p.name.should.equal('Job 2');
done(); done();
}); });
}); });
@ -2409,12 +2409,12 @@ describe('relations', function () {
it('should find items on scope - findById', function (done) { it('should find items on scope - findById', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.productIds.should.eql([product2.id]); cat.jobIds.should.eql([job2.id]);
cat.products.findById(product2.id, function(err, p) { cat.jobs.findById(job2.id, function(err, p) {
should.not.exist(err); should.not.exist(err);
p.should.be.instanceof(Product); p.should.be.instanceof(Job);
p.id.should.eql(product2.id); p.id.should.eql(job2.id);
p.name.should.equal('Product 2'); p.name.should.equal('Job 2');
done(); done();
}); });
}); });
@ -2422,7 +2422,7 @@ describe('relations', function () {
it('should check if a record exists on scope', function (done) { it('should check if a record exists on scope', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.exists(product2.id, function(err, exists) { cat.jobs.exists(job2.id, function(err, exists) {
should.not.exist(err); should.not.exist(err);
should.exist(exists); should.exist(exists);
done(); done();
@ -2432,8 +2432,8 @@ describe('relations', function () {
it('should update a record on scope', function (done) { it('should update a record on scope', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
var attrs = { name: 'Product 2 - edit' }; var attrs = { name: 'Job 2 - edit' };
cat.products.updateById(product2.id, attrs, function(err, p) { cat.jobs.updateById(job2.id, attrs, function(err, p) {
should.not.exist(err); should.not.exist(err);
p.name.should.equal(attrs.name); p.name.should.equal(attrs.name);
done(); done();
@ -2443,11 +2443,11 @@ describe('relations', function () {
it('should get a record by index - at', function (done) { it('should get a record by index - at', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.at(0, function(err, p) { cat.jobs.at(0, function(err, p) {
should.not.exist(err); should.not.exist(err);
p.should.be.instanceof(Product); p.should.be.instanceof(Job);
p.id.should.eql(product2.id); p.id.should.eql(job2.id);
p.name.should.equal('Product 2 - edit'); p.name.should.equal('Job 2 - edit');
done(); done();
}); });
}); });
@ -2455,10 +2455,10 @@ describe('relations', function () {
it('should add a record to scope - object', function (done) { it('should add a record to scope - object', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.add(product1, function(err, prod) { cat.jobs.add(job1, function(err, prod) {
should.not.exist(err); should.not.exist(err);
cat.productIds.should.eql([product2.id, product1.id]); cat.jobIds.should.eql([job2.id, job1.id]);
prod.id.should.eql(product1.id); prod.id.should.eql(job1.id);
prod.should.have.property('name'); prod.should.have.property('name');
done(); done();
}); });
@ -2467,11 +2467,11 @@ describe('relations', function () {
it('should add a record to scope - object', function (done) { it('should add a record to scope - object', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.add(product3.id, function(err, prod) { cat.jobs.add(job3.id, function(err, prod) {
should.not.exist(err); should.not.exist(err);
var expected = [product2.id, product1.id, product3.id]; var expected = [job2.id, job1.id, job3.id];
cat.productIds.should.eql(expected); cat.jobIds.should.eql(expected);
prod.id.should.eql(product3.id); prod.id.should.eql(job3.id);
prod.should.have.property('name'); prod.should.have.property('name');
done(); done();
}); });
@ -2480,10 +2480,10 @@ describe('relations', function () {
it('should find items on scope - findById', function (done) { it('should find items on scope - findById', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.findById(product3.id, function(err, p) { cat.jobs.findById(job3.id, function(err, p) {
should.not.exist(err); should.not.exist(err);
p.id.should.eql(product3.id); p.id.should.eql(job3.id);
p.name.should.equal('Product 3'); p.name.should.equal('Job 3');
done(); done();
}); });
}); });
@ -2491,13 +2491,13 @@ describe('relations', function () {
it('should find items on scope - filter', function (done) { it('should find items on scope - filter', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
var filter = { where: { name: 'Product 1' } }; var filter = { where: { name: 'Job 1' } };
cat.products(filter, function(err, products) { cat.jobs(filter, function(err, jobs) {
should.not.exist(err); should.not.exist(err);
products.should.have.length(1); jobs.should.have.length(1);
var p = products[0]; var p = jobs[0];
p.id.should.eql(product1.id); p.id.should.eql(job1.id);
p.name.should.equal('Product 1'); p.name.should.equal('Job 1');
done(); done();
}); });
}); });
@ -2505,11 +2505,11 @@ describe('relations', function () {
it('should remove items from scope', function (done) { it('should remove items from scope', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.remove(product1.id, function(err, ids) { cat.jobs.remove(job1.id, function(err, ids) {
should.not.exist(err); should.not.exist(err);
var expected = [product2.id, product3.id]; var expected = [job2.id, job3.id];
cat.productIds.should.eql(expected); cat.jobIds.should.eql(expected);
ids.should.eql(cat.productIds); ids.should.eql(cat.jobIds);
done(); done();
}); });
}); });
@ -2517,13 +2517,13 @@ describe('relations', function () {
it('should find items on scope - verify', function (done) { it('should find items on scope - verify', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
var expected = [product2.id, product3.id]; var expected = [job2.id, job3.id];
cat.productIds.should.eql(expected); cat.jobIds.should.eql(expected);
cat.products(function(err, products) { cat.jobs(function(err, jobs) {
should.not.exist(err); should.not.exist(err);
products.should.have.length(2); jobs.should.have.length(2);
products[0].id.should.eql(product2.id); jobs[0].id.should.eql(job2.id);
products[1].id.should.eql(product3.id); jobs[1].id.should.eql(job3.id);
done(); done();
}); });
}); });
@ -2531,34 +2531,34 @@ describe('relations', function () {
it('should allow custom scope methods - reverse', function(done) { it('should allow custom scope methods - reverse', function(done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.reverse(function(err, ids) { cat.jobs.reverse(function(err, ids) {
var expected = [product3.id, product2.id]; var expected = [job3.id, job2.id];
ids.should.eql(expected); ids.should.eql(expected);
cat.productIds.should.eql(expected); cat.jobIds.should.eql(expected);
done(); done();
}); });
}) })
}); });
it('should include related items from scope', function(done) { it('should include related items from scope', function(done) {
Category.find({ include: 'products' }, function(err, categories) { Category.find({ include: 'jobs' }, function(err, categories) {
categories.should.have.length(1); categories.should.have.length(1);
var cat = categories[0].toObject(); var cat = categories[0].toObject();
cat.name.should.equal('Category A'); cat.name.should.equal('Category A');
cat.products.should.have.length(2); cat.jobs.should.have.length(2);
cat.products[0].id.should.eql(product3.id); cat.jobs[0].id.should.eql(job3.id);
cat.products[1].id.should.eql(product2.id); cat.jobs[1].id.should.eql(job2.id);
done(); done();
}); });
}); });
it('should destroy items from scope - destroyById', function (done) { it('should destroy items from scope - destroyById', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.destroy(product2.id, function(err) { cat.jobs.destroy(job2.id, function(err) {
should.not.exist(err); should.not.exist(err);
var expected = [product3.id]; var expected = [job3.id];
cat.productIds.should.eql(expected); cat.jobIds.should.eql(expected);
Product.exists(product2.id, function(err, exists) { Job.exists(job2.id, function(err, exists) {
should.not.exist(err); should.not.exist(err);
should.exist(exists); should.exist(exists);
done(); done();
@ -2569,12 +2569,12 @@ describe('relations', function () {
it('should find items on scope - verify', function (done) { it('should find items on scope - verify', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
var expected = [product3.id]; var expected = [job3.id];
cat.productIds.should.eql(expected); cat.jobIds.should.eql(expected);
cat.products(function(err, products) { cat.jobs(function(err, jobs) {
should.not.exist(err); should.not.exist(err);
products.should.have.length(1); jobs.should.have.length(1);
products[0].id.should.eql(product3.id); jobs[0].id.should.eql(job3.id);
done(); done();
}); });
}); });
@ -2588,17 +2588,17 @@ describe('relations', function () {
before(function (done) { before(function (done) {
db = getSchema(); db = getSchema();
Category = db.define('Category', {name: String}); Category = db.define('Category', {name: String});
Product = db.define('Product', {name: String}); Job = db.define('Job', {name: String});
db.automigrate(function () { db.automigrate(function () {
Category.destroyAll(function() { Category.destroyAll(function() {
Product.destroyAll(done); Job.destroyAll(done);
}); });
}); });
}); });
it('can be declared', function (done) { it('can be declared', function (done) {
var relation = Category.hasMany(Product); var relation = Category.hasMany(Job);
var summarize = function(cb) { var summarize = function(cb) {
var modelInstance = this.modelInstance; var modelInstance = this.modelInstance;
@ -2614,13 +2614,13 @@ describe('relations', function () {
}; };
summarize.shared = true; // remoting summarize.shared = true; // remoting
summarize.http = { verb: 'get', path: '/products/summary' }; summarize.http = { verb: 'get', path: '/jobs/summary' };
relation.defineMethod('summarize', summarize); relation.defineMethod('summarize', summarize);
Category.prototype['__summarize__products'].should.be.a.function; Category.prototype['__summarize__jobs'].should.be.a.function;
should.exist(Category.prototype['__summarize__products'].shared); should.exist(Category.prototype['__summarize__jobs'].shared);
Category.prototype['__summarize__products'].http.should.eql(summarize.http); Category.prototype['__summarize__jobs'].http.should.eql(summarize.http);
db.automigrate(done); db.automigrate(done);
}); });
@ -2628,8 +2628,8 @@ describe('relations', function () {
it('should setup test records', function (done) { it('should setup test records', function (done) {
Category.create({ name: 'Category A' }, function(err, cat) { Category.create({ name: 'Category A' }, function(err, cat) {
categoryId = cat.id; categoryId = cat.id;
cat.products.create({ name: 'Product 1' }, function(err, p) { cat.jobs.create({ name: 'Job 1' }, function(err, p) {
cat.products.create({ name: 'Product 2' }, function(err, p) { cat.jobs.create({ name: 'Job 2' }, function(err, p) {
done(); done();
}); });
}) })
@ -2638,12 +2638,12 @@ describe('relations', function () {
it('should allow custom scope methods - summarize', function(done) { it('should allow custom scope methods - summarize', function(done) {
var expected = [ var expected = [
{ name: 'Product 1', categoryId: categoryId, categoryName: 'Category A' }, { name: 'Job 1', categoryId: categoryId, categoryName: 'Category A' },
{ name: 'Product 2', categoryId: categoryId, categoryName: 'Category A' } { name: 'Job 2', categoryId: categoryId, categoryName: 'Category A' }
]; ];
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.summarize(function(err, summary) { cat.jobs.summarize(function(err, summary) {
should.not.exist(err); should.not.exist(err);
var result = summary.map(function(item) { var result = summary.map(function(item) {
delete item.id; delete item.id;