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

Commit a1c82f3d by Mick Hansen

Merge pull request #3467 from BridgeAR/master

Refactor last tests to use promise style
2 parents 45e6700b c1fa0bb1
......@@ -2,4 +2,4 @@
The entry point.
@module Sequelize
**/
module.exports = require("./lib/sequelize")
module.exports = require("./lib/sequelize");
......@@ -203,7 +203,7 @@ module.exports = (function() {
options = Utils._.extend({
events: proxyEventKeys,
skipEvents: []
}, options ||  {});
}, options || {});
options.events = Utils._.difference(options.events, options.skipEvents);
......
......@@ -81,11 +81,11 @@ module.exports = (function() {
return association.source.update(newValues, { where: query });
});
},
increment: function (targetId) {
increment: function (targetId) {
var query = CounterUtil._sourceQuery(targetId);
return association.source.find({ where: query }).then(function (instance) {
return instance.increment(counterCacheInstance.columnName, { by: 1 });
return instance.increment(counterCacheInstance.columnName, { by: 1 });
});
},
decrement: function (targetId) {
......
......@@ -43,7 +43,6 @@
"validator": "^3.34.0"
},
"devDependencies": {
"async": "~0.9.0",
"chai": "^2.1.2",
"chai-as-promised": "^4.3.0",
"chai-datetime": "~1.3.0",
......
......@@ -1793,7 +1793,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
self.User.belongsToMany(self.Task, { onDelete: 'RESTRICT'});
self.Task.belongsToMany(self.User, { onDelete: 'CASCADE'});
return this.sequelize.sync({ force: true, logging: true }).bind({}).then(function() {
return this.sequelize.sync({ force: true }).bind({}).then(function() {
return Sequelize.Promise.join(
self.User.create({ id: 67, username: 'foo' }),
self.Task.create({ id: 52, title: 'task' }),
......
......@@ -13,7 +13,7 @@ if (dialect.match(/^postgres/)) {
describe('[POSTGRES Specific] associations', function() {
describe('many-to-many', function() {
describe('where tables have the same prefix', function() {
it('should create a table wp_table1wp_table2s', function(done) {
it('should create a table wp_table1wp_table2s', function() {
var Table2 = this.sequelize.define('wp_table2', {foo: DataTypes.STRING})
, Table1 = this.sequelize.define('wp_table1', {foo: DataTypes.STRING});
......@@ -21,35 +21,24 @@ if (dialect.match(/^postgres/)) {
Table2.hasMany(Table1);
expect(this.sequelize.daoFactoryManager.getDAO('wp_table1swp_table2s')).to.exist;
done();
});
});
describe('when join table name is specified', function() {
beforeEach(function(done) {
beforeEach(function() {
var Table2 = this.sequelize.define('ms_table1', {foo: DataTypes.STRING})
, Table1 = this.sequelize.define('ms_table2', {foo: DataTypes.STRING});
Table1.hasMany(Table2, {joinTableName: 'table1_to_table2'});
Table2.hasMany(Table1, {joinTableName: 'table1_to_table2'});
setTimeout(function() {
done();
}, 50);
});
it('should not use a combined name', function(done) {
it('should not use a combined name', function() {
expect(this.sequelize.daoFactoryManager.getDAO('ms_table1sms_table2s')).not.to.exist;
setTimeout(function() {
done();
}, 50);
});
it('should use the specified name', function(done) {
it('should use the specified name', function() {
expect(this.sequelize.daoFactoryManager.getDAO('table1_to_table2')).to.exist;
setTimeout(function() {
done();
}, 50);
});
});
});
......
......@@ -363,38 +363,37 @@ if (dialect.match(/^postgres/)) {
});
});
it('should be able to add enum types', function(done) {
it('should be able to add enum types', function() {
var self = this
, User = this.sequelize.define('UserEnums', {
mood: DataTypes.ENUM('happy', 'sad', 'meh')
});
var _done = _.after(4, function() {
done();
});
})
, count = 0;
User.sync({ force: true }).then(function() {
return User.sync({ force: true }).then(function() {
User = self.sequelize.define('UserEnums', {
mood: DataTypes.ENUM('neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful')
});
User.sync().then(function() {
return User.sync().then(function() {
expect(User.rawAttributes.mood.values).to.deep.equal(['neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful']);
_done();
count++;
}).on('sql', function(sql) {
if (sql.indexOf('neutral') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'neutral' BEFORE 'happy'");
_done();
count++;
}
else if (sql.indexOf('ecstatic') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'ecstatic' BEFORE 'meh'");
_done();
count++;
}
else if (sql.indexOf('joyful') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'joyful' AFTER 'meh'");
_done();
count++;
}
});
}).then(function() {
expect(count).to.equal(4);
});
});
});
......
......@@ -318,9 +318,9 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Task.belongsTo(Project);
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Project.bulkCreate([{ id: 1 }, { id: 2 }]),
User.create({
return Project.bulkCreate([{ id: 1 }, { id: 2 }]);
}).then(function (projects) {
return User.create({
Tasks: [
{ProjectId: 1},
{ProjectId: 2},
......@@ -329,9 +329,8 @@ describe(Support.getTestDialectTeaser('Include'), function() {
]
}, {
include: [Task]
})
]);
}).spread(function (projects, user) {
});
}).then(function (user) {
return User.find({
where: {
id: user.id
......
......@@ -6,7 +6,6 @@ var chai = require('chai')
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, datetime = require('chai-datetime')
, async = require('async')
, Promise = Sequelize.Promise
, _ = require('lodash');
......@@ -128,7 +127,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
, tags = results.tags
, companies = results.companies;
return Promise.reduce(_.range(5), function (memo, i) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) {
return Promise.props({
user: User.create(),
products: Product.bulkCreate([
......@@ -195,7 +194,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
])
);
});
}, []);
});
});
});
};
......@@ -380,98 +379,66 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Group.hasMany(GroupMember, {as: 'Memberships'});
return this.sequelize.sync({force: true}).then(function() {
return new Promise(function (resolve, reject) {
var count = 4
, i = -1;
async.auto({
groups: function(callback) {
return Promise.all([
Group.bulkCreate([
{name: 'Developers'},
{name: 'Designers'}
]).then(function() {
return Group.findAll();
}).nodeify(callback);
},
ranks: function(callback) {
}),
Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1},
{name: 'Member', canInvite: 1, canRemove: 0}
]).then(function() {
return Rank.findAll();
}).nodeify(callback);
},
tags: function(callback) {
}),
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'}
]).then(function() {
return Tag.findAll();
}).nodeify(callback);
},
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().nodeify(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}
]).nodeify(callback);
}],
products: function(callback) {
})
]).spread(function(groups, ranks, tags) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) {
return Promise.all([
User.create(),
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'}
]).then(function() {
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([
})
]).spread(function(user, products) {
return Promise.all([
GroupMember.bulkCreate([
{UserId: user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{UserId: user.id, GroupId: groups[1].id, RankId: ranks[1].id}
]),
user.setProducts([
products[(i * 2) + 0],
products[(i * 2) + 1]
]),
products[(i * 2) + 0].setTags([
tags[0],
tags[2]
]),
results.products[(i * 2) + 1].setTags([
products[(i * 2) + 1].setTags([
tags[1]
]),
results.products[(i * 2) + 0].setCategory(tags[1])
).nodeify(callback);
}],
prices: ['products', function(callback, 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}
]).nodeify(callback);
}]
}, callback);
},
function(err) {
User.findAll({
{ProductId: products[(i * 2) + 0].id, value: 5},
{ProductId: products[(i * 2) + 0].id, value: 10},
{ProductId: products[(i * 2) + 1].id, value: 5},
{ProductId: products[(i * 2) + 1].id, value: 10},
{ProductId: products[(i * 2) + 1].id, value: 15},
{ProductId: products[(i * 2) + 1].id, value: 20}
])
]);
});
}).then(function() {
return User.findAll({
include: [
{model: GroupMember, as: 'Memberships', include: [
Group,
......@@ -506,13 +473,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
expect(user.Products[0].Prices.length).to.equal(2);
expect(user.Products[1].Prices.length).to.equal(4);
});
}).nodeify(done);
}
);
}]
}, function (err) {
if (err) return reject(err);
resolve();
});
});
});
});
......@@ -1290,32 +1251,28 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Group.hasMany(GroupMember, {as: 'Memberships'});
return this.sequelize.sync({force: true}).then(function() {
return Promise.props({
groups: Group.bulkCreate([
return Promise.all([
Group.bulkCreate([
{name: 'Developers'},
{name: 'Designers'}
]).then(function() {
return Group.findAll();
}),
ranks: Rank.bulkCreate([
Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1},
{name: 'Member', canInvite: 1, canRemove: 0}
]).then(function() {
return Rank.findAll();
}),
tags: Tag.bulkCreate([
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'}
]).then(function() {
return Tag.findAll();
})
}).then(function (results) {
var groups = results.groups
, ranks = results.ranks
, tags = results.tags;
return Promise.reduce([0, 1, 2, 3, 4], function (memo, i) {
]).spread(function (groups, ranks, tags) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) {
return Promise.props({
user: User.create({name: 'FooBarzz'}),
products: Product.bulkCreate([
......@@ -1354,7 +1311,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
])
);
});
}, []);
});
}).then(function () {
return User.findAll({
include: [
......
......@@ -6,8 +6,8 @@ var chai = require('chai')
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, datetime = require('chai-datetime')
, async = require('async')
, Promise = Sequelize.Promise
, dialect = Support.getTestDialect()
, _ = require('lodash');
chai.use(datetime);
......@@ -93,63 +93,53 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
Group.hasMany(GroupMember, {as: 'Memberships'});
return self.sequelize.sync({force: true}).then(function() {
return Promise.props({
groups: Group.bulkCreate([
return Promise.all([
Group.bulkCreate([
{name: 'Developers'},
{name: 'Designers'},
{name: 'Managers'}
]).then(function() {
return Group.findAll();
}),
companies: Company.bulkCreate([
]),
Company.bulkCreate([
{name: 'Sequelize'},
{name: 'Coca Cola'},
{name: 'Bonanza'},
{name: 'NYSE'},
{name: 'Coshopr'}
]).then(function() {
return Company.findAll();
}),
ranks: Rank.bulkCreate([
]),
Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1},
{name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1},
{name: 'Member', canInvite: 1, canRemove: 0, canPost: 0}
]).then(function() {
return Rank.findAll();
}),
tags: Tag.bulkCreate([
]),
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'},
{name: 'D'},
{name: 'E'}
])
]).then(function() {
return Tag.findAll();
})
}).then(function (results) {
var groups = results.groups
, ranks = results.ranks
, tags = results.tags
, companies = results.companies;
return Promise.reduce(_.range(5), function (memo, i) {
return Promise.props({
user: AccUser.create(),
products: Product.bulkCreate([
return Promise.all([
Group.findAll(),
Company.findAll(),
Rank.findAll(),
Tag.findAll()
]);
}).spread(function (groups, companies, ranks, tags) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) {
return Promise.all([
AccUser.create(),
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Bed'},
{title: 'Pen'},
{title: 'Monitor'}
]).then(function(err) {
]).then(function() {
return Product.findAll();
})
}).then(function (results) {
var user = results.user
, products = results.products
, groupMembers;
groupMembers = [
]).spread(function (user, products) {
var groupMembers = [
{AccUserId: user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{AccUserId: user.id, GroupId: groups[1].id, RankId: ranks[2].id}
];
......@@ -199,7 +189,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
])
);
});
}, []);
});
});
});
});
......@@ -207,11 +197,11 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), 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 self = this;
self.sequelize.dropAllSchemas().then(function() {
self.sequelize.createSchema('account').then(function() {
return self.sequelize.dropAllSchemas().then(function() {
return self.sequelize.createSchema('account').then(function() {
var AccUser = self.sequelize.define('AccUser', {}, {schema: 'account'})
, Product = self.sequelize.define('Product', {
title: DataTypes.STRING
......@@ -259,100 +249,68 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
GroupMember.belongsTo(Group);
Group.hasMany(GroupMember, {as: 'Memberships'});
self.sequelize.sync({force: true}).done(function() {
var count = 4
, i = -1;
async.auto({
groups: function(callback) {
return self.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'Developers'},
{name: 'Designers'}
]).done(function() {
Group.findAll().done(callback);
});
},
ranks: function(callback) {
]).then(function() {
return Group.findAll();
}),
Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1},
{name: 'Member', canInvite: 1, canRemove: 0}
]).done(function() {
Rank.findAll().done(callback);
});
},
tags: function(callback) {
]).then(function() {
return Rank.findAll();
}),
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'}
]).done(function() {
Tag.findAll().done(callback);
});
},
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) {
AccUser.create().done(callback);
},
memberships: ['user', function(callback, results) {
GroupMember.bulkCreate([
{AccUserId: results.user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{AccUserId: results.user.id, GroupId: groups[1].id, RankId: ranks[1].id}
]).done(callback);
}],
products: function(callback) {
]).then(function() {
return Tag.findAll();
})
]).spread(function(groups, ranks, tags) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) {
return Promise.all([
AccUser.create(),
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([
]).then(function() {
return Product.findAll();
})
]).spread(function(user, products) {
return Promise.all([
GroupMember.bulkCreate([
{AccUserId: user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{AccUserId: user.id, GroupId: groups[1].id, RankId: ranks[1].id}
]),
user.setProducts([
products[(i * 2) + 0],
products[(i * 2) + 1]
]),
products[(i * 2) + 0].setTags([
tags[0],
tags[2]
]));
chainer.add(results.products[(i * 2) + 1].setTags([
]),
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) {
]),
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}
]).done(callback);
}]
}, callback);
},
function(err) {
expect(err).not.to.be.ok;
AccUser.findAll({
{ProductId: products[(i * 2) + 0].id, value: 5},
{ProductId: products[(i * 2) + 0].id, value: 10},
{ProductId: products[(i * 2) + 1].id, value: 5},
{ProductId: products[(i * 2) + 1].id, value: 10},
{ProductId: products[(i * 2) + 1].id, value: 15},
{ProductId: products[(i * 2) + 1].id, value: 20}
])
]);
});
});
}).then(function() {
return AccUser.findAll({
include: [
{model: GroupMember, as: 'Memberships', include: [
Group,
......@@ -367,9 +325,8 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
order: [
[AccUser.rawAttributes.id, 'ASC']
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
users.forEach(function(user) {
}).then(function(users) {
users.forEach(function(user, a) {
expect(user.Memberships).to.be.ok;
user.Memberships.sort(sortById);
......@@ -388,20 +345,15 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
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', {}, {schema: 'account'})
, B = this.sequelize.define('b', {}, {schema: 'account'})
, C = this.sequelize.define('c', {}, {schema: 'account'})
......@@ -429,51 +381,32 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
H
];
this.sequelize.sync().done(function() {
async.auto({
as: function(callback) {
A.bulkCreate([
{},
{},
{},
{},
{},
{},
{},
{}
]).done(function() {
A.findAll().done(callback);
});
},
singleChain: function(callback) {
return this.sequelize.sync().then(function() {
return A.bulkCreate([
{}, {}, {}, {}, {}, {}, {}, {}
]).then(function() {
var previousInstance;
async.eachSeries(singles, function(model, callback) {
model.create({}).done(function(err, instance) {
return Promise.resolve(singles).each(function(model) {
return model.create({}).then(function(instance) {
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;
callback();
});
} else {
previousInstance = b = instance;
callback();
}
previousInstance = b = instance;
return void(0);
});
}, 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);
}]
}, function() {
A.findAll({
}).then(function() {
return A.findAll();
}).then(function(as) {
var promises = [];
as.forEach(function(a) {
promises.push(a.setB(b));
});
return Promise.all(promises);
}).then(function() {
return A.findAll({
include: [
{model: B, include: [
{model: C, include: [
......@@ -489,20 +422,17 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
]}
]}
]
}).done(function(err, as) {
expect(err).not.to.be.ok;
}).then(function(as) {
expect(as.length).to.be.ok;
as.forEach(function(a) {
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('SpecialUser', {}, {schema: 'account'})
, Item = this.sequelize.define('Item', {'test': DataTypes.STRING}, {schema: 'account'})
, Order = this.sequelize.define('Order', {'position': DataTypes.INTEGER}, {schema: 'account'});
......@@ -511,64 +441,40 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Item, {'as': 'itemB', foreignKey: 'itemB_id'});
User.belongsTo(Order);
this.sequelize.sync().done(function() {
async.auto({
users: function(callback) {
User.bulkCreate([{}, {}, {}]).done(function() {
User.findAll().done(callback);
});
},
items: function(callback) {
return this.sequelize.sync().then(function() {
return Promise.all([
User.bulkCreate([{}, {}, {}]),
Item.bulkCreate([
{'test': 'abc'},
{'test': 'def'},
{'test': 'ghi'},
{'test': 'jkl'}
]).done(function() {
Item.findAll({order: ['id']}).done(callback);
});
},
orders: function(callback) {
]),
Order.bulkCreate([
{'position': 2},
{'position': 3},
{'position': 1}
]).done(function() {
Order.findAll({order: ['id']}).done(callback);
});
},
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));
chainer.add(user2.setItemB(item4));
chainer.add(user2.setOrder(order2));
chainer.add(user3.setItemA(item1));
chainer.add(user3.setItemB(item4));
chainer.add(user3.setOrder(order1));
chainer.run().done(callback);
}]
}, function() {
User.findAll({
])
]).then(function() {
return Promise.all([
User.findAll(),
Item.findAll({order: ['id']}),
Order.findAll({order: ['id']})
]);
}).spread(function(users, items, orders) {
return Promise.all([
users[0].setItemA(items[0]),
users[0].setItemB(items[1]),
users[0].setOrder(orders[2]),
users[1].setItemA(items[2]),
users[1].setItemB(items[3]),
users[1].setOrder(orders[1]),
users[2].setItemA(items[0]),
users[2].setItemB(items[3]),
users[2].setOrder(orders[0])
]);
}).spread(function() {
return User.findAll({
'include': [
{'model': Item, 'as': 'itemA', where: {test: 'abc'}},
{'model': Item, 'as': 'itemB'},
......@@ -576,23 +482,18 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
'order': [
[Order, 'position']
]
}).done(function(err, as) {
expect(err).not.to.be.ok;
}).then(function(as) {
expect(as.length).to.eql(2);
expect(as[0].itemA.test).to.eql('abc');
expect(as[1].itemA.test).to.eql('abc');
expect(as[0].Order.position).to.eql(1);
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', {
title: DataTypes.STRING
}, {schema: 'account'})
......@@ -606,44 +507,34 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
Product.hasMany(Tag, {through: ProductTag});
Tag.hasMany(Product, {through: ProductTag});
this.sequelize.sync({force: true}).done(function() {
async.auto({
products: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Dress'}
]).done(function() {
Product.findAll().done(callback);
});
},
tags: function(callback) {
]),
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'}
]).done(function() {
Tag.findAll().done(callback);
});
},
productTags: ['products', 'tags', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.products[0].addTag(results.tags[0], {priority: 1}));
chainer.add(results.products[0].addTag(results.tags[1], {priority: 2}));
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({
])
]).spread(function() {
return Promise.all([
Product.findAll(),
Tag.findAll()
]);
}).spread(function(products, tags) {
return Promise.all([
products[0].addTag(tags[0], {priority: 1}),
products[0].addTag(tags[1], {priority: 2}),
products[1].addTag(tags[1], {priority: 1}),
products[2].addTag(tags[0], {priority: 3}),
products[2].addTag(tags[1], {priority: 1}),
products[2].addTag(tags[2], {priority: 2})
]);
}).spread(function() {
return Product.findAll({
include: [
{model: Tag}
],
......@@ -651,63 +542,49 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
['id', 'ASC'],
[Tag, 'id', 'ASC']
]
}).done(function(err, products) {
expect(err).not.to.be.ok;
}).then(function(products) {
expect(products[0].Tags[0].ProductTag.priority).to.equal(1);
expect(products[0].Tags[1].ProductTag.priority).to.equal(2);
expect(products[1].Tags[0].ProductTag.priority).to.equal(1);
expect(products[2].Tags[0].ProductTag.priority).to.equal(3);
expect(products[2].Tags[1].ProductTag.priority).to.equal(1);
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', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', {}, {schema: 'account'});
User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
Group.bulkCreate([{}, {}]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}, {}]).done(function() {
User.findAll().done(callback);
});
},
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({
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([{}, {}]),
User.bulkCreate([{}, {}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll()
]);
}).spread(function(groups, users) {
return users[2].setGroup(groups[1]);
}).then(function() {
return User.findAll({
include: [
{model: Group, required: true}
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
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', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', {
name: DataTypes.STRING
......@@ -715,46 +592,38 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[0].setGroup(results.groups[1]));
chainer.add(results.users[1].setGroup(results.groups[0]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
]),
User.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll()
]);
}).spread(function(groups, users) {
return Promise.all([
users[0].setGroup(groups[1]),
users[1].setGroup(groups[0])
]);
}).then(function() {
return User.findAll({
include: [
{model: Group, where: {name: 'A'}}
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
expect(users[0].Group).to.be.ok;
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', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', {
name: DataTypes.STRING
......@@ -762,46 +631,38 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[0].setGroup(results.groups[1]));
chainer.add(results.users[1].setGroup(results.groups[0]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
]),
User.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll()
]);
}).spread(function(groups, users) {
return Promise.all([
users[0].setGroup(groups[1]),
users[1].setGroup(groups[0])
]);
}).then(function() {
return User.findAll({
include: [
{model: Group, required: true}
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
users.forEach(function(user) {
expect(user.Group).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', {}, {schema: 'account'}) , Group = this.sequelize.define('Group', {
name: DataTypes.STRING
}, {schema: 'account'})
......@@ -812,65 +673,49 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group);
Group.hasMany(Category);
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
categories: function(callback) {
Category.bulkCreate([{}, {}]).done(function() {
Category.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[0].setGroup(results.groups[1]));
chainer.add(results.users[1].setGroup(results.groups[0]));
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));
]),
User.bulkCreate([{}, {}]),
Category.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll(),
Category.findAll()
]);
}).spread(function(groups, users, categories) {
var promises = [
users[0].setGroup(groups[1]),
users[1].setGroup(groups[0])
];
groups.forEach(function(group) {
promises.push(group.setCategories(categories));
});
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
return Promise.all(promises);
}).then(function() {
return User.findAll({
include: [
{model: Group, required: true, include: [
{model: Category}
]}
],
limit: 1
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
users.forEach(function(user) {
expect(user.Group).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', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', {
name: DataTypes.STRING
......@@ -882,65 +727,49 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group, {as: 'Team'});
Group.hasMany(Category, {as: 'Tags'});
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
categories: function(callback) {
Category.bulkCreate([{}, {}]).done(function() {
Category.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[0].setTeam(results.groups[1]));
chainer.add(results.users[1].setTeam(results.groups[0]));
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));
]),
User.bulkCreate([{}, {}]),
Category.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll(),
Category.findAll()
]);
}).spread(function(groups, users, categories) {
var promises = [
users[0].setTeam(groups[1]),
users[1].setTeam(groups[0])
];
groups.forEach(function(group) {
promises.push(group.setTags(categories));
});
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
return Promise.all(promises);
}).then(function() {
return User.findAll({
include: [
{model: Group, required: true, as: 'Team', include: [
{model: Category, as: 'Tags'}
]}
],
limit: 1
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
users.forEach(function(user) {
expect(user.Team).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', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', {
name: DataTypes.STRING
......@@ -952,65 +781,49 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group);
Group.hasMany(Category);
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
categories: function(callback) {
Category.bulkCreate([{}, {}]).done(function() {
Category.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[0].setGroup(results.groups[1]));
chainer.add(results.users[1].setGroup(results.groups[0]));
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));
]),
User.bulkCreate([{}, {}]),
Category.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll(),
Category.findAll()
]);
}).spread(function (groups, users, categories) {
var promises = [
users[0].setGroup(groups[1]),
users[1].setGroup(groups[0])
];
groups.forEach(function(group) {
promises.push(group.setCategories(categories));
});
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
return Promise.all(promises);
}).then(function() {
return User.findAll({
include: [
{model: Group, required: true, include: [
{model: Category, required: false}
]}
],
limit: 1
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
users.forEach(function(user) {
expect(user.Group).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', {}, {schema: 'account'})
, Project = this.sequelize.define('Project', {
title: DataTypes.STRING
......@@ -1018,46 +831,38 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.hasOne(Project, {as: 'LeaderOf'});
this.sequelize.sync({force: true}).done(function() {
async.auto({
projects: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Project.bulkCreate([
{title: 'Alpha'},
{title: 'Beta'}
]).done(function() {
Project.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
userProjects: ['users', 'projects', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[1].setLeaderOf(results.projects[1]));
chainer.add(results.users[0].setLeaderOf(results.projects[0]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
]),
User.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Project.findAll(),
User.findAll()
]);
}).spread(function(projects, users) {
return Promise.all([
users[1].setLeaderOf(projects[1]),
users[0].setLeaderOf(projects[0])
]);
}).then(function() {
return User.findAll({
include: [
{model: Project, as: 'LeaderOf', where: {title: 'Beta'}}
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
expect(users[0].LeaderOf).to.be.ok;
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', {
title: DataTypes.STRING
}, {schema: 'account'})
......@@ -1071,60 +876,46 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
Product.hasMany(Tag, {through: ProductTag});
Tag.hasMany(Product, {through: ProductTag});
this.sequelize.sync({force: true}).done(function() {
async.auto({
products: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Dress'}
]).done(function() {
Product.findAll().done(callback);
});
},
tags: function(callback) {
]),
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'}
]).done(function() {
Tag.findAll().done(callback);
});
},
productTags: ['products', 'tags', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.products[0].addTag(results.tags[0], {priority: 1}));
chainer.add(results.products[0].addTag(results.tags[1], {priority: 2}));
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({
])
]).then(function() {
return Promise.all([
Product.findAll(),
Tag.findAll()
]);
}).spread(function(products, tags) {
return Promise.all([
products[0].addTag(tags[0], {priority: 1}),
products[0].addTag(tags[1], {priority: 2}),
products[1].addTag(tags[1], {priority: 1}),
products[2].addTag(tags[0], {priority: 3}),
products[2].addTag(tags[1], {priority: 1}),
products[2].addTag(tags[2], {priority: 2})
]);
}).then(function() {
return Product.findAll({
include: [
{model: Tag, where: {name: 'C'}}
]
}).done(function(err, products) {
expect(err).not.to.be.ok;
}).then(function(products) {
expect(products.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', {
name: DataTypes.STRING
}, {schema: 'account'})
......@@ -1174,100 +965,68 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
GroupMember.belongsTo(Group);
Group.hasMany(GroupMember, {as: 'Memberships'});
this.sequelize.sync({force: true}).done(function() {
var count = 4
, i = -1;
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'Developers'},
{name: 'Designers'}
]).done(function() {
Group.findAll().done(callback);
});
},
ranks: function(callback) {
]),
Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1},
{name: 'Member', canInvite: 1, canRemove: 0}
]).done(function() {
Rank.findAll().done(callback);
});
},
tags: function(callback) {
]),
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'}
]).done(function() {
Tag.findAll().done(callback);
});
},
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) {
])
]).then(function() {
return Promise.all([
Group.findAll(),
Rank.findAll(),
Tag.findAll()
]);
}).spread(function(groups, ranks, tags) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) {
return Promise.all([
User.create({name: 'FooBarzz'}),
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([
]).then(function() {
return Product.findAll();
})
]).spread(function(user, products) {
return Promise.all([
GroupMember.bulkCreate([
{UserId: user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{UserId: user.id, GroupId: groups[1].id, RankId: ranks[1].id}
]),
user.setProducts([
products[(i * 2) + 0],
products[(i * 2) + 1]
]),
products[(i * 2) + 0].setTags([
tags[0],
tags[2]
]));
chainer.add(results.products[(i * 2) + 1].setTags([
]),
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) {
]),
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}
]).done(callback);
}]
}, callback);
},
function(err) {
expect(err).not.to.be.ok;
User.findAll({
{ProductId: products[(i * 2) + 0].id, value: 5},
{ProductId: products[(i * 2) + 0].id, value: 10},
{ProductId: products[(i * 2) + 1].id, value: 5},
{ProductId: products[(i * 2) + 1].id, value: 10},
{ProductId: products[(i * 2) + 1].id, value: 15},
{ProductId: products[(i * 2) + 1].id, value: 20}
])
]);
});
});
}).then(function(){
return User.findAll({
include: [
{model: GroupMember, as: 'Memberships', include: [
Group,
......@@ -1286,22 +1045,15 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
order: [
['id', 'ASC']
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).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);
});
done();
});
}
);
}]
}, done);
});
});
});
......@@ -1432,7 +1184,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
});
});
it.skip('should support including date fields, with the correct timeszone', function() {
it('should support including date fields, with the correct timeszone', function() {
var User = this.sequelize.define('user', {
dateField: Sequelize.DATE
}, {timestamps: false, schema: 'account'})
......@@ -1453,8 +1205,13 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
},
include: [Group]
}).then(function(users) {
if (dialect === 'sqlite') {
expect(new Date(users[0].dateField).getTime()).to.equal(Date.UTC(2014, 1, 20));
expect(new Date(users[0].groups[0].dateField).getTime()).to.equal(Date.UTC(2014, 1, 20));
} else {
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));
}
});
});
});
......
......@@ -341,11 +341,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
it('should still work right with other concurrent increments', function() {
var self = this;
return this.User.find(1).then(function(user1) {
return user1.increment(['aNumber'], { by: 2 }).then(function() {
return user1.increment(['aNumber'], { by: 2 });
}).then(function() {
return user1.increment(['aNumber'], { by: 2 });
}).then(function() {
return this.sequelize.Promise.all([
user1.increment(['aNumber'], { by: 2 }),
user1.increment(['aNumber'], { by: 2 }),
user1.increment(['aNumber'], { by: 2 })
]).then(function() {
return self.User.find(1).then(function(user2) {
expect(user2.aNumber).to.equal(6);
});
......@@ -468,11 +468,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
it('should still work right with other concurrent increments', function() {
var self = this;
return this.User.find(1).then(function(user1) {
return user1.decrement(['aNumber'], { by: 2 }).then(function() {
return user1.decrement(['aNumber'], { by: 2 });
}).then(function() {
return user1.decrement(['aNumber'], { by: 2 });
}).then(function() {
return this.sequelize.Promise.all([
user1.decrement(['aNumber'], { by: 2 }),
user1.decrement(['aNumber'], { by: 2 }),
user1.decrement(['aNumber'], { by: 2 })
]).then(function() {
return self.User.find(1).then(function(user2) {
expect(user2.aNumber).to.equal(-6);
});
......
......@@ -11,7 +11,6 @@ var chai = require('chai')
, datetime = require('chai-datetime')
, _ = require('lodash')
, moment = require('moment')
, async = require('async')
, current = Support.sequelize;
chai.use(datetime);
......@@ -2348,12 +2347,13 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
if (dialect !== 'sqlite' && current.dialect.supports.transactions) {
it('supports multiple async transactions', function(done) {
it('supports multiple async transactions', function() {
this.timeout(25000);
var self = this;
return Support.prepareTransactionTest(this.sequelize).bind({}).then(function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING });
var testAsync = function(i, done) {
sequelize.transaction().then(function(t) {
var testAsync = function() {
return sequelize.transaction().then(function(t) {
return User.create({
username: 'foo'
}, {
......@@ -2380,14 +2380,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
}).then(function(t) {
return t.rollback();
}).nodeify(done);
});
};
User.sync({ force: true }).then(function() {
return User.sync({ force: true }).then(function() {
var tasks = [];
for (var i = 0; i < 1000; i++) {
tasks.push(testAsync.bind(this, i));
tasks.push(testAsync.bind(this));
}
async.parallelLimit(tasks, (sequelize.config.pool && sequelize.config.pool.max || 5) - 1, done); // Needs to be one less than 1 else the non transaction query won't ever get a connection
return self.sequelize.Promise.resolve(tasks).map(function(entry) {
return entry();
}, {
// Needs to be one less than ??? else the non transaction query won't ever get a connection
concurrency: (sequelize.config.pool && sequelize.config.pool.max || 5) - 1
});
});
});
});
......
......@@ -924,12 +924,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
it('can omit autoincremental columns', function(done) {
it('can omit autoincremental columns', function() {
var self = this
, data = { title: 'Iliad' }
, dataTypes = [Sequelize.INTEGER, Sequelize.BIGINT]
, chain = new Sequelize.Utils.QueryChainer()
, chain2 = new Sequelize.Utils.QueryChainer()
, sync = []
, promises = []
, books = [];
dataTypes.forEach(function(dataType, index) {
......@@ -940,21 +940,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
books.forEach(function(b) {
chain.add(b.sync({ force: true }));
sync.push(b.sync({ force: true }));
});
chain.run().then(function() {
books.forEach(function(b) {
chain2.add(b.create(data));
});
chain2.run().then(function(results) {
results.forEach(function(book, index) {
return Promise.all(sync).then(function() {
books.forEach(function(b, index) {
promises.push(b.create(data).then(function(book) {
expect(book.title).to.equal(data.title);
expect(book.author).to.equal(data.author);
expect(books[index].rawAttributes.id.type instanceof dataTypes[index]).to.be.ok;
}));
});
done();
});
return Promise.all(promises);
});
});
......
......@@ -11,7 +11,6 @@ var chai = require('chai')
, datetime = require('chai-datetime')
, promised = require('chai-as-promised')
, _ = require('lodash')
, async = require('async')
, current = Support.sequelize;
chai.use(promised);
......@@ -760,64 +759,42 @@ describe(Support.getTestDialectTeaser('Model'), function() {
this.Tag = this.sequelize.define('Tag', { name: Sequelize.STRING });
});
it('returns the associated models when using through as string and alias', function(done) {
it('returns the associated models when using through as string and alias', function() {
var self = this;
this.Product.hasMany(this.Tag, {as: 'tags', through: 'product_tag'});
this.Tag.hasMany(this.Product, {as: 'products', through: 'product_tag'});
this.sequelize.sync().done(function() {
async.auto({
createProducts: function(callback) {
return this.sequelize.sync().then(function() {
return Promise.all([
self.Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Handbag'},
{title: 'Dress'},
{title: 'Jan'}
]).done(callback);
},
// bulkCreate doesn't include id for some reason, not going to fix tis now
products: ['createProducts', function(callback) {
self.Product.findAll().done(callback);
}],
createTags: function(callback) {
]),
self.Tag.bulkCreate([
{name: 'Furniture'},
{name: 'Clothing'},
{name: 'People'}
]).done(callback);
},
tags: ['createTags', function(callback) {
self.Tag.findAll().done(callback);
}]
}, function(err, results) {
expect(err).not.to.exist;
var products = results.products
, tags = results.tags;
async.parallel([
function(callback) {
products[0].setTags([tags[0], tags[1]]).done(callback);
},
function(callback) {
products[1].addTag(tags[0]).done(callback);
},
function(callback) {
products[2].addTag(tags[1]).done(callback);
},
function(callback) {
products[3].setTags([tags[1]]).done(callback);
},
function(callback) {
products[4].setTags([tags[2]]).done(callback);
}
], function(err) {
expect(err).not.to.exist;
async.parallel([
function(callback) {
])
]).then(function() {
return Promise.all([
self.Product.findAll(),
self.Tag.findAll()
]);
}).spread(function(products, tags) {
self.products = products;
self.tags = tags;
return Promise.all([
products[0].setTags([tags[0], tags[1]]),
products[1].addTag(tags[0]),
products[2].addTag(tags[1]),
products[3].setTags([tags[1]]),
products[4].setTags([tags[2]])
]).then(function() {
return Promise.all([
self.Tag.find({
where: {
id: tags[0].id
......@@ -825,19 +802,13 @@ describe(Support.getTestDialectTeaser('Model'), function() {
include: [
{model: self.Product, as: 'products'}
]
}).done(function(err, tag) {
}).then(function(tag) {
expect(tag).to.exist;
expect(tag.products.length).to.equal(2);
callback();
});
},
function(callback) {
tags[1].getProducts().done(function(err, products) {
}),
tags[1].getProducts().then(function(products) {
expect(products.length).to.equal(3);
callback();
});
},
function(callback) {
}),
self.Product.find({
where: {
id: products[0].id
......@@ -845,19 +816,14 @@ describe(Support.getTestDialectTeaser('Model'), function() {
include: [
{model: self.Tag, as: 'tags'}
]
}).done(function(err, product) {
}).then(function(product) {
expect(product).to.exist;
expect(product.tags.length).to.equal(2);
callback();
});
},
function(callback) {
products[1].getTags().done(function(err, tags) {
}),
products[1].getTags().then(function(tags) {
expect(tags.length).to.equal(1);
callback();
});
}
], done);
})
]);
});
});
});
......
......@@ -43,14 +43,14 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
});
describe('increment', function() {
beforeEach(function(done) {
this.User.create({ id: 1, aNumber: 0, bNumber: 0 }).done(done);
beforeEach(function() {
return this.User.create({ id: 1, aNumber: 0, bNumber: 0 });
});
it('with array', function(done) {
it('with array', function() {
var self = this;
this.User
return this.User
.find(1)
.then(function(user) {
expect(user.id).to.equal(1);
......@@ -63,15 +63,14 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
})
.then(function(user) {
expect(user.aNumber).to.equal(2);
done();
});
});
it('should still work right with other concurrent updates', function(done) {
it('should still work right with other concurrent updates', function() {
var self = this;
// Select something
this.User
return this.User
.find(1)
.then(function(user1) {
// Select the user again (simulating a concurrent query)
......@@ -83,16 +82,15 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
.then(function() { return self.User.find(1); })
.then(function(user5) {
expect(user5.aNumber).to.equal(3);
done();
});
});
});
});
it('with key value pair', function(done) {
it('with key value pair', function() {
var self = this;
this.User
return this.User
.find(1)
.then(function(user1) {
return user1.increment({ 'aNumber': 1, 'bNumber': 2});
......@@ -103,20 +101,19 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
.then(function(user3) {
expect(user3.aNumber).to.equal(1);
expect(user3.bNumber).to.equal(2);
done();
});
});
});
describe('decrement', function() {
beforeEach(function(done) {
this.User.create({ id: 1, aNumber: 0, bNumber: 0 }).done(done);
beforeEach(function() {
return this.User.create({ id: 1, aNumber: 0, bNumber: 0 });
});
it('with array', function(done) {
it('with array', function() {
var self = this;
this.User
return this.User
.find(1)
.then(function(user1) {
return user1.decrement(['aNumber'], { by: 2 });
......@@ -126,14 +123,12 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
})
.then(function(user3) {
expect(user3.aNumber).to.equal(-2);
done();
});
});
it('with single field', function(done) {
it('with single field', function() {
var self = this;
this.User
return this.User
.find(1)
.then(function(user1) {
return user1.decrement(['aNumber'], { by: 2 });
......@@ -143,35 +138,32 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
})
.then(function(user3) {
expect(user3.aNumber).to.equal(-2);
done();
});
});
it('should still work right with other concurrent decrements', function(done) {
it('should still work right with other concurrent decrements', function() {
var self = this;
this.User
return this.User
.find(1)
.then(function(user1) {
var _done = _.after(3, function() {
self.User
return this.sequelize.Promise.all([
user1.decrement(['aNumber'], { by: 2 }),
user1.decrement(['aNumber'], { by: 2 }),
user1.decrement(['aNumber'], { by: 2 })
]).then(function() {
return self.User
.find(1)
.then(function(user2) {
expect(user2.aNumber).to.equal(-6);
done();
});
});
user1.decrement(['aNumber'], { by: 2 }).done(_done);
user1.decrement(['aNumber'], { by: 2 }).done(_done);
user1.decrement(['aNumber'], { by: 2 }).done(_done);
});
});
});
describe('reload', function() {
it('should return a reference to the same DAO instead of creating a new one', function(done) {
this.User
it('should return a reference to the same DAO instead of creating a new one', function() {
return this.User
.create({ username: 'John Doe' })
.then(function(originalUser) {
return originalUser
......@@ -181,15 +173,13 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
})
.then(function(updatedUser) {
expect(originalUser === updatedUser).to.be.true;
done();
});
});
});
it('should update the values on all references to the DAO', function(done) {
it('should update the values on all references to the DAO', function() {
var self = this;
this.User
return this.User
.create({ username: 'John Doe' })
.then(function(originalUser) {
return self.User
......@@ -204,23 +194,22 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
}).then(function(updatedUser) {
expect(originalUser.username).to.equal('Doe John');
expect(updatedUser.username).to.equal('Doe John');
done();
});
});
});
it('should update the associations as well', function(done) {
it('should update the associations as well', function() {
var Book = this.sequelize.define('Book', { title: DataTypes.STRING })
, Page = this.sequelize.define('Page', { content: DataTypes.TEXT });
Book.hasMany(Page);
Page.belongsTo(Book);
Book
return Book
.sync({ force: true })
.then(function() {
Page
return Page
.sync({ force: true })
.then(function() {
return Book.create({ title: 'A very old book' });
......@@ -249,49 +238,42 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
.then(function(leBook) {
expect(leBook.Pages[0].content).to.equal('something totally different');
expect(page.content).to.equal('something totally different');
done();
});
});
});
});
});
}, done);
});
});
});
});
describe('complete', function() {
it('gets triggered if an error occurs', function(done) {
this.User.find({ where: 'asdasdasd' }).then(null, function(err) {
expect(err).to.be.ok;
expect(err.message).to.be.ok;
done();
});
it('gets triggered if an error occurs', function() {
return expect( this.User.find({ where: 'asdasdasd' })).to.be.rejected;
});
it('gets triggered if everything was ok', function(done) {
this.User.count().then(function(result) {
it('gets triggered if everything was ok', function() {
return this.User.count().then(function(result) {
expect(result).to.not.be.undefined;
done();
});
});
});
describe('save', function() {
it('should fail a validation upon creating', function(done) {
this.User.create({aNumber: 0, validateTest: 'hello'})
it('should fail a validation upon creating', function() {
return this.User.create({aNumber: 0, validateTest: 'hello'})
.catch (function(err) {
expect(err).to.be.ok;
expect(err).to.be.an('object');
expect(err.get('validateTest')).to.be.an('array');
expect(err.get('validateTest')[0]).to.be.ok;
expect(err.get('validateTest')[0].message).to.equal('Validation isInt failed');
done();
});
});
it('should fail a validation upon building', function(done) {
this.User.build({aNumber: 0, validateCustom: 'aaaaaaaaaaaaaaaaaaaaaaaaaa'}).save()
it('should fail a validation upon building', function() {
return this.User.build({aNumber: 0, validateCustom: 'aaaaaaaaaaaaaaaaaaaaaaaaaa'}).save()
.catch (function(err) {
expect(err).to.be.ok;
expect(err).to.be.an('object');
......@@ -299,12 +281,11 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
expect(err.get('validateCustom')).to.be.an('array');
expect(err.get('validateCustom')[0]).to.be.ok;
expect(err.get('validateCustom')[0].message).to.equal('Length failed.');
done();
});
});
it('should fail a validation when updating', function(done) {
this.User.create({aNumber: 0}).then(function(user) {
it('should fail a validation when updating', function() {
return this.User.create({aNumber: 0}).then(function(user) {
return user.updateAttributes({validateTest: 'hello'});
}).catch (function(err) {
expect(err).to.be.ok;
......@@ -313,36 +294,33 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
expect(err.get('validateTest')).to.be.an('array');
expect(err.get('validateTest')[0]).to.be.ok;
expect(err.get('validateTest')[0].message).to.equal('Validation isInt failed');
done();
});
});
});
describe('findOrCreate', function() {
beforeEach(function(done) {
this.User.create({ id: 1, aNumber: 0, bNumber: 0 }).done(done);
beforeEach(function() {
return this.User.create({ id: 1, aNumber: 0, bNumber: 0 });
});
describe('with spread', function() {
it('user not created', function(done) {
this.User
it('user not created', function() {
return this.User
.findOrCreate({ where: { id: 1}})
.spread(function(user, created) {
expect(user.id).to.equal(1);
expect(created).to.equal(false);
expect(arguments.length).to.equal(2);
done();
});
});
it('user created', function(done) {
this.User
it('user created', function() {
return this.User
.findOrCreate({ where: { id: 2}})
.spread(function(user, created) {
expect(user.id).to.equal(2);
expect(created).to.equal(true);
expect(arguments.length).to.equal(2);
done();
});
});
});
......@@ -377,7 +355,7 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
});
});
it('should still work with .on(\'success\') when resolving', function(done) {
it('should still work with then when resolving', function(done) {
var spy = sinon.spy()
, promise = new SequelizePromise(function(resolve, reject) {
resolve('yoohoo');
......@@ -407,18 +385,17 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
});
});
it('should still work with .complete() after chaining', function(done) {
it('should still work with .complete() after chaining', function() {
var spy = sinon.spy()
, promise = new SequelizePromise(function(resolve, reject) {
resolve('Heyo');
});
promise.then(function(result) {
return promise.then(function(result) {
return result + '123';
}).complete(function(err, result) {
expect(err).not.to.be.ok;
expect(result).to.equal('Heyo123');
done();
});
});
......@@ -496,7 +473,7 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
promise.emit('error', new Error('noway'));
});
it('should still support sql events', function(done) {
it('should still support sql events', function() {
var spy = sinon.spy()
, promise = new SequelizePromise(function(resolve, reject) {
resolve('yay');
......@@ -507,9 +484,8 @@ describe(Support.getTestDialectTeaser('Promise'), function() {
promise.emit('sql', 'SQL STATEMENT 1');
promise.emit('sql', 'SQL STATEMENT 2');
promise.then(function() {
return promise.then(function() {
expect(spy.calledTwice).to.be.true;
done();
});
});
......
......@@ -9,24 +9,21 @@ var chai = require('chai')
chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('QueryChainer'), function() {
beforeEach(function(done) {
beforeEach(function() {
this.queryChainer = new QueryChainer();
done();
});
describe('add', function() {
it('adds a new serial item if method is passed', function(done) {
it('adds a new serial item if method is passed', function() {
expect(this.queryChainer.serials.length).to.equal(0);
this.queryChainer.add({}, 'foo');
expect(this.queryChainer.serials.length).to.equal(1);
done();
});
it('adds a new emitter if no method is passed', function(done) {
it('adds a new emitter if no method is passed', function() {
expect(this.queryChainer.emitters.length).to.equal(0);
this.queryChainer.add(new CustomEventEmitter());
expect(this.queryChainer.emitters.length).to.equal(1);
done();
});
});
......
......@@ -131,26 +131,21 @@ describe(Support.getTestDialectTeaser('Sequelize#transaction'), function() {
});
});
it('triggers the error event for the second transactions', function(done) {
it('triggers the error event for the second transactions', function() {
var self = this;
this.sequelize.transaction().then(function(t1) {
self.sequelize.transaction().then(function(t2) {
self
.Model
.create({ name: 'omnom' }, { transaction: t1 })
.success(function(m1) {
self
.Model
.create({ name: 'omnom' }, { transaction: t2 })
.error(function(err) {
t2.rollback().success(function() {
return this.sequelize.transaction().then(function(t1) {
return self.sequelize.transaction().then(function(t2) {
return self.Model.create({ name: 'omnom' }, { transaction: t1 }).then(function(m1) {
return Promise.all([
self.Model.create({ name: 'omnom' }, { transaction: t2 }).catch(function(err) {
expect(err).to.be.defined;
done();
});
});
setTimeout(function() { t1.commit(); }, 100);
return t2.rollback();
}),
Promise.delay(100).then(function() {
return t1.commit();
})
]);
});
});
});
......
......@@ -57,7 +57,7 @@ var Support = {
, _sequelize = new Sequelize(sequelize.config.database, null, null, options);
if (callback) {
_sequelize.sync({ force: true }).success(function() { callback(_sequelize); });
_sequelize.sync({ force: true }).then(function() { callback(_sequelize); });
} else {
return _sequelize.sync({ force: true }).return (_sequelize);
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!