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

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([
async.auto({
groups: function(callback) {
Group.bulkCreate([
{name: 'Developers'}, {name: 'Developers'},
{name: 'Designers'}, {name: 'Designers'},
{name: 'Managers'} {name: 'Managers'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }),
}, companies: Company.bulkCreate([
companies: function(callback) {
Company.bulkCreate([
{name: 'Sequelize'}, {name: 'Sequelize'},
{name: 'Coca Cola'}, {name: 'Coca Cola'},
{name: 'Bonanza'}, {name: 'Bonanza'},
{name: 'NYSE'}, {name: 'NYSE'},
{name: 'Coshopr'} {name: 'Coshopr'}
]).done(function(err) { ]).then(function() {
if (err) return callback(err); return Company.findAll();
Company.findAll().done(callback); }),
}); ranks: Rank.bulkCreate([
},
ranks: function(callback) {
Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1}, {name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1},
{name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1}, {name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1},
{name: 'Member', canInvite: 1, canRemove: 0, canPost: 0} {name: 'Member', canInvite: 1, canRemove: 0, canPost: 0}
]).done(function(err) { ]).then(function() {
Rank.findAll().done(callback); return Rank.findAll();
}); }),
}, tags: Tag.bulkCreate([
tags: function(callback) {
Tag.bulkCreate([
{name: 'A'}, {name: 'A'},
{name: 'B'}, {name: 'B'},
{name: 'C'}, {name: 'C'},
{name: 'D'}, {name: 'D'},
{name: 'E'} {name: 'E'}
]).done(function() { ]).then(function() {
Tag.findAll().done(callback); return Tag.findAll();
}); })
}, }).then(function (results) {
loop: ['groups', 'ranks', 'tags', 'companies', function(done, results) { var count = 4
var groups = results.groups , i = -1
, groups = results.groups
, ranks = results.ranks , ranks = results.ranks
, tags = results.tags , tags = results.tags
, companies = results.companies; , 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,7 +405,8 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -414,7 +405,8 @@ 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() {
return new Promise(function (resolve, reject) {
var count = 4 var count = 4
, i = -1; , i = -1;
...@@ -423,26 +415,26 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -423,26 +415,26 @@ describe(Support.getTestDialectTeaser('Include'), function() {
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
...@@ -456,41 +448,39 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -456,41 +448,39 @@ describe(Support.getTestDialectTeaser('Include'), function() {
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) { userProducts: ['user', 'products', function(callback, results) {
results.user.setProducts([ results.user.setProducts([
results.products[(i * 2) + 0], results.products[(i * 2) + 0],
results.products[(i * 2) + 1] results.products[(i * 2) + 1]
]).done(callback); ]).nodeify(callback);
}], }],
productTags: ['products', function(callback, results) { productTags: ['products', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer(); return Promise.join(
results.products[(i * 2) + 0].setTags([
chainer.add(results.products[(i * 2) + 0].setTags([
tags[0], tags[0],
tags[2] tags[2]
])); ]),
chainer.add(results.products[(i * 2) + 1].setTags([ results.products[(i * 2) + 1].setTags([
tags[1] tags[1]
])); ]),
chainer.add(results.products[(i * 2) + 0].setCategory(tags[1])); results.products[(i * 2) + 0].setCategory(tags[1])
).nodeify(callback);
chainer.run().done(callback);
}], }],
prices: ['products', function(callback, results) { prices: ['products', function(callback, results) {
Price.bulkCreate([ Price.bulkCreate([
...@@ -500,12 +490,12 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -500,12 +490,12 @@ describe(Support.getTestDialectTeaser('Include'), function() {
{ProductId: results.products[(i * 2) + 1].id, value: 10}, {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: 15},
{ProductId: results.products[(i * 2) + 1].id, value: 20} {ProductId: results.products[(i * 2) + 1].id, value: 20}
]).done(callback); ]).nodeify(callback);
}] }]
}, callback); }, callback);
}, },
function(err) { function(err) {
expect(err).not.to.be.ok;
User.findAll({ User.findAll({
include: [ include: [
...@@ -522,8 +512,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -522,8 +512,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
order: [ order: [
['id', 'ASC'] ['id', 'ASC']
] ]
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
users.forEach(function(user) { users.forEach(function(user) {
user.Memberships.sort(sortById); user.Memberships.sort(sortById);
...@@ -542,18 +531,20 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -542,18 +531,20 @@ describe(Support.getTestDialectTeaser('Include'), function() {
expect(user.Products[0].Prices.length).to.equal(2); expect(user.Products[0].Prices.length).to.equal(2);
expect(user.Products[1].Prices.length).to.equal(4); expect(user.Products[1].Prices.length).to.equal(4);
done();
});
}); });
}).nodeify(done);
} }
); );
}] }]
}, done); }, function (err) {
if (err) return reject(err);
resolve();
});
});
}); });
}); });
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,19 +562,8 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -571,19 +562,8 @@ 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,
D,
E,
F,
G,
H
];
this.sequelize.sync().done(function() {
async.auto({
as: function(callback) {
A.bulkCreate([ A.bulkCreate([
{}, {},
{}, {},
...@@ -593,39 +573,40 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -593,39 +573,40 @@ describe(Support.getTestDialectTeaser('Include'), function() {
{}, {},
{}, {},
{} {}
]).done(function() { ]).then(function() {
A.findAll().done(callback); return A.findAll();
}); }),
}, (function (singles) {
singleChain: function(callback) { var promise = Promise.resolve()
var previousInstance; , previousInstance
, b;
async.eachSeries(singles, function(model, callback) {
model.create({}).done(function(err, instance) { singles.forEach(function (model) {
promise = promise.then(function () {
return model.create({}).then(function (instance) {
if (previousInstance) { if (previousInstance) {
previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).done(function() { return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() {
previousInstance = instance; previousInstance = instance;
callback();
}); });
} else { } else {
previousInstance = b = instance; 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,19 +655,8 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -676,19 +655,8 @@ 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,
D,
E,
F,
G,
H
];
this.sequelize.sync().done(function() {
async.auto({
as: function(callback) {
A.bulkCreate([ A.bulkCreate([
{}, {},
{}, {},
...@@ -698,44 +666,46 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -698,44 +666,46 @@ describe(Support.getTestDialectTeaser('Include'), function() {
{}, {},
{}, {},
{} {}
]).done(function() { ]).then(function() {
A.findAll().done(callback); return A.findAll();
}); }),
}, (function (singles) {
singleChain: function(callback) { var promise = Promise.resolve()
var previousInstance; , previousInstance
, b;
async.eachSeries(singles, function(model, callback) { singles.forEach(function (model) {
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) {
promise = promise.then(function () {
return model.create(values).then(function (instance) {
if (previousInstance) { if (previousInstance) {
previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).done(function() { return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() {
previousInstance = instance; previousInstance = instance;
callback();
}); });
} else { } else {
previousInstance = b = instance; 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: [
...@@ -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,35 +743,27 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -775,35 +743,27 @@ 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([
},
items: function(callback) {
Item.bulkCreate([
{'test': 'abc'}, {'test': 'abc'},
{'test': 'def'}, {'test': 'def'},
{'test': 'ghi'}, {'test': 'ghi'},
{'test': 'jkl'} {'test': 'jkl'}
]).done(function() { ]).then(function() {
Item.findAll({order: ['id']}).done(callback); return Item.findAll({order: ['id']});
}); }),
}, orders: Order.bulkCreate([
orders: function(callback) {
Order.bulkCreate([
{'position': 2}, {'position': 2},
{'position': 3}, {'position': 3},
{'position': 1} {'position': 1}
]).done(function() { ]).then(function() {
Order.findAll({order: ['id']}).done(callback); return Order.findAll({order: ['id']});
}); })
}, }).then(function (results) {
associate: ['users', 'items', 'orders', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
var user1 = results.users[0]; var user1 = results.users[0];
var user2 = results.users[1]; var user2 = results.users[1];
var user3 = results.users[2]; var user3 = results.users[2];
...@@ -817,22 +777,19 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -817,22 +777,19 @@ describe(Support.getTestDialectTeaser('Include'), function() {
var order2 = results.orders[1]; var order2 = results.orders[1];
var order3 = results.orders[2]; var order3 = results.orders[2];
chainer.add(user1.setItemA(item1)); return Promise.join(
chainer.add(user1.setItemB(item2)); user1.setItemA(item1),
chainer.add(user1.setOrder(order3)); user1.setItemB(item2),
user1.setOrder(order3),
chainer.add(user2.setItemA(item3)); user2.setItemA(item3),
chainer.add(user2.setItemB(item4)); user2.setItemB(item4),
chainer.add(user2.setOrder(order2)); user2.setOrder(order2),
user3.setItemA(item1),
chainer.add(user3.setItemA(item1)); user3.setItemB(item4),
chainer.add(user3.setItemB(item4)); user3.setOrder(order1)
chainer.add(user3.setOrder(order1)); );
}).then(function () {
chainer.run().done(callback); return User.findAll({
}]
}, function() {
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'}
]).done(function() { ]).then(function() {
Product.findAll().done(callback); return Product.findAll();
}); }),
}, tags: Tag.bulkCreate([
tags: function(callback) {
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();
}); })
}, }).then(function (results) {
productTags: ['products', 'tags', function(callback, results) { return Promise.join(
var chainer = new Sequelize.Utils.QueryChainer(); results.products[0].addTag(results.tags[0], {priority: 1}),
results.products[0].addTag(results.tags[1], {priority: 2}),
chainer.add(results.products[0].addTag(results.tags[0], {priority: 1})); results.products[1].addTag(results.tags[1], {priority: 1}),
chainer.add(results.products[0].addTag(results.tags[1], {priority: 2})); results.products[2].addTag(results.tags[0], {priority: 3}),
results.products[2].addTag(results.tags[1], {priority: 1}),
chainer.add(results.products[1].addTag(results.tags[1], {priority: 1})); results.products[2].addTag(results.tags[2], {priority: 2})
);
chainer.add(results.products[2].addTag(results.tags[0], {priority: 3})); }).then(function () {
chainer.add(results.products[2].addTag(results.tags[1], {priority: 1})); return Product.findAll({
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'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }),
}, users: User.bulkCreate([{}, {}]).then(function() {
users: function(callback) { return User.findAll();
User.bulkCreate([{}, {}]).done(function() { }),
User.findAll().done(callback); }).then(function (results) {
}); return Promise.join(
}, results.users[0].setGroup(results.groups[1]),
userGroups: ['users', 'groups', function(callback, results) { results.users[1].setGroup(results.groups[0])
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);
}]
}, 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'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }),
}, users: User.bulkCreate([{}, {}]).then(function() {
users: function(callback) { return User.findAll();
User.bulkCreate([{}, {}]).done(function() { })
User.findAll().done(callback); }).then(function (results) {
}); return Promise.join(
}, results.users[0].setGroup(results.groups[1]),
userGroups: ['users', 'groups', function(callback, results) { results.users[1].setGroup(results.groups[0])
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);
}]
}, 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,15 +990,11 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1079,15 +990,11 @@ 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;
// Test
User.find({
where: { username: 'John'}, where: { username: 'John'},
include: [{ include: [{
model: Address, model: Address,
...@@ -1099,21 +1006,17 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1099,21 +1006,17 @@ describe(Support.getTestDialectTeaser('Include'), function() {
model: Street model: Street
}] }]
}] }]
}).done(function(err, john) { }).then(function(john) {
expect(err).not.to.be.ok;
expect(john.Address).to.be.ok; expect(john.Address).to.be.ok;
expect(john.Address.Street).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'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }),
}, users: User.bulkCreate([{}, {}]).then(function() {
users: function(callback) { return User.findAll();
User.bulkCreate([{}, {}]).done(function() { }),
User.findAll().done(callback); categories: Category.bulkCreate([{}, {}]).then(function() {
}); return Category.findAll();
}, })
categories: function(callback) { }).then(function (results) {
Category.bulkCreate([{}, {}]).done(function() { return Promise.join(
Category.findAll().done(callback); results.users[0].setGroup(results.groups[1]),
}); results.users[1].setGroup(results.groups[0]),
}, results.groups.map(function (group) {
userGroups: ['users', 'groups', function(callback, results) { return group.setCategories(results.categories);
var chainer = new Sequelize.Utils.QueryChainer(); })
chainer.add(results.users[0].setGroup(results.groups[1])); );
chainer.add(results.users[1].setGroup(results.groups[0])); }).then(function () {
chainer.run().done(callback); return User.findAll({
}],
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'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }),
}, users: User.bulkCreate([{}, {}]).then(function() {
users: function(callback) { return User.findAll();
User.bulkCreate([{}, {}]).done(function() { }),
User.findAll().done(callback); categories: Category.bulkCreate([{}, {}]).then(function() {
}); return Category.findAll();
}, })
categories: function(callback) { }).then(function (results) {
Category.bulkCreate([{}, {}]).done(function() { return Promise.join(
Category.findAll().done(callback); results.users[0].setTeam(results.groups[1]),
}); results.users[1].setTeam(results.groups[0]),
}, results.groups.map(function (group) {
userGroups: ['users', 'groups', function(callback, results) { return group.setTags(results.categories);
var chainer = new Sequelize.Utils.QueryChainer(); })
chainer.add(results.users[0].setTeam(results.groups[1])); );
chainer.add(results.users[1].setTeam(results.groups[0])); }).then(function () {
chainer.run().done(callback); return User.findAll({
}],
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'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }),
}, users: User.bulkCreate([{}, {}]).then(function() {
users: function(callback) { return User.findAll();
User.bulkCreate([{}, {}]).done(function() { }),
User.findAll().done(callback); categories: Category.bulkCreate([{}, {}]).then(function() {
}); return Category.findAll();
}, })
categories: function(callback) { }).then(function (results) {
Category.bulkCreate([{}, {}]).done(function() { return Promise.join(
Category.findAll().done(callback); results.users[0].setGroup(results.groups[1]),
}); results.users[1].setGroup(results.groups[0]),
}, results.groups.map(function (group) {
userGroups: ['users', 'groups', function(callback, results) { return group.setCategories(results.categories);
var chainer = new Sequelize.Utils.QueryChainer(); })
chainer.add(results.users[0].setGroup(results.groups[1])); );
chainer.add(results.users[1].setGroup(results.groups[0])); }).then(function () {
chainer.run().done(callback); return User.findAll({
}],
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'}
]).done(function() { ]).then(function() {
Project.findAll().done(callback); return Project.findAll();
}); }),
}, users: User.bulkCreate([{}, {}]).then(function() {
users: function(callback) { return User.findAll();
User.bulkCreate([{}, {}]).done(function() { })
User.findAll().done(callback); }).then(function (results) {
}); return Promise.join(
}, results.users[1].setLeaderOf(results.projects[1]),
userProjects: ['users', 'projects', function(callback, results) { results.users[0].setLeaderOf(results.projects[0])
var chainer = new Sequelize.Utils.QueryChainer(); );
chainer.add(results.users[1].setLeaderOf(results.projects[1])); }).then(function () {
chainer.add(results.users[0].setLeaderOf(results.projects[0])); return User.findAll({
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'}
]).done(function() { ]).then(function() {
Product.findAll().done(callback); return Product.findAll();
}); }),
}, tags: Tag.bulkCreate([
tags: function(callback) {
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();
}); })
}, }).then(function (results) {
productTags: ['products', 'tags', function(callback, results) { return Promise.join(
var chainer = new Sequelize.Utils.QueryChainer(); results.products[0].addTag(results.tags[0], {priority: 1}),
results.products[0].addTag(results.tags[1], {priority: 2}),
chainer.add(results.products[0].addTag(results.tags[0], {priority: 1})); results.products[1].addTag(results.tags[1], {priority: 1}),
chainer.add(results.products[0].addTag(results.tags[1], {priority: 2})); results.products[2].addTag(results.tags[0], {priority: 3}),
results.products[2].addTag(results.tags[1], {priority: 1}),
chainer.add(results.products[1].addTag(results.tags[1], {priority: 1})); results.products[2].addTag(results.tags[2], {priority: 2})
);
chainer.add(results.products[2].addTag(results.tags[0], {priority: 3})); }).then(function () {
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,85 +1315,61 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1487,85 +1315,61 @@ 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([
async.auto({
groups: function(callback) {
Group.bulkCreate([
{name: 'Developers'}, {name: 'Developers'},
{name: 'Designers'} {name: 'Designers'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }),
}, ranks: Rank.bulkCreate([
ranks: function(callback) {
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();
}); }),
}, tags: Tag.bulkCreate([
tags: function(callback) {
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();
}); })
}, }).then(function (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( return Promise.map([0, 1, 2, 3, 4], function (i) {
function() { return i < count; }, return Promise.props({
function(callback) { user: User.create({name: 'FooBarzz'}),
i++; products: Product.bulkCreate([
{title: 'Chair'},
async.auto({ {title: 'Desk'}
user: function(callback) { ]).then(function() {
User.create({name: 'FooBarzz'}).done(callback); return Product.findAll();
}, })
memberships: ['user', function(callback, results) { }).then(function (results) {
return Promise.join(
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); ]),
}],
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.user.setProducts([
results.products[(i * 2) + 0], results.products[(i * 2) + 0],
results.products[(i * 2) + 1] results.products[(i * 2) + 1]
]).done(callback); ]),
}], Promise.join(
productTags: ['products', function(callback, results) { results.products[(i * 2) + 0].setTags([
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.products[(i * 2) + 0].setTags([
tags[0], tags[0],
tags[2] tags[2]
])); ]),
chainer.add(results.products[(i * 2) + 1].setTags([ results.products[(i * 2) + 1].setTags([
tags[1] tags[1]
])); ]),
chainer.add(results.products[(i * 2) + 0].setCategory(tags[1])); results.products[(i * 2) + 0].setCategory(tags[1])
),
chainer.run().done(callback);
}],
prices: ['products', function(callback, results) {
Price.bulkCreate([ Price.bulkCreate([
{ProductId: results.products[(i * 2) + 0].id, value: 5}, {ProductId: results.products[(i * 2) + 0].id, value: 5},
{ProductId: results.products[(i * 2) + 0].id, value: 10}, {ProductId: results.products[(i * 2) + 0].id, value: 10},
...@@ -1573,14 +1377,12 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1573,14 +1377,12 @@ describe(Support.getTestDialectTeaser('Include'), function() {
{ProductId: results.products[(i * 2) + 1].id, value: 10}, {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: 15},
{ProductId: results.products[(i * 2) + 1].id, value: 20} {ProductId: results.products[(i * 2) + 1].id, value: 20}
]).done(callback); ])
}] );
}, callback); });
}, });
function(err) { }).then(function () {
expect(err).not.to.be.ok; return User.findAll({
User.findAll({
include: [ include: [
{model: GroupMember, as: 'Memberships', include: [ {model: GroupMember, as: 'Memberships', include: [
Group, Group,
...@@ -1599,26 +1401,19 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1599,26 +1401,19 @@ describe(Support.getTestDialectTeaser('Include'), function() {
order: [ order: [
['id', 'ASC'] ['id', 'ASC']
] ]
}).done(function(err, users) { }).then(function(users) {
expect(err).not.to.be.ok;
users.forEach(function(user) { users.forEach(function(user) {
expect(user.Memberships.length).to.equal(1); expect(user.Memberships.length).to.equal(1);
expect(user.Memberships[0].Rank.name).to.equal('Admin'); expect(user.Memberships[0].Rank.name).to.equal('Admin');
expect(user.Products.length).to.equal(1); expect(user.Products.length).to.equal(1);
expect(user.Products[0].Prices.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'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }),
}, users: User.bulkCreate([{}, {}, {}, {}]).then(function() {
users: function(callback) { return User.findAll();
User.bulkCreate([{}, {}, {}, {}]).done(function() { }),
User.findAll().done(callback); }).then(function (results) {
}); return Promise.join(
}, results.users[0].setGroup(results.groups[0]),
userGroups: ['users', 'groups', function(callback, results) { results.users[1].setGroup(results.groups[0]),
var chainer = new Sequelize.Utils.QueryChainer(); results.users[2].setGroup(results.groups[0]),
chainer.add(results.users[0].setGroup(results.groups[0])); results.users[3].setGroup(results.groups[1])
chainer.add(results.users[1].setGroup(results.groups[0])); );
chainer.add(results.users[2].setGroup(results.groups[0])); }).then(function () {
chainer.add(results.users[3].setGroup(results.groups[1])); return User.findAll({
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([
async.auto({
groups: function(callback) {
Group.bulkCreate([
{name: 'Developers'}, {name: 'Developers'},
{name: 'Designers'}, {name: 'Designers'},
{name: 'Managers'} {name: 'Managers'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }),
}, companies: Company.bulkCreate([
companies: function(callback) {
Company.bulkCreate([
{name: 'Sequelize'}, {name: 'Sequelize'},
{name: 'Coca Cola'}, {name: 'Coca Cola'},
{name: 'Bonanza'}, {name: 'Bonanza'},
{name: 'NYSE'}, {name: 'NYSE'},
{name: 'Coshopr'} {name: 'Coshopr'}
]).done(function(err) { ]).then(function() {
if (err) return callback(err); return Company.findAll();
Company.findAll().done(callback); }),
}); ranks: Rank.bulkCreate([
},
ranks: function(callback) {
Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1}, {name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1},
{name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1}, {name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1},
{name: 'Member', canInvite: 1, canRemove: 0, canPost: 0} {name: 'Member', canInvite: 1, canRemove: 0, canPost: 0}
]).done(function() { ]).then(function() {
Rank.findAll().done(callback); return Rank.findAll();
}); }),
}, tags: Tag.bulkCreate([
tags: function(callback) {
Tag.bulkCreate([
{name: 'A'}, {name: 'A'},
{name: 'B'}, {name: 'B'},
{name: 'C'}, {name: 'C'},
{name: 'D'}, {name: 'D'},
{name: 'E'} {name: 'E'}
]).done(function() { ]).then(function() {
Tag.findAll().done(callback); return Tag.findAll();
}); })
}, }).then(function (results) {
loop: ['groups', 'ranks', 'tags', 'companies', function(done, results) { var count = 4
var groups = results.groups , i = -1
, groups = results.groups
, ranks = results.ranks , ranks = results.ranks
, tags = results.tags , tags = results.tags
, companies = results.companies; , 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'}
]).done(function() { ]).then(function() {
Group.findAll().done(callback); return Group.findAll();
}); }),
}, users: User.bulkCreate([{}, {}, {}, {}]).then(function() {
users: function(callback) { return User.findAll();
User.bulkCreate([{}, {}, {}, {}]).done(function() { })
User.findAll().done(callback); }).then(function (results) {
}); return Promise.join(
}, 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])); results.users[0].setGroup(results.groups[0])
chainer.add(results.users[1].setGroup(results.groups[0])); );
chainer.add(results.users[2].setGroup(results.groups[0])); }).then(function () {
chainer.add(results.users[3].setGroup(results.groups[1])); return User.findAll({
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!