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

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