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

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 @@ ...@@ -2,4 +2,4 @@
The entry point. The entry point.
@module Sequelize @module Sequelize
**/ **/
module.exports = require("./lib/sequelize") module.exports = require("./lib/sequelize");
...@@ -203,7 +203,7 @@ module.exports = (function() { ...@@ -203,7 +203,7 @@ module.exports = (function() {
options = Utils._.extend({ options = Utils._.extend({
events: proxyEventKeys, events: proxyEventKeys,
skipEvents: [] skipEvents: []
}, options ||  {}); }, options || {});
options.events = Utils._.difference(options.events, options.skipEvents); options.events = Utils._.difference(options.events, options.skipEvents);
......
...@@ -81,11 +81,11 @@ module.exports = (function() { ...@@ -81,11 +81,11 @@ module.exports = (function() {
return association.source.update(newValues, { where: query }); return association.source.update(newValues, { where: query });
}); });
}, },
increment: function (targetId) { increment: function (targetId) {
var query = CounterUtil._sourceQuery(targetId); var query = CounterUtil._sourceQuery(targetId);
return association.source.find({ where: query }).then(function (instance) { 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) { decrement: function (targetId) {
......
...@@ -43,7 +43,6 @@ ...@@ -43,7 +43,6 @@
"validator": "^3.34.0" "validator": "^3.34.0"
}, },
"devDependencies": { "devDependencies": {
"async": "~0.9.0",
"chai": "^2.1.2", "chai": "^2.1.2",
"chai-as-promised": "^4.3.0", "chai-as-promised": "^4.3.0",
"chai-datetime": "~1.3.0", "chai-datetime": "~1.3.0",
......
...@@ -1793,7 +1793,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() { ...@@ -1793,7 +1793,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
self.User.belongsToMany(self.Task, { onDelete: 'RESTRICT'}); self.User.belongsToMany(self.Task, { onDelete: 'RESTRICT'});
self.Task.belongsToMany(self.User, { onDelete: 'CASCADE'}); 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( return Sequelize.Promise.join(
self.User.create({ id: 67, username: 'foo' }), self.User.create({ id: 67, username: 'foo' }),
self.Task.create({ id: 52, title: 'task' }), self.Task.create({ id: 52, title: 'task' }),
......
...@@ -13,7 +13,7 @@ if (dialect.match(/^postgres/)) { ...@@ -13,7 +13,7 @@ if (dialect.match(/^postgres/)) {
describe('[POSTGRES Specific] associations', function() { describe('[POSTGRES Specific] associations', function() {
describe('many-to-many', function() { describe('many-to-many', function() {
describe('where tables have the same prefix', 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}) var Table2 = this.sequelize.define('wp_table2', {foo: DataTypes.STRING})
, Table1 = this.sequelize.define('wp_table1', {foo: DataTypes.STRING}); , Table1 = this.sequelize.define('wp_table1', {foo: DataTypes.STRING});
...@@ -21,35 +21,24 @@ if (dialect.match(/^postgres/)) { ...@@ -21,35 +21,24 @@ if (dialect.match(/^postgres/)) {
Table2.hasMany(Table1); Table2.hasMany(Table1);
expect(this.sequelize.daoFactoryManager.getDAO('wp_table1swp_table2s')).to.exist; expect(this.sequelize.daoFactoryManager.getDAO('wp_table1swp_table2s')).to.exist;
done();
}); });
}); });
describe('when join table name is specified', function() { describe('when join table name is specified', function() {
beforeEach(function(done) { beforeEach(function() {
var Table2 = this.sequelize.define('ms_table1', {foo: DataTypes.STRING}) var Table2 = this.sequelize.define('ms_table1', {foo: DataTypes.STRING})
, Table1 = this.sequelize.define('ms_table2', {foo: DataTypes.STRING}); , Table1 = this.sequelize.define('ms_table2', {foo: DataTypes.STRING});
Table1.hasMany(Table2, {joinTableName: 'table1_to_table2'}); Table1.hasMany(Table2, {joinTableName: 'table1_to_table2'});
Table2.hasMany(Table1, {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; 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; expect(this.sequelize.daoFactoryManager.getDAO('table1_to_table2')).to.exist;
setTimeout(function() {
done();
}, 50);
}); });
}); });
}); });
......
...@@ -363,38 +363,37 @@ if (dialect.match(/^postgres/)) { ...@@ -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 var self = this
, User = this.sequelize.define('UserEnums', { , User = this.sequelize.define('UserEnums', {
mood: DataTypes.ENUM('happy', 'sad', 'meh') mood: DataTypes.ENUM('happy', 'sad', 'meh')
}); })
, count = 0;
var _done = _.after(4, function() {
done();
});
User.sync({ force: true }).then(function() { return User.sync({ force: true }).then(function() {
User = self.sequelize.define('UserEnums', { User = self.sequelize.define('UserEnums', {
mood: DataTypes.ENUM('neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful') 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']); expect(User.rawAttributes.mood.values).to.deep.equal(['neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful']);
_done(); count++;
}).on('sql', function(sql) { }).on('sql', function(sql) {
if (sql.indexOf('neutral') > -1) { if (sql.indexOf('neutral') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'neutral' BEFORE 'happy'"); expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'neutral' BEFORE 'happy'");
_done(); count++;
} }
else if (sql.indexOf('ecstatic') > -1) { else if (sql.indexOf('ecstatic') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'ecstatic' BEFORE 'meh'"); expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'ecstatic' BEFORE 'meh'");
_done(); count++;
} }
else if (sql.indexOf('joyful') > -1) { else if (sql.indexOf('joyful') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'joyful' AFTER 'meh'"); 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() { ...@@ -318,9 +318,9 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Task.belongsTo(Project); Task.belongsTo(Project);
return this.sequelize.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function() {
return Promise.all([ return Project.bulkCreate([{ id: 1 }, { id: 2 }]);
Project.bulkCreate([{ id: 1 }, { id: 2 }]), }).then(function (projects) {
User.create({ return User.create({
Tasks: [ Tasks: [
{ProjectId: 1}, {ProjectId: 1},
{ProjectId: 2}, {ProjectId: 2},
...@@ -329,9 +329,8 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -329,9 +329,8 @@ describe(Support.getTestDialectTeaser('Include'), function() {
] ]
}, { }, {
include: [Task] include: [Task]
}) });
]); }).then(function (user) {
}).spread(function (projects, user) {
return User.find({ return User.find({
where: { where: {
id: user.id id: user.id
......
...@@ -6,7 +6,6 @@ var chai = require('chai') ...@@ -6,7 +6,6 @@ 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')
, Promise = Sequelize.Promise , Promise = Sequelize.Promise
, _ = require('lodash'); , _ = require('lodash');
...@@ -128,7 +127,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -128,7 +127,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
, tags = results.tags , tags = results.tags
, companies = results.companies; , 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({ return Promise.props({
user: User.create(), user: User.create(),
products: Product.bulkCreate([ products: Product.bulkCreate([
...@@ -195,7 +194,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -195,7 +194,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
]) ])
); );
}); });
}, []); });
}); });
}); });
}; };
...@@ -380,98 +379,66 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -380,98 +379,66 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Group.hasMany(GroupMember, {as: 'Memberships'}); Group.hasMany(GroupMember, {as: 'Memberships'});
return this.sequelize.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function() {
return new Promise(function (resolve, reject) { return Promise.all([
var count = 4
, i = -1;
async.auto({
groups: function(callback) {
Group.bulkCreate([ Group.bulkCreate([
{name: 'Developers'}, {name: 'Developers'},
{name: 'Designers'} {name: 'Designers'}
]).then(function() { ]).then(function() {
return Group.findAll(); return Group.findAll();
}).nodeify(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}
]).then(function() { ]).then(function() {
return Rank.findAll(); return Rank.findAll();
}).nodeify(callback); }),
},
tags: function(callback) {
Tag.bulkCreate([ Tag.bulkCreate([
{name: 'A'}, {name: 'A'},
{name: 'B'}, {name: 'B'},
{name: 'C'} {name: 'C'}
]).then(function() { ]).then(function() {
return Tag.findAll(); return Tag.findAll();
}).nodeify(callback); })
}, ]).spread(function(groups, ranks, tags) {
loop: ['groups', 'ranks', 'tags', function(done, results) { return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) {
var groups = results.groups return Promise.all([
, ranks = results.ranks User.create(),
, 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) {
Product.bulkCreate([ Product.bulkCreate([
{title: 'Chair'}, {title: 'Chair'},
{title: 'Desk'} {title: 'Desk'}
]).then(function() { ]).then(function() {
return Product.findAll(); return Product.findAll();
}).nodeify(callback); })
}, ]).spread(function(user, products) {
userProducts: ['user', 'products', function(callback, results) { return Promise.all([
results.user.setProducts([ GroupMember.bulkCreate([
results.products[(i * 2) + 0], {UserId: user.id, GroupId: groups[0].id, RankId: ranks[0].id},
results.products[(i * 2) + 1] {UserId: user.id, GroupId: groups[1].id, RankId: ranks[1].id}
]).nodeify(callback); ]),
}], user.setProducts([
productTags: ['products', function(callback, results) { products[(i * 2) + 0],
return Promise.join( products[(i * 2) + 1]
results.products[(i * 2) + 0].setTags([ ]),
products[(i * 2) + 0].setTags([
tags[0], tags[0],
tags[2] tags[2]
]), ]),
results.products[(i * 2) + 1].setTags([ products[(i * 2) + 1].setTags([
tags[1] tags[1]
]), ]),
results.products[(i * 2) + 0].setCategory(tags[1]) products[(i * 2) + 0].setCategory(tags[1]),
).nodeify(callback);
}],
prices: ['products', function(callback, results) {
Price.bulkCreate([ Price.bulkCreate([
{ProductId: results.products[(i * 2) + 0].id, value: 5}, {ProductId: products[(i * 2) + 0].id, value: 5},
{ProductId: results.products[(i * 2) + 0].id, value: 10}, {ProductId: products[(i * 2) + 0].id, value: 10},
{ProductId: results.products[(i * 2) + 1].id, value: 5}, {ProductId: products[(i * 2) + 1].id, value: 5},
{ProductId: results.products[(i * 2) + 1].id, value: 10}, {ProductId: products[(i * 2) + 1].id, value: 10},
{ProductId: results.products[(i * 2) + 1].id, value: 15}, {ProductId: products[(i * 2) + 1].id, value: 15},
{ProductId: results.products[(i * 2) + 1].id, value: 20} {ProductId: products[(i * 2) + 1].id, value: 20}
]).nodeify(callback); ])
}] ]);
}, callback); });
}, }).then(function() {
function(err) { return User.findAll({
User.findAll({
include: [ include: [
{model: GroupMember, as: 'Memberships', include: [ {model: GroupMember, as: 'Memberships', include: [
Group, Group,
...@@ -506,13 +473,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -506,13 +473,7 @@ 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);
}); });
}).nodeify(done); });
}
);
}]
}, function (err) {
if (err) return reject(err);
resolve();
}); });
}); });
}); });
...@@ -1290,32 +1251,28 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1290,32 +1251,28 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Group.hasMany(GroupMember, {as: 'Memberships'}); Group.hasMany(GroupMember, {as: 'Memberships'});
return this.sequelize.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function() {
return Promise.props({ return Promise.all([
groups: Group.bulkCreate([ Group.bulkCreate([
{name: 'Developers'}, {name: 'Developers'},
{name: 'Designers'} {name: 'Designers'}
]).then(function() { ]).then(function() {
return Group.findAll(); return Group.findAll();
}), }),
ranks: 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}
]).then(function() { ]).then(function() {
return Rank.findAll(); return Rank.findAll();
}), }),
tags: Tag.bulkCreate([ Tag.bulkCreate([
{name: 'A'}, {name: 'A'},
{name: 'B'}, {name: 'B'},
{name: 'C'} {name: 'C'}
]).then(function() { ]).then(function() {
return Tag.findAll(); return Tag.findAll();
}) })
}).then(function (results) { ]).spread(function (groups, ranks, tags) {
var groups = results.groups return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) {
, ranks = results.ranks
, tags = results.tags;
return Promise.reduce([0, 1, 2, 3, 4], function (memo, i) {
return Promise.props({ return Promise.props({
user: User.create({name: 'FooBarzz'}), user: User.create({name: 'FooBarzz'}),
products: Product.bulkCreate([ products: Product.bulkCreate([
...@@ -1354,7 +1311,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -1354,7 +1311,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
]) ])
); );
}); });
}, []); });
}).then(function () { }).then(function () {
return User.findAll({ return User.findAll({
include: [ include: [
......
...@@ -341,11 +341,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -341,11 +341,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
it('should still work right with other concurrent increments', function() { it('should still work right with other concurrent increments', function() {
var self = this; var self = this;
return this.User.find(1).then(function(user1) { return this.User.find(1).then(function(user1) {
return user1.increment(['aNumber'], { by: 2 }).then(function() { return this.sequelize.Promise.all([
return user1.increment(['aNumber'], { by: 2 }); user1.increment(['aNumber'], { by: 2 }),
}).then(function() { user1.increment(['aNumber'], { by: 2 }),
return user1.increment(['aNumber'], { by: 2 }); user1.increment(['aNumber'], { by: 2 })
}).then(function() { ]).then(function() {
return self.User.find(1).then(function(user2) { return self.User.find(1).then(function(user2) {
expect(user2.aNumber).to.equal(6); expect(user2.aNumber).to.equal(6);
}); });
...@@ -468,11 +468,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -468,11 +468,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
it('should still work right with other concurrent increments', function() { it('should still work right with other concurrent increments', function() {
var self = this; var self = this;
return this.User.find(1).then(function(user1) { return this.User.find(1).then(function(user1) {
return user1.decrement(['aNumber'], { by: 2 }).then(function() { return this.sequelize.Promise.all([
return user1.decrement(['aNumber'], { by: 2 }); user1.decrement(['aNumber'], { by: 2 }),
}).then(function() { user1.decrement(['aNumber'], { by: 2 }),
return user1.decrement(['aNumber'], { by: 2 }); user1.decrement(['aNumber'], { by: 2 })
}).then(function() { ]).then(function() {
return self.User.find(1).then(function(user2) { return self.User.find(1).then(function(user2) {
expect(user2.aNumber).to.equal(-6); expect(user2.aNumber).to.equal(-6);
}); });
......
...@@ -11,7 +11,6 @@ var chai = require('chai') ...@@ -11,7 +11,6 @@ var chai = require('chai')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, _ = require('lodash') , _ = require('lodash')
, moment = require('moment') , moment = require('moment')
, async = require('async')
, current = Support.sequelize; , current = Support.sequelize;
chai.use(datetime); chai.use(datetime);
...@@ -2348,12 +2347,13 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -2348,12 +2347,13 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
if (dialect !== 'sqlite' && current.dialect.supports.transactions) { if (dialect !== 'sqlite' && current.dialect.supports.transactions) {
it('supports multiple async transactions', function(done) { it('supports multiple async transactions', function() {
this.timeout(25000); this.timeout(25000);
var self = this;
return Support.prepareTransactionTest(this.sequelize).bind({}).then(function(sequelize) { return Support.prepareTransactionTest(this.sequelize).bind({}).then(function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING }); var User = sequelize.define('User', { username: Sequelize.STRING });
var testAsync = function(i, done) { var testAsync = function() {
sequelize.transaction().then(function(t) { return sequelize.transaction().then(function(t) {
return User.create({ return User.create({
username: 'foo' username: 'foo'
}, { }, {
...@@ -2380,14 +2380,19 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -2380,14 +2380,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}).then(function(t) { }).then(function(t) {
return t.rollback(); return t.rollback();
}).nodeify(done); });
}; };
User.sync({ force: true }).then(function() { return User.sync({ force: true }).then(function() {
var tasks = []; var tasks = [];
for (var i = 0; i < 1000; i++) { 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() { ...@@ -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 var self = this
, data = { title: 'Iliad' } , data = { title: 'Iliad' }
, dataTypes = [Sequelize.INTEGER, Sequelize.BIGINT] , dataTypes = [Sequelize.INTEGER, Sequelize.BIGINT]
, chain = new Sequelize.Utils.QueryChainer() , sync = []
, chain2 = new Sequelize.Utils.QueryChainer() , promises = []
, books = []; , books = [];
dataTypes.forEach(function(dataType, index) { dataTypes.forEach(function(dataType, index) {
...@@ -940,21 +940,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -940,21 +940,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
books.forEach(function(b) { books.forEach(function(b) {
chain.add(b.sync({ force: true })); sync.push(b.sync({ force: true }));
}); });
chain.run().then(function() { return Promise.all(sync).then(function() {
books.forEach(function(b) { books.forEach(function(b, index) {
chain2.add(b.create(data)); promises.push(b.create(data).then(function(book) {
});
chain2.run().then(function(results) {
results.forEach(function(book, index) {
expect(book.title).to.equal(data.title); expect(book.title).to.equal(data.title);
expect(book.author).to.equal(data.author); expect(book.author).to.equal(data.author);
expect(books[index].rawAttributes.id.type instanceof dataTypes[index]).to.be.ok; 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') ...@@ -11,7 +11,6 @@ var chai = require('chai')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, promised = require('chai-as-promised') , promised = require('chai-as-promised')
, _ = require('lodash') , _ = require('lodash')
, async = require('async')
, current = Support.sequelize; , current = Support.sequelize;
chai.use(promised); chai.use(promised);
...@@ -760,64 +759,42 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -760,64 +759,42 @@ describe(Support.getTestDialectTeaser('Model'), function() {
this.Tag = this.sequelize.define('Tag', { name: Sequelize.STRING }); 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; var self = this;
this.Product.hasMany(this.Tag, {as: 'tags', through: 'product_tag'}); this.Product.hasMany(this.Tag, {as: 'tags', through: 'product_tag'});
this.Tag.hasMany(this.Product, {as: 'products', through: 'product_tag'}); this.Tag.hasMany(this.Product, {as: 'products', through: 'product_tag'});
this.sequelize.sync().done(function() { return this.sequelize.sync().then(function() {
async.auto({ return Promise.all([
createProducts: function(callback) {
self.Product.bulkCreate([ self.Product.bulkCreate([
{title: 'Chair'}, {title: 'Chair'},
{title: 'Desk'}, {title: 'Desk'},
{title: 'Handbag'}, {title: 'Handbag'},
{title: 'Dress'}, {title: 'Dress'},
{title: 'Jan'} {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([ self.Tag.bulkCreate([
{name: 'Furniture'}, {name: 'Furniture'},
{name: 'Clothing'}, {name: 'Clothing'},
{name: 'People'} {name: 'People'}
]).done(callback); ])
}, ]).then(function() {
tags: ['createTags', function(callback) { return Promise.all([
self.Tag.findAll().done(callback); self.Product.findAll(),
}] self.Tag.findAll()
}, function(err, results) { ]);
expect(err).not.to.exist; }).spread(function(products, tags) {
self.products = products;
var products = results.products self.tags = tags;
, tags = results.tags; return Promise.all([
products[0].setTags([tags[0], tags[1]]),
async.parallel([ products[1].addTag(tags[0]),
function(callback) { products[2].addTag(tags[1]),
products[0].setTags([tags[0], tags[1]]).done(callback); products[3].setTags([tags[1]]),
}, products[4].setTags([tags[2]])
function(callback) { ]).then(function() {
products[1].addTag(tags[0]).done(callback); return Promise.all([
},
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) {
self.Tag.find({ self.Tag.find({
where: { where: {
id: tags[0].id id: tags[0].id
...@@ -825,19 +802,13 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -825,19 +802,13 @@ describe(Support.getTestDialectTeaser('Model'), function() {
include: [ include: [
{model: self.Product, as: 'products'} {model: self.Product, as: 'products'}
] ]
}).done(function(err, tag) { }).then(function(tag) {
expect(tag).to.exist; expect(tag).to.exist;
expect(tag.products.length).to.equal(2); expect(tag.products.length).to.equal(2);
callback(); }),
}); tags[1].getProducts().then(function(products) {
},
function(callback) {
tags[1].getProducts().done(function(err, products) {
expect(products.length).to.equal(3); expect(products.length).to.equal(3);
callback(); }),
});
},
function(callback) {
self.Product.find({ self.Product.find({
where: { where: {
id: products[0].id id: products[0].id
...@@ -845,19 +816,14 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -845,19 +816,14 @@ describe(Support.getTestDialectTeaser('Model'), function() {
include: [ include: [
{model: self.Tag, as: 'tags'} {model: self.Tag, as: 'tags'}
] ]
}).done(function(err, product) { }).then(function(product) {
expect(product).to.exist; expect(product).to.exist;
expect(product.tags.length).to.equal(2); expect(product.tags.length).to.equal(2);
callback(); }),
}); products[1].getTags().then(function(tags) {
},
function(callback) {
products[1].getTags().done(function(err, tags) {
expect(tags.length).to.equal(1); expect(tags.length).to.equal(1);
callback(); })
}); ]);
}
], done);
}); });
}); });
}); });
......
...@@ -9,24 +9,21 @@ var chai = require('chai') ...@@ -9,24 +9,21 @@ var chai = require('chai')
chai.config.includeStack = true; chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('QueryChainer'), function() { describe(Support.getTestDialectTeaser('QueryChainer'), function() {
beforeEach(function(done) { beforeEach(function() {
this.queryChainer = new QueryChainer(); this.queryChainer = new QueryChainer();
done();
}); });
describe('add', function() { 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); expect(this.queryChainer.serials.length).to.equal(0);
this.queryChainer.add({}, 'foo'); this.queryChainer.add({}, 'foo');
expect(this.queryChainer.serials.length).to.equal(1); 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); expect(this.queryChainer.emitters.length).to.equal(0);
this.queryChainer.add(new CustomEventEmitter()); this.queryChainer.add(new CustomEventEmitter());
expect(this.queryChainer.emitters.length).to.equal(1); expect(this.queryChainer.emitters.length).to.equal(1);
done();
}); });
}); });
......
...@@ -131,26 +131,21 @@ describe(Support.getTestDialectTeaser('Sequelize#transaction'), function() { ...@@ -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; var self = this;
this.sequelize.transaction().then(function(t1) { return this.sequelize.transaction().then(function(t1) {
self.sequelize.transaction().then(function(t2) { return self.sequelize.transaction().then(function(t2) {
self return self.Model.create({ name: 'omnom' }, { transaction: t1 }).then(function(m1) {
.Model return Promise.all([
.create({ name: 'omnom' }, { transaction: t1 }) self.Model.create({ name: 'omnom' }, { transaction: t2 }).catch(function(err) {
.success(function(m1) {
self
.Model
.create({ name: 'omnom' }, { transaction: t2 })
.error(function(err) {
t2.rollback().success(function() {
expect(err).to.be.defined; expect(err).to.be.defined;
done(); return t2.rollback();
}); }),
}); Promise.delay(100).then(function() {
return t1.commit();
setTimeout(function() { t1.commit(); }, 100); })
]);
}); });
}); });
}); });
......
...@@ -57,7 +57,7 @@ var Support = { ...@@ -57,7 +57,7 @@ var Support = {
, _sequelize = new Sequelize(sequelize.config.database, null, null, options); , _sequelize = new Sequelize(sequelize.config.database, null, null, options);
if (callback) { if (callback) {
_sequelize.sync({ force: true }).success(function() { callback(_sequelize); }); _sequelize.sync({ force: true }).then(function() { callback(_sequelize); });
} else { } else {
return _sequelize.sync({ force: true }).return (_sequelize); 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!