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

Commit 9512ab91 by Pedro Augusto de Paula Barbosa Committed by Sushant

refactor(tests): use async/await (#11876)

1 parent f843582c
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
const chai = require('chai'), const chai = require('chai'),
Sequelize = require('../../index'), Sequelize = require('../../index'),
Op = Sequelize.Op, Op = Sequelize.Op,
Promise = Sequelize.Promise,
expect = chai.expect, expect = chai.expect,
Support = require('../support'), Support = require('../support'),
DataTypes = require('../../lib/data-types'), DataTypes = require('../../lib/data-types'),
...@@ -11,7 +10,7 @@ const chai = require('chai'), ...@@ -11,7 +10,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Operators'), () => { describe(Support.getTestDialectTeaser('Operators'), () => {
describe('REGEXP', () => { describe('REGEXP', () => {
beforeEach(function() { beforeEach(async function() {
this.User = this.sequelize.define('user', { this.User = this.sequelize.define('user', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
...@@ -29,145 +28,93 @@ describe(Support.getTestDialectTeaser('Operators'), () => { ...@@ -29,145 +28,93 @@ describe(Support.getTestDialectTeaser('Operators'), () => {
timestamps: false timestamps: false
}); });
return Promise.all([ await this.sequelize.getQueryInterface().createTable('users', {
this.sequelize.getQueryInterface().createTable('users', { userId: {
userId: { type: DataTypes.INTEGER,
type: DataTypes.INTEGER, allowNull: false,
allowNull: false, primaryKey: true,
primaryKey: true, autoIncrement: true
autoIncrement: true },
}, full_name: {
full_name: { type: DataTypes.STRING
type: DataTypes.STRING }
} });
})
]);
}); });
if (dialect === 'mysql' || dialect === 'postgres') { if (dialect === 'mysql' || dialect === 'postgres') {
describe('case sensitive', () => { describe('case sensitive', () => {
it('should work with a regexp where', function() { it('should work with a regexp where', async function() {
return this.User.create({ await this.User.create({ name: 'Foobar' });
name: 'Foobar' const user = await this.User.findOne({
}).then(() => { where: {
return this.User.findOne({ name: { [Op.regexp]: '^Foo' }
where: { }
name: {
[Op.regexp]: '^Foo'
}
}
});
}).then(user => {
expect(user).to.be.ok;
}); });
expect(user).to.be.ok;
}); });
it('should work with a not regexp where', function() { it('should work with a not regexp where', async function() {
return this.User.create({ await this.User.create({ name: 'Foobar' });
name: 'Foobar' const user = await this.User.findOne({
}).then(() => { where: {
return this.User.findOne({ name: { [Op.notRegexp]: '^Foo' }
where: { }
name: {
[Op.notRegexp]: '^Foo'
}
}
});
}).then(user => {
expect(user).to.not.be.ok;
}); });
expect(user).to.not.be.ok;
}); });
it('should properly escape regular expressions', function() { it('should properly escape regular expressions', async function() {
return this.User.bulkCreate([{ await this.User.bulkCreate([{ name: 'John' }, { name: 'Bob' }]);
name: 'John' await this.User.findAll({
}, { where: {
name: 'Bob' name: { [Op.notRegexp]: "Bob'; drop table users --" }
}]).then(() => { }
return this.User.findAll({
where: {
name: {
[Op.notRegexp]: "Bob'; drop table users --"
}
}
});
}).then(() => {
return this.User.findAll({
where: {
name: {
[Op.regexp]: "Bob'; drop table users --"
}
}
});
}).then(() => {
return this.User.findAll();
}).then(users => {
expect(users).length(2);
}); });
await this.User.findAll({
where: {
name: { [Op.regexp]: "Bob'; drop table users --" }
}
});
expect(await this.User.findAll()).to.have.length(2);
}); });
}); });
} }
if (dialect === 'postgres') { if (dialect === 'postgres') {
describe('case insensitive', () => { describe('case insensitive', () => {
it('should work with a case-insensitive regexp where', function() { it('should work with a case-insensitive regexp where', async function() {
return this.User.create({ await this.User.create({ name: 'Foobar' });
name: 'Foobar' const user = await this.User.findOne({
}).then(() => { where: {
return this.User.findOne({ name: { [Op.iRegexp]: '^foo' }
where: { }
name: {
[Op.iRegexp]: '^foo'
}
}
});
}).then(user => {
expect(user).to.be.ok;
}); });
expect(user).to.be.ok;
}); });
it('should work with a case-insensitive not regexp where', function() { it('should work with a case-insensitive not regexp where', async function() {
return this.User.create({ await this.User.create({ name: 'Foobar' });
name: 'Foobar' const user = await this.User.findOne({
}).then(() => { where: {
return this.User.findOne({ name: { [Op.notIRegexp]: '^foo' }
where: { }
name: {
[Op.notIRegexp]: '^foo'
}
}
});
}).then(user => {
expect(user).to.not.be.ok;
}); });
expect(user).to.not.be.ok;
}); });
it('should properly escape regular expressions', function() { it('should properly escape regular expressions', async function() {
return this.User.bulkCreate([{ await this.User.bulkCreate([{ name: 'John' }, { name: 'Bob' }]);
name: 'John' await this.User.findAll({
}, { where: {
name: 'Bob' name: { [Op.iRegexp]: "Bob'; drop table users --" }
}]).then(() => { }
return this.User.findAll({ });
where: { await this.User.findAll({
name: { where: {
[Op.iRegexp]: "Bob'; drop table users --" name: { [Op.notIRegexp]: "Bob'; drop table users --" }
} }
}
});
}).then(() => {
return this.User.findAll({
where: {
name: {
[Op.notIRegexp]: "Bob'; drop table users --"
}
}
});
}).then(() => {
return this.User.findAll();
}).then(users => {
expect(users).length(2);
}); });
expect(await this.User.findAll()).to.have.length(2);
}); });
}); });
} }
......
...@@ -69,175 +69,118 @@ describe(Support.getTestDialectTeaser('Pooling'), () => { ...@@ -69,175 +69,118 @@ describe(Support.getTestDialectTeaser('Pooling'), () => {
}); });
describe('network / connection errors', () => { describe('network / connection errors', () => {
it('should obtain new connection when old connection is abruptly closed', () => { it('should obtain new connection when old connection is abruptly closed', async () => {
const sequelize = Support.createSequelizeInstance({ function simulateUnexpectedError(connection) {
pool: { // should never be returned again
max: 1, if (dialect === 'mssql') {
idle: 5000 connection = unwrapAndAttachMSSQLUniqueId(connection);
} }
}); connection.emit('error', { code: 'ECONNRESET' });
}
const sequelize = Support.createSequelizeInstance({
pool: { max: 1, idle: 5000 }
});
const cm = sequelize.connectionManager; const cm = sequelize.connectionManager;
let conn; await sequelize.sync();
return sequelize
.sync()
.then(() => cm.getConnection())
.then(connection => {
// Save current connection
conn = connection;
if (dialect === 'mssql') {
connection = unwrapAndAttachMSSQLUniqueId(connection);
}
// simulate an unexpected error
// should never be returned again
connection.emit('error', {
code: 'ECONNRESET'
});
})
.then(() => {
// Get next available connection
return cm.getConnection();
})
.then(connection => {
assertNewConnection(connection, conn);
expect(sequelize.connectionManager.pool.size).to.equal(1); const firstConnection = await cm.getConnection();
expect(cm.validate(conn)).to.be.not.ok; simulateUnexpectedError(firstConnection);
const secondConnection = await cm.getConnection();
return cm.releaseConnection(connection); assertNewConnection(secondConnection, firstConnection);
}); expect(cm.pool.size).to.equal(1);
expect(cm.validate(firstConnection)).to.be.not.ok;
await cm.releaseConnection(secondConnection);
}); });
it('should obtain new connection when released connection dies inside pool', () => { it('should obtain new connection when released connection dies inside pool', async () => {
const sequelize = Support.createSequelizeInstance({ function simulateUnexpectedError(connection) {
pool: { // should never be returned again
max: 1, if (dialect === 'mssql') {
idle: 5000 unwrapAndAttachMSSQLUniqueId(connection).close();
} else if (dialect === 'postgres') {
connection.end();
} else {
connection.close();
} }
}); }
const sequelize = Support.createSequelizeInstance({
pool: { max: 1, idle: 5000 }
});
const cm = sequelize.connectionManager; const cm = sequelize.connectionManager;
let oldConnection; await sequelize.sync();
return sequelize const oldConnection = await cm.getConnection();
.sync() await cm.releaseConnection(oldConnection);
.then(() => cm.getConnection()) simulateUnexpectedError(oldConnection);
.then(connection => { const newConnection = await cm.getConnection();
// Save current connection
oldConnection = connection;
return cm.releaseConnection(connection);
})
.then(() => {
let connection = oldConnection;
if (dialect === 'mssql') {
connection = unwrapAndAttachMSSQLUniqueId(connection);
}
// simulate an unexpected error
// should never be returned again
if (dialect.match(/postgres/)) {
connection.end();
} else {
connection.close();
}
})
.then(() => {
// Get next available connection
return cm.getConnection();
})
.then(connection => {
assertNewConnection(connection, oldConnection);
expect(sequelize.connectionManager.pool.size).to.equal(1); assertNewConnection(newConnection, oldConnection);
expect(cm.validate(oldConnection)).to.be.not.ok; expect(cm.pool.size).to.equal(1);
expect(cm.validate(oldConnection)).to.be.not.ok;
return cm.releaseConnection(connection); await cm.releaseConnection(newConnection);
});
}); });
}); });
describe('idle', () => { describe('idle', () => {
it('should maintain connection within idle range', () => { it('should maintain connection within idle range', async () => {
const sequelize = Support.createSequelizeInstance({ const sequelize = Support.createSequelizeInstance({
pool: { pool: { max: 1, idle: 100 }
max: 1,
idle: 10
}
}); });
const cm = sequelize.connectionManager; const cm = sequelize.connectionManager;
let conn; await sequelize.sync();
return sequelize.sync() const firstConnection = await cm.getConnection();
.then(() => cm.getConnection())
.then(connection => {
// Save current connection
conn = connection;
if (dialect === 'mssql') { // TODO - Do we really need this call?
connection = unwrapAndAttachMSSQLUniqueId(connection); unwrapAndAttachMSSQLUniqueId(firstConnection);
}
// returning connection back to pool // returning connection back to pool
return cm.releaseConnection(conn); await cm.releaseConnection(firstConnection);
})
.then(() => { // Wait a little and then get next available connection
// Get next available connection await Sequelize.Promise.delay(90);
return Sequelize.Promise.delay(9).then(() => cm.getConnection()); const secondConnection = await cm.getConnection();
})
.then(connection => {
assertSameConnection(connection, conn);
expect(cm.validate(conn)).to.be.ok;
return cm.releaseConnection(connection); assertSameConnection(secondConnection, firstConnection);
}); expect(cm.validate(firstConnection)).to.be.ok;
await cm.releaseConnection(secondConnection);
}); });
it('should get new connection beyond idle range', () => { it('should get new connection beyond idle range', async () => {
const sequelize = Support.createSequelizeInstance({ const sequelize = Support.createSequelizeInstance({
pool: { pool: { max: 1, idle: 100, evict: 10 }
max: 1,
idle: 100,
evict: 10
}
}); });
const cm = sequelize.connectionManager; const cm = sequelize.connectionManager;
let conn; await sequelize.sync();
return sequelize.sync() const firstConnection = await cm.getConnection();
.then(() => cm.getConnection())
.then(connection => {
// Save current connection
conn = connection;
if (dialect === 'mssql') { // TODO - Do we really need this call?
connection = unwrapAndAttachMSSQLUniqueId(connection); unwrapAndAttachMSSQLUniqueId(firstConnection);
}
// returning connection back to pool // returning connection back to pool
return cm.releaseConnection(conn); await cm.releaseConnection(firstConnection);
})
.then(() => {
// Get next available connection
return Sequelize.Promise.delay(110).then(() => cm.getConnection());
})
.then(connection => {
assertNewConnection(connection, conn);
expect(cm.validate(conn)).not.to.be.ok;
return cm.releaseConnection(connection); // Wait a little and then get next available connection
}); await Sequelize.Promise.delay(110);
const secondConnection = await cm.getConnection();
assertNewConnection(secondConnection, firstConnection);
expect(cm.validate(firstConnection)).not.to.be.ok;
await cm.releaseConnection(secondConnection);
}); });
}); });
describe('acquire', () => { describe('acquire', () => {
it('should reject with ConnectionAcquireTimeoutError when unable to acquire connection', function() { it('should reject with ConnectionAcquireTimeoutError when unable to acquire connection', async function() {
this.testInstance = new Sequelize('localhost', 'ffd', 'dfdf', { this.testInstance = new Sequelize('localhost', 'ffd', 'dfdf', {
dialect, dialect,
databaseVersion: '1.2.3', databaseVersion: '1.2.3',
...@@ -249,11 +192,12 @@ describe(Support.getTestDialectTeaser('Pooling'), () => { ...@@ -249,11 +192,12 @@ describe(Support.getTestDialectTeaser('Pooling'), () => {
this.sinon.stub(this.testInstance.connectionManager, '_connect') this.sinon.stub(this.testInstance.connectionManager, '_connect')
.returns(new Sequelize.Promise(() => {})); .returns(new Sequelize.Promise(() => {}));
return expect(this.testInstance.authenticate()) await expect(
.to.eventually.be.rejectedWith(Sequelize.ConnectionAcquireTimeoutError); this.testInstance.authenticate()
).to.eventually.be.rejectedWith(Sequelize.ConnectionAcquireTimeoutError);
}); });
it('should reject with ConnectionAcquireTimeoutError when unable to acquire connection for transaction', function() { it('should reject with ConnectionAcquireTimeoutError when unable to acquire connection for transaction', async function() {
this.testInstance = new Sequelize('localhost', 'ffd', 'dfdf', { this.testInstance = new Sequelize('localhost', 'ffd', 'dfdf', {
dialect, dialect,
databaseVersion: '1.2.3', databaseVersion: '1.2.3',
...@@ -266,9 +210,11 @@ describe(Support.getTestDialectTeaser('Pooling'), () => { ...@@ -266,9 +210,11 @@ describe(Support.getTestDialectTeaser('Pooling'), () => {
this.sinon.stub(this.testInstance.connectionManager, '_connect') this.sinon.stub(this.testInstance.connectionManager, '_connect')
.returns(new Sequelize.Promise(() => {})); .returns(new Sequelize.Promise(() => {}));
return expect(this.testInstance.transaction(() => { await expect(
return this.testInstance.transaction(() => {}); this.testInstance.transaction(async () => {
})).to.eventually.be.rejectedWith(Sequelize.ConnectionAcquireTimeoutError); await this.testInstance.transaction(() => {});
})
).to.eventually.be.rejectedWith(Sequelize.ConnectionAcquireTimeoutError);
}); });
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!