-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
json.test.js
321 lines (261 loc) · 12 KB
/
json.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('./support'),
dialect = Support.getTestDialect(),
Sequelize = Support.Sequelize,
current = Support.sequelize,
DataTypes = Sequelize.DataTypes;
describe('model', () => {
if (current.dialect.supports.JSON) {
describe('json', () => {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING,
emergency_contact: DataTypes.JSON,
emergencyContact: DataTypes.JSON
});
this.Order = this.sequelize.define('Order');
this.Order.belongsTo(this.User);
await this.sequelize.sync({ force: true });
});
it('should tell me that a column is json', async function() {
const table = await this.sequelize.queryInterface.describeTable('Users');
// expected for mariadb 10.4 : https://jira.mariadb.org/browse/MDEV-15558
// oracledb 19c doesn't support JSON and the DB datatype is BLOB
if (dialect === 'oracle') {
expect(table.emergency_contact.type).to.equal('BLOB');
} else if (dialect !== 'mariadb') {
expect(table.emergency_contact.type).to.equal('JSON');
}
});
it('should use a placeholder for json with insert', async function() {
await this.User.create({
username: 'bob',
emergency_contact: { name: 'joe', phones: [1337, 42] }
}, {
fields: ['id', 'username', 'document', 'emergency_contact'],
logging: sql => {
if (dialect.match(/^mysql|mariadb/)) {
expect(sql).to.include('?');
} else if (dialect === 'oracle') {
expect(sql).to.include(':1');
} else {
expect(sql).to.include('$1');
}
}
});
});
it('should insert json using a custom field name', async function() {
this.UserFields = this.sequelize.define('UserFields', {
emergencyContact: { type: DataTypes.JSON, field: 'emergy_contact' }
});
await this.UserFields.sync({ force: true });
const user = await this.UserFields.create({
emergencyContact: { name: 'joe', phones: [1337, 42] }
});
expect(user.emergencyContact.name).to.equal('joe');
});
it('should update json using a custom field name', async function() {
this.UserFields = this.sequelize.define('UserFields', {
emergencyContact: { type: DataTypes.JSON, field: 'emergy_contact' }
});
await this.UserFields.sync({ force: true });
const user0 = await this.UserFields.create({
emergencyContact: { name: 'joe', phones: [1337, 42] }
});
user0.emergencyContact = { name: 'larry' };
const user = await user0.save();
expect(user.emergencyContact.name).to.equal('larry');
});
it('should be able retrieve json value as object', async function() {
const emergencyContact = { name: 'kate', phone: 1337 };
const user0 = await this.User.create({ username: 'swen', emergency_contact: emergencyContact });
expect(user0.emergency_contact).to.eql(emergencyContact);
const user = await this.User.findOne({ where: { username: 'swen' }, attributes: ['emergency_contact'] });
expect(user.emergency_contact).to.eql(emergencyContact);
});
it('should be able to retrieve element of array by index', async function() {
const emergencyContact = { name: 'kate', phones: [1337, 42] };
const user0 = await this.User.create({ username: 'swen', emergency_contact: emergencyContact });
expect(user0.emergency_contact).to.eql(emergencyContact);
const user = await this.User.findOne({
where: { username: 'swen' },
attributes: [[Sequelize.json('emergency_contact.phones[1]'), 'firstEmergencyNumber']]
});
expect(parseInt(user.getDataValue('firstEmergencyNumber'), 10)).to.equal(42);
});
it('should be able to retrieve root level value of an object by key', async function() {
const emergencyContact = { kate: 1337 };
const user0 = await this.User.create({ username: 'swen', emergency_contact: emergencyContact });
expect(user0.emergency_contact).to.eql(emergencyContact);
const user = await this.User.findOne({
where: { username: 'swen' },
attributes: [[Sequelize.json('emergency_contact.kate'), 'katesNumber']]
});
expect(parseInt(user.getDataValue('katesNumber'), 10)).to.equal(1337);
});
it('should be able to retrieve nested value of an object by path', async function() {
const emergencyContact = { kate: { email: 'kate@kate.com', phones: [1337, 42] } };
const user1 = await this.User.create({ username: 'swen', emergency_contact: emergencyContact });
expect(user1.emergency_contact).to.eql(emergencyContact);
const user0 = await this.User.findOne({
where: { username: 'swen' },
attributes: [[Sequelize.json('emergency_contact.kate.email'), 'katesEmail']]
});
expect(user0.getDataValue('katesEmail')).to.equal('kate@kate.com');
const user = await this.User.findOne({
where: { username: 'swen' },
attributes: [[Sequelize.json('emergency_contact.kate.phones[1]'), 'katesFirstPhone']]
});
expect(parseInt(user.getDataValue('katesFirstPhone'), 10)).to.equal(42);
});
it('should be able to retrieve a row based on the values of the json document', async function() {
await Promise.all([
this.User.create({ username: 'swen', emergency_contact: { name: 'kate' } }),
this.User.create({ username: 'anna', emergency_contact: { name: 'joe' } })
]);
const user = await this.User.findOne({
where: Sequelize.json('emergency_contact.name', 'kate'),
attributes: ['username', 'emergency_contact']
});
expect(user.emergency_contact.name).to.equal('kate');
});
it('should be able to query using the nested query language', async function() {
await Promise.all([
this.User.create({ username: 'swen', emergency_contact: { name: 'kate' } }),
this.User.create({ username: 'anna', emergency_contact: { name: 'joe' } })
]);
const user = await this.User.findOne({
where: Sequelize.json({ emergency_contact: { name: 'kate' } })
});
expect(user.emergency_contact.name).to.equal('kate');
});
it('should be able to query using dot notation', async function() {
await Promise.all([
this.User.create({ username: 'swen', emergency_contact: { name: 'kate' } }),
this.User.create({ username: 'anna', emergency_contact: { name: 'joe' } })
]);
const user = await this.User.findOne({ where: Sequelize.json('emergency_contact.name', 'joe') });
expect(user.emergency_contact.name).to.equal('joe');
});
it('should be able to query using dot notation with uppercase name', async function() {
await Promise.all([
this.User.create({ username: 'swen', emergencyContact: { name: 'kate' } }),
this.User.create({ username: 'anna', emergencyContact: { name: 'joe' } })
]);
const user = await this.User.findOne({
attributes: [[Sequelize.json('emergencyContact.name'), 'contactName']],
where: Sequelize.json('emergencyContact.name', 'joe')
});
expect(user.get('contactName')).to.equal('joe');
});
it('should be able to query array using property accessor', async function() {
await Promise.all([
this.User.create({ username: 'swen', emergency_contact: ['kate', 'joe'] }),
this.User.create({ username: 'anna', emergency_contact: [{ name: 'joe' }] })
]);
const user0 = await this.User.findOne({ where: Sequelize.json('emergency_contact.0', 'kate') });
expect(user0.username).to.equal('swen');
const user = await this.User.findOne({ where: Sequelize.json('emergency_contact[0].name', 'joe') });
expect(user.username).to.equal('anna');
});
it('should be able to store strings', async function() {
if (dialect === 'oracle') {
const dbVersion = this.sequelize.options.databaseVersion;
// Oracle DB below 21c doesn't recognize a string as a valid json
if (dbVersion.localeCompare('21.0.0.0') === -1) {
this.skip();
}
}
await this.User.create({ username: 'swen', emergency_contact: 'joe' });
const user = await this.User.findOne({ where: { username: 'swen' } });
expect(user.emergency_contact).to.equal('joe');
});
it('should be able to store values that require JSON escaping', async function() {
const text = 'Multi-line \'$string\' needing "escaping" for $$ and $1 type values';
const user0 = await this.User.create({
username: 'swen',
emergency_contact: { value: text }
});
expect(user0.isNewRecord).to.equal(false);
await this.User.findOne({ where: { username: 'swen' } });
const user = await this.User.findOne({ where: Sequelize.json('emergency_contact.value', text) });
expect(user.username).to.equal('swen');
});
it('should be able to findOrCreate with values that require JSON escaping', async function() {
const text = 'Multi-line \'$string\' needing "escaping" for $$ and $1 type values';
const user0 = await this.User.findOrCreate({
where: { username: 'swen' },
defaults: { emergency_contact: { value: text } }
});
expect(!user0.isNewRecord).to.equal(true);
await this.User.findOne({ where: { username: 'swen' } });
const user = await this.User.findOne({ where: Sequelize.json('emergency_contact.value', text) });
expect(user.username).to.equal('swen');
});
// JSONB Supports this, but not JSON in postgres/mysql
if (current.dialect.name === 'sqlite') {
it('should be able to find with just string', async function() {
await this.User.create({
username: 'swen123',
emergency_contact: 'Unknown'
});
const user = await this.User.findOne({ where: {
emergency_contact: 'Unknown'
} });
expect(user.username).to.equal('swen123');
});
}
it('should be able retrieve json value with nested include', async function() {
const user = await this.User.create({
emergency_contact: {
name: 'kate'
}
});
await this.Order.create({ UserId: user.id });
const orders = await this.Order.findAll({
attributes: ['id'],
include: [{
model: this.User,
attributes: [
[this.sequelize.json('emergency_contact.name'), 'katesName']
]
}]
});
expect(orders[0].User.getDataValue('katesName')).to.equal('kate');
});
});
}
if (current.dialect.supports.JSONB) {
describe('jsonb', () => {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING,
emergency_contact: DataTypes.JSONB
});
this.Order = this.sequelize.define('Order');
this.Order.belongsTo(this.User);
await this.sequelize.sync({ force: true });
});
it('should be able retrieve json value with nested include', async function() {
const user = await this.User.create({
emergency_contact: {
name: 'kate'
}
});
await this.Order.create({ UserId: user.id });
const orders = await this.Order.findAll({
attributes: ['id'],
include: [{
model: this.User,
attributes: [
[this.sequelize.json('emergency_contact.name'), 'katesName']
]
}]
});
expect(orders[0].User.getDataValue('katesName')).to.equal('kate');
});
});
}
});