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

Commit fd11d98f by Andy Edwards Committed by GitHub

test(integration/query-interface): asyncify (#12293)

1 parent e34efd47
...@@ -12,14 +12,15 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -12,14 +12,15 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
this.queryInterface = this.sequelize.getQueryInterface(); this.queryInterface = this.sequelize.getQueryInterface();
}); });
afterEach(function() { afterEach(async function() {
return Support.dropTestSchemas(this.sequelize); await Support.dropTestSchemas(this.sequelize);
}); });
describe('changeColumn', () => { describe('changeColumn', () => {
it('should support schemas', function() { it('should support schemas', async function() {
return this.sequelize.createSchema('archive').then(() => { await this.sequelize.createSchema('archive');
return this.queryInterface.createTable({
await this.queryInterface.createTable({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, { }, {
...@@ -29,30 +30,29 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -29,30 +30,29 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
autoIncrement: true autoIncrement: true
}, },
currency: DataTypes.INTEGER currency: DataTypes.INTEGER
}).then(() => { });
return this.queryInterface.changeColumn({
await this.queryInterface.changeColumn({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, 'currency', { }, 'currency', {
type: DataTypes.FLOAT type: DataTypes.FLOAT
}); });
}).then(() => {
return this.queryInterface.describeTable({ const table = await this.queryInterface.describeTable({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}); });
}).then(table => {
if (dialect === 'postgres' || dialect === 'postgres-native') { if (dialect === 'postgres' || dialect === 'postgres-native') {
expect(table.currency.type).to.equal('DOUBLE PRECISION'); expect(table.currency.type).to.equal('DOUBLE PRECISION');
} else { } else {
expect(table.currency.type).to.equal('FLOAT'); expect(table.currency.type).to.equal('FLOAT');
} }
}); });
});
});
it('should change columns', function() { it('should change columns', async function() {
return this.queryInterface.createTable({ await this.queryInterface.createTable({
tableName: 'users' tableName: 'users'
}, { }, {
id: { id: {
...@@ -61,76 +61,76 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -61,76 +61,76 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
autoIncrement: true autoIncrement: true
}, },
currency: DataTypes.INTEGER currency: DataTypes.INTEGER
}).then(() => { });
return this.queryInterface.changeColumn('users', 'currency', {
await this.queryInterface.changeColumn('users', 'currency', {
type: DataTypes.FLOAT, type: DataTypes.FLOAT,
allowNull: true allowNull: true
}); });
}).then(() => {
return this.queryInterface.describeTable({ const table = await this.queryInterface.describeTable({
tableName: 'users' tableName: 'users'
}); });
}).then(table => {
if (dialect === 'postgres' || dialect === 'postgres-native') { if (dialect === 'postgres' || dialect === 'postgres-native') {
expect(table.currency.type).to.equal('DOUBLE PRECISION'); expect(table.currency.type).to.equal('DOUBLE PRECISION');
} else { } else {
expect(table.currency.type).to.equal('FLOAT'); expect(table.currency.type).to.equal('FLOAT');
} }
}); });
});
// MSSQL doesn't support using a modified column in a check constraint. // MSSQL doesn't support using a modified column in a check constraint.
// https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql // https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql
if (dialect !== 'mssql') { if (dialect !== 'mssql') {
it('should work with enums (case 1)', function() { it('should work with enums (case 1)', async function() {
return this.queryInterface.createTable({ await this.queryInterface.createTable({
tableName: 'users' tableName: 'users'
}, { }, {
firstName: DataTypes.STRING firstName: DataTypes.STRING
}).then(() => {
return this.queryInterface.changeColumn('users', 'firstName', {
type: DataTypes.ENUM(['value1', 'value2', 'value3'])
}); });
await this.queryInterface.changeColumn('users', 'firstName', {
type: DataTypes.ENUM(['value1', 'value2', 'value3'])
}); });
}); });
it('should work with enums (case 2)', function() { it('should work with enums (case 2)', async function() {
return this.queryInterface.createTable({ await this.queryInterface.createTable({
tableName: 'users' tableName: 'users'
}, { }, {
firstName: DataTypes.STRING firstName: DataTypes.STRING
}).then(() => { });
return this.queryInterface.changeColumn('users', 'firstName', {
await this.queryInterface.changeColumn('users', 'firstName', {
type: DataTypes.ENUM, type: DataTypes.ENUM,
values: ['value1', 'value2', 'value3'] values: ['value1', 'value2', 'value3']
}); });
}); });
});
it('should work with enums with schemas', function() { it('should work with enums with schemas', async function() {
return this.sequelize.createSchema('archive').then(() => { await this.sequelize.createSchema('archive');
return this.queryInterface.createTable({
await this.queryInterface.createTable({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, { }, {
firstName: DataTypes.STRING firstName: DataTypes.STRING
}); });
}).then(() => {
return this.queryInterface.changeColumn({ await this.queryInterface.changeColumn({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, 'firstName', { }, 'firstName', {
type: DataTypes.ENUM(['value1', 'value2', 'value3']) type: DataTypes.ENUM(['value1', 'value2', 'value3'])
}); });
}); });
});
} }
//SQlite natively doesn't support ALTER Foreign key //SQlite natively doesn't support ALTER Foreign key
if (dialect !== 'sqlite') { if (dialect !== 'sqlite') {
describe('should support foreign keys', () => { describe('should support foreign keys', () => {
beforeEach(function() { beforeEach(async function() {
return this.queryInterface.createTable('users', { await this.queryInterface.createTable('users', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
...@@ -140,8 +140,9 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -140,8 +140,9 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false allowNull: false
} }
}).then(() => { });
return this.queryInterface.createTable('level', {
await this.queryInterface.createTable('level', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
...@@ -149,13 +150,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -149,13 +150,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
} }
}); });
}); });
});
it('able to change column to foreign key', function() { it('able to change column to foreign key', async function() {
return this.queryInterface.getForeignKeyReferencesForTable('users').then( foreignKeys => { const foreignKeys = await this.queryInterface.getForeignKeyReferencesForTable('users');
expect(foreignKeys).to.be.an('array'); expect(foreignKeys).to.be.an('array');
expect(foreignKeys).to.be.empty; expect(foreignKeys).to.be.empty;
return this.queryInterface.changeColumn('users', 'level_id', {
await this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: { references: {
model: 'level', model: 'level',
...@@ -164,17 +165,14 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -164,17 +165,14 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
onUpdate: 'cascade', onUpdate: 'cascade',
onDelete: 'cascade' onDelete: 'cascade'
}); });
}).then(() => {
return this.queryInterface.getForeignKeyReferencesForTable('users'); const newForeignKeys = await this.queryInterface.getForeignKeyReferencesForTable('users');
}).then(newForeignKeys => {
expect(newForeignKeys).to.be.an('array'); expect(newForeignKeys).to.be.an('array');
expect(newForeignKeys).to.have.lengthOf(1); expect(newForeignKeys).to.have.lengthOf(1);
expect(newForeignKeys[0].columnName).to.be.equal('level_id'); expect(newForeignKeys[0].columnName).to.be.equal('level_id');
}); });
});
it('able to change column property without affecting other properties', function() { it('able to change column property without affecting other properties', async function() {
let firstTable, firstForeignKeys;
// 1. look for users table information // 1. look for users table information
// 2. change column level_id on users to have a Foreign Key // 2. change column level_id on users to have a Foreign Key
// 3. look for users table Foreign Keys information // 3. look for users table Foreign Keys information
...@@ -182,11 +180,11 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -182,11 +180,11 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
// 5. look for new foreign keys information // 5. look for new foreign keys information
// 6. look for new table structure information // 6. look for new table structure information
// 7. compare foreign keys and tables(before and after the changes) // 7. compare foreign keys and tables(before and after the changes)
return this.queryInterface.describeTable({ const firstTable = await this.queryInterface.describeTable({
tableName: 'users' tableName: 'users'
}).then( describedTable => { });
firstTable = describedTable;
return this.queryInterface.changeColumn('users', 'level_id', { await this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: { references: {
model: 'level', model: 'level',
...@@ -195,47 +193,45 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -195,47 +193,45 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
onUpdate: 'cascade', onUpdate: 'cascade',
onDelete: 'cascade' onDelete: 'cascade'
}); });
}).then( () => {
return this.queryInterface.getForeignKeyReferencesForTable('users'); const keys = await this.queryInterface.getForeignKeyReferencesForTable('users');
}).then( keys => { const firstForeignKeys = keys;
firstForeignKeys = keys;
return this.queryInterface.changeColumn('users', 'level_id', { await this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: true allowNull: true
}); });
}).then( () => {
return this.queryInterface.getForeignKeyReferencesForTable('users'); const newForeignKeys = await this.queryInterface.getForeignKeyReferencesForTable('users');
}).then( newForeignKeys => {
expect(firstForeignKeys.length).to.be.equal(newForeignKeys.length); expect(firstForeignKeys.length).to.be.equal(newForeignKeys.length);
expect(firstForeignKeys[0].columnName).to.be.equal('level_id'); expect(firstForeignKeys[0].columnName).to.be.equal('level_id');
expect(firstForeignKeys[0].columnName).to.be.equal(newForeignKeys[0].columnName); expect(firstForeignKeys[0].columnName).to.be.equal(newForeignKeys[0].columnName);
return this.queryInterface.describeTable({ const describedTable = await this.queryInterface.describeTable({
tableName: 'users' tableName: 'users'
}); });
}).then( describedTable => {
expect(describedTable.level_id).to.have.property('allowNull'); expect(describedTable.level_id).to.have.property('allowNull');
expect(describedTable.level_id.allowNull).to.not.equal(firstTable.level_id.allowNull); expect(describedTable.level_id.allowNull).to.not.equal(firstTable.level_id.allowNull);
expect(describedTable.level_id.allowNull).to.be.equal(true); expect(describedTable.level_id.allowNull).to.be.equal(true);
}); });
});
it('should change the comment of column', function() { it('should change the comment of column', async function() {
return this.queryInterface.describeTable({ const describedTable = await this.queryInterface.describeTable({
tableName: 'users' tableName: 'users'
}).then(describedTable => { });
expect(describedTable.level_id.comment).to.be.equal(null); expect(describedTable.level_id.comment).to.be.equal(null);
return this.queryInterface.changeColumn('users', 'level_id', {
await this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
comment: 'FooBar' comment: 'FooBar'
}); });
}).then(() => {
return this.queryInterface.describeTable({ tableName: 'users' }); const describedTable2 = await this.queryInterface.describeTable({ tableName: 'users' });
}).then(describedTable2 => {
expect(describedTable2.level_id.comment).to.be.equal('FooBar'); expect(describedTable2.level_id.comment).to.be.equal('FooBar');
}); });
}); });
});
} }
}); });
}); });
...@@ -12,29 +12,27 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -12,29 +12,27 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
this.queryInterface = this.sequelize.getQueryInterface(); this.queryInterface = this.sequelize.getQueryInterface();
}); });
afterEach(function() { afterEach(async function() {
return Support.dropTestSchemas(this.sequelize); await Support.dropTestSchemas(this.sequelize);
}); });
// FIXME: These tests should make assertions against the created table using describeTable // FIXME: These tests should make assertions against the created table using describeTable
describe('createTable', () => { describe('createTable', () => {
it('should create a auto increment primary key', function() { it('should create a auto increment primary key', async function() {
return this.queryInterface.createTable('TableWithPK', { await this.queryInterface.createTable('TableWithPK', {
table_id: { table_id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
autoIncrement: true autoIncrement: true
} }
}).then(() => {
return this.queryInterface.insert(null, 'TableWithPK', {}, { raw: true, returning: true, plain: true })
.then(([response]) => {
expect(response.table_id || typeof response !== 'object' && response).to.be.ok;
});
}); });
const [response] = await this.queryInterface.insert(null, 'TableWithPK', {}, { raw: true, returning: true, plain: true });
expect(response.table_id || typeof response !== 'object' && response).to.be.ok;
}); });
it('should create unique constraint with uniqueKeys', function() { it('should create unique constraint with uniqueKeys', async function() {
return this.queryInterface.createTable('MyTable', { await this.queryInterface.createTable('MyTable', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
...@@ -55,9 +53,9 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -55,9 +53,9 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
fields: ['name'] fields: ['name']
} }
} }
}).then(() => { });
return this.queryInterface.showIndex('MyTable');
}).then(indexes => { const indexes = await this.queryInterface.showIndex('MyTable');
switch (dialect) { switch (dialect) {
case 'postgres': case 'postgres':
case 'postgres-native': case 'postgres-native':
...@@ -73,7 +71,6 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -73,7 +71,6 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
expect(indexes[1].unique).to.be.true; expect(indexes[1].unique).to.be.true;
expect(indexes[1].fields[0].attribute).to.equal('name'); expect(indexes[1].fields[0].attribute).to.equal('name');
break; break;
case 'mariadb': case 'mariadb':
case 'mysql': case 'mysql':
// name + email // name + email
...@@ -85,16 +82,15 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -85,16 +82,15 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
expect(indexes[2].unique).to.be.true; expect(indexes[2].unique).to.be.true;
expect(indexes[2].fields[0].attribute).to.equal('name'); expect(indexes[2].fields[0].attribute).to.equal('name');
break; break;
default: default:
throw new Error(`Not implemented fpr ${dialect}`); throw new Error(`Not implemented fpr ${dialect}`);
} }
}); });
});
it('should work with schemas', function() { it('should work with schemas', async function() {
return this.sequelize.createSchema('hero').then(() => { await this.sequelize.createSchema('hero');
return this.queryInterface.createTable('User', {
await this.queryInterface.createTable('User', {
name: { name: {
type: DataTypes.STRING type: DataTypes.STRING
} }
...@@ -102,79 +98,74 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -102,79 +98,74 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
schema: 'hero' schema: 'hero'
}); });
}); });
});
describe('enums', () => { describe('enums', () => {
it('should work with enums (1)', function() { it('should work with enums (1)', async function() {
return this.queryInterface.createTable('SomeTable', { await this.queryInterface.createTable('SomeTable', {
someEnum: DataTypes.ENUM('value1', 'value2', 'value3') someEnum: DataTypes.ENUM('value1', 'value2', 'value3')
}).then(() => { });
return this.queryInterface.describeTable('SomeTable');
}).then(table => { const table = await this.queryInterface.describeTable('SomeTable');
if (dialect.includes('postgres')) { if (dialect.includes('postgres')) {
expect(table.someEnum.special).to.deep.equal(['value1', 'value2', 'value3']); expect(table.someEnum.special).to.deep.equal(['value1', 'value2', 'value3']);
} }
}); });
});
it('should work with enums (2)', function() { it('should work with enums (2)', async function() {
return this.queryInterface.createTable('SomeTable', { await this.queryInterface.createTable('SomeTable', {
someEnum: { someEnum: {
type: DataTypes.ENUM, type: DataTypes.ENUM,
values: ['value1', 'value2', 'value3'] values: ['value1', 'value2', 'value3']
} }
}).then(() => { });
return this.queryInterface.describeTable('SomeTable');
}).then(table => { const table = await this.queryInterface.describeTable('SomeTable');
if (dialect.includes('postgres')) { if (dialect.includes('postgres')) {
expect(table.someEnum.special).to.deep.equal(['value1', 'value2', 'value3']); expect(table.someEnum.special).to.deep.equal(['value1', 'value2', 'value3']);
} }
}); });
});
it('should work with enums (3)', function() { it('should work with enums (3)', async function() {
return this.queryInterface.createTable('SomeTable', { await this.queryInterface.createTable('SomeTable', {
someEnum: { someEnum: {
type: DataTypes.ENUM, type: DataTypes.ENUM,
values: ['value1', 'value2', 'value3'], values: ['value1', 'value2', 'value3'],
field: 'otherName' field: 'otherName'
} }
}).then(() => { });
return this.queryInterface.describeTable('SomeTable');
}).then(table => { const table = await this.queryInterface.describeTable('SomeTable');
if (dialect.includes('postgres')) { if (dialect.includes('postgres')) {
expect(table.otherName.special).to.deep.equal(['value1', 'value2', 'value3']); expect(table.otherName.special).to.deep.equal(['value1', 'value2', 'value3']);
} }
}); });
});
it('should work with enums (4)', function() { it('should work with enums (4)', async function() {
return this.queryInterface.createSchema('archive').then(() => { await this.queryInterface.createSchema('archive');
return this.queryInterface.createTable('SomeTable', {
await this.queryInterface.createTable('SomeTable', {
someEnum: { someEnum: {
type: DataTypes.ENUM, type: DataTypes.ENUM,
values: ['value1', 'value2', 'value3'], values: ['value1', 'value2', 'value3'],
field: 'otherName' field: 'otherName'
} }
}, { schema: 'archive' }); }, { schema: 'archive' });
}).then(() => {
return this.queryInterface.describeTable('SomeTable', { schema: 'archive' }); const table = await this.queryInterface.describeTable('SomeTable', { schema: 'archive' });
}).then(table => {
if (dialect.includes('postgres')) { if (dialect.includes('postgres')) {
expect(table.otherName.special).to.deep.equal(['value1', 'value2', 'value3']); expect(table.otherName.special).to.deep.equal(['value1', 'value2', 'value3']);
} }
}); });
});
it('should work with enums (5)', function() { it('should work with enums (5)', async function() {
return this.queryInterface.createTable('SomeTable', { await this.queryInterface.createTable('SomeTable', {
someEnum: { someEnum: {
type: DataTypes.ENUM(['COMMENT']), type: DataTypes.ENUM(['COMMENT']),
comment: 'special enum col' comment: 'special enum col'
} }
}).then(() => { });
return this.queryInterface.describeTable('SomeTable');
}).then(table => { const table = await this.queryInterface.describeTable('SomeTable');
if (dialect.includes('postgres')) { if (dialect.includes('postgres')) {
expect(table.someEnum.special).to.deep.equal(['COMMENT']); expect(table.someEnum.special).to.deep.equal(['COMMENT']);
expect(table.someEnum.comment).to.equal('special enum col'); expect(table.someEnum.comment).to.equal('special enum col');
...@@ -182,5 +173,4 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -182,5 +173,4 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
}); });
}); });
}); });
});
}); });
...@@ -12,13 +12,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -12,13 +12,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
this.queryInterface = this.sequelize.getQueryInterface(); this.queryInterface = this.sequelize.getQueryInterface();
}); });
afterEach(function() { afterEach(async function() {
return Support.dropTestSchemas(this.sequelize); await Support.dropTestSchemas(this.sequelize);
}); });
describe('describeTable', () => { describe('describeTable', () => {
if (Support.sequelize.dialect.supports.schemas) { if (Support.sequelize.dialect.supports.schemas) {
it('reads the metadata of the table with schema', function() { it('reads the metadata of the table with schema', async function() {
const MyTable1 = this.sequelize.define('my_table', { const MyTable1 = this.sequelize.define('my_table', {
username1: DataTypes.STRING username1: DataTypes.STRING
}); });
...@@ -27,36 +27,25 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -27,36 +27,25 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
username2: DataTypes.STRING username2: DataTypes.STRING
}, { schema: 'test_meta' }); }, { schema: 'test_meta' });
return this.sequelize.createSchema('test_meta') await this.sequelize.createSchema('test_meta');
.then(() => { await MyTable1.sync({ force: true });
return MyTable1.sync({ force: true }); await MyTable2.sync({ force: true });
}) const metadata0 = await this.queryInterface.describeTable('my_tables', 'test_meta');
.then(() => { expect(metadata0.username2).not.to.be.undefined;
return MyTable2.sync({ force: true }); const metadata = await this.queryInterface.describeTable('my_tables');
})
.then(() => {
return this.queryInterface.describeTable('my_tables', 'test_meta');
})
.then(metadata => {
expect(metadata.username2).not.to.be.undefined;
})
.then(() => {
return this.queryInterface.describeTable('my_tables');
})
.then(metadata => {
expect(metadata.username1).not.to.be.undefined; expect(metadata.username1).not.to.be.undefined;
return this.sequelize.dropSchema('test_meta');
}); await this.sequelize.dropSchema('test_meta');
}); });
} }
it('rejects when no data is available', function() { it('rejects when no data is available', async function() {
return expect( await expect(
this.queryInterface.describeTable('_some_random_missing_table') this.queryInterface.describeTable('_some_random_missing_table')
).to.be.rejectedWith('No description found for "_some_random_missing_table" table. Check the table name and schema; remember, they _are_ case sensitive.'); ).to.be.rejectedWith('No description found for "_some_random_missing_table" table. Check the table name and schema; remember, they _are_ case sensitive.');
}); });
it('reads the metadata of the table', function() { it('reads the metadata of the table', async function() {
const Users = this.sequelize.define('_Users', { const Users = this.sequelize.define('_Users', {
username: DataTypes.STRING, username: DataTypes.STRING,
city: { city: {
...@@ -68,8 +57,8 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -68,8 +57,8 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
enumVals: DataTypes.ENUM('hello', 'world') enumVals: DataTypes.ENUM('hello', 'world')
}, { freezeTableName: true }); }, { freezeTableName: true });
return Users.sync({ force: true }).then(() => { await Users.sync({ force: true });
return this.queryInterface.describeTable('_Users').then(metadata => { const metadata = await this.queryInterface.describeTable('_Users');
const id = metadata.id; const id = metadata.id;
const username = metadata.username; const username = metadata.username;
const city = metadata.city; const city = metadata.city;
...@@ -139,10 +128,8 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -139,10 +128,8 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
expect(username.comment).to.equal(null); expect(username.comment).to.equal(null);
} }
}); });
});
});
it('should correctly determine the primary key columns', function() { it('should correctly determine the primary key columns', async function() {
const Country = this.sequelize.define('_Country', { const Country = this.sequelize.define('_Country', {
code: { type: DataTypes.STRING, primaryKey: true }, code: { type: DataTypes.STRING, primaryKey: true },
name: { type: DataTypes.STRING, allowNull: false } name: { type: DataTypes.STRING, allowNull: false }
...@@ -160,15 +147,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -160,15 +147,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
} }
}, { freezeTableName: true }); }, { freezeTableName: true });
return Country.sync({ force: true }).then(() => { await Country.sync({ force: true });
return this.queryInterface.describeTable('_Country').then( const metacountry = await this.queryInterface.describeTable('_Country');
metacountry => {
expect(metacountry.code.primaryKey).to.eql(true); expect(metacountry.code.primaryKey).to.eql(true);
expect(metacountry.name.primaryKey).to.eql(false); expect(metacountry.name.primaryKey).to.eql(false);
return Alumni.sync({ force: true }).then(() => { await Alumni.sync({ force: true });
return this.queryInterface.describeTable('_Alumni').then( const metalumni = await this.queryInterface.describeTable('_Alumni');
metalumni => {
expect(metalumni.year.primaryKey).to.eql(true); expect(metalumni.year.primaryKey).to.eql(true);
expect(metalumni.num.primaryKey).to.eql(true); expect(metalumni.num.primaryKey).to.eql(true);
expect(metalumni.username.primaryKey).to.eql(false); expect(metalumni.username.primaryKey).to.eql(false);
...@@ -178,8 +163,4 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -178,8 +163,4 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
expect(metalumni.city.primaryKey).to.eql(false); expect(metalumni.city.primaryKey).to.eql(false);
}); });
}); });
});
});
});
});
}); });
...@@ -12,13 +12,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -12,13 +12,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
this.queryInterface = this.sequelize.getQueryInterface(); this.queryInterface = this.sequelize.getQueryInterface();
}); });
afterEach(function() { afterEach(async function() {
return Support.dropTestSchemas(this.sequelize); await Support.dropTestSchemas(this.sequelize);
}); });
describe('dropEnum', () => { describe('dropEnum', () => {
beforeEach(function() { beforeEach(async function() {
return this.queryInterface.createTable('menus', { await this.queryInterface.createTable('menus', {
structuretype: { structuretype: {
type: DataTypes.ENUM('menus', 'submenu', 'routine'), type: DataTypes.ENUM('menus', 'submenu', 'routine'),
allowNull: true allowNull: true
...@@ -35,21 +35,16 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -35,21 +35,16 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
}); });
if (dialect === 'postgres') { if (dialect === 'postgres') {
it('should be able to drop the specified enum', function() { it('should be able to drop the specified enum', async function() {
return this.queryInterface.removeColumn('menus', 'structuretype').then(() => { await this.queryInterface.removeColumn('menus', 'structuretype');
return this.queryInterface.pgListEnums('menus'); const enumList0 = await this.queryInterface.pgListEnums('menus');
}).then(enumList => { expect(enumList0).to.have.lengthOf(1);
expect(enumList).to.have.lengthOf(1); expect(enumList0[0]).to.have.property('enum_name').and.to.equal('enum_menus_structuretype');
expect(enumList[0]).to.have.property('enum_name').and.to.equal('enum_menus_structuretype'); await this.queryInterface.dropEnum('enum_menus_structuretype');
}).then(() => { const enumList = await this.queryInterface.pgListEnums('menus');
return this.queryInterface.dropEnum('enum_menus_structuretype');
}).then(() => {
return this.queryInterface.pgListEnums('menus');
}).then(enumList => {
expect(enumList).to.be.an('array'); expect(enumList).to.be.an('array');
expect(enumList).to.have.lengthOf(0); expect(enumList).to.have.lengthOf(0);
}); });
});
} }
}); });
}); });
...@@ -12,14 +12,14 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -12,14 +12,14 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
this.queryInterface = this.sequelize.getQueryInterface(); this.queryInterface = this.sequelize.getQueryInterface();
}); });
afterEach(function() { afterEach(async function() {
return Support.dropTestSchemas(this.sequelize); await Support.dropTestSchemas(this.sequelize);
}); });
describe('removeColumn', () => { describe('removeColumn', () => {
describe('(without a schema)', () => { describe('(without a schema)', () => {
beforeEach(function() { beforeEach(async function() {
return this.queryInterface.createTable('users', { await this.queryInterface.createTable('users', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
...@@ -46,62 +46,51 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -46,62 +46,51 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
}); });
}); });
it('should be able to remove a column with a default value', function() { it('should be able to remove a column with a default value', async function() {
return this.queryInterface.removeColumn('users', 'firstName').then(() => { await this.queryInterface.removeColumn('users', 'firstName');
return this.queryInterface.describeTable('users'); const table = await this.queryInterface.describeTable('users');
}).then(table => {
expect(table).to.not.have.property('firstName'); expect(table).to.not.have.property('firstName');
}); });
});
it('should be able to remove a column without default value', function() { it('should be able to remove a column without default value', async function() {
return this.queryInterface.removeColumn('users', 'lastName').then(() => { await this.queryInterface.removeColumn('users', 'lastName');
return this.queryInterface.describeTable('users'); const table = await this.queryInterface.describeTable('users');
}).then(table => {
expect(table).to.not.have.property('lastName'); expect(table).to.not.have.property('lastName');
}); });
});
it('should be able to remove a column with a foreign key constraint', function() { it('should be able to remove a column with a foreign key constraint', async function() {
return this.queryInterface.removeColumn('users', 'manager').then(() => { await this.queryInterface.removeColumn('users', 'manager');
return this.queryInterface.describeTable('users'); const table = await this.queryInterface.describeTable('users');
}).then(table => {
expect(table).to.not.have.property('manager'); expect(table).to.not.have.property('manager');
}); });
});
it('should be able to remove a column with primaryKey', function() { it('should be able to remove a column with primaryKey', async function() {
return this.queryInterface.removeColumn('users', 'manager').then(() => { await this.queryInterface.removeColumn('users', 'manager');
return this.queryInterface.describeTable('users'); const table0 = await this.queryInterface.describeTable('users');
}).then(table => { expect(table0).to.not.have.property('manager');
expect(table).to.not.have.property('manager'); await this.queryInterface.removeColumn('users', 'id');
return this.queryInterface.removeColumn('users', 'id'); const table = await this.queryInterface.describeTable('users');
}).then(() => {
return this.queryInterface.describeTable('users');
}).then(table => {
expect(table).to.not.have.property('id'); expect(table).to.not.have.property('id');
}); });
});
// From MSSQL documentation on ALTER COLUMN: // From MSSQL documentation on ALTER COLUMN:
// The modified column cannot be any one of the following: // The modified column cannot be any one of the following:
// - Used in a CHECK or UNIQUE constraint. // - Used in a CHECK or UNIQUE constraint.
// https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql#arguments // https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql#arguments
if (dialect !== 'mssql') { if (dialect !== 'mssql') {
it('should be able to remove a column with unique contraint', function() { it('should be able to remove a column with unique contraint', async function() {
return this.queryInterface.removeColumn('users', 'email').then(() => { await this.queryInterface.removeColumn('users', 'email');
return this.queryInterface.describeTable('users'); const table = await this.queryInterface.describeTable('users');
}).then(table => {
expect(table).to.not.have.property('email'); expect(table).to.not.have.property('email');
}); });
});
} }
}); });
describe('(with a schema)', () => { describe('(with a schema)', () => {
beforeEach(function() { beforeEach(async function() {
return this.sequelize.createSchema('archive').then(() => { await this.sequelize.createSchema('archive');
return this.queryInterface.createTable({
await this.queryInterface.createTable({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, { }, {
...@@ -123,70 +112,69 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -123,70 +112,69 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
} }
}); });
}); });
});
it('should be able to remove a column with a default value', function() { it('should be able to remove a column with a default value', async function() {
return this.queryInterface.removeColumn({ await this.queryInterface.removeColumn({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, 'firstName' }, 'firstName'
).then(() => { );
return this.queryInterface.describeTable({
const table = await this.queryInterface.describeTable({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}); });
}).then(table => {
expect(table).to.not.have.property('firstName'); expect(table).to.not.have.property('firstName');
}); });
});
it('should be able to remove a column without default value', function() { it('should be able to remove a column without default value', async function() {
return this.queryInterface.removeColumn({ await this.queryInterface.removeColumn({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, 'lastName' }, 'lastName'
).then(() => { );
return this.queryInterface.describeTable({
const table = await this.queryInterface.describeTable({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}); });
}).then(table => {
expect(table).to.not.have.property('lastName'); expect(table).to.not.have.property('lastName');
}); });
});
it('should be able to remove a column with primaryKey', function() { it('should be able to remove a column with primaryKey', async function() {
return this.queryInterface.removeColumn({ await this.queryInterface.removeColumn({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, 'id').then(() => { }, 'id');
return this.queryInterface.describeTable({
const table = await this.queryInterface.describeTable({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}); });
}).then(table => {
expect(table).to.not.have.property('id'); expect(table).to.not.have.property('id');
}); });
});
// From MSSQL documentation on ALTER COLUMN: // From MSSQL documentation on ALTER COLUMN:
// The modified column cannot be any one of the following: // The modified column cannot be any one of the following:
// - Used in a CHECK or UNIQUE constraint. // - Used in a CHECK or UNIQUE constraint.
// https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql#arguments // https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql#arguments
if (dialect !== 'mssql') { if (dialect !== 'mssql') {
it('should be able to remove a column with unique contraint', function() { it('should be able to remove a column with unique contraint', async function() {
return this.queryInterface.removeColumn({ await this.queryInterface.removeColumn({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, 'email').then(() => { }, 'email');
return this.queryInterface.describeTable({
const table = await this.queryInterface.describeTable({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}); });
}).then(table => {
expect(table).to.not.have.property('email'); expect(table).to.not.have.property('email');
}); });
});
} }
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!