describeTable.test.js
5.01 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
'use strict';
const chai = require('chai');
const expect = chai.expect;
const Support = require(__dirname + '/../support');
const DataTypes = require(__dirname + '/../../../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 this.sequelize.dropAllSchemas();
});
describe('describeTable', () => {
it('reads the metadata of the table', function() {
const self = this;
const Users = self.sequelize.define('_Users', {
username: DataTypes.STRING,
city: {
type: DataTypes.STRING,
defaultValue: null,
comment: 'Users City'
},
isAdmin: DataTypes.BOOLEAN,
enumVals: DataTypes.ENUM('hello', 'world')
}, { freezeTableName: true });
return Users.sync({ force: true }).then(() => {
return self.queryInterface.describeTable('_Users').then(metadata => {
const id = metadata.id;
const username = metadata.username;
const city = metadata.city;
const isAdmin = metadata.isAdmin;
const enumVals = metadata.enumVals;
expect(id.primaryKey).to.be.true;
if (['mysql', 'mssql'].includes(dialect)) {
expect(id.autoIncrement).to.be.true;
}
let assertVal = 'VARCHAR(255)';
switch (dialect) {
case 'postgres':
assertVal = 'CHARACTER VARYING(255)';
break;
case 'mssql':
assertVal = 'NVARCHAR(255)';
break;
}
expect(username.type).to.equal(assertVal);
expect(username.allowNull).to.be.true;
switch (dialect) {
case 'sqlite':
expect(username.defaultValue).to.be.undefined;
break;
default:
expect(username.defaultValue).to.be.null;
}
switch (dialect) {
case 'sqlite':
expect(city.defaultValue).to.be.null;
break;
}
assertVal = 'TINYINT(1)';
switch (dialect) {
case 'postgres':
assertVal = 'BOOLEAN';
break;
case 'mssql':
assertVal = 'BIT';
break;
}
expect(isAdmin.type).to.equal(assertVal);
expect(isAdmin.allowNull).to.be.true;
switch (dialect) {
case 'sqlite':
expect(isAdmin.defaultValue).to.be.undefined;
break;
default:
expect(isAdmin.defaultValue).to.be.null;
}
if (dialect.match(/^postgres/)) {
expect(enumVals.special).to.be.instanceof(Array);
expect(enumVals.special).to.have.length(2);
} else if (dialect === 'mysql') {
expect(enumVals.type).to.eql('ENUM(\'hello\',\'world\')');
}
if (dialect === 'postgres' || dialect === 'mysql' || dialect === 'mssql') {
expect(city.comment).to.equal('Users City');
expect(username.comment).to.equal(null);
}
});
});
});
it('should correctly determine the primary key columns', function() {
const self = this;
const Country = self.sequelize.define('_Country', {
code: {type: DataTypes.STRING, primaryKey: true },
name: {type: DataTypes.STRING, allowNull: false}
}, { freezeTableName: true });
const Alumni = self.sequelize.define('_Alumni', {
year: {type: DataTypes.INTEGER, primaryKey: true },
num: {type: DataTypes.INTEGER, primaryKey: true },
username: {type: DataTypes.STRING, allowNull: false, unique: true },
dob: {type: DataTypes.DATEONLY, allowNull: false },
dod: {type: DataTypes.DATEONLY, allowNull: true },
city: {type: DataTypes.STRING, allowNull: false},
ctrycod: {type: DataTypes.STRING, allowNull: false,
references: { model: Country, key: 'code'}}
}, { freezeTableName: true });
return Country.sync({ force: true }).then(() => {
return self.queryInterface.describeTable('_Country').then(metacountry => {
expect(metacountry.code.primaryKey).to.eql(true);
expect(metacountry.name.primaryKey).to.eql(false);
return Alumni.sync({ force: true }).then(() => {
return self.queryInterface.describeTable('_Alumni').then(metalumni => {
expect(metalumni.year.primaryKey).to.eql(true);
expect(metalumni.num.primaryKey).to.eql(true);
expect(metalumni.username.primaryKey).to.eql(false);
expect(metalumni.dob.primaryKey).to.eql(false);
expect(metalumni.dod.primaryKey).to.eql(false);
expect(metalumni.ctrycod.primaryKey).to.eql(false);
expect(metalumni.city.primaryKey).to.eql(false);
});
});
});
});
});
});
});