indexes.test.js
2.2 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../support'),
current = Support.sequelize,
DataTypes = require(__dirname + '/../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('indexes', () => {
it('should automatically set a gin index for JSONB indexes', () => {
const Model = current.define('event', {
eventData: {
type: DataTypes.JSONB,
index: true,
field: 'data'
}
});
expect(Model.rawAttributes.eventData.index).not.to.equal(true);
expect(Model.options.indexes.length).to.equal(1);
expect(Model.options.indexes[0].fields).to.eql(['data']);
expect(Model.options.indexes[0].using).to.equal('gin');
});
it('should set the unique property when type is unique', () => {
const Model = current.define('m', {}, {
indexes: [
{
type: 'unique'
},
{
type: 'UNIQUE'
}
]
});
expect(Model.options.indexes[0].unique).to.eql(true);
expect(Model.options.indexes[1].unique).to.eql(true);
});
it('should set rawAttributes when indexes are defined via options', () => {
const User = current.define('User', {
username: DataTypes.STRING
}, {
indexes: [{
unique: true,
fields: ['username']
}]
});
expect(User.rawAttributes.username).to.have.property('unique');
expect(User.rawAttributes.username.unique).to.be.true;
});
it('should set rawAttributes when composite unique indexes are defined via options', () => {
const User = current.define('User', {
name: DataTypes.STRING,
address: DataTypes.STRING
}, {
indexes: [{
unique: 'users_name_address',
fields: ['name', 'address']
}]
});
expect(User.rawAttributes.name).to.have.property('unique');
expect(User.rawAttributes.name.unique).to.be.equal('users_name_address');
expect(User.rawAttributes.address).to.have.property('unique');
expect(User.rawAttributes.address.unique).to.be.equal('users_name_address');
});
});
});