/
define.test.js
133 lines (118 loc) · 3.77 KB
/
define.test.js
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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
DataTypes = require('../../../lib/data-types'),
sinon = require('sinon'),
current = Support.sequelize,
dialect = Support.getTestDialect();
describe(Support.getTestDialectTeaser('Model'), () => {
describe('define', () => {
it('should allow custom timestamps with underscored: true', () => {
const Model = current.define(
'User',
{},
{
createdAt: 'createdAt',
updatedAt: 'updatedAt',
timestamps: true,
underscored: true
}
);
expect(Model.rawAttributes).to.haveOwnProperty('createdAt');
expect(Model.rawAttributes).to.haveOwnProperty('updatedAt');
expect(Model._timestampAttributes.createdAt).to.equal('createdAt');
expect(Model._timestampAttributes.updatedAt).to.equal('updatedAt');
expect(Model.rawAttributes).not.to.have.property('created_at');
expect(Model.rawAttributes).not.to.have.property('updated_at');
});
it('should throw when id is added but not marked as PK', () => {
expect(() => {
current.define('foo', {
id: DataTypes.INTEGER
});
}).to.throw("A column called 'id' was added to the attributes of 'foos' but not marked with 'primaryKey: true'");
expect(() => {
current.define('bar', {
id: {
type: DataTypes.INTEGER
}
});
}).to.throw("A column called 'id' was added to the attributes of 'bars' but not marked with 'primaryKey: true'");
});
it('should defend against null or undefined "unique" attributes', () => {
expect(() => {
current.define('baz', {
foo: {
type: DataTypes.STRING,
unique: null
},
bar: {
type: DataTypes.STRING,
unique: undefined
},
bop: {
type: DataTypes.DATE
}
});
}).not.to.throw();
});
it('should throw for unknown data type', () => {
expect(() => {
current.define('bar', {
name: {
type: DataTypes.MY_UNKNOWN_TYPE
}
});
}).to.throw('Unrecognized datatype for attribute "bar.name"');
});
it('should throw for notNull validator without allowNull', () => {
expect(() => {
current.define('user', {
name: {
type: DataTypes.STRING,
allowNull: true,
validate: {
notNull: {
msg: 'Please enter the name'
}
}
}
});
}).to.throw('Invalid definition for "user.name", "notNull" validator is only allowed with "allowNull:false"');
expect(() => {
current.define('part', {
name: {
type: DataTypes.STRING,
validate: {
notNull: {
msg: 'Please enter the part name'
}
}
}
});
}).to.throw('Invalid definition for "part.name", "notNull" validator is only allowed with "allowNull:false"');
});
describe('datatype warnings', () => {
beforeEach(() => {
sinon.spy(console, 'warn');
});
afterEach(() => {
console.warn.restore();
});
it('warn for unsupported INTEGER options', () => {
current.define('A', {
age: {
type: DataTypes.TINYINT.UNSIGNED
}
});
if (dialect === 'postgres' || dialect === 'sqlite' || dialect === 'mssql') {
expect(true).to.equal(console.warn.calledOnce);
expect(console.warn.args[0][0]).to.contain("does not support 'TINYINT'");
} else {
expect(false).to.equal(console.warn.calledOnce);
}
});
});
});
});