createTable.test.js
4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
const chai = require('chai');
const expect = chai.expect;
const Support = require('../support');
const DataTypes = require('../../../lib/data-types');
const dialect = Support.getTestDialect();
describe(Support.getTestDialectTeaser('QueryInterface'), () => {
beforeEach(function() {
this.sequelize.options.quoteIdenifiers = true;
this.queryInterface = this.sequelize.getQueryInterface();
});
afterEach(function() {
return Support.dropTestSchemas(this.sequelize);
});
// FIXME: These tests should make assertions against the created table using describeTable
describe('createTable', () => {
it('should create a auto increment primary key', function() {
return this.queryInterface.createTable('TableWithPK', {
table_id: {
type: DataTypes.INTEGER,
primaryKey: 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;
});
});
});
it('should create unique constraint with uniqueKeys', function() {
return this.queryInterface.createTable('MyTable', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING
},
email: {
type: DataTypes.STRING
}
}, {
uniqueKeys: {
myCustomIndex: {
fields: ['name', 'email']
},
myOtherIndex: {
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}`);
}
});
});
it('should work with enums (1)', function() {
return this.queryInterface.createTable('SomeTable', {
someEnum: DataTypes.ENUM('value1', 'value2', 'value3')
});
});
it('should work with enums (2)', function() {
return this.queryInterface.createTable('SomeTable', {
someEnum: {
type: DataTypes.ENUM,
values: ['value1', 'value2', 'value3']
}
});
});
it('should work with enums (3)', function() {
return this.queryInterface.createTable('SomeTable', {
someEnum: {
type: DataTypes.ENUM,
values: ['value1', 'value2', 'value3'],
field: 'otherName'
}
});
});
it('should work with enums (4)', function() {
return this.queryInterface.createSchema('archive').then(() => {
return this.queryInterface.createTable('SomeTable', {
someEnum: {
type: DataTypes.ENUM,
values: ['value1', 'value2', 'value3'],
field: 'otherName'
}
}, { schema: 'archive' });
});
});
it('should work with enums (5)', function() {
return this.queryInterface.createTable('SomeTable', {
someEnum: {
type: DataTypes.ENUM(['COMMENT']),
comment: 'special enum col'
}
});
});
it('should work with schemas', function() {
return this.sequelize.createSchema('hero').then(() => {
return this.queryInterface.createTable('User', {
name: {
type: DataTypes.STRING
}
}, {
schema: 'hero'
});
});
});
});
});