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

Commit a1a13bd1 by Ruben Bridgewater

Refactor transaction test to promise style

Refactor a couple promise tests to use promise style

Refactor another schema test

Fix bad characters used

Refactor the last schema tests to use promises

Use sqlite specifics on timezone test

Refactor last model test to use promises and fix sqlite test

Abandon async from tests

Remove async devDependency
1 parent c4cbb3f3
......@@ -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' }),
......
......@@ -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: [
......
......@@ -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
});
});
});
});
......
......@@ -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!