json.test.js
5.76 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
'use strict';
const Support = require(__dirname + '/../support'),
DataTypes = require(__dirname + '/../../../lib/data-types'),
expect = require('chai').expect,
expectsql = Support.expectsql,
Sequelize = Support.Sequelize,
current = Support.sequelize,
sql = current.dialect.QueryGenerator;
// Notice: [] will be replaced by dialect specific tick/quote character when there is not dialect specific expectation but only a default expectation
if (current.dialect.supports.JSON) {
suite(Support.getTestDialectTeaser('SQL'), () => {
suite('JSON', () => {
suite('escape', () => {
test('plain string', () => {
expectsql(sql.escape('string', { type: new DataTypes.JSON() }), {
default: '\'"string"\''
});
});
test('plain int', () => {
expectsql(sql.escape(0, { type: new DataTypes.JSON() }), {
default: '\'0\''
});
expectsql(sql.escape(123, { type: new DataTypes.JSON() }), {
default: '\'123\''
});
});
test('boolean', () => {
expectsql(sql.escape(true, { type: new DataTypes.JSON() }), {
default: '\'true\''
});
expectsql(sql.escape(false, { type: new DataTypes.JSON() }), {
default: '\'false\''
});
});
test('NULL', () => {
expectsql(sql.escape(null, { type: new DataTypes.JSON() }), {
default: 'NULL'
});
});
test('nested object', () => {
expectsql(sql.escape({ some: 'nested', more: { nested: true }, answer: 42 }, { type: new DataTypes.JSON() }), {
default: '\'{"some":"nested","more":{"nested":true},"answer":42}\''
});
});
if (current.dialect.supports.ARRAY) {
test('array of JSON', () => {
expectsql(sql.escape([
{ some: 'nested', more: { nested: true }, answer: 42 },
43,
'joe'
], { type: DataTypes.ARRAY(DataTypes.JSON) }), {
postgres: 'ARRAY[\'{"some":"nested","more":{"nested":true},"answer":42}\',\'43\',\'"joe"\']::JSON[]'
});
});
if (current.dialect.supports.JSONB) {
test('array of JSONB', () => {
expectsql(sql.escape([
{ some: 'nested', more: { nested: true }, answer: 42 },
43,
'joe'
], { type: DataTypes.ARRAY(DataTypes.JSONB) }), {
postgres: 'ARRAY[\'{"some":"nested","more":{"nested":true},"answer":42}\',\'43\',\'"joe"\']::JSONB[]'
});
});
}
}
});
suite('path extraction', () => {
test('condition object', () => {
expectsql(sql.whereItemQuery(undefined, Sequelize.json({ id: 1 })), {
postgres: '("id"#>>\'{}\') = \'1\'',
sqlite: "json_extract(`id`, '$') = '1'"
});
});
test('nested condition object', () => {
expectsql(sql.whereItemQuery(undefined, Sequelize.json({ profile: { id: 1 } })), {
postgres: '("profile"#>>\'{id}\') = \'1\'',
sqlite: "json_extract(`profile`, '$.id') = '1'"
});
});
test('multiple condition object', () => {
expectsql(sql.whereItemQuery(undefined, Sequelize.json({ property: { value: 1 }, another: { value: 'string' } })), {
postgres: '("property"#>>\'{value}\') = \'1\' AND ("another"#>>\'{value}\') = \'string\'',
sqlite: "json_extract(`property`, '$.value') = '1' AND json_extract(`another`, '$.value') = 'string'"
});
});
test('dot notaion', () => {
expectsql(sql.whereItemQuery(Sequelize.json('profile.id'), '1'), {
postgres: '("profile"#>>\'{id}\') = \'1\'',
sqlite: "json_extract(`profile`, '$.id') = '1'"
});
});
test('column named "json"', () => {
expectsql(sql.whereItemQuery(Sequelize.json('json'), '{}'), {
postgres: '("json"#>>\'{}\') = \'{}\'',
sqlite: "json_extract(`json`, '$') = '{}'"
});
});
});
suite('raw json query', () => {
if (current.dialect.name === 'postgres') {
test('#>> operator', () => {
expectsql(sql.whereItemQuery(Sequelize.json('("data"#>>\'{id}\')'), 'id'), {
postgres: '("data"#>>\'{id}\') = \'id\''
});
});
}
test('json function', () => {
expectsql(sql.handleSequelizeMethod(Sequelize.json('json(\'{"profile":{"name":"david"}}\')')), {
default: 'json(\'{"profile":{"name":"david"}}\')'
});
});
test('nested json functions', () => {
expectsql(sql.handleSequelizeMethod(Sequelize.json('json_extract(json_object(\'{"profile":null}\'), "profile")')), {
default: 'json_extract(json_object(\'{"profile":null}\'), "profile")'
});
});
test('escaped string argument', () => {
expectsql(sql.handleSequelizeMethod(Sequelize.json('json(\'{"quote":{"single":"\'\'","double":""""},"parenthesis":"())("}\')')), {
default: 'json(\'{"quote":{"single":"\'\'","double":""""},"parenthesis":"())("}\')'
});
});
test('unbalnced statement', () => {
expect(() => sql.handleSequelizeMethod(Sequelize.json('json())'))).to.throw();
expect(() => sql.handleSequelizeMethod(Sequelize.json('json_extract(json()'))).to.throw();
});
test('separator injection', () => {
expect(() => sql.handleSequelizeMethod(Sequelize.json('json(; DELETE YOLO INJECTIONS; -- )'))).to.throw();
expect(() => sql.handleSequelizeMethod(Sequelize.json('json(); DELETE YOLO INJECTIONS; -- '))).to.throw();
});
});
});
});
}