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

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: [
......
......@@ -7,7 +7,7 @@ var chai = require('chai')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, datetime = require('chai-datetime')
, Promise = Sequelize.Promise
, async = require('async')
, dialect = Support.getTestDialect()
, _ = require('lodash');
chai.use(datetime);
......@@ -126,7 +126,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
Tag.findAll()
]);
}).spread(function (groups, companies, ranks, tags) {
return Promise.reduce(_.range(5), function (memo, i) {
return Promise.resolve([0, 1, 2, 3, 4]).each(function (i) {
return Promise.all([
AccUser.create(),
Product.bulkCreate([
......@@ -189,7 +189,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
])
);
});
}, []);
});
});
});
});
......@@ -197,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
......@@ -249,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,
......@@ -357,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);
......@@ -378,14 +345,9 @@ 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);
});
});
});
});
......@@ -953,7 +915,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
});
});
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'})
......@@ -1003,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,
......@@ -1115,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);
});
});
});
......@@ -1261,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'})
......@@ -1282,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));
}
});
});
});
......
......@@ -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
});
});
});
});
......
......@@ -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,45 @@ 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) {
it('gets triggered if an error occurs', function() {
return this.User.find({ where: 'asdasdasd' }).catch(function(err) {
expect(err).to.be.ok;
expect(err.message).to.be.ok;
done();
});
});
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 +284,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 +297,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 +358,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 +388,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 +476,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 +487,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();
});
});
......
......@@ -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!