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

Commit fd11d98f by Andy Edwards Committed by GitHub

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

1 parent e34efd47
...@@ -12,47 +12,47 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -12,47 +12,47 @@ 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({
tableName: 'users', await this.queryInterface.createTable({
schema: 'archive' tableName: 'users',
}, { schema: 'archive'
id: { }, {
type: DataTypes.INTEGER, id: {
primaryKey: true, type: DataTypes.INTEGER,
autoIncrement: true primaryKey: true,
}, autoIncrement: true
currency: DataTypes.INTEGER },
}).then(() => { currency: DataTypes.INTEGER
return this.queryInterface.changeColumn({
tableName: 'users',
schema: 'archive'
}, 'currency', {
type: DataTypes.FLOAT
});
}).then(() => {
return this.queryInterface.describeTable({
tableName: 'users',
schema: 'archive'
});
}).then(table => {
if (dialect === 'postgres' || dialect === 'postgres-native') {
expect(table.currency.type).to.equal('DOUBLE PRECISION');
} else {
expect(table.currency.type).to.equal('FLOAT');
}
});
}); });
await this.queryInterface.changeColumn({
tableName: 'users',
schema: 'archive'
}, 'currency', {
type: DataTypes.FLOAT
});
const table = await this.queryInterface.describeTable({
tableName: 'users',
schema: 'archive'
});
if (dialect === 'postgres' || dialect === 'postgres-native') {
expect(table.currency.type).to.equal('DOUBLE PRECISION');
} else {
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,67 +61,67 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -61,67 +61,67 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
autoIncrement: true autoIncrement: true
}, },
currency: DataTypes.INTEGER currency: DataTypes.INTEGER
}).then(() => {
return this.queryInterface.changeColumn('users', 'currency', {
type: DataTypes.FLOAT,
allowNull: true
});
}).then(() => {
return this.queryInterface.describeTable({
tableName: 'users'
});
}).then(table => {
if (dialect === 'postgres' || dialect === 'postgres-native') {
expect(table.currency.type).to.equal('DOUBLE PRECISION');
} else {
expect(table.currency.type).to.equal('FLOAT');
}
}); });
await this.queryInterface.changeColumn('users', 'currency', {
type: DataTypes.FLOAT,
allowNull: true
});
const table = await this.queryInterface.describeTable({
tableName: 'users'
});
if (dialect === 'postgres' || dialect === 'postgres-native') {
expect(table.currency.type).to.equal('DOUBLE PRECISION');
} else {
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', {
type: DataTypes.ENUM, await this.queryInterface.changeColumn('users', 'firstName', {
values: ['value1', 'value2', 'value3'] type: DataTypes.ENUM,
}); 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({
tableName: 'users', await this.queryInterface.createTable({
schema: 'archive' tableName: 'users',
}, { schema: 'archive'
firstName: DataTypes.STRING }, {
}); firstName: DataTypes.STRING
}).then(() => { });
return this.queryInterface.changeColumn({
tableName: 'users', await this.queryInterface.changeColumn({
schema: 'archive' tableName: 'users',
}, 'firstName', { schema: 'archive'
type: DataTypes.ENUM(['value1', 'value2', 'value3']) }, 'firstName', {
}); type: DataTypes.ENUM(['value1', 'value2', 'value3'])
}); });
}); });
} }
...@@ -129,8 +129,8 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -129,8 +129,8 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
//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,41 +140,39 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -140,41 +140,39 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false allowNull: false
} }
}).then(() => {
return this.queryInterface.createTable('level', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
}
});
}); });
});
it('able to change column to foreign key', function() { await this.queryInterface.createTable('level', {
return this.queryInterface.getForeignKeyReferencesForTable('users').then( foreignKeys => { id: {
expect(foreignKeys).to.be.an('array');
expect(foreignKeys).to.be.empty;
return this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: { primaryKey: true,
model: 'level', autoIncrement: true
key: 'id' }
}, });
onUpdate: 'cascade', });
onDelete: 'cascade'
}); it('able to change column to foreign key', async function() {
}).then(() => { const foreignKeys = await this.queryInterface.getForeignKeyReferencesForTable('users');
return this.queryInterface.getForeignKeyReferencesForTable('users'); expect(foreignKeys).to.be.an('array');
}).then(newForeignKeys => { expect(foreignKeys).to.be.empty;
expect(newForeignKeys).to.be.an('array');
expect(newForeignKeys).to.have.lengthOf(1); await this.queryInterface.changeColumn('users', 'level_id', {
expect(newForeignKeys[0].columnName).to.be.equal('level_id'); type: DataTypes.INTEGER,
references: {
model: 'level',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
}); });
const newForeignKeys = await this.queryInterface.getForeignKeyReferencesForTable('users');
expect(newForeignKeys).to.be.an('array');
expect(newForeignKeys).to.have.lengthOf(1);
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,58 +180,56 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -182,58 +180,56 @@ 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', {
type: DataTypes.INTEGER,
references: {
model: 'level',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
});
}).then( () => {
return this.queryInterface.getForeignKeyReferencesForTable('users');
}).then( keys => {
firstForeignKeys = keys;
return this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER,
allowNull: true
});
}).then( () => {
return this.queryInterface.getForeignKeyReferencesForTable('users');
}).then( newForeignKeys => {
expect(firstForeignKeys.length).to.be.equal(newForeignKeys.length);
expect(firstForeignKeys[0].columnName).to.be.equal('level_id');
expect(firstForeignKeys[0].columnName).to.be.equal(newForeignKeys[0].columnName);
return this.queryInterface.describeTable({
tableName: 'users'
});
}).then( describedTable => {
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.be.equal(true);
}); });
await this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER,
references: {
model: 'level',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
});
const keys = await this.queryInterface.getForeignKeyReferencesForTable('users');
const firstForeignKeys = keys;
await this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER,
allowNull: true
});
const newForeignKeys = await this.queryInterface.getForeignKeyReferencesForTable('users');
expect(firstForeignKeys.length).to.be.equal(newForeignKeys.length);
expect(firstForeignKeys[0].columnName).to.be.equal('level_id');
expect(firstForeignKeys[0].columnName).to.be.equal(newForeignKeys[0].columnName);
const describedTable = await this.queryInterface.describeTable({
tableName: 'users'
});
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.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);
return this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER,
comment: 'FooBar'
});
}).then(() => {
return this.queryInterface.describeTable({ tableName: 'users' });
}).then(describedTable2 => {
expect(describedTable2.level_id.comment).to.be.equal('FooBar');
}); });
expect(describedTable.level_id.comment).to.be.equal(null);
await this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER,
comment: 'FooBar'
});
const describedTable2 = await this.queryInterface.describeTable({ tableName: 'users' });
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,131 +53,123 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -55,131 +53,123 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
fields: ['name'] fields: ['name']
} }
} }
}).then(() => {
return this.queryInterface.showIndex('MyTable');
}).then(indexes => {
switch (dialect) {
case 'postgres':
case 'postgres-native':
case 'sqlite':
case 'mssql':
// name + email
expect(indexes[0].unique).to.be.true;
expect(indexes[0].fields[0].attribute).to.equal('name');
expect(indexes[0].fields[1].attribute).to.equal('email');
// name
expect(indexes[1].unique).to.be.true;
expect(indexes[1].fields[0].attribute).to.equal('name');
break;
case 'mariadb':
case 'mysql':
// name + email
expect(indexes[1].unique).to.be.true;
expect(indexes[1].fields[0].attribute).to.equal('name');
expect(indexes[1].fields[1].attribute).to.equal('email');
// name
expect(indexes[2].unique).to.be.true;
expect(indexes[2].fields[0].attribute).to.equal('name');
break;
default:
throw new Error(`Not implemented fpr ${dialect}`);
}
}); });
const indexes = await this.queryInterface.showIndex('MyTable');
switch (dialect) {
case 'postgres':
case 'postgres-native':
case 'sqlite':
case 'mssql':
// name + email
expect(indexes[0].unique).to.be.true;
expect(indexes[0].fields[0].attribute).to.equal('name');
expect(indexes[0].fields[1].attribute).to.equal('email');
// name
expect(indexes[1].unique).to.be.true;
expect(indexes[1].fields[0].attribute).to.equal('name');
break;
case 'mariadb':
case 'mysql':
// name + email
expect(indexes[1].unique).to.be.true;
expect(indexes[1].fields[0].attribute).to.equal('name');
expect(indexes[1].fields[1].attribute).to.equal('email');
// name
expect(indexes[2].unique).to.be.true;
expect(indexes[2].fields[0].attribute).to.equal('name');
break;
default:
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', {
name: { await this.queryInterface.createTable('User', {
type: DataTypes.STRING name: {
} type: DataTypes.STRING
}, { }
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 => {
if (dialect.includes('postgres')) {
expect(table.someEnum.special).to.deep.equal(['value1', 'value2', 'value3']);
}
}); });
const table = await this.queryInterface.describeTable('SomeTable');
if (dialect.includes('postgres')) {
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 => {
if (dialect.includes('postgres')) {
expect(table.someEnum.special).to.deep.equal(['value1', 'value2', 'value3']);
}
}); });
const table = await this.queryInterface.describeTable('SomeTable');
if (dialect.includes('postgres')) {
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 => {
if (dialect.includes('postgres')) {
expect(table.otherName.special).to.deep.equal(['value1', 'value2', 'value3']);
}
}); });
const table = await this.queryInterface.describeTable('SomeTable');
if (dialect.includes('postgres')) {
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', {
someEnum: { await this.queryInterface.createTable('SomeTable', {
type: DataTypes.ENUM, someEnum: {
values: ['value1', 'value2', 'value3'], type: DataTypes.ENUM,
field: 'otherName' values: ['value1', 'value2', 'value3'],
} field: 'otherName'
}, { schema: 'archive' });
}).then(() => {
return this.queryInterface.describeTable('SomeTable', { schema: 'archive' });
}).then(table => {
if (dialect.includes('postgres')) {
expect(table.otherName.special).to.deep.equal(['value1', 'value2', 'value3']);
} }
}); }, { schema: 'archive' });
const table = await this.queryInterface.describeTable('SomeTable', { schema: 'archive' });
if (dialect.includes('postgres')) {
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 => {
if (dialect.includes('postgres')) {
expect(table.someEnum.special).to.deep.equal(['COMMENT']);
expect(table.someEnum.comment).to.equal('special enum col');
}
}); });
const table = await this.queryInterface.describeTable('SomeTable');
if (dialect.includes('postgres')) {
expect(table.someEnum.special).to.deep.equal(['COMMENT']);
expect(table.someEnum.comment).to.equal('special enum col');
}
}); });
}); });
}); });
......
...@@ -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');
}) expect(metadata.username1).not.to.be.undefined;
.then(() => {
return this.queryInterface.describeTable('my_tables', 'test_meta'); await this.sequelize.dropSchema('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;
return 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,81 +57,79 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -68,81 +57,79 @@ 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;
const isAdmin = metadata.isAdmin; const isAdmin = metadata.isAdmin;
const enumVals = metadata.enumVals; const enumVals = metadata.enumVals;
expect(id.primaryKey).to.be.true; expect(id.primaryKey).to.be.true;
if (['mysql', 'mssql'].includes(dialect)) { if (['mysql', 'mssql'].includes(dialect)) {
expect(id.autoIncrement).to.be.true; expect(id.autoIncrement).to.be.true;
} }
let assertVal = 'VARCHAR(255)'; let assertVal = 'VARCHAR(255)';
switch (dialect) { switch (dialect) {
case 'postgres': case 'postgres':
assertVal = 'CHARACTER VARYING(255)'; assertVal = 'CHARACTER VARYING(255)';
break; break;
case 'mssql': case 'mssql':
assertVal = 'NVARCHAR(255)'; assertVal = 'NVARCHAR(255)';
break; break;
} }
expect(username.type).to.equal(assertVal); expect(username.type).to.equal(assertVal);
expect(username.allowNull).to.be.true; expect(username.allowNull).to.be.true;
switch (dialect) { switch (dialect) {
case 'sqlite': case 'sqlite':
expect(username.defaultValue).to.be.undefined; expect(username.defaultValue).to.be.undefined;
break; break;
default: default:
expect(username.defaultValue).to.be.null; expect(username.defaultValue).to.be.null;
} }
switch (dialect) { switch (dialect) {
case 'sqlite': case 'sqlite':
expect(city.defaultValue).to.be.null; expect(city.defaultValue).to.be.null;
break; break;
} }
assertVal = 'TINYINT(1)'; assertVal = 'TINYINT(1)';
switch (dialect) { switch (dialect) {
case 'postgres': case 'postgres':
assertVal = 'BOOLEAN'; assertVal = 'BOOLEAN';
break; break;
case 'mssql': case 'mssql':
assertVal = 'BIT'; assertVal = 'BIT';
break; break;
} }
expect(isAdmin.type).to.equal(assertVal); expect(isAdmin.type).to.equal(assertVal);
expect(isAdmin.allowNull).to.be.true; expect(isAdmin.allowNull).to.be.true;
switch (dialect) { switch (dialect) {
case 'sqlite': case 'sqlite':
expect(isAdmin.defaultValue).to.be.undefined; expect(isAdmin.defaultValue).to.be.undefined;
break; break;
default: default:
expect(isAdmin.defaultValue).to.be.null; expect(isAdmin.defaultValue).to.be.null;
} }
if (dialect.match(/^postgres/)) { if (dialect.match(/^postgres/)) {
expect(enumVals.special).to.be.instanceof(Array); expect(enumVals.special).to.be.instanceof(Array);
expect(enumVals.special).to.have.length(2); expect(enumVals.special).to.have.length(2);
} else if (dialect === 'mysql') { } else if (dialect === 'mysql') {
expect(enumVals.type).to.eql('ENUM(\'hello\',\'world\')'); expect(enumVals.type).to.eql('ENUM(\'hello\',\'world\')');
} }
if (dialect === 'postgres' || dialect === 'mysql' || dialect === 'mssql') { if (dialect === 'postgres' || dialect === 'mysql' || dialect === 'mssql') {
expect(city.comment).to.equal('Users City'); expect(city.comment).to.equal('Users City');
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,26 +147,20 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -160,26 +147,20 @@ 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);
await Alumni.sync({ force: true });
return Alumni.sync({ force: true }).then(() => { const metalumni = await this.queryInterface.describeTable('_Alumni');
return this.queryInterface.describeTable('_Alumni').then( expect(metalumni.year.primaryKey).to.eql(true);
metalumni => { expect(metalumni.num.primaryKey).to.eql(true);
expect(metalumni.year.primaryKey).to.eql(true); expect(metalumni.username.primaryKey).to.eql(false);
expect(metalumni.num.primaryKey).to.eql(true); expect(metalumni.dob.primaryKey).to.eql(false);
expect(metalumni.username.primaryKey).to.eql(false); expect(metalumni.dod.primaryKey).to.eql(false);
expect(metalumni.dob.primaryKey).to.eql(false); expect(metalumni.ctrycod.primaryKey).to.eql(false);
expect(metalumni.dod.primaryKey).to.eql(false); expect(metalumni.city.primaryKey).to.eql(false);
expect(metalumni.ctrycod.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,20 +35,15 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -35,20 +35,15 @@ 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'); expect(enumList).to.be.an('array');
}).then(() => { expect(enumList).to.have.lengthOf(0);
return this.queryInterface.pgListEnums('menus');
}).then(enumList => {
expect(enumList).to.be.an('array');
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,41 +46,31 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -46,41 +46,31 @@ 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(() => { expect(table).to.not.have.property('id');
return this.queryInterface.describeTable('users');
}).then(table => {
expect(table).to.not.have.property('id');
});
}); });
// From MSSQL documentation on ALTER COLUMN: // From MSSQL documentation on ALTER COLUMN:
...@@ -88,85 +78,83 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -88,85 +78,83 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
// - 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({
tableName: 'users', await this.queryInterface.createTable({
schema: 'archive' tableName: 'users',
}, { schema: 'archive'
id: { }, {
type: DataTypes.INTEGER, id: {
primaryKey: true, type: DataTypes.INTEGER,
autoIncrement: true primaryKey: true,
}, autoIncrement: true
firstName: { },
type: DataTypes.STRING, firstName: {
defaultValue: 'Someone' type: DataTypes.STRING,
}, defaultValue: 'Someone'
lastName: { },
type: DataTypes.STRING lastName: {
}, type: DataTypes.STRING
email: { },
type: DataTypes.STRING, email: {
unique: true type: DataTypes.STRING,
} unique: true
}); }
}); });
}); });
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({
tableName: 'users', const table = await this.queryInterface.describeTable({
schema: 'archive' tableName: 'users',
}); 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({
tableName: 'users', const table = await this.queryInterface.describeTable({
schema: 'archive' tableName: 'users',
}); 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',
schema: 'archive'
}, 'id');
const table = await this.queryInterface.describeTable({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, 'id').then(() => {
return this.queryInterface.describeTable({
tableName: 'users',
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:
...@@ -174,18 +162,18 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -174,18 +162,18 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
// - 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',
schema: 'archive'
}, 'email');
const table = await this.queryInterface.describeTable({
tableName: 'users', tableName: 'users',
schema: 'archive' schema: 'archive'
}, 'email').then(() => {
return this.queryInterface.describeTable({
tableName: 'users',
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!