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

Commit b49a24d4 by Andy Edwards Committed by GitHub

fix(test/integration/hooks): asyncify (#12251)

1 parent 4dbfb5d1
......@@ -6,7 +6,7 @@ const chai = require('chai'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: {
type: DataTypes.STRING,
......@@ -17,12 +17,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
values: ['happy', 'sad', 'neutral']
}
});
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
describe('#count', () => {
beforeEach(function() {
return this.User.bulkCreate([
beforeEach(async function() {
await this.User.bulkCreate([
{ username: 'adam', mood: 'happy' },
{ username: 'joe', mood: 'sad' },
{ username: 'joe', mood: 'happy' }
......@@ -30,35 +30,34 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
});
describe('on success', () => {
it('hook runs', function() {
it('hook runs', async function() {
let beforeHook = false;
this.User.beforeCount(() => {
beforeHook = true;
});
return this.User.count().then(count => {
const count = await this.User.count();
expect(count).to.equal(3);
expect(beforeHook).to.be.true;
});
});
it('beforeCount hook can change options', function() {
it('beforeCount hook can change options', async function() {
this.User.beforeCount(options => {
options.where.username = 'adam';
});
return expect(this.User.count({ where: { username: 'joe' } })).to.eventually.equal(1);
await expect(this.User.count({ where: { username: 'joe' } })).to.eventually.equal(1);
});
});
describe('on error', () => {
it('in beforeCount hook returns error', function() {
it('in beforeCount hook returns error', async function() {
this.User.beforeCount(() => {
throw new Error('Oops!');
});
return expect(this.User.count({ where: { username: 'adam' } })).to.be.rejectedWith('Oops!');
await expect(this.User.count({ where: { username: 'adam' } })).to.be.rejectedWith('Oops!');
});
});
});
......
......@@ -8,7 +8,7 @@ const chai = require('chai'),
sinon = require('sinon');
describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: {
type: DataTypes.STRING,
......@@ -19,12 +19,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
values: ['happy', 'sad', 'neutral']
}
});
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
describe('#create', () => {
describe('on success', () => {
it('should run hooks', function() {
it('should run hooks', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy(),
beforeSave = sinon.spy(),
......@@ -35,17 +35,16 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(() => {
await this.User.create({ username: 'Toni', mood: 'happy' });
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce;
expect(beforeSave).to.have.been.calledOnce;
expect(afterSave).to.have.been.calledOnce;
});
});
});
describe('on error', () => {
it('should return an error from before', function() {
it('should return an error from before', async function() {
const beforeHook = sinon.spy(),
beforeSave = sinon.spy(),
afterHook = sinon.spy(),
......@@ -59,15 +58,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave);
return expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejected.then(() => {
await expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).not.to.have.been.called;
expect(beforeSave).not.to.have.been.called;
expect(afterSave).not.to.have.been.called;
});
});
it('should return an error from after', function() {
it('should return an error from after', async function() {
const beforeHook = sinon.spy(),
beforeSave = sinon.spy(),
afterHook = sinon.spy(),
......@@ -82,16 +80,15 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave);
return expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejected.then(() => {
await expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce;
expect(beforeSave).to.have.been.calledOnce;
expect(afterSave).not.to.have.been.called;
});
});
});
it('should not trigger hooks on parent when using N:M association setters', function() {
it('should not trigger hooks on parent when using N:M association setters', async function() {
const A = this.sequelize.define('A', {
name: Sequelize.STRING
});
......@@ -101,28 +98,26 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
let hookCalled = 0;
A.addHook('afterCreate', () => {
A.addHook('afterCreate', async () => {
hookCalled++;
return Promise.resolve();
});
B.belongsToMany(A, { through: 'a_b' });
A.belongsToMany(B, { through: 'a_b' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
await this.sequelize.sync({ force: true });
const [a, b] = await Promise.all([
A.create({ name: 'a' }),
B.create({ name: 'b' })
]).then(([a, b]) => {
return a.addB(b).then(() => {
]);
await a.addB(b);
expect(hookCalled).to.equal(1);
});
});
});
});
describe('preserves changes to instance', () => {
it('beforeValidate', function() {
it('beforeValidate', async function() {
let hookCalled = 0;
this.User.beforeValidate(user => {
......@@ -130,14 +125,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++;
});
return this.User.create({ mood: 'sad', username: 'leafninja' }).then(user => {
const user = await this.User.create({ mood: 'sad', username: 'leafninja' });
expect(user.mood).to.equal('happy');
expect(user.username).to.equal('leafninja');
expect(hookCalled).to.equal(1);
});
});
it('afterValidate', function() {
it('afterValidate', async function() {
let hookCalled = 0;
this.User.afterValidate(user => {
......@@ -145,14 +139,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++;
});
return this.User.create({ mood: 'sad', username: 'fireninja' }).then(user => {
const user = await this.User.create({ mood: 'sad', username: 'fireninja' });
expect(user.mood).to.equal('neutral');
expect(user.username).to.equal('fireninja');
expect(hookCalled).to.equal(1);
});
});
it('beforeCreate', function() {
it('beforeCreate', async function() {
let hookCalled = 0;
this.User.beforeCreate(user => {
......@@ -160,14 +153,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++;
});
return this.User.create({ username: 'akira' }).then(user => {
const user = await this.User.create({ username: 'akira' });
expect(user.mood).to.equal('happy');
expect(user.username).to.equal('akira');
expect(hookCalled).to.equal(1);
});
});
it('beforeSave', function() {
it('beforeSave', async function() {
let hookCalled = 0;
this.User.beforeSave(user => {
......@@ -175,14 +167,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++;
});
return this.User.create({ username: 'akira' }).then(user => {
const user = await this.User.create({ username: 'akira' });
expect(user.mood).to.equal('happy');
expect(user.username).to.equal('akira');
expect(hookCalled).to.equal(1);
});
});
it('beforeSave with beforeCreate', function() {
it('beforeSave with beforeCreate', async function() {
let hookCalled = 0;
this.User.beforeCreate(user => {
......@@ -195,12 +186,11 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++;
});
return this.User.create({ username: 'akira' }).then(user => {
const user = await this.User.create({ username: 'akira' });
expect(user.mood).to.equal('happy');
expect(user.username).to.equal('akira');
expect(hookCalled).to.equal(2);
});
});
});
});
});
......@@ -7,7 +7,7 @@ const chai = require('chai'),
sinon = require('sinon');
describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: {
type: DataTypes.STRING,
......@@ -18,29 +18,27 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
values: ['happy', 'sad', 'neutral']
}
});
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
describe('#destroy', () => {
describe('on success', () => {
it('should run hooks', function() {
it('should run hooks', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy();
this.User.beforeDestroy(beforeHook);
this.User.afterDestroy(afterHook);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => {
return user.destroy().then(() => {
const user = await this.User.create({ username: 'Toni', mood: 'happy' });
await user.destroy();
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce;
});
});
});
});
describe('on error', () => {
it('should return an error from before', function() {
it('should return an error from before', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy();
......@@ -50,15 +48,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
});
this.User.afterDestroy(afterHook);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => {
return expect(user.destroy()).to.be.rejected.then(() => {
const user = await this.User.create({ username: 'Toni', mood: 'happy' });
await expect(user.destroy()).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).not.to.have.been.called;
});
});
});
it('should return an error from after', function() {
it('should return an error from after', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy();
......@@ -68,14 +64,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
throw new Error('Whoops!');
});
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => {
return expect(user.destroy()).to.be.rejected.then(() => {
const user = await this.User.create({ username: 'Toni', mood: 'happy' });
await expect(user.destroy()).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce;
});
});
});
});
describe('with paranoid mode enabled', () => {
beforeEach(function() {
......@@ -96,28 +90,24 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
});
});
it('sets other changed values when soft deleting and a beforeDestroy hooks kicks in', function() {
return this.ParanoidUser.sync({ force: true })
.then(() => this.ParanoidUser.create({ username: 'user1' }))
.then(user => user.destroy())
.then(() => this.ParanoidUser.findOne({ paranoid: false }))
.then(user => {
it('sets other changed values when soft deleting and a beforeDestroy hooks kicks in', async function() {
await this.ParanoidUser.sync({ force: true });
const user0 = await this.ParanoidUser.create({ username: 'user1' });
await user0.destroy();
const user = await this.ParanoidUser.findOne({ paranoid: false });
expect(user.updatedBy).to.equal(1);
});
});
it('should not throw error when a beforeDestroy hook changes a virtual column', function() {
it('should not throw error when a beforeDestroy hook changes a virtual column', async function() {
this.ParanoidUser.beforeDestroy(instance => instance.virtualField = 2);
return this.ParanoidUser.sync({ force: true })
.then(() => this.ParanoidUser.create({ username: 'user1' }))
.then(user => user.destroy())
.then(() => this.ParanoidUser.findOne({ paranoid: false }))
.then(user => {
await this.ParanoidUser.sync({ force: true });
const user0 = await this.ParanoidUser.create({ username: 'user1' });
await user0.destroy();
const user = await this.ParanoidUser.findOne({ paranoid: false });
expect(user.virtualField).to.equal(0);
});
});
});
});
});
......@@ -6,7 +6,7 @@ const chai = require('chai'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: {
type: DataTypes.STRING,
......@@ -18,28 +18,28 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
}
});
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
describe('#find', () => {
beforeEach(function() {
return this.User.bulkCreate([
beforeEach(async function() {
await this.User.bulkCreate([
{ username: 'adam', mood: 'happy' },
{ username: 'joe', mood: 'sad' }
]);
});
it('allow changing attributes via beforeFind #5675', function() {
it('allow changing attributes via beforeFind #5675', async function() {
this.User.beforeFind(options => {
options.attributes = {
include: [['id', 'my_id']]
};
});
return this.User.findAll({});
await this.User.findAll({});
});
describe('on success', () => {
it('all hooks run', function() {
it('all hooks run', async function() {
let beforeHook = false,
beforeHook2 = false,
beforeHook3 = false,
......@@ -61,95 +61,98 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
afterHook = true;
});
return this.User.findOne({ where: { username: 'adam' } }).then(user => {
const user = await this.User.findOne({ where: { username: 'adam' } });
expect(user.mood).to.equal('happy');
expect(beforeHook).to.be.true;
expect(beforeHook2).to.be.true;
expect(beforeHook3).to.be.true;
expect(afterHook).to.be.true;
});
});
it('beforeFind hook can change options', function() {
it('beforeFind hook can change options', async function() {
this.User.beforeFind(options => {
options.where.username = 'joe';
});
return this.User.findOne({ where: { username: 'adam' } }).then(user => {
const user = await this.User.findOne({ where: { username: 'adam' } });
expect(user.mood).to.equal('sad');
});
});
it('beforeFindAfterExpandIncludeAll hook can change options', function() {
it('beforeFindAfterExpandIncludeAll hook can change options', async function() {
this.User.beforeFindAfterExpandIncludeAll(options => {
options.where.username = 'joe';
});
return this.User.findOne({ where: { username: 'adam' } }).then(user => {
const user = await this.User.findOne({ where: { username: 'adam' } });
expect(user.mood).to.equal('sad');
});
});
it('beforeFindAfterOptions hook can change options', function() {
it('beforeFindAfterOptions hook can change options', async function() {
this.User.beforeFindAfterOptions(options => {
options.where.username = 'joe';
});
return this.User.findOne({ where: { username: 'adam' } }).then(user => {
const user = await this.User.findOne({ where: { username: 'adam' } });
expect(user.mood).to.equal('sad');
});
});
it('afterFind hook can change results', function() {
it('afterFind hook can change results', async function() {
this.User.afterFind(user => {
user.mood = 'sad';
});
return this.User.findOne({ where: { username: 'adam' } }).then(user => {
const user = await this.User.findOne({ where: { username: 'adam' } });
expect(user.mood).to.equal('sad');
});
});
});
describe('on error', () => {
it('in beforeFind hook returns error', function() {
it('in beforeFind hook returns error', async function() {
this.User.beforeFind(() => {
throw new Error('Oops!');
});
return this.User.findOne({ where: { username: 'adam' } }).catch(err => {
try {
await this.User.findOne({ where: { username: 'adam' } });
} catch (err) {
expect(err.message).to.equal('Oops!');
});
}
});
it('in beforeFindAfterExpandIncludeAll hook returns error', function() {
it('in beforeFindAfterExpandIncludeAll hook returns error', async function() {
this.User.beforeFindAfterExpandIncludeAll(() => {
throw new Error('Oops!');
});
return this.User.findOne({ where: { username: 'adam' } }).catch(err => {
try {
await this.User.findOne({ where: { username: 'adam' } });
} catch (err) {
expect(err.message).to.equal('Oops!');
});
}
});
it('in beforeFindAfterOptions hook returns error', function() {
it('in beforeFindAfterOptions hook returns error', async function() {
this.User.beforeFindAfterOptions(() => {
throw new Error('Oops!');
});
return this.User.findOne({ where: { username: 'adam' } }).catch(err => {
try {
await this.User.findOne({ where: { username: 'adam' } });
} catch (err) {
expect(err.message).to.equal('Oops!');
});
}
});
it('in afterFind hook returns error', function() {
it('in afterFind hook returns error', async function() {
this.User.afterFind(() => {
throw new Error('Oops!');
});
return this.User.findOne({ where: { username: 'adam' } }).catch(err => {
try {
await this.User.findOne({ where: { username: 'adam' } });
} catch (err) {
expect(err.message).to.equal('Oops!');
});
}
});
});
});
......
......@@ -7,7 +7,7 @@ const chai = require('chai'),
sinon = require('sinon');
describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: {
type: DataTypes.STRING,
......@@ -29,31 +29,28 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
paranoid: true
});
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
describe('#restore', () => {
describe('on success', () => {
it('should run hooks', function() {
it('should run hooks', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy();
this.ParanoidUser.beforeRestore(beforeHook);
this.ParanoidUser.afterRestore(afterHook);
return this.ParanoidUser.create({ username: 'Toni', mood: 'happy' }).then(user => {
return user.destroy().then(() => {
return user.restore().then(() => {
const user = await this.ParanoidUser.create({ username: 'Toni', mood: 'happy' });
await user.destroy();
await user.restore();
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce;
});
});
});
});
});
describe('on error', () => {
it('should return an error from before', function() {
it('should return an error from before', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy();
......@@ -63,17 +60,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
});
this.ParanoidUser.afterRestore(afterHook);
return this.ParanoidUser.create({ username: 'Toni', mood: 'happy' }).then(user => {
return user.destroy().then(() => {
return expect(user.restore()).to.be.rejected.then(() => {
const user = await this.ParanoidUser.create({ username: 'Toni', mood: 'happy' });
await user.destroy();
await expect(user.restore()).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).not.to.have.been.called;
});
});
});
});
it('should return an error from after', function() {
it('should return an error from after', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy();
......@@ -83,16 +77,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
throw new Error('Whoops!');
});
return this.ParanoidUser.create({ username: 'Toni', mood: 'happy' }).then(user => {
return user.destroy().then(() => {
return expect(user.restore()).to.be.rejected.then(() => {
const user = await this.ParanoidUser.create({ username: 'Toni', mood: 'happy' });
await user.destroy();
await expect(user.restore()).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce;
});
});
});
});
});
});
});
......@@ -7,7 +7,7 @@ const chai = require('chai'),
sinon = require('sinon');
describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: {
type: DataTypes.STRING,
......@@ -18,12 +18,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
values: ['happy', 'sad', 'neutral']
}
});
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
describe('#update', () => {
describe('on success', () => {
it('should run hooks', function() {
it('should run hooks', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy(),
beforeSave = sinon.spy(),
......@@ -34,20 +34,18 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => {
return user.update({ username: 'Chong' }).then(user => {
const user = await this.User.create({ username: 'Toni', mood: 'happy' });
const user0 = await user.update({ username: 'Chong' });
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce;
expect(beforeSave).to.have.been.calledTwice;
expect(afterSave).to.have.been.calledTwice;
expect(user.username).to.equal('Chong');
});
});
expect(user0.username).to.equal('Chong');
});
});
describe('on error', () => {
it('should return an error from before', function() {
it('should return an error from before', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy(),
beforeSave = sinon.spy(),
......@@ -61,17 +59,15 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => {
return expect(user.update({ username: 'Chong' })).to.be.rejected.then(() => {
const user = await this.User.create({ username: 'Toni', mood: 'happy' });
await expect(user.update({ username: 'Chong' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce;
expect(beforeSave).to.have.been.calledOnce;
expect(afterHook).not.to.have.been.called;
expect(afterSave).to.have.been.calledOnce;
});
});
});
it('should return an error from after', function() {
it('should return an error from after', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy(),
beforeSave = sinon.spy(),
......@@ -85,47 +81,39 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => {
return expect(user.update({ username: 'Chong' })).to.be.rejected.then(() => {
const user = await this.User.create({ username: 'Toni', mood: 'happy' });
await expect(user.update({ username: 'Chong' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce;
expect(beforeSave).to.have.been.calledTwice;
expect(afterSave).to.have.been.calledOnce;
});
});
});
});
describe('preserves changes to instance', () => {
it('beforeValidate', function() {
it('beforeValidate', async function() {
this.User.beforeValidate(user => {
user.mood = 'happy';
});
return this.User.create({ username: 'fireninja', mood: 'invalid' }).then(user => {
return user.update({ username: 'hero' });
}).then(user => {
const user0 = await this.User.create({ username: 'fireninja', mood: 'invalid' });
const user = await user0.update({ username: 'hero' });
expect(user.username).to.equal('hero');
expect(user.mood).to.equal('happy');
});
});
it('afterValidate', function() {
it('afterValidate', async function() {
this.User.afterValidate(user => {
user.mood = 'sad';
});
return this.User.create({ username: 'fireninja', mood: 'nuetral' }).then(user => {
return user.update({ username: 'spider' });
}).then(user => {
const user0 = await this.User.create({ username: 'fireninja', mood: 'nuetral' });
const user = await user0.update({ username: 'spider' });
expect(user.username).to.equal('spider');
expect(user.mood).to.equal('sad');
});
});
it('beforeSave', function() {
it('beforeSave', async function() {
let hookCalled = 0;
this.User.beforeSave(user => {
......@@ -133,16 +121,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++;
});
return this.User.create({ username: 'fireninja', mood: 'nuetral' }).then(user => {
return user.update({ username: 'spider', mood: 'sad' });
}).then(user => {
const user0 = await this.User.create({ username: 'fireninja', mood: 'nuetral' });
const user = await user0.update({ username: 'spider', mood: 'sad' });
expect(user.username).to.equal('spider');
expect(user.mood).to.equal('happy');
expect(hookCalled).to.equal(2);
});
});
it('beforeSave with beforeUpdate', function() {
it('beforeSave with beforeUpdate', async function() {
let hookCalled = 0;
this.User.beforeUpdate(user => {
......@@ -155,14 +141,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++;
});
return this.User.create({ username: 'akira' }).then(user => {
return user.update({ username: 'spider', mood: 'sad' });
}).then(user => {
const user0 = await this.User.create({ username: 'akira' });
const user = await user0.update({ username: 'spider', mood: 'sad' });
expect(user.mood).to.equal('happy');
expect(user.username).to.equal('spider');
expect(hookCalled).to.equal(3);
});
});
});
});
});
......@@ -8,7 +8,7 @@ const chai = require('chai'),
if (Support.sequelize.dialect.supports.upserts) {
describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: {
type: DataTypes.STRING,
......@@ -20,27 +20,26 @@ if (Support.sequelize.dialect.supports.upserts) {
values: ['happy', 'sad', 'neutral']
}
});
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
describe('#upsert', () => {
describe('on success', () => {
it('should run hooks', function() {
it('should run hooks', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy();
this.User.beforeUpsert(beforeHook);
this.User.afterUpsert(afterHook);
return this.User.upsert({ username: 'Toni', mood: 'happy' }).then(() => {
await this.User.upsert({ username: 'Toni', mood: 'happy' });
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce;
});
});
});
describe('on error', () => {
it('should return an error from before', function() {
it('should return an error from before', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy();
......@@ -50,13 +49,12 @@ if (Support.sequelize.dialect.supports.upserts) {
});
this.User.afterUpsert(afterHook);
return expect(this.User.upsert({ username: 'Toni', mood: 'happy' })).to.be.rejected.then(() => {
await expect(this.User.upsert({ username: 'Toni', mood: 'happy' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).not.to.have.been.called;
});
});
it('should return an error from after', function() {
it('should return an error from after', async function() {
const beforeHook = sinon.spy(),
afterHook = sinon.spy();
......@@ -66,15 +64,14 @@ if (Support.sequelize.dialect.supports.upserts) {
throw new Error('Whoops!');
});
return expect(this.User.upsert({ username: 'Toni', mood: 'happy' })).to.be.rejected.then(() => {
await expect(this.User.upsert({ username: 'Toni', mood: 'happy' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce;
});
});
});
describe('preserves changes to values', () => {
it('beforeUpsert', function() {
it('beforeUpsert', async function() {
let hookCalled = 0;
const valuesOriginal = { mood: 'sad', username: 'leafninja' };
......@@ -83,12 +80,11 @@ if (Support.sequelize.dialect.supports.upserts) {
hookCalled++;
});
return this.User.upsert(valuesOriginal).then(() => {
await this.User.upsert(valuesOriginal);
expect(valuesOriginal.mood).to.equal('happy');
expect(hookCalled).to.equal(1);
});
});
});
});
});
}
......@@ -7,7 +7,7 @@ const Support = require('../support');
const DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: {
type: DataTypes.STRING,
......@@ -18,12 +18,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
values: ['happy', 'sad', 'neutral']
}
});
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
describe('#validate', () => {
describe('#create', () => {
it('should return the user', function() {
it('should return the user', async function() {
this.User.beforeValidate(user => {
user.username = 'Bob';
user.mood = 'happy';
......@@ -33,15 +33,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
user.username = 'Toni';
});
return this.User.create({ mood: 'ecstatic' }).then(user => {
const user = await this.User.create({ mood: 'ecstatic' });
expect(user.mood).to.equal('happy');
expect(user.username).to.equal('Toni');
});
});
});
describe('#3534, hooks modifications', () => {
it('fields modified in hooks are saved', function() {
it('fields modified in hooks are saved', async function() {
this.User.afterValidate(user => {
//if username is defined and has more than 5 char
user.username = user.username
......@@ -56,7 +55,7 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
});
return this.User.create({ username: 'T', mood: 'neutral' }).then(user => {
const user = await this.User.create({ username: 'T', mood: 'neutral' });
expect(user.mood).to.equal('neutral');
expect(user.username).to.equal('Samorost 3');
......@@ -64,87 +63,77 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
user.mood = 'sad';
user.username = 'Samorost Good One';
return user.save();
}).then(uSaved => {
expect(uSaved.mood).to.equal('sad');
expect(uSaved.username).to.equal('Samorost Good One');
const uSaved0 = await user.save();
expect(uSaved0.mood).to.equal('sad');
expect(uSaved0.username).to.equal('Samorost Good One');
//change attributes, expect to be replaced by hooks
uSaved.username = 'One';
uSaved0.username = 'One';
return uSaved.save();
}).then(uSaved => {
const uSaved = await uSaved0.save();
//attributes were replaced by hooks ?
expect(uSaved.mood).to.equal('sad');
expect(uSaved.username).to.equal('Samorost 3');
return this.User.findByPk(uSaved.id);
}).then(uFetched => {
expect(uFetched.mood).to.equal('sad');
expect(uFetched.username).to.equal('Samorost 3');
const uFetched0 = await this.User.findByPk(uSaved.id);
expect(uFetched0.mood).to.equal('sad');
expect(uFetched0.username).to.equal('Samorost 3');
uFetched.mood = null;
uFetched.username = 'New Game is Needed';
uFetched0.mood = null;
uFetched0.username = 'New Game is Needed';
return uFetched.save();
}).then(uFetchedSaved => {
expect(uFetchedSaved.mood).to.equal('neutral');
expect(uFetchedSaved.username).to.equal('New Game is Needed');
const uFetchedSaved0 = await uFetched0.save();
expect(uFetchedSaved0.mood).to.equal('neutral');
expect(uFetchedSaved0.username).to.equal('New Game is Needed');
return this.User.findByPk(uFetchedSaved.id);
}).then(uFetched => {
const uFetched = await this.User.findByPk(uFetchedSaved0.id);
expect(uFetched.mood).to.equal('neutral');
expect(uFetched.username).to.equal('New Game is Needed');
//expect to be replaced by hooks
uFetched.username = 'New';
uFetched.mood = 'happy';
return uFetched.save();
}).then(uFetchedSaved => {
const uFetchedSaved = await uFetched.save();
expect(uFetchedSaved.mood).to.equal('happy');
expect(uFetchedSaved.username).to.equal('Samorost 3');
});
});
});
describe('on error', () => {
it('should emit an error from after hook', function() {
it('should emit an error from after hook', async function() {
this.User.afterValidate(user => {
user.mood = 'ecstatic';
throw new Error('Whoops! Changed user.mood!');
});
return expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejectedWith('Whoops! Changed user.mood!');
await expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejectedWith('Whoops! Changed user.mood!');
});
it('should call validationFailed hook', function() {
it('should call validationFailed hook', async function() {
const validationFailedHook = sinon.spy();
this.User.validationFailed(validationFailedHook);
return expect(this.User.create({ mood: 'happy' })).to.be.rejected.then(() => {
await expect(this.User.create({ mood: 'happy' })).to.be.rejected;
expect(validationFailedHook).to.have.been.calledOnce;
});
});
it('should not replace the validation error in validationFailed hook by default', function() {
it('should not replace the validation error in validationFailed hook by default', async function() {
const validationFailedHook = sinon.stub();
this.User.validationFailed(validationFailedHook);
return expect(this.User.create({ mood: 'happy' })).to.be.rejected.then(err => {
const err = await expect(this.User.create({ mood: 'happy' })).to.be.rejected;
expect(err.name).to.equal('SequelizeValidationError');
});
});
it('should replace the validation error if validationFailed hook creates a new error', function() {
it('should replace the validation error if validationFailed hook creates a new error', async function() {
const validationFailedHook = sinon.stub().throws(new Error('Whoops!'));
this.User.validationFailed(validationFailedHook);
return expect(this.User.create({ mood: 'happy' })).to.be.rejected.then(err => {
const err = await expect(this.User.create({ mood: 'happy' })).to.be.rejected;
expect(err.message).to.equal('Whoops!');
});
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!