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

Commit bb05cd14 by Jan Aagaard Meier

refactor(test): Replace promise.delay with fake timers except for transaction tests

1 parent f778e402
...@@ -14,6 +14,14 @@ var chai = require('chai') ...@@ -14,6 +14,14 @@ var chai = require('chai')
, current = Support.sequelize; , current = Support.sequelize;
describe(Support.getTestDialectTeaser('Instance'), function() { describe(Support.getTestDialectTeaser('Instance'), function() {
before(function () {
this.clock = sinon.useFakeTimers();
});
after(function () {
this.clock.restore();
});
beforeEach(function() { beforeEach(function() {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: { type: DataTypes.STRING }, username: { type: DataTypes.STRING },
...@@ -242,19 +250,18 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -242,19 +250,18 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
it('with timestamps set to true', function() { it('with timestamps set to true', function() {
var User = this.sequelize.define('IncrementUser', { var User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER aNumber: DataTypes.INTEGER
}, { timestamps: true }); }, { timestamps: true })
, oldDate;
return User.sync({ force: true }).then(function() {
return User.create({aNumber: 1}).then(function(user) { return User.sync({ force: true }).bind(this).then(function() {
var oldDate = user.updatedAt; return User.create({aNumber: 1});
return this.sequelize.Promise.delay(1000).then(function() { }).then(function(user) {
return user.increment('aNumber', { by: 1 }).then(function() { oldDate = user.updatedAt;
return User.findById(1).then(function(user) {
expect(user.updatedAt).to.be.afterTime(oldDate); this.clock.tick(1000);
}); return user.increment('aNumber', {by: 1});
}); }).then(function() {
}); return expect(User.findById(1)).to.eventually.have.property('updatedAt').afterTime(oldDate);
});
}); });
}); });
}); });
...@@ -369,19 +376,17 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -369,19 +376,17 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
it('with timestamps set to true', function() { it('with timestamps set to true', function() {
var User = this.sequelize.define('IncrementUser', { var User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER aNumber: DataTypes.INTEGER
}, { timestamps: true }); }, { timestamps: true })
, oldDate;
return User.sync({ force: true }).then(function() {
return User.create({aNumber: 1}).then(function(user) { return User.sync({ force: true }).bind(this).then(function() {
var oldDate = user.updatedAt; return User.create({aNumber: 1});
return this.sequelize.Promise.delay(1000).then(function() { }).then(function(user) {
return user.decrement('aNumber', { by: 1 }).then(function() { oldDate = user.updatedAt;
return User.findById(1).then(function(user) { this.clock.tick(1000);
expect(user.updatedAt).to.be.afterTime(oldDate); return user.decrement('aNumber', {by: 1});
}); }).then(function() {
}); return expect(User.findById(1)).to.eventually.have.property('updatedAt').afterTime(oldDate);
});
});
}); });
}); });
}); });
...@@ -460,22 +465,21 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -460,22 +465,21 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}); });
it('should update read only attributes as well (updatedAt)', function() { it('should update read only attributes as well (updatedAt)', function() {
var self = this; return this.User.create({ username: 'John Doe' }).bind(this).then(function(originalUser) {
this.originallyUpdatedAt = originalUser.updatedAt;
return this.User.create({ username: 'John Doe' }).then(function(originalUser) { this.originalUser = originalUser;
var originallyUpdatedAt = originalUser.updatedAt;
// Wait for a second, so updatedAt will actually be different // Wait for a second, so updatedAt will actually be different
return this.sequelize.Promise.delay(1000).then(function() { this.clock.tick(1000);
return self.User.findById(originalUser.id).then(function(updater) { return this.User.findById(originalUser.id);
return updater.updateAttributes({ username: 'Doe John' }).then(function() { }).then(function(updater) {
return originalUser.reload().then(function(updatedUser) { return updater.updateAttributes({username: 'Doe John'});
expect(originalUser.updatedAt).to.be.above(originallyUpdatedAt); }).then(function(updatedUser) {
expect(updatedUser.updatedAt).to.be.above(originallyUpdatedAt); this.updatedUser = updatedUser;
}); return this.originalUser.reload();
}); }).then(function() {
}); expect(this.originalUser.updatedAt).to.be.above(this.originallyUpdatedAt);
}); expect(this.updatedUser.updatedAt).to.be.above(this.originallyUpdatedAt);
}); });
}); });
...@@ -1100,31 +1104,24 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1100,31 +1104,24 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}); });
it('updates the timestamps', function() { it('updates the timestamps', function() {
var now = Date.now() var now = new Date()
, user = null , user = null;
, User = this.User;
// timeout is needed, in order to check the update of the timestamp user = this.User.build({ username: 'user' });
return this.sequelize.Promise.delay(1000).then(function() { this.clock.tick(1000);
user = User.build({ username: 'user' }); return expect(user.save()).to.eventually.have.property('updatedAt').afterTime(now);
return user.save().then(function() {
expect(now).to.be.below(user.updatedAt.getTime());
});
});
}); });
it('does not update timestamps when passing silent=true', function() { it('does not update timestamps when passing silent=true', function() {
return this.User.create({ username: 'user' }).then(function(user) { return this.User.create({ username: 'user' }).bind(this).then(function(user) {
var updatedAt = user.updatedAt; var updatedAt = user.updatedAt;
return this.sequelize.Promise.delay(2000).then(function() {
return user.update({ this.clock.tick(1000);
username: 'userman' return expect(user.update({
}, { username: 'userman'
silent: true }, {
}).then(function(user1) { silent: true
expect(user1.updatedAt).to.equalTime(updatedAt); })).to.eventually.have.property('updatedAt').equalTime(updatedAt);
});
});
}); });
}); });
...@@ -1137,17 +1134,15 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1137,17 +1134,15 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
{ username: 'Peter' } { username: 'Peter' }
]; ];
return this.User.bulkCreate(data).then(function() { return this.User.bulkCreate(data).bind(this).then(function() {
return self.User.findAll(); return this.User.findAll();
}).then(function(users) { }).then(function(users) {
updatedAtPaul = users[0].updatedAt; updatedAtPaul = users[0].updatedAt;
updatedAtPeter = users[1].updatedAt; updatedAtPeter = users[1].updatedAt;
}) })
.then(function() { .then(function() {
return self.sequelize.Promise.delay(150); this.clock.tick(150);
}) return this.User.update(
.then(function() {
return self.User.update(
{ aNumber: 1 }, { aNumber: 1 },
{ where: {}, silent: true } { where: {}, silent: true }
); );
...@@ -1216,9 +1211,10 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1216,9 +1211,10 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}); });
describe('with custom timestamp options', function() { describe('with custom timestamp options', function() {
var now = Date.now();
it('updates the createdAt column if updatedAt is disabled', function() { it('updates the createdAt column if updatedAt is disabled', function() {
var now = new Date();
this.clock.tick(1000);
var User2 = this.sequelize.define('User2', { var User2 = this.sequelize.define('User2', {
username: DataTypes.STRING username: DataTypes.STRING
}, { updatedAt: false }); }, { updatedAt: false });
...@@ -1226,12 +1222,15 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1226,12 +1222,15 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
return User2.sync().then(function() { return User2.sync().then(function() {
return User2.create({ username: 'john doe' }).then(function(johnDoe) { return User2.create({ username: 'john doe' }).then(function(johnDoe) {
expect(johnDoe.updatedAt).to.be.undefined; expect(johnDoe.updatedAt).to.be.undefined;
expect(now).to.be.below(johnDoe.createdAt.getTime()); expect(now).to.be.beforeTime(johnDoe.createdAt);
}); });
}); });
}); });
it('updates the updatedAt column if createdAt is disabled', function() { it('updates the updatedAt column if createdAt is disabled', function() {
var now = new Date();
this.clock.tick(1000);
var User2 = this.sequelize.define('User2', { var User2 = this.sequelize.define('User2', {
username: DataTypes.STRING username: DataTypes.STRING
}, { createdAt: false }); }, { createdAt: false });
...@@ -1239,7 +1238,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1239,7 +1238,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
return User2.sync().then(function() { return User2.sync().then(function() {
return User2.create({ username: 'john doe' }).then(function(johnDoe) { return User2.create({ username: 'john doe' }).then(function(johnDoe) {
expect(johnDoe.createdAt).to.be.undefined; expect(johnDoe.createdAt).to.be.undefined;
expect(now).to.be.below(johnDoe.updatedAt.getTime()); expect(now).to.be.beforeTime(johnDoe.updatedAt);
}); });
}); });
}); });
......
...@@ -12,6 +12,13 @@ var chai = require('chai') ...@@ -12,6 +12,13 @@ var chai = require('chai')
, current = Support.sequelize; , current = Support.sequelize;
describe(Support.getTestDialectTeaser('Instance'), function() { describe(Support.getTestDialectTeaser('Instance'), function() {
before(function () {
this.clock = sinon.useFakeTimers();
});
after(function () {
this.clock.restore();
});
describe('update', function() { describe('update', function() {
beforeEach(function () { beforeEach(function () {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
...@@ -347,26 +354,25 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -347,26 +354,25 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
identifier: {type: DataTypes.STRING, primaryKey: true} identifier: {type: DataTypes.STRING, primaryKey: true}
}); });
return User.sync({ force: true }).then(function() { return User.sync({ force: true }).bind(this).then(function() {
return User.create({ return User.create({
name: 'snafu', name: 'snafu',
identifier: 'identifier' identifier: 'identifier'
}).then(function(user) { });
var oldCreatedAt = user.createdAt }).then(function(user) {
, oldUpdatedAt = user.updatedAt var oldCreatedAt = user.createdAt
, oldIdentifier = user.identifier; , oldUpdatedAt = user.updatedAt
, oldIdentifier = user.identifier;
return this.sequelize.Promise.delay(1000).then(function() { this.clock.tick(1000);
return user.update({ return user.update({
name: 'foobar', name: 'foobar',
createdAt: new Date(2000, 1, 1), createdAt: new Date(2000, 1, 1),
identifier: 'another identifier' identifier: 'another identifier'
}).then(function(user) { }).then(function(user) {
expect(new Date(user.createdAt)).to.equalDate(new Date(oldCreatedAt)); expect(new Date(user.createdAt)).to.equalDate(new Date(oldCreatedAt));
expect(new Date(user.updatedAt)).to.not.equalTime(new Date(oldUpdatedAt)); expect(new Date(user.updatedAt)).to.not.equalTime(new Date(oldUpdatedAt));
expect(user.identifier).to.equal(oldIdentifier); expect(user.identifier).to.equal(oldIdentifier);
});
});
}); });
}); });
}); });
......
...@@ -15,6 +15,14 @@ var chai = require('chai') ...@@ -15,6 +15,14 @@ var chai = require('chai')
, current = Support.sequelize; , current = Support.sequelize;
describe(Support.getTestDialectTeaser('Model'), function() { describe(Support.getTestDialectTeaser('Model'), function() {
before(function () {
this.clock = sinon.useFakeTimers();
});
after(function () {
this.clock.restore();
});
beforeEach(function() { beforeEach(function() {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: DataTypes.STRING, username: DataTypes.STRING,
...@@ -1080,9 +1088,8 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1080,9 +1088,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(this.updatedAt).to.equalTime(users[2].updatedAt); // All users should have the same updatedAt expect(this.updatedAt).to.equalTime(users[2].updatedAt); // All users should have the same updatedAt
// Pass the time so we can actually see a change // Pass the time so we can actually see a change
return this.sequelize.Promise.delay(1000).bind(this).then(function() { this.clock.tick(1000);
return this.User.update({username: 'Bill'}, {where: {secretValue: '42'}}); return this.User.update({username: 'Bill'}, {where: {secretValue: '42'}});
});
}).then(function() { }).then(function() {
return this.User.findAll({order: 'id'}); return this.User.findAll({order: 'id'});
}).then(function(users) { }).then(function(users) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!