不要怂,就是干,撸起袖子干!

Commit 7c8b3ab1 by Mick Hansen

convert tests to promises

1 parent dee58c69
...@@ -255,7 +255,7 @@ module.exports = (function() { ...@@ -255,7 +255,7 @@ module.exports = (function() {
item.primary = false; item.primary = false;
item.unique = !!item.unique; item.unique = !!item.unique;
return self.run('PRAGMA INDEX_INFO(' + item.name + ')').then(function (columns) { return self.run('PRAGMA INDEX_INFO(`' + item.name + '`)').then(function (columns) {
columns.forEach(function (column) { columns.forEach(function (column) {
item.fields[column.seqno] = { item.fields[column.seqno] = {
attribute: column.name, attribute: column.name,
......
...@@ -19,7 +19,7 @@ var sortById = function(a, b) { ...@@ -19,7 +19,7 @@ var sortById = function(a, b) {
describe(Support.getTestDialectTeaser('Include'), function() { describe(Support.getTestDialectTeaser('Include'), function() {
describe('findAll', function() { describe('findAll', function() {
beforeEach(function() { beforeEach(function() {
this.fixtureA = function(done) { this.fixtureA = function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Company = this.sequelize.define('Company', { , Company = this.sequelize.define('Company', {
name: DataTypes.STRING name: DataTypes.STRING
...@@ -87,77 +87,68 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -87,77 +87,68 @@ describe(Support.getTestDialectTeaser('Include'), function() {
GroupMember.belongsTo(Group); GroupMember.belongsTo(Group);
Group.hasMany(GroupMember, {as: 'Memberships'}); Group.hasMany(GroupMember, {as: 'Memberships'});
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
var count = 4 return Promise.props({
, i = -1; groups: Group.bulkCreate([
{name: 'Developers'},
async.auto({ {name: 'Designers'},
groups: function(callback) { {name: 'Managers'}
Group.bulkCreate([ ]).then(function() {
{name: 'Developers'}, return Group.findAll();
{name: 'Designers'}, }),
{name: 'Managers'} companies: Company.bulkCreate([
]).done(function() { {name: 'Sequelize'},
Group.findAll().done(callback); {name: 'Coca Cola'},
}); {name: 'Bonanza'},
}, {name: 'NYSE'},
companies: function(callback) { {name: 'Coshopr'}
Company.bulkCreate([ ]).then(function() {
{name: 'Sequelize'}, return Company.findAll();
{name: 'Coca Cola'}, }),
{name: 'Bonanza'}, ranks: Rank.bulkCreate([
{name: 'NYSE'}, {name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1},
{name: 'Coshopr'} {name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1},
]).done(function(err) { {name: 'Member', canInvite: 1, canRemove: 0, canPost: 0}
if (err) return callback(err); ]).then(function() {
Company.findAll().done(callback); return Rank.findAll();
}); }),
}, tags: Tag.bulkCreate([
ranks: function(callback) { {name: 'A'},
Rank.bulkCreate([ {name: 'B'},
{name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1}, {name: 'C'},
{name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1}, {name: 'D'},
{name: 'Member', canInvite: 1, canRemove: 0, canPost: 0} {name: 'E'}
]).done(function(err) { ]).then(function() {
Rank.findAll().done(callback); return Tag.findAll();
}); })
}, }).then(function (results) {
tags: function(callback) { var count = 4
Tag.bulkCreate([ , i = -1
{name: 'A'}, , groups = results.groups
{name: 'B'}, , ranks = results.ranks
{name: 'C'}, , tags = results.tags
{name: 'D'}, , companies = results.companies;
{name: 'E'}
]).done(function() {
Tag.findAll().done(callback);
});
},
loop: ['groups', 'ranks', 'tags', 'companies', function(done, results) {
var groups = results.groups
, ranks = results.ranks
, tags = results.tags
, companies = results.companies;
return new Promise(function (resolve, reject) {
async.whilst( async.whilst(
function() { return i < count; }, function() { return i < count; },
function(callback) { function(callback) {
i++; i++;
async.auto({ async.auto({
user: function(callback) { user: function(callback) {
User.create().done(callback); User.create().nodeify(callback);
}, },
memberships: ['user', function(callback, results) { memberships: ['user', function(callback, results) {
var groupMembers = [ var groupMembers = [
{UserId: results.user.id, GroupId: groups[0].id, RankId: ranks[0].id}, {AccUserId: results.user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{UserId: results.user.id, GroupId: groups[1].id, RankId: ranks[2].id} {AccUserId: results.user.id, GroupId: groups[1].id, RankId: ranks[2].id}
]; ];
if (i < 3) { if (i < 3) {
groupMembers.push({UserId: results.user.id, GroupId: groups[2].id, RankId: ranks[1].id}); groupMembers.push({AccUserId: results.user.id, GroupId: groups[2].id, RankId: ranks[1].id});
} }
GroupMember.bulkCreate(groupMembers).done(callback); GroupMember.bulkCreate(groupMembers).nodeify(callback);
}], }],
products: function(callback) { products: function(callback) {
Product.bulkCreate([ Product.bulkCreate([
...@@ -168,7 +159,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -168,7 +159,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
{title: 'Monitor'} {title: 'Monitor'}
]).done(function(err) { ]).done(function(err) {
if (err) return callback(err); if (err) return callback(err);
Product.findAll().done(callback); Product.findAll().nodeify(callback);
}); });
}, },
userProducts: ['user', 'products', function(callback, results) { userProducts: ['user', 'products', function(callback, results) {
...@@ -176,7 +167,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -176,7 +167,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
results.products[(i * 5) + 0], results.products[(i * 5) + 0],
results.products[(i * 5) + 1], results.products[(i * 5) + 1],
results.products[(i * 5) + 3] results.products[(i * 5) + 3]
]).done(callback); ]).nodeify(callback);
}], }],
productTags: ['products', function(callback, results) { productTags: ['products', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer(); var chainer = new Sequelize.Utils.QueryChainer();
...@@ -221,17 +212,17 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -221,17 +212,17 @@ describe(Support.getTestDialectTeaser('Include'), function() {
{ProductId: results.products[(i * 5) + 1].id, value: 20}, {ProductId: results.products[(i * 5) + 1].id, value: 20},
{ProductId: results.products[(i * 5) + 2].id, value: 20}, {ProductId: results.products[(i * 5) + 2].id, value: 20},
{ProductId: results.products[(i * 5) + 3].id, value: 20} {ProductId: results.products[(i * 5) + 3].id, value: 20}
]).done(callback); ]).nodeify(callback);
}] }]
}, callback); }, callback);
}, },
function(err) { function(err) {
expect(err).not.to.be.ok; if (err) return reject(err);
done(); resolve();
} }
); );
}] });
}, done.bind(this)); });
}); });
}; };
}); });
...@@ -366,7 +357,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -366,7 +357,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
}); });
}); });
it('should support an include with multiple different association types', function(done) { it('should support an include with multiple different association types', function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Product = this.sequelize.define('Product', { , Product = this.sequelize.define('Product', {
title: DataTypes.STRING title: DataTypes.STRING
...@@ -414,146 +405,146 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -414,146 +405,146 @@ describe(Support.getTestDialectTeaser('Include'), function() {
GroupMember.belongsTo(Group); GroupMember.belongsTo(Group);
Group.hasMany(GroupMember, {as: 'Memberships'}); Group.hasMany(GroupMember, {as: 'Memberships'});
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
var count = 4 return new Promise(function (resolve, reject) {
, i = -1; var count = 4
, i = -1;
async.auto({ async.auto({
groups: function(callback) { groups: function(callback) {
Group.bulkCreate([ Group.bulkCreate([
{name: 'Developers'}, {name: 'Developers'},
{name: 'Designers'} {name: 'Designers'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }).nodeify(callback);
}, },
ranks: function(callback) { ranks: function(callback) {
Rank.bulkCreate([ Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1}, {name: 'Admin', canInvite: 1, canRemove: 1},
{name: 'Member', canInvite: 1, canRemove: 0} {name: 'Member', canInvite: 1, canRemove: 0}
]).done(function() { ]).then(function() {
Rank.findAll().done(callback); return Rank.findAll();
}); }).nodeify(callback);
}, },
tags: function(callback) { tags: function(callback) {
Tag.bulkCreate([ Tag.bulkCreate([
{name: 'A'}, {name: 'A'},
{name: 'B'}, {name: 'B'},
{name: 'C'} {name: 'C'}
]).done(function() { ]).then(function() {
Tag.findAll().done(callback); return Tag.findAll();
}); }).nodeify(callback);
}, },
loop: ['groups', 'ranks', 'tags', function(done, results) { loop: ['groups', 'ranks', 'tags', function(done, results) {
var groups = results.groups var groups = results.groups
, ranks = results.ranks , ranks = results.ranks
, tags = results.tags; , tags = results.tags;
async.whilst( async.whilst(
function() { return i < count; }, function() { return i < count; },
function(callback) { function(callback) {
i++; i++;
async.auto({ async.auto({
user: function(callback) { user: function(callback) {
User.create().done(callback); User.create().nodeify(callback);
}, },
memberships: ['user', function(callback, results) { memberships: ['user', function(callback, results) {
GroupMember.bulkCreate([ GroupMember.bulkCreate([
{UserId: results.user.id, GroupId: groups[0].id, RankId: ranks[0].id}, {UserId: results.user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{UserId: results.user.id, GroupId: groups[1].id, RankId: ranks[1].id} {UserId: results.user.id, GroupId: groups[1].id, RankId: ranks[1].id}
]).done(callback); ]).nodeify(callback);
}], }],
products: function(callback) { products: function(callback) {
Product.bulkCreate([ Product.bulkCreate([
{title: 'Chair'}, {title: 'Chair'},
{title: 'Desk'} {title: 'Desk'}
]).done(function() { ]).then(function() {
Product.findAll().done(callback); return Product.findAll();
}).nodeify(callback);
},
userProducts: ['user', 'products', function(callback, results) {
results.user.setProducts([
results.products[(i * 2) + 0],
results.products[(i * 2) + 1]
]).nodeify(callback);
}],
productTags: ['products', function(callback, results) {
return Promise.join(
results.products[(i * 2) + 0].setTags([
tags[0],
tags[2]
]),
results.products[(i * 2) + 1].setTags([
tags[1]
]),
results.products[(i * 2) + 0].setCategory(tags[1])
).nodeify(callback);
}],
prices: ['products', function(callback, results) {
Price.bulkCreate([
{ProductId: results.products[(i * 2) + 0].id, value: 5},
{ProductId: results.products[(i * 2) + 0].id, value: 10},
{ProductId: results.products[(i * 2) + 1].id, value: 5},
{ProductId: results.products[(i * 2) + 1].id, value: 10},
{ProductId: results.products[(i * 2) + 1].id, value: 15},
{ProductId: results.products[(i * 2) + 1].id, value: 20}
]).nodeify(callback);
}]
}, callback);
},
function(err) {
User.findAll({
include: [
{model: GroupMember, as: 'Memberships', include: [
Group,
Rank
]},
{model: Product, include: [
Tag,
{model: Tag, as: 'Category'},
Price
]}
],
order: [
['id', 'ASC']
]
}).then(function(users) {
users.forEach(function(user) {
user.Memberships.sort(sortById);
expect(user.Memberships.length).to.equal(2);
expect(user.Memberships[0].Group.name).to.equal('Developers');
expect(user.Memberships[0].Rank.canRemove).to.equal(1);
expect(user.Memberships[1].Group.name).to.equal('Designers');
expect(user.Memberships[1].Rank.canRemove).to.equal(0);
user.Products.sort(sortById);
expect(user.Products.length).to.equal(2);
expect(user.Products[0].Tags.length).to.equal(2);
expect(user.Products[1].Tags.length).to.equal(1);
expect(user.Products[0].Category).to.be.ok;
expect(user.Products[1].Category).not.to.be.ok;
expect(user.Products[0].Prices.length).to.equal(2);
expect(user.Products[1].Prices.length).to.equal(4);
}); });
}, }).nodeify(done);
userProducts: ['user', 'products', function(callback, results) { }
results.user.setProducts([ );
results.products[(i * 2) + 0], }]
results.products[(i * 2) + 1] }, function (err) {
]).done(callback); if (err) return reject(err);
}], resolve();
productTags: ['products', function(callback, results) { });
var chainer = new Sequelize.Utils.QueryChainer(); });
chainer.add(results.products[(i * 2) + 0].setTags([
tags[0],
tags[2]
]));
chainer.add(results.products[(i * 2) + 1].setTags([
tags[1]
]));
chainer.add(results.products[(i * 2) + 0].setCategory(tags[1]));
chainer.run().done(callback);
}],
prices: ['products', function(callback, results) {
Price.bulkCreate([
{ProductId: results.products[(i * 2) + 0].id, value: 5},
{ProductId: results.products[(i * 2) + 0].id, value: 10},
{ProductId: results.products[(i * 2) + 1].id, value: 5},
{ProductId: results.products[(i * 2) + 1].id, value: 10},
{ProductId: results.products[(i * 2) + 1].id, value: 15},
{ProductId: results.products[(i * 2) + 1].id, value: 20}
]).done(callback);
}]
}, callback);
},
function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [
{model: GroupMember, as: 'Memberships', include: [
Group,
Rank
]},
{model: Product, include: [
Tag,
{model: Tag, as: 'Category'},
Price
]}
],
order: [
['id', 'ASC']
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
users.forEach(function(user) {
user.Memberships.sort(sortById);
expect(user.Memberships.length).to.equal(2);
expect(user.Memberships[0].Group.name).to.equal('Developers');
expect(user.Memberships[0].Rank.canRemove).to.equal(1);
expect(user.Memberships[1].Group.name).to.equal('Designers');
expect(user.Memberships[1].Rank.canRemove).to.equal(0);
user.Products.sort(sortById);
expect(user.Products.length).to.equal(2);
expect(user.Products[0].Tags.length).to.equal(2);
expect(user.Products[1].Tags.length).to.equal(1);
expect(user.Products[0].Category).to.be.ok;
expect(user.Products[1].Category).not.to.be.ok;
expect(user.Products[0].Prices.length).to.equal(2);
expect(user.Products[1].Prices.length).to.equal(4);
done();
});
});
}
);
}]
}, done);
}); });
}); });
it('should support many levels of belongsTo', function(done) { it('should support many levels of belongsTo', function() {
var A = this.sequelize.define('a', {}) var A = this.sequelize.define('a', {})
, B = this.sequelize.define('b', {}) , B = this.sequelize.define('b', {})
, C = this.sequelize.define('c', {}) , C = this.sequelize.define('c', {})
...@@ -571,61 +562,51 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -571,61 +562,51 @@ describe(Support.getTestDialectTeaser('Include'), function() {
F.belongsTo(G); F.belongsTo(G);
G.belongsTo(H); G.belongsTo(H);
var b, singles = [ return this.sequelize.sync().then(function() {
B, return Promise.join(
C, A.bulkCreate([
D, {},
E, {},
F, {},
G, {},
H {},
]; {},
{},
this.sequelize.sync().done(function() { {}
async.auto({ ]).then(function() {
as: function(callback) { return A.findAll();
A.bulkCreate([ }),
{}, (function (singles) {
{}, var promise = Promise.resolve()
{}, , previousInstance
{}, , b;
{},
{}, singles.forEach(function (model) {
{}, promise = promise.then(function () {
{} return model.create({}).then(function (instance) {
]).done(function() { if (previousInstance) {
A.findAll().done(callback); return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() {
}); previousInstance = instance;
}, });
singleChain: function(callback) { } else {
var previousInstance; previousInstance = b = instance;
}
async.eachSeries(singles, function(model, callback) { });
model.create({}).done(function(err, instance) {
if (previousInstance) {
previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).done(function() {
previousInstance = instance;
callback();
});
} else {
previousInstance = b = instance;
callback();
}
}); });
}, callback);
},
abs: ['as', 'singleChain', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
results.as.forEach(function(a) {
chainer.add(a.setB(b));
}); });
chainer.run().done(callback); promise = promise.then(function () {
}] return b;
}, function() { });
A.findAll({ return promise;
})([B, C, D, E, F, G, H])
).spread(function (as, b) {
return as.map(function (a) {
return a.setB(b);
});
}).then(function () {
return A.findAll({
include: [ include: [
{model: B, include: [ {model: B, include: [
{model: C, include: [ {model: C, include: [
...@@ -641,20 +622,18 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -641,20 +622,18 @@ describe(Support.getTestDialectTeaser('Include'), function() {
]} ]}
]} ]}
] ]
}).done(function(err, as) { }).then(function(as) {
expect(err).not.to.be.ok;
expect(as.length).to.be.ok; expect(as.length).to.be.ok;
as.forEach(function(a) { as.forEach(function(a) {
expect(a.b.c.d.e.f.g.h).to.be.ok; expect(a.b.c.d.e.f.g.h).to.be.ok;
}); });
done();
}); });
}); });
}); });
}); });
it('should support many levels of belongsTo (with a lower level having a where)', function(done) { it('should support many levels of belongsTo (with a lower level having a where)', function() {
var A = this.sequelize.define('a', {}) var A = this.sequelize.define('a', {})
, B = this.sequelize.define('b', {}) , B = this.sequelize.define('b', {})
, C = this.sequelize.define('c', {}) , C = this.sequelize.define('c', {})
...@@ -676,66 +655,57 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -676,66 +655,57 @@ describe(Support.getTestDialectTeaser('Include'), function() {
F.belongsTo(G); F.belongsTo(G);
G.belongsTo(H); G.belongsTo(H);
var b, singles = [ return this.sequelize.sync().then(function() {
B, return Promise.join(
C, A.bulkCreate([
D, {},
E, {},
F, {},
G, {},
H {},
]; {},
{},
this.sequelize.sync().done(function() { {}
async.auto({ ]).then(function() {
as: function(callback) { return A.findAll();
A.bulkCreate([ }),
{}, (function (singles) {
{}, var promise = Promise.resolve()
{}, , previousInstance
{}, , b;
{},
{}, singles.forEach(function (model) {
{},
{}
]).done(function() {
A.findAll().done(callback);
});
},
singleChain: function(callback) {
var previousInstance;
async.eachSeries(singles, function(model, callback) {
var values = {}; var values = {};
if (model.name === 'g') { if (model.name === 'g') {
values.name = 'yolo'; values.name = 'yolo';
} }
model.create(values).done(function(err, instance) {
if (previousInstance) {
previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).done(function() {
previousInstance = instance;
callback();
});
} else {
previousInstance = b = instance;
callback();
}
});
}, callback);
},
abs: ['as', 'singleChain', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
results.as.forEach(function(a) { promise = promise.then(function () {
chainer.add(a.setB(b)); return model.create(values).then(function (instance) {
if (previousInstance) {
return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() {
previousInstance = instance;
});
} else {
previousInstance = b = instance;
}
});
});
}); });
chainer.run().done(callback); promise = promise.then(function () {
}] return b;
}, function() { });
A.findAll({ return promise;
})([B, C, D, E, F, G, H])
).spread(function (as, b) {
return as.map(function (a) {
return a.setB(b);
});
}).then(function () {
return A.findAll({
include: [ include: [
{model: B, include: [ {model: B, include: [
{model: C, include: [ {model: C, include: [
...@@ -753,20 +723,18 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -753,20 +723,18 @@ describe(Support.getTestDialectTeaser('Include'), function() {
]} ]}
]} ]}
] ]
}).done(function(err, as) { }).then(function(as) {
expect(err).not.to.be.ok;
expect(as.length).to.be.ok; expect(as.length).to.be.ok;
as.forEach(function(a) { as.forEach(function(a) {
expect(a.b.c.d.e.f.g.h).to.be.ok; expect(a.b.c.d.e.f.g.h).to.be.ok;
}); });
done();
}); });
}); });
}); });
}); });
it('should support ordering with only belongsTo includes', function(done) { it('should support ordering with only belongsTo includes', function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Item = this.sequelize.define('Item', {'test': DataTypes.STRING}) , Item = this.sequelize.define('Item', {'test': DataTypes.STRING})
, Order = this.sequelize.define('Order', {'position': DataTypes.INTEGER}); , Order = this.sequelize.define('Order', {'position': DataTypes.INTEGER});
...@@ -775,64 +743,53 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -775,64 +743,53 @@ describe(Support.getTestDialectTeaser('Include'), function() {
User.belongsTo(Item, {'as': 'itemB', foreignKey: 'itemB_id'}); User.belongsTo(Item, {'as': 'itemB', foreignKey: 'itemB_id'});
User.belongsTo(Order); User.belongsTo(Order);
this.sequelize.sync().done(function() { return this.sequelize.sync().then(function() {
async.auto({ return Promise.props({
users: function(callback) { users: User.bulkCreate([{}, {}, {}]).then(function() {
User.bulkCreate([{}, {}, {}]).done(function() { return User.findAll();
User.findAll().done(callback); }),
}); items: Item.bulkCreate([
}, {'test': 'abc'},
items: function(callback) { {'test': 'def'},
Item.bulkCreate([ {'test': 'ghi'},
{'test': 'abc'}, {'test': 'jkl'}
{'test': 'def'}, ]).then(function() {
{'test': 'ghi'}, return Item.findAll({order: ['id']});
{'test': 'jkl'} }),
]).done(function() { orders: Order.bulkCreate([
Item.findAll({order: ['id']}).done(callback); {'position': 2},
}); {'position': 3},
}, {'position': 1}
orders: function(callback) { ]).then(function() {
Order.bulkCreate([ return Order.findAll({order: ['id']});
{'position': 2}, })
{'position': 3}, }).then(function (results) {
{'position': 1} var user1 = results.users[0];
]).done(function() { var user2 = results.users[1];
Order.findAll({order: ['id']}).done(callback); var user3 = results.users[2];
});
},
associate: ['users', 'items', 'orders', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
var user1 = results.users[0];
var user2 = results.users[1];
var user3 = results.users[2];
var item1 = results.items[0];
var item2 = results.items[1];
var item3 = results.items[2];
var item4 = results.items[3];
var order1 = results.orders[0];
var order2 = results.orders[1];
var order3 = results.orders[2];
chainer.add(user1.setItemA(item1));
chainer.add(user1.setItemB(item2));
chainer.add(user1.setOrder(order3));
chainer.add(user2.setItemA(item3)); var item1 = results.items[0];
chainer.add(user2.setItemB(item4)); var item2 = results.items[1];
chainer.add(user2.setOrder(order2)); var item3 = results.items[2];
var item4 = results.items[3];
chainer.add(user3.setItemA(item1)); var order1 = results.orders[0];
chainer.add(user3.setItemB(item4)); var order2 = results.orders[1];
chainer.add(user3.setOrder(order1)); var order3 = results.orders[2];
chainer.run().done(callback); return Promise.join(
}] user1.setItemA(item1),
}, function() { user1.setItemB(item2),
User.findAll({ user1.setOrder(order3),
user2.setItemA(item3),
user2.setItemB(item4),
user2.setOrder(order2),
user3.setItemA(item1),
user3.setItemB(item4),
user3.setOrder(order1)
);
}).then(function () {
return User.findAll({
'include': [ 'include': [
{'model': Item, 'as': 'itemA', where: {test: 'abc'}}, {'model': Item, 'as': 'itemA', where: {test: 'abc'}},
{'model': Item, 'as': 'itemB'}, {'model': Item, 'as': 'itemB'},
...@@ -840,8 +797,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -840,8 +797,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
'order': [ 'order': [
[Order, 'position'] [Order, 'position']
] ]
}).done(function(err, as) { }).then(function(as) {
expect(err).not.to.be.ok;
expect(as.length).to.eql(2); expect(as.length).to.eql(2);
expect(as[0].itemA.test).to.eql('abc'); expect(as[0].itemA.test).to.eql('abc');
...@@ -849,14 +805,12 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -849,14 +805,12 @@ describe(Support.getTestDialectTeaser('Include'), function() {
expect(as[0].Order.position).to.eql(1); expect(as[0].Order.position).to.eql(1);
expect(as[1].Order.position).to.eql(2); expect(as[1].Order.position).to.eql(2);
done();
}); });
}); });
}); });
}); });
it('should include attributes from through models', function(done) { it('should include attributes from through models', function() {
var Product = this.sequelize.define('Product', { var Product = this.sequelize.define('Product', {
title: DataTypes.STRING title: DataTypes.STRING
}) })
...@@ -870,44 +824,33 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -870,44 +824,33 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Product.hasMany(Tag, {through: ProductTag}); Product.hasMany(Tag, {through: ProductTag});
Tag.hasMany(Product, {through: ProductTag}); Tag.hasMany(Product, {through: ProductTag});
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
products: function(callback) { products: Product.bulkCreate([
Product.bulkCreate([ {title: 'Chair'},
{title: 'Chair'}, {title: 'Desk'},
{title: 'Desk'}, {title: 'Dress'}
{title: 'Dress'} ]).then(function() {
]).done(function() { return Product.findAll();
Product.findAll().done(callback); }),
}); tags: Tag.bulkCreate([
}, {name: 'A'},
tags: function(callback) { {name: 'B'},
Tag.bulkCreate([ {name: 'C'}
{name: 'A'}, ]).then(function() {
{name: 'B'}, return Tag.findAll();
{name: 'C'} })
]).done(function() { }).then(function (results) {
Tag.findAll().done(callback); return Promise.join(
}); results.products[0].addTag(results.tags[0], {priority: 1}),
}, results.products[0].addTag(results.tags[1], {priority: 2}),
productTags: ['products', 'tags', function(callback, results) { results.products[1].addTag(results.tags[1], {priority: 1}),
var chainer = new Sequelize.Utils.QueryChainer(); results.products[2].addTag(results.tags[0], {priority: 3}),
results.products[2].addTag(results.tags[1], {priority: 1}),
chainer.add(results.products[0].addTag(results.tags[0], {priority: 1})); results.products[2].addTag(results.tags[2], {priority: 2})
chainer.add(results.products[0].addTag(results.tags[1], {priority: 2})); );
}).then(function () {
chainer.add(results.products[1].addTag(results.tags[1], {priority: 1})); return Product.findAll({
chainer.add(results.products[2].addTag(results.tags[0], {priority: 3}));
chainer.add(results.products[2].addTag(results.tags[1], {priority: 1}));
chainer.add(results.products[2].addTag(results.tags[2], {priority: 2}));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
Product.findAll({
include: [ include: [
{model: Tag} {model: Tag}
], ],
...@@ -915,9 +858,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -915,9 +858,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
['id', 'ASC'], ['id', 'ASC'],
[Tag, 'id', 'ASC'] [Tag, 'id', 'ASC']
] ]
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
expect(products[0].Tags[0].ProductTag.priority).to.equal(1); expect(products[0].Tags[0].ProductTag.priority).to.equal(1);
expect(products[0].Tags[1].ProductTag.priority).to.equal(2); expect(products[0].Tags[1].ProductTag.priority).to.equal(2);
...@@ -926,52 +867,41 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -926,52 +867,41 @@ describe(Support.getTestDialectTeaser('Include'), function() {
expect(products[2].Tags[0].ProductTag.priority).to.equal(3); expect(products[2].Tags[0].ProductTag.priority).to.equal(3);
expect(products[2].Tags[1].ProductTag.priority).to.equal(1); expect(products[2].Tags[1].ProductTag.priority).to.equal(1);
expect(products[2].Tags[2].ProductTag.priority).to.equal(2); expect(products[2].Tags[2].ProductTag.priority).to.equal(2);
done();
}); });
}); });
}); });
}); });
it('should support a required belongsTo include', function(done) { it('should support a required belongsTo include', function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {}); , Group = this.sequelize.define('Group', {});
User.belongsTo(Group); User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
groups: function(callback) { groups: Group.bulkCreate([{}, {}]).then(function() {
Group.bulkCreate([{}, {}]).done(function() { return Group.findAll();
Group.findAll().done(callback); }),
}); users: User.bulkCreate([{}, {}, {}]).then(function() {
}, return User.findAll();
users: function(callback) { })
User.bulkCreate([{}, {}, {}]).done(function() { }).then(function (results) {
User.findAll().done(callback); return results.users[2].setGroup(results.groups[1]);
}); }).then(function () {
}, return User.findAll({
userGroups: ['users', 'groups', function(callback, results) {
results.users[2].setGroup(results.groups[1]).done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [ include: [
{model: Group, required: true} {model: Group, required: true}
] ]
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
expect(users.length).to.equal(1); expect(users.length).to.equal(1);
expect(users[0].Group).to.be.ok; expect(users[0].Group).to.be.ok;
done();
}); });
}); });
}); });
}); });
it('should be possible to extend the on clause with a where option on a belongsTo include', function(done) { it('should be possible to extend the on clause with a where option on a belongsTo include', function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', { , Group = this.sequelize.define('Group', {
name: DataTypes.STRING name: DataTypes.STRING
...@@ -979,46 +909,37 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -979,46 +909,37 @@ describe(Support.getTestDialectTeaser('Include'), function() {
User.belongsTo(Group); User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
groups: function(callback) { groups: Group.bulkCreate([
Group.bulkCreate([ {name: 'A'},
{name: 'A'}, {name: 'B'}
{name: 'B'} ]).then(function() {
]).done(function() { return Group.findAll();
Group.findAll().done(callback); }),
}); users: User.bulkCreate([{}, {}]).then(function() {
}, return User.findAll();
users: function(callback) { }),
User.bulkCreate([{}, {}]).done(function() { }).then(function (results) {
User.findAll().done(callback); return Promise.join(
}); results.users[0].setGroup(results.groups[1]),
}, results.users[1].setGroup(results.groups[0])
userGroups: ['users', 'groups', function(callback, results) { );
var chainer = new Sequelize.Utils.QueryChainer(); }).then(function () {
chainer.add(results.users[0].setGroup(results.groups[1])); return User.findAll({
chainer.add(results.users[1].setGroup(results.groups[0]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [ include: [
{model: Group, where: {name: 'A'}} {model: Group, where: {name: 'A'}}
] ]
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
expect(users.length).to.equal(1); expect(users.length).to.equal(1);
expect(users[0].Group).to.be.ok; expect(users[0].Group).to.be.ok;
expect(users[0].Group.name).to.equal('A'); expect(users[0].Group.name).to.equal('A');
done();
}); });
}); });
}); });
}); });
it('should be possible to extend the on clause with a where option on a belongsTo include', function(done) { it('should be possible to extend the on clause with a where option on a belongsTo include', function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', { , Group = this.sequelize.define('Group', {
name: DataTypes.STRING name: DataTypes.STRING
...@@ -1026,50 +947,40 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1026,50 +947,40 @@ describe(Support.getTestDialectTeaser('Include'), function() {
User.belongsTo(Group); User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
groups: function(callback) { groups: Group.bulkCreate([
Group.bulkCreate([ {name: 'A'},
{name: 'A'}, {name: 'B'}
{name: 'B'} ]).then(function() {
]).done(function() { return Group.findAll();
Group.findAll().done(callback); }),
}); users: User.bulkCreate([{}, {}]).then(function() {
}, return User.findAll();
users: function(callback) { })
User.bulkCreate([{}, {}]).done(function() { }).then(function (results) {
User.findAll().done(callback); return Promise.join(
}); results.users[0].setGroup(results.groups[1]),
}, results.users[1].setGroup(results.groups[0])
userGroups: ['users', 'groups', function(callback, results) { );
var chainer = new Sequelize.Utils.QueryChainer(); }).then(function () {
chainer.add(results.users[0].setGroup(results.groups[1])); return User.findAll({
chainer.add(results.users[1].setGroup(results.groups[0]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [ include: [
{model: Group, required: true} {model: Group, required: true}
] ]
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
users.forEach(function(user) { users.forEach(function(user) {
expect(user.Group).to.be.ok; expect(user.Group).to.be.ok;
}); });
done();
}); });
}); });
}); });
}); });
it('should be possible to define a belongsTo include as required with child hasMany not required', function(done) { it('should be possible to define a belongsTo include as required with child hasMany not required', function() {
var S = this.sequelize var Address = this.sequelize.define('Address', { 'active': DataTypes.BOOLEAN })
, Address = S.define('Address', { 'active': DataTypes.BOOLEAN }) , Street = this.sequelize.define('Street', { 'active': DataTypes.BOOLEAN })
, Street = S.define('Street', { 'active': DataTypes.BOOLEAN }) , User = this.sequelize.define('User', { 'username': DataTypes.STRING });
, User = S.define('User', { 'username': DataTypes.STRING });
// Associate // Associate
User.belongsTo(Address, { foreignKey: 'addressId' }); User.belongsTo(Address, { foreignKey: 'addressId' });
...@@ -1079,41 +990,33 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1079,41 +990,33 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Street.hasMany(Address, { foreignKey: 'streetId' }); Street.hasMany(Address, { foreignKey: 'streetId' });
// Sync // Sync
S.sync({ force: true }).success(function() { return this.sequelize.sync({ force: true }).then(function() {
return Street.create({ active: true }).then(function(street) {
// Create instances return Address.create({ active: true, streetId: street.id }).then(function(address ) {
Street.create({ active: true }).done(function(err, street ) { expect(err).not.to.be.ok; expect(street).to.be.ok; return User.create({ username: 'John', addressId: address.id }).then(function(john ) {
Address.create({ active: true, streetId: street.id }).done(function(err, address ) { expect(err).not.to.be.ok; expect(address).to.be.ok; return User.find({
User.create({ username: 'John', addressId: address.id }).done(function(err, john ) { expect(err).not.to.be.ok; expect(john).to.be.ok; where: { username: 'John'},
// Test
User.find({
where: { username: 'John'},
include: [{
model: Address,
required: true,
where: {
active: true
},
include: [{ include: [{
model: Street model: Address,
required: true,
where: {
active: true
},
include: [{
model: Street
}]
}] }]
}] }).then(function(john) {
}).done(function(err, john) { expect(john.Address).to.be.ok;
expect(err).not.to.be.ok; expect(john.Address.Street).to.be.ok;
expect(john.Address).to.be.ok; });
expect(john.Address.Street).to.be.ok;
done();
}); });
});
});
});
}); });
}); });
}); });
it('should be possible to define a belongsTo include as required with child hasMany with limit', function(done) { it('should be possible to define a belongsTo include as required with child hasMany with limit', function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', { , Group = this.sequelize.define('Group', {
name: DataTypes.STRING name: DataTypes.STRING
...@@ -1125,65 +1028,48 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1125,65 +1028,48 @@ describe(Support.getTestDialectTeaser('Include'), function() {
User.belongsTo(Group); User.belongsTo(Group);
Group.hasMany(Category); Group.hasMany(Category);
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
groups: function(callback) { groups: Group.bulkCreate([
Group.bulkCreate([ {name: 'A'},
{name: 'A'}, {name: 'B'}
{name: 'B'} ]).then(function() {
]).done(function() { return Group.findAll();
Group.findAll().done(callback); }),
}); users: User.bulkCreate([{}, {}]).then(function() {
}, return User.findAll();
users: function(callback) { }),
User.bulkCreate([{}, {}]).done(function() { categories: Category.bulkCreate([{}, {}]).then(function() {
User.findAll().done(callback); return Category.findAll();
}); })
}, }).then(function (results) {
categories: function(callback) { return Promise.join(
Category.bulkCreate([{}, {}]).done(function() { results.users[0].setGroup(results.groups[1]),
Category.findAll().done(callback); results.users[1].setGroup(results.groups[0]),
}); results.groups.map(function (group) {
}, return group.setCategories(results.categories);
userGroups: ['users', 'groups', function(callback, results) { })
var chainer = new Sequelize.Utils.QueryChainer(); );
chainer.add(results.users[0].setGroup(results.groups[1])); }).then(function () {
chainer.add(results.users[1].setGroup(results.groups[0])); return User.findAll({
chainer.run().done(callback);
}],
groupCategories: ['groups', 'categories', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
results.groups.forEach(function(group) {
chainer.add(group.setCategories(results.categories));
});
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [ include: [
{model: Group, required: true, include: [ {model: Group, required: true, include: [
{model: Category} {model: Category}
]} ]}
], ],
limit: 1 limit: 1
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
expect(users.length).to.equal(1); expect(users.length).to.equal(1);
users.forEach(function(user) { users.forEach(function(user) {
expect(user.Group).to.be.ok; expect(user.Group).to.be.ok;
expect(user.Group.Categories).to.be.ok; expect(user.Group.Categories).to.be.ok;
}); });
done();
}); });
}); });
}); });
}); });
it('should be possible to define a belongsTo include as required with child hasMany with limit and aliases', function(done) { it('should be possible to define a belongsTo include as required with child hasMany with limit and aliases', function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', { , Group = this.sequelize.define('Group', {
name: DataTypes.STRING name: DataTypes.STRING
...@@ -1195,65 +1081,48 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1195,65 +1081,48 @@ describe(Support.getTestDialectTeaser('Include'), function() {
User.belongsTo(Group, {as: 'Team'}); User.belongsTo(Group, {as: 'Team'});
Group.hasMany(Category, {as: 'Tags'}); Group.hasMany(Category, {as: 'Tags'});
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
groups: function(callback) { groups: Group.bulkCreate([
Group.bulkCreate([ {name: 'A'},
{name: 'A'}, {name: 'B'}
{name: 'B'} ]).then(function() {
]).done(function() { return Group.findAll();
Group.findAll().done(callback); }),
}); users: User.bulkCreate([{}, {}]).then(function() {
}, return User.findAll();
users: function(callback) { }),
User.bulkCreate([{}, {}]).done(function() { categories: Category.bulkCreate([{}, {}]).then(function() {
User.findAll().done(callback); return Category.findAll();
}); })
}, }).then(function (results) {
categories: function(callback) { return Promise.join(
Category.bulkCreate([{}, {}]).done(function() { results.users[0].setTeam(results.groups[1]),
Category.findAll().done(callback); results.users[1].setTeam(results.groups[0]),
}); results.groups.map(function (group) {
}, return group.setTags(results.categories);
userGroups: ['users', 'groups', function(callback, results) { })
var chainer = new Sequelize.Utils.QueryChainer(); );
chainer.add(results.users[0].setTeam(results.groups[1])); }).then(function () {
chainer.add(results.users[1].setTeam(results.groups[0])); return User.findAll({
chainer.run().done(callback);
}],
groupCategories: ['groups', 'categories', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
results.groups.forEach(function(group) {
chainer.add(group.setTags(results.categories));
});
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [ include: [
{model: Group, required: true, as: 'Team', include: [ {model: Group, required: true, as: 'Team', include: [
{model: Category, as: 'Tags'} {model: Category, as: 'Tags'}
]} ]}
], ],
limit: 1 limit: 1
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
expect(users.length).to.equal(1); expect(users.length).to.equal(1);
users.forEach(function(user) { users.forEach(function(user) {
expect(user.Team).to.be.ok; expect(user.Team).to.be.ok;
expect(user.Team.Tags).to.be.ok; expect(user.Team.Tags).to.be.ok;
}); });
done();
}); });
}); });
}); });
}); });
it('should be possible to define a belongsTo include as required with child hasMany which is not required with limit', function(done) { it('should be possible to define a belongsTo include as required with child hasMany which is not required with limit', function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', { , Group = this.sequelize.define('Group', {
name: DataTypes.STRING name: DataTypes.STRING
...@@ -1265,65 +1134,48 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1265,65 +1134,48 @@ describe(Support.getTestDialectTeaser('Include'), function() {
User.belongsTo(Group); User.belongsTo(Group);
Group.hasMany(Category); Group.hasMany(Category);
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
groups: function(callback) { groups: Group.bulkCreate([
Group.bulkCreate([ {name: 'A'},
{name: 'A'}, {name: 'B'}
{name: 'B'} ]).then(function() {
]).done(function() { return Group.findAll();
Group.findAll().done(callback); }),
}); users: User.bulkCreate([{}, {}]).then(function() {
}, return User.findAll();
users: function(callback) { }),
User.bulkCreate([{}, {}]).done(function() { categories: Category.bulkCreate([{}, {}]).then(function() {
User.findAll().done(callback); return Category.findAll();
}); })
}, }).then(function (results) {
categories: function(callback) { return Promise.join(
Category.bulkCreate([{}, {}]).done(function() { results.users[0].setGroup(results.groups[1]),
Category.findAll().done(callback); results.users[1].setGroup(results.groups[0]),
}); results.groups.map(function (group) {
}, return group.setCategories(results.categories);
userGroups: ['users', 'groups', function(callback, results) { })
var chainer = new Sequelize.Utils.QueryChainer(); );
chainer.add(results.users[0].setGroup(results.groups[1])); }).then(function () {
chainer.add(results.users[1].setGroup(results.groups[0])); return User.findAll({
chainer.run().done(callback);
}],
groupCategories: ['groups', 'categories', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
results.groups.forEach(function(group) {
chainer.add(group.setCategories(results.categories));
});
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [ include: [
{model: Group, required: true, include: [ {model: Group, required: true, include: [
{model: Category, required: false} {model: Category, required: false}
]} ]}
], ],
limit: 1 limit: 1
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
expect(users.length).to.equal(1); expect(users.length).to.equal(1);
users.forEach(function(user) { users.forEach(function(user) {
expect(user.Group).to.be.ok; expect(user.Group).to.be.ok;
expect(user.Group.Categories).to.be.ok; expect(user.Group.Categories).to.be.ok;
}); });
done();
}); });
}); });
}); });
}); });
it('should be possible to extend the on clause with a where option on a hasOne include', function(done) { it('should be possible to extend the on clause with a where option on a hasOne include', function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Project = this.sequelize.define('Project', { , Project = this.sequelize.define('Project', {
title: DataTypes.STRING title: DataTypes.STRING
...@@ -1331,46 +1183,37 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1331,46 +1183,37 @@ describe(Support.getTestDialectTeaser('Include'), function() {
User.hasOne(Project, {as: 'LeaderOf'}); User.hasOne(Project, {as: 'LeaderOf'});
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
projects: function(callback) { projects: Project.bulkCreate([
Project.bulkCreate([ {title: 'Alpha'},
{title: 'Alpha'}, {title: 'Beta'}
{title: 'Beta'} ]).then(function() {
]).done(function() { return Project.findAll();
Project.findAll().done(callback); }),
}); users: User.bulkCreate([{}, {}]).then(function() {
}, return User.findAll();
users: function(callback) { })
User.bulkCreate([{}, {}]).done(function() { }).then(function (results) {
User.findAll().done(callback); return Promise.join(
}); results.users[1].setLeaderOf(results.projects[1]),
}, results.users[0].setLeaderOf(results.projects[0])
userProjects: ['users', 'projects', function(callback, results) { );
var chainer = new Sequelize.Utils.QueryChainer(); }).then(function () {
chainer.add(results.users[1].setLeaderOf(results.projects[1])); return User.findAll({
chainer.add(results.users[0].setLeaderOf(results.projects[0]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [ include: [
{model: Project, as: 'LeaderOf', where: {title: 'Beta'}} {model: Project, as: 'LeaderOf', where: {title: 'Beta'}}
] ]
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
expect(users.length).to.equal(1); expect(users.length).to.equal(1);
expect(users[0].LeaderOf).to.be.ok; expect(users[0].LeaderOf).to.be.ok;
expect(users[0].LeaderOf.title).to.equal('Beta'); expect(users[0].LeaderOf.title).to.equal('Beta');
done();
}); });
}); });
}); });
}); });
it('should be possible to extend the on clause with a where option on a hasMany include with a through model', function(done) { it('should be possible to extend the on clause with a where option on a hasMany include with a through model', function() {
var Product = this.sequelize.define('Product', { var Product = this.sequelize.define('Product', {
title: DataTypes.STRING title: DataTypes.STRING
}) })
...@@ -1384,60 +1227,45 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1384,60 +1227,45 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Product.hasMany(Tag, {through: ProductTag}); Product.hasMany(Tag, {through: ProductTag});
Tag.hasMany(Product, {through: ProductTag}); Tag.hasMany(Product, {through: ProductTag});
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
products: function(callback) { products: Product.bulkCreate([
Product.bulkCreate([ {title: 'Chair'},
{title: 'Chair'}, {title: 'Desk'},
{title: 'Desk'}, {title: 'Dress'}
{title: 'Dress'} ]).then(function() {
]).done(function() { return Product.findAll();
Product.findAll().done(callback); }),
}); tags: Tag.bulkCreate([
}, {name: 'A'},
tags: function(callback) { {name: 'B'},
Tag.bulkCreate([ {name: 'C'}
{name: 'A'}, ]).then(function() {
{name: 'B'}, return Tag.findAll();
{name: 'C'} })
]).done(function() { }).then(function (results) {
Tag.findAll().done(callback); return Promise.join(
}); results.products[0].addTag(results.tags[0], {priority: 1}),
}, results.products[0].addTag(results.tags[1], {priority: 2}),
productTags: ['products', 'tags', function(callback, results) { results.products[1].addTag(results.tags[1], {priority: 1}),
var chainer = new Sequelize.Utils.QueryChainer(); results.products[2].addTag(results.tags[0], {priority: 3}),
results.products[2].addTag(results.tags[1], {priority: 1}),
chainer.add(results.products[0].addTag(results.tags[0], {priority: 1})); results.products[2].addTag(results.tags[2], {priority: 2})
chainer.add(results.products[0].addTag(results.tags[1], {priority: 2})); );
}).then(function () {
chainer.add(results.products[1].addTag(results.tags[1], {priority: 1}));
chainer.add(results.products[2].addTag(results.tags[0], {priority: 3}));
chainer.add(results.products[2].addTag(results.tags[1], {priority: 1}));
chainer.add(results.products[2].addTag(results.tags[2], {priority: 2}));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
Product.findAll({ Product.findAll({
include: [ include: [
{model: Tag, where: {name: 'C'}} {model: Tag, where: {name: 'C'}}
] ]
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
expect(products.length).to.equal(1); expect(products.length).to.equal(1);
expect(products[0].Tags.length).to.equal(1); expect(products[0].Tags.length).to.equal(1);
done();
}); });
}); });
}); });
}); });
it('should be possible to extend the on clause with a where option on nested includes', function(done) { it('should be possible to extend the on clause with a where option on nested includes', function() {
var User = this.sequelize.define('User', { var User = this.sequelize.define('User', {
name: DataTypes.STRING name: DataTypes.STRING
}) })
...@@ -1487,138 +1315,105 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1487,138 +1315,105 @@ describe(Support.getTestDialectTeaser('Include'), function() {
GroupMember.belongsTo(Group); GroupMember.belongsTo(Group);
Group.hasMany(GroupMember, {as: 'Memberships'}); Group.hasMany(GroupMember, {as: 'Memberships'});
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
var count = 4 return Promise.props({
, i = -1; groups: Group.bulkCreate([
{name: 'Developers'},
async.auto({ {name: 'Designers'}
groups: function(callback) { ]).then(function() {
Group.bulkCreate([ return Group.findAll();
{name: 'Developers'}, }),
{name: 'Designers'} ranks: Rank.bulkCreate([
]).done(function() { {name: 'Admin', canInvite: 1, canRemove: 1},
Group.findAll().done(callback); {name: 'Member', canInvite: 1, canRemove: 0}
}); ]).then(function() {
}, return Rank.findAll();
ranks: function(callback) { }),
Rank.bulkCreate([ tags: Tag.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1}, {name: 'A'},
{name: 'Member', canInvite: 1, canRemove: 0} {name: 'B'},
]).done(function() { {name: 'C'}
Rank.findAll().done(callback); ]).then(function() {
return Tag.findAll();
})
}).then(function (results) {
var groups = results.groups
, ranks = results.ranks
, tags = results.tags;
return Promise.map([0, 1, 2, 3, 4], function (i) {
return Promise.props({
user: User.create({name: 'FooBarzz'}),
products: Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'}
]).then(function() {
return Product.findAll();
})
}).then(function (results) {
return Promise.join(
GroupMember.bulkCreate([
{UserId: results.user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{UserId: results.user.id, GroupId: groups[1].id, RankId: ranks[1].id}
]),
results.user.setProducts([
results.products[(i * 2) + 0],
results.products[(i * 2) + 1]
]),
Promise.join(
results.products[(i * 2) + 0].setTags([
tags[0],
tags[2]
]),
results.products[(i * 2) + 1].setTags([
tags[1]
]),
results.products[(i * 2) + 0].setCategory(tags[1])
),
Price.bulkCreate([
{ProductId: results.products[(i * 2) + 0].id, value: 5},
{ProductId: results.products[(i * 2) + 0].id, value: 10},
{ProductId: results.products[(i * 2) + 1].id, value: 5},
{ProductId: results.products[(i * 2) + 1].id, value: 10},
{ProductId: results.products[(i * 2) + 1].id, value: 15},
{ProductId: results.products[(i * 2) + 1].id, value: 20}
])
);
}); });
}, });
tags: function(callback) { }).then(function () {
Tag.bulkCreate([ return User.findAll({
{name: 'A'}, include: [
{name: 'B'}, {model: GroupMember, as: 'Memberships', include: [
{name: 'C'} Group,
]).done(function() { {model: Rank, where: {name: 'Admin'}}
Tag.findAll().done(callback); ]},
{model: Product, include: [
Tag,
{model: Tag, as: 'Category'},
{model: Price, where: {
value: {
gt: 15
}
}}
]}
],
order: [
['id', 'ASC']
]
}).then(function(users) {
users.forEach(function(user) {
expect(user.Memberships.length).to.equal(1);
expect(user.Memberships[0].Rank.name).to.equal('Admin');
expect(user.Products.length).to.equal(1);
expect(user.Products[0].Prices.length).to.equal(1);
}); });
}, });
loop: ['groups', 'ranks', 'tags', function(done, results) { });
var groups = results.groups
, ranks = results.ranks
, tags = results.tags;
async.whilst(
function() { return i < count; },
function(callback) {
i++;
async.auto({
user: function(callback) {
User.create({name: 'FooBarzz'}).done(callback);
},
memberships: ['user', function(callback, results) {
GroupMember.bulkCreate([
{UserId: results.user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{UserId: results.user.id, GroupId: groups[1].id, RankId: ranks[1].id}
]).done(callback);
}],
products: function(callback) {
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'}
]).done(function() {
Product.findAll().done(callback);
});
},
userProducts: ['user', 'products', function(callback, results) {
results.user.setProducts([
results.products[(i * 2) + 0],
results.products[(i * 2) + 1]
]).done(callback);
}],
productTags: ['products', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.products[(i * 2) + 0].setTags([
tags[0],
tags[2]
]));
chainer.add(results.products[(i * 2) + 1].setTags([
tags[1]
]));
chainer.add(results.products[(i * 2) + 0].setCategory(tags[1]));
chainer.run().done(callback);
}],
prices: ['products', function(callback, results) {
Price.bulkCreate([
{ProductId: results.products[(i * 2) + 0].id, value: 5},
{ProductId: results.products[(i * 2) + 0].id, value: 10},
{ProductId: results.products[(i * 2) + 1].id, value: 5},
{ProductId: results.products[(i * 2) + 1].id, value: 10},
{ProductId: results.products[(i * 2) + 1].id, value: 15},
{ProductId: results.products[(i * 2) + 1].id, value: 20}
]).done(callback);
}]
}, callback);
},
function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [
{model: GroupMember, as: 'Memberships', include: [
Group,
{model: Rank, where: {name: 'Admin'}}
]},
{model: Product, include: [
Tag,
{model: Tag, as: 'Category'},
{model: Price, where: {
value: {
gt: 15
}
}}
]}
],
order: [
['id', 'ASC']
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
users.forEach(function(user) {
expect(user.Memberships.length).to.equal(1);
expect(user.Memberships[0].Rank.name).to.equal('Admin');
expect(user.Products.length).to.equal(1);
expect(user.Products[0].Prices.length).to.equal(1);
});
done();
});
}
);
}]
}, done);
}); });
}); });
it('should be possible to use limit and a where with a belongsTo include', function(done) { it('should be possible to use limit and a where with a belongsTo include', function() {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', { , Group = this.sequelize.define('Group', {
name: DataTypes.STRING name: DataTypes.STRING
...@@ -1626,54 +1421,45 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1626,54 +1421,45 @@ describe(Support.getTestDialectTeaser('Include'), function() {
User.belongsTo(Group); User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
groups: function(callback) { groups: Group.bulkCreate([
Group.bulkCreate([ {name: 'A'},
{name: 'A'}, {name: 'B'}
{name: 'B'} ]).then(function() {
]).done(function() { return Group.findAll();
Group.findAll().done(callback); }),
}); users: User.bulkCreate([{}, {}, {}, {}]).then(function() {
}, return User.findAll();
users: function(callback) { }),
User.bulkCreate([{}, {}, {}, {}]).done(function() { }).then(function (results) {
User.findAll().done(callback); return Promise.join(
}); results.users[0].setGroup(results.groups[0]),
}, results.users[1].setGroup(results.groups[0]),
userGroups: ['users', 'groups', function(callback, results) { results.users[2].setGroup(results.groups[0]),
var chainer = new Sequelize.Utils.QueryChainer(); results.users[3].setGroup(results.groups[1])
chainer.add(results.users[0].setGroup(results.groups[0])); );
chainer.add(results.users[1].setGroup(results.groups[0])); }).then(function () {
chainer.add(results.users[2].setGroup(results.groups[0])); return User.findAll({
chainer.add(results.users[3].setGroup(results.groups[1]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [ include: [
{model: Group, where: {name: 'A'}} {model: Group, where: {name: 'A'}}
], ],
limit: 2 limit: 2
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
expect(users.length).to.equal(2); expect(users.length).to.equal(2);
users.forEach(function(user) { users.forEach(function(user) {
expect(user.Group.name).to.equal('A'); expect(user.Group.name).to.equal('A');
}); });
done();
}); });
}); });
}); });
}); });
it('should be possible use limit, attributes and a where on a belongsTo with additional hasMany includes', function(done) { it('should be possible use limit, attributes and a where on a belongsTo with additional hasMany includes', function() {
var self = this; var self = this;
this.fixtureA(function() { return this.fixtureA().then(function () {
self.models.Product.findAll({ return self.models.Product.findAll({
attributes: ['id', 'title'], attributes: ['id', 'title'],
include: [ include: [
{model: self.models.Company, where: {name: 'NYSE'}}, {model: self.models.Company, where: {name: 'NYSE'}},
...@@ -1684,8 +1470,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1684,8 +1470,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
order: [ order: [
[self.sequelize.col(self.models.Product.name + '.id'), 'ASC'] [self.sequelize.col(self.models.Product.name + '.id'), 'ASC']
] ]
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
expect(products.length).to.equal(3); expect(products.length).to.equal(3);
products.forEach(function(product) { products.forEach(function(product) {
...@@ -1693,7 +1478,6 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1693,7 +1478,6 @@ describe(Support.getTestDialectTeaser('Include'), function() {
expect(product.Tags.length).to.be.ok; expect(product.Tags.length).to.be.ok;
expect(product.Prices.length).to.be.ok; expect(product.Prices.length).to.be.ok;
}); });
done();
}); });
}); });
}); });
...@@ -1729,33 +1513,30 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1729,33 +1513,30 @@ describe(Support.getTestDialectTeaser('Include'), function() {
}); });
}); });
it('should be possible to turn off the attributes for the through table', function(done) { it('should be possible to turn off the attributes for the through table', function() {
var self = this; var self = this;
this.fixtureA(function() { return this.fixtureA().then(function () {
self.models.Product.findAll({ return self.models.Product.findAll({
attributes: ['title'], attributes: ['title'],
include: [ include: [
{model: self.models.Tag, through: {attributes: []}, required: true} {model: self.models.Tag, through: {attributes: []}, required: true}
] ]
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
products.forEach(function(product) { products.forEach(function(product) {
expect(product.Tags.length).to.be.ok; expect(product.Tags.length).to.be.ok;
product.Tags.forEach(function(tag) { product.Tags.forEach(function(tag) {
expect(tag.get().productTags).not.to.be.ok; expect(tag.get().productTags).not.to.be.ok;
}); });
}); });
done();
}); });
}); });
}); });
it('should be possible to select on columns inside a through table', function(done) { it('should be possible to select on columns inside a through table', function() {
var self = this; var self = this;
this.fixtureA(function() { return this.fixtureA().then(function () {
self.models.Product.findAll({ return self.models.Product.findAll({
attributes: ['title'], attributes: ['title'],
include: [ include: [
{ {
...@@ -1768,19 +1549,16 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1768,19 +1549,16 @@ describe(Support.getTestDialectTeaser('Include'), function() {
required: true required: true
} }
] ]
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
expect(products).have.length(1); expect(products).have.length(1);
done();
}); });
}); });
}); });
it('should be possible to select on columns inside a through table and a limit', function(done) { it('should be possible to select on columns inside a through table and a limit', function() {
var self = this; var self = this;
this.fixtureA(function() { return this.fixtureA().then(function () {
self.models.Product.findAll({ return self.models.Product.findAll({
attributes: ['title'], attributes: ['title'],
include: [ include: [
{ {
...@@ -1794,17 +1572,14 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1794,17 +1572,14 @@ describe(Support.getTestDialectTeaser('Include'), function() {
} }
], ],
limit: 5 limit: 5
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
expect(products).have.length(1); expect(products).have.length(1);
done();
}); });
}); });
}); });
// Test case by @eshell // Test case by @eshell
it('should be possible not to include the main id in the attributes', function(done) { it('should be possible not to include the main id in the attributes', function() {
var Member = this.sequelize.define('Member', { var Member = this.sequelize.define('Member', {
id: { id: {
type: Sequelize.BIGINT, type: Sequelize.BIGINT,
...@@ -1838,9 +1613,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1838,9 +1613,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Album.belongsTo(Member); Album.belongsTo(Member);
Member.hasMany(Album); Member.hasMany(Album);
this.sequelize.sync({force: true}).done(function(err) { return this.sequelize.sync({force: true}).then(function () {
expect(err).not.to.be.ok;
var members = [] var members = []
, albums = [] , albums = []
, memberCount = 20; , memberCount = 20;
...@@ -1857,10 +1630,8 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1857,10 +1630,8 @@ describe(Support.getTestDialectTeaser('Include'), function() {
}); });
} }
Member.bulkCreate(members).done(function(err) { return Member.bulkCreate(members).then(function () {
expect(err).not.to.be.ok; return Album.bulkCreate(albums).then(function () {
Album.bulkCreate(albums).done(function(err) {
expect(err).not.to.be.ok;
Member.findAll({ Member.findAll({
attributes: ['email'], attributes: ['email'],
...@@ -1869,26 +1640,22 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1869,26 +1640,22 @@ describe(Support.getTestDialectTeaser('Include'), function() {
model: Album model: Album
} }
] ]
}).done(function(err, members) { }).then(function(members) {
expect(err).not.to.be.ok;
expect(members.length).to.equal(20); expect(members.length).to.equal(20);
members.forEach(function(member) { members.forEach(function(member) {
expect(member.get('id')).not.to.be.ok; expect(member.get('id')).not.to.be.ok;
expect(member.Albums.length).to.equal(1); expect(member.Albums.length).to.equal(1);
}); });
done();
}); });
}); });
}); });
}); });
}); });
it('should be possible to use limit and a where on a hasMany with additional includes', function(done) { it('should be possible to use limit and a where on a hasMany with additional includes', function() {
var self = this; var self = this;
this.fixtureA(function() { return this.fixtureA().then(function () {
self.models.Product.findAll({ return self.models.Product.findAll({
include: [ include: [
{model: self.models.Company}, {model: self.models.Company},
{model: self.models.Tag}, {model: self.models.Tag},
...@@ -1900,8 +1667,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1900,8 +1667,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
order: [ order: [
['id', 'ASC'] ['id', 'ASC']
] ]
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
expect(products.length).to.equal(6); expect(products.length).to.equal(6);
products.forEach(function(product) { products.forEach(function(product) {
...@@ -1912,15 +1678,14 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1912,15 +1678,14 @@ describe(Support.getTestDialectTeaser('Include'), function() {
expect(price.value).to.be.above(5); expect(price.value).to.be.above(5);
}); });
}); });
done();
}); });
}); });
}); });
it('should be possible to use limit and a where on a hasMany with a through model with additional includes', function(done) { it('should be possible to use limit and a where on a hasMany with a through model with additional includes', function() {
var self = this; var self = this;
this.fixtureA(function() { return this.fixtureA().then(function () {
self.models.Product.findAll({ return self.models.Product.findAll({
include: [ include: [
{model: self.models.Company}, {model: self.models.Company},
{model: self.models.Tag, where: {name: ['A', 'B', 'C']}}, {model: self.models.Tag, where: {name: ['A', 'B', 'C']}},
...@@ -1930,8 +1695,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1930,8 +1695,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
order: [ order: [
['id', 'ASC'] ['id', 'ASC']
] ]
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
expect(products.length).to.equal(10); expect(products.length).to.equal(10);
products.forEach(function(product) { products.forEach(function(product) {
...@@ -1942,12 +1706,11 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1942,12 +1706,11 @@ describe(Support.getTestDialectTeaser('Include'), function() {
expect(['A', 'B', 'C']).to.include(tag.name); expect(['A', 'B', 'C']).to.include(tag.name);
}); });
}); });
done();
}); });
}); });
}); });
it('should support including date fields, with the correct timeszone', function(done) { it('should support including date fields, with the correct timeszone', function() {
var User = this.sequelize.define('user', { var User = this.sequelize.define('user', {
dateField: Sequelize.DATE dateField: Sequelize.DATE
}, {timestamps: false}) }, {timestamps: false})
...@@ -1958,20 +1721,18 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1958,20 +1721,18 @@ describe(Support.getTestDialectTeaser('Include'), function() {
User.hasMany(Group); User.hasMany(Group);
Group.hasMany(User); Group.hasMany(User);
this.sequelize.sync().success(function() { return this.sequelize.sync().then(function() {
User.create({ dateField: Date.UTC(2014, 1, 20) }).success(function(user) { return User.create({ dateField: Date.UTC(2014, 1, 20) }).then(function(user) {
Group.create({ dateField: Date.UTC(2014, 1, 20) }).success(function(group) { return Group.create({ dateField: Date.UTC(2014, 1, 20) }).then(function(group) {
user.addGroup(group).success(function() { return user.addGroup(group).then(function() {
User.findAll({ return User.findAll({
where: { where: {
id: user.id id: user.id
}, },
include: [Group] include: [Group]
}).success(function(users) { }).then(function(users) {
expect(users[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20)); expect(users[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20));
expect(users[0].groups[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20)); expect(users[0].groups[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20));
done();
}); });
}); });
}); });
......
...@@ -6,7 +6,8 @@ var chai = require('chai') ...@@ -6,7 +6,8 @@ var chai = require('chai')
, Support = require(__dirname + '/../support') , Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types') , DataTypes = require(__dirname + '/../../../lib/data-types')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, async = require('async'); , async = require('async')
, Promise = Sequelize.Promise;
chai.use(datetime); chai.use(datetime);
chai.config.includeStack = true; chai.config.includeStack = true;
...@@ -20,9 +21,9 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -20,9 +21,9 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
this.timeout(30000); this.timeout(30000);
beforeEach(function() { beforeEach(function() {
var self = this; var self = this;
this.fixtureA = function(done) { this.fixtureA = function() {
self.sequelize.dropAllSchemas().success(function() { return self.sequelize.dropAllSchemas().then(function() {
self.sequelize.createSchema('account').success(function() { return self.sequelize.createSchema('account').then(function() {
var AccUser = self.sequelize.define('AccUser', {}, {schema: 'account'}) var AccUser = self.sequelize.define('AccUser', {}, {schema: 'account'})
, Company = self.sequelize.define('Company', { , Company = self.sequelize.define('Company', {
name: DataTypes.STRING name: DataTypes.STRING
...@@ -90,58 +91,49 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -90,58 +91,49 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
GroupMember.belongsTo(Group); GroupMember.belongsTo(Group);
Group.hasMany(GroupMember, {as: 'Memberships'}); Group.hasMany(GroupMember, {as: 'Memberships'});
self.sequelize.sync({force: true}).done(function() { return self.sequelize.sync({force: true}).then(function() {
var count = 4 return Promise.props({
, i = -1; groups: Group.bulkCreate([
{name: 'Developers'},
async.auto({ {name: 'Designers'},
groups: function(callback) { {name: 'Managers'}
Group.bulkCreate([ ]).then(function() {
{name: 'Developers'}, return Group.findAll();
{name: 'Designers'}, }),
{name: 'Managers'} companies: Company.bulkCreate([
]).done(function() { {name: 'Sequelize'},
Group.findAll().done(callback); {name: 'Coca Cola'},
}); {name: 'Bonanza'},
}, {name: 'NYSE'},
companies: function(callback) { {name: 'Coshopr'}
Company.bulkCreate([ ]).then(function() {
{name: 'Sequelize'}, return Company.findAll();
{name: 'Coca Cola'}, }),
{name: 'Bonanza'}, ranks: Rank.bulkCreate([
{name: 'NYSE'}, {name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1},
{name: 'Coshopr'} {name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1},
]).done(function(err) { {name: 'Member', canInvite: 1, canRemove: 0, canPost: 0}
if (err) return callback(err); ]).then(function() {
Company.findAll().done(callback); return Rank.findAll();
}); }),
}, tags: Tag.bulkCreate([
ranks: function(callback) { {name: 'A'},
Rank.bulkCreate([ {name: 'B'},
{name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1}, {name: 'C'},
{name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1}, {name: 'D'},
{name: 'Member', canInvite: 1, canRemove: 0, canPost: 0} {name: 'E'}
]).done(function() { ]).then(function() {
Rank.findAll().done(callback); return Tag.findAll();
}); })
}, }).then(function (results) {
tags: function(callback) { var count = 4
Tag.bulkCreate([ , i = -1
{name: 'A'}, , groups = results.groups
{name: 'B'}, , ranks = results.ranks
{name: 'C'}, , tags = results.tags
{name: 'D'}, , companies = results.companies;
{name: 'E'}
]).done(function() {
Tag.findAll().done(callback);
});
},
loop: ['groups', 'ranks', 'tags', 'companies', function(done, results) {
var groups = results.groups
, ranks = results.ranks
, tags = results.tags
, companies = results.companies;
return new Promise(function (resolve, reject) {
async.whilst( async.whilst(
function() { return i < count; }, function() { return i < count; },
function(callback) { function(callback) {
...@@ -229,13 +221,13 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -229,13 +221,13 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
}, callback); }, callback);
}, },
function(err) { function(err) {
expect(err).not.to.be.ok; if (err) return reject(err);
done(); resolve();
} }
); );
}] });
}, done.bind(this)); });
}).error(done); });
}); });
}); });
}; };
...@@ -1339,7 +1331,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1339,7 +1331,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
}); });
}); });
it('should be possible to use limit and a where with a belongsTo include', function(done) { it('should be possible to use limit and a where with a belongsTo include', function() {
var User = this.sequelize.define('User', {}, {schema: 'account'}) var User = this.sequelize.define('User', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', { , Group = this.sequelize.define('Group', {
name: DataTypes.STRING name: DataTypes.STRING
...@@ -1347,54 +1339,45 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1347,54 +1339,45 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group); User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() { return this.sequelize.sync({force: true}).then(function() {
async.auto({ return Promise.props({
groups: function(callback) { groups: Group.bulkCreate([
Group.bulkCreate([ {name: 'A'},
{name: 'A'}, {name: 'B'}
{name: 'B'} ]).then(function() {
]).done(function() { return Group.findAll();
Group.findAll().done(callback); }),
}); users: User.bulkCreate([{}, {}, {}, {}]).then(function() {
}, return User.findAll();
users: function(callback) { })
User.bulkCreate([{}, {}, {}, {}]).done(function() { }).then(function (results) {
User.findAll().done(callback); return Promise.join(
}); results.users[1].setGroup(results.groups[0]),
}, results.users[2].setGroup(results.groups[0]),
userGroups: ['users', 'groups', function(callback, results) { results.users[3].setGroup(results.groups[1]),
var chainer = new Sequelize.Utils.QueryChainer(); results.users[0].setGroup(results.groups[0])
chainer.add(results.users[0].setGroup(results.groups[0])); );
chainer.add(results.users[1].setGroup(results.groups[0])); }).then(function () {
chainer.add(results.users[2].setGroup(results.groups[0])); return User.findAll({
chainer.add(results.users[3].setGroup(results.groups[1]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
include: [ include: [
{model: Group, where: {name: 'A'}} {model: Group, where: {name: 'A'}}
], ],
limit: 2 limit: 2
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
expect(users.length).to.equal(2); expect(users.length).to.equal(2);
users.forEach(function(user) { users.forEach(function(user) {
expect(user.Group.name).to.equal('A'); expect(user.Group.name).to.equal('A');
}); });
done();
}); });
}); });
}); });
}); });
it('should be possible use limit, attributes and a where on a belongsTo with additional hasMany includes', function(done) { it('should be possible use limit, attributes and a where on a belongsTo with additional hasMany includes', function() {
var self = this; var self = this;
this.fixtureA(function() { return this.fixtureA().then(function () {
self.models.Product.findAll({ return self.models.Product.findAll({
attributes: ['title'], attributes: ['title'],
include: [ include: [
{model: self.models.Company, where: {name: 'NYSE'}}, {model: self.models.Company, where: {name: 'NYSE'}},
...@@ -1405,8 +1388,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1405,8 +1388,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
order: [ order: [
['id', 'ASC'] ['id', 'ASC']
] ]
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
expect(products.length).to.equal(3); expect(products.length).to.equal(3);
products.forEach(function(product) { products.forEach(function(product) {
...@@ -1414,15 +1396,14 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1414,15 +1396,14 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
expect(product.Tags.length).to.be.ok; expect(product.Tags.length).to.be.ok;
expect(product.Prices.length).to.be.ok; expect(product.Prices.length).to.be.ok;
}); });
done();
}); });
}); });
}); });
it('should be possible to use limit and a where on a hasMany with additional includes', function(done) { it('should be possible to use limit and a where on a hasMany with additional includes', function() {
var self = this; var self = this;
this.fixtureA(function() { return this.fixtureA().then(function () {
self.models.Product.findAll({ return self.models.Product.findAll({
include: [ include: [
{model: self.models.Company}, {model: self.models.Company},
{model: self.models.Tag}, {model: self.models.Tag},
...@@ -1434,8 +1415,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1434,8 +1415,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
order: [ order: [
['id', 'ASC'] ['id', 'ASC']
] ]
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
expect(products.length).to.equal(6); expect(products.length).to.equal(6);
products.forEach(function(product) { products.forEach(function(product) {
...@@ -1446,15 +1426,14 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1446,15 +1426,14 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
expect(price.value).to.be.above(5); expect(price.value).to.be.above(5);
}); });
}); });
done();
}); });
}); });
}); });
it('should be possible to use limit and a where on a hasMany with a through model with additional includes', function(done) { it('should be possible to use limit and a where on a hasMany with a through model with additional includes', function() {
var self = this; var self = this;
this.fixtureA(function() { return this.fixtureA().then(function () {
self.models.Product.findAll({ return self.models.Product.findAll({
include: [ include: [
{model: self.models.Company}, {model: self.models.Company},
{model: self.models.Tag, where: {name: ['A', 'B', 'C']}}, {model: self.models.Tag, where: {name: ['A', 'B', 'C']}},
...@@ -1464,8 +1443,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1464,8 +1443,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
order: [ order: [
['id', 'ASC'] ['id', 'ASC']
] ]
}).done(function(err, products) { }).then(function(products) {
expect(err).not.to.be.ok;
expect(products.length).to.equal(10); expect(products.length).to.equal(10);
products.forEach(function(product) { products.forEach(function(product) {
...@@ -1476,12 +1454,11 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1476,12 +1454,11 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
expect(['A', 'B', 'C']).to.include(tag.name); expect(['A', 'B', 'C']).to.include(tag.name);
}); });
}); });
done();
}); });
}); });
}); });
it.skip('should support including date fields, with the correct timeszone', function(done) { it.skip('should support including date fields, with the correct timeszone', function() {
var User = this.sequelize.define('user', { var User = this.sequelize.define('user', {
dateField: Sequelize.DATE dateField: Sequelize.DATE
}, {timestamps: false, schema: 'account'}) }, {timestamps: false, schema: 'account'})
...@@ -1492,20 +1469,18 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1492,20 +1469,18 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.hasMany(Group); User.hasMany(Group);
Group.hasMany(User); Group.hasMany(User);
this.sequelize.sync().success(function() { return this.sequelize.sync().then(function() {
User.create({ dateField: Date.UTC(2014, 1, 20) }).success(function(user) { return User.create({ dateField: Date.UTC(2014, 1, 20) }).then(function(user) {
Group.create({ dateField: Date.UTC(2014, 1, 20) }).success(function(group) { return Group.create({ dateField: Date.UTC(2014, 1, 20) }).then(function(group) {
user.addGroup(group).success(function() { return user.addGroup(group).then(function() {
User.findAll({ return User.findAll({
where: { where: {
id: user.id id: user.id
}, },
include: [Group] include: [Group]
}).success(function(users) { }).then(function(users) {
expect(users[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20)); expect(users[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20));
expect(users[0].groups[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20)); expect(users[0].groups[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20));
done();
}); });
}); });
}); });
...@@ -1583,8 +1558,6 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1583,8 +1558,6 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
model: ResumeModel, model: ResumeModel,
as: 'Resume' as: 'Resume'
}] }]
}).on('sql', function(sql) {
console.log(sql);
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!