utils.test.js
6.21 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use strict';
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/support')
, DataTypes = require(__dirname + '/../../lib/data-types')
, Utils = require(__dirname + '/../../lib/utils');
// Notice: [] will be replaced by dialect specific tick/quote character when there is not dialect specific expectation but only a default expectation
suite(Support.getTestDialectTeaser('Utils'), function() {
suite('toDefaultValue', function () {
test('return plain data types', function () {
expect(Utils.toDefaultValue(DataTypes.UUIDV4)).to.equal('UUIDV4');
});
test('return uuid v1', function () {
expect(/^[a-z0-9\-]{36}$/.test(Utils.toDefaultValue(DataTypes.UUIDV1()))).to.be.equal(true);
});
test('return uuid v4', function () {
expect(/^[a-z0-9\-]{36}/.test(Utils.toDefaultValue(DataTypes.UUIDV4()))).to.be.equal(true);
});
test('return now', function () {
expect(Object.prototype.toString.call(Utils.toDefaultValue(DataTypes.NOW()))).to.be.equal('[object Date]');
});
test('return plain string', function () {
expect(Utils.toDefaultValue('Test')).to.equal('Test');
});
test('return plain object', function () {
chai.assert.deepEqual({}, Utils.toDefaultValue({}));
});
});
suite('mapFinderOptions', function () {
test('virtual attribute dependencies', function () {
expect(Utils.mapFinderOptions({
attributes: [
'active'
]
}, this.sequelize.define('User', {
createdAt: {
type: DataTypes.DATE,
field: 'created_at'
},
active: {
type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['createdAt'])
}
})).attributes).to.eql([
[
'created_at',
'createdAt'
]
]);
});
test('multiple calls', function () {
var Model = this.sequelize.define('User', {
createdAt: {
type: DataTypes.DATE,
field: 'created_at'
},
active: {
type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['createdAt'])
}
});
expect(
Utils.mapFinderOptions(
Utils.mapFinderOptions({
attributes: [
'active'
]
}, Model),
Model
).attributes
).to.eql([
[
'created_at',
'createdAt'
]
]);
});
});
suite('mapOptionFieldNames', function () {
test('plain where', function () {
expect(Utils.mapOptionFieldNames({
where: {
firstName: 'Paul',
lastName: 'Atreides'
}
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
}
}))).to.eql({
where: {
first_name: 'Paul',
last_name: 'Atreides'
}
});
});
test('$or where', function () {
expect(Utils.mapOptionFieldNames({
where: {
$or: {
firstName: 'Paul',
lastName: 'Atreides'
}
}
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
}
}))).to.eql({
where: {
$or: {
first_name: 'Paul',
last_name: 'Atreides'
}
}
});
});
test('$or[] where', function () {
expect(Utils.mapOptionFieldNames({
where: {
$or: [
{firstName: 'Paul'},
{lastName: 'Atreides'}
]
}
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
}
}))).to.eql({
where: {
$or: [
{first_name: 'Paul'},
{last_name: 'Atreides'}
]
}
});
});
test('$and where', function () {
expect(Utils.mapOptionFieldNames({
where: {
$and: {
firstName: 'Paul',
lastName: 'Atreides'
}
}
}, this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
}
}))).to.eql({
where: {
$and: {
first_name: 'Paul',
last_name: 'Atreides'
}
}
});
});
});
suite('stack', function() {
test('stack trace starts after call to Util.stack()', function this_here_test() {
function a() {
return b();
}
function b() {
return c();
}
function c() {
return Utils.stack();
}
var stack = a();
expect(stack[0].getFunctionName()).to.eql('c');
expect(stack[1].getFunctionName()).to.eql('b');
expect(stack[2].getFunctionName()).to.eql('a');
expect(stack[3].getFunctionName()).to.eql('this_here_test');
});
});
suite('formatReferences', function () {
([
[{referencesKey: 1}, {references: {model: undefined, key: 1, deferrable: undefined}, referencesKey: undefined, referencesDeferrable: undefined}],
[{references: 'a'}, {references: {model: 'a', key: undefined, deferrable: undefined}, referencesKey: undefined, referencesDeferrable: undefined}],
[{references: 'a', referencesKey: 1}, {references: {model: 'a', key: 1, deferrable: undefined}, referencesKey: undefined, referencesDeferrable: undefined}],
[{references: {model: 1}}, {references: {model: 1}}],
[{references: 1, referencesKey: 2, referencesDeferrable: 3}, {references: {model: 1, key: 2, deferrable: 3}, referencesKey: undefined, referencesDeferrable: undefined}]
]).forEach(function (test) {
var input = test[0];
var output = test[1];
it(JSON.stringify(input) + ' to ' + JSON.stringify(output), function () {
expect(Utils.formatReferences(input)).to.deep.equal(output);
});
});
});
});