forked from nearform/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL.test.js
265 lines (216 loc) · 9.88 KB
/
SQL.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
'use strict'
const test = require('tap').test
const SQL = require('./SQL')
test('SQL helper - build complex query with append', (t) => {
const name = 'Team 5'
const description = 'description'
const teamId = 7
const organizationId = 'WONKA'
const sql = SQL`UPDATE teams SET name = ${name}, description = ${description} `
sql.append(SQL`WHERE id = ${teamId} AND org_id = ${organizationId}`)
t.equal(sql.text, 'UPDATE teams SET name = $1, description = $2 WHERE id = $3 AND org_id = $4')
t.equal(sql.sql, 'UPDATE teams SET name = ?, description = ? WHERE id = ? AND org_id = ?')
t.equal(sql.debug, `UPDATE teams SET name = '${name}', description = '${description}' WHERE id = ${teamId} AND org_id = '${organizationId}'`)
t.deepEqual(sql.values, [name, description, teamId, organizationId])
t.end()
})
test('SQL helper - multiline', (t) => {
const name = 'Team 5'
const description = 'description'
const teamId = 7
const organizationId = 'WONKA'
const sql = SQL`
UPDATE teams SET name = ${name}, description = ${description}
WHERE id = ${teamId} AND org_id = ${organizationId}
`
t.equal(sql.text, 'UPDATE teams SET name = $1, description = $2\nWHERE id = $3 AND org_id = $4')
t.equal(sql.sql, 'UPDATE teams SET name = ?, description = ?\nWHERE id = ? AND org_id = ?')
t.equal(sql.debug, `UPDATE teams SET name = '${name}', description = '${description}'\nWHERE id = ${teamId} AND org_id = '${organizationId}'`)
t.deepEqual(sql.values, [name, description, teamId, organizationId])
t.end()
})
test('SQL helper - multiline with emtpy lines', (t) => {
const name = 'Team 5'
const description = 'description'
const teamId = 7
const organizationId = 'WONKA'
const sql = SQL`
UPDATE teams SET name = ${name}, description = ${description}
WHERE id = ${teamId} AND org_id = ${organizationId}
RETURNING id
`
t.equal(sql.text, 'UPDATE teams SET name = $1, description = $2\nWHERE id = $3 AND org_id = $4\nRETURNING id')
t.equal(sql.sql, 'UPDATE teams SET name = ?, description = ?\nWHERE id = ? AND org_id = ?\nRETURNING id')
t.equal(sql.debug, `UPDATE teams SET name = '${name}', description = '${description}'\nWHERE id = ${teamId} AND org_id = '${organizationId}'\nRETURNING id`)
t.deepEqual(sql.values, [name, description, teamId, organizationId])
t.end()
})
test('SQL helper - build complex query with glue', (t) => {
const name = 'Team 5'
const description = 'description'
const teamId = 7
const organizationId = 'WONKA'
const sql = SQL` UPDATE teams SET `
const updates = []
updates.push(SQL`name = ${name}`)
updates.push(SQL`description = ${description}`)
sql.append(sql.glue(updates, ' , '))
sql.append(SQL`WHERE id = ${teamId} AND org_id = ${organizationId}`)
t.equal(sql.text, 'UPDATE teams SET name = $1 , description = $2 WHERE id = $3 AND org_id = $4')
t.equal(sql.sql, 'UPDATE teams SET name = ? , description = ? WHERE id = ? AND org_id = ?')
t.equal(sql.debug, `UPDATE teams SET name = '${name}' , description = '${description}' WHERE id = ${teamId} AND org_id = '${organizationId}'`)
t.deepEqual(sql.values, [name, description, teamId, organizationId])
t.end()
})
test('SQL helper - build complex query with glue - regression #13', (t) => {
const name = 'Team 5'
const ids = [1, 2, 3].map(id => SQL`${id}`)
const sql = SQL`UPDATE teams SET name = ${name} `
sql.append(SQL`WHERE id IN (`)
sql.append(sql.glue(ids, ' , '))
sql.append(SQL`)`)
t.equal(sql.text, 'UPDATE teams SET name = $1 WHERE id IN ($2 , $3 , $4 )')
t.equal(sql.sql, 'UPDATE teams SET name = ? WHERE id IN (? , ? , ? )')
t.equal(sql.debug, `UPDATE teams SET name = '${name}' WHERE id IN (1 , 2 , 3 )`)
t.deepEqual(sql.values, [name, 1, 2, 3])
t.end()
})
test('SQL helper - build complex query with glue - regression #17', (t) => {
const ids = [1, 2, 3].map(id => SQL`(${id})`)
const sql = SQL`INSERT INTO users (id) VALUES `
sql.append(sql.glue(ids, ' , '))
t.equal(sql.text, 'INSERT INTO users (id) VALUES ($1) , ($2) , ($3)')
t.equal(sql.sql, 'INSERT INTO users (id) VALUES (?) , (?) , (?)')
t.equal(sql.debug, `INSERT INTO users (id) VALUES (1) , (2) , (3)`)
t.deepEqual(sql.values, [1, 2, 3])
t.end()
})
test('SQL helper - build complex query with static glue - regression #17', (t) => {
const ids = [1, 2, 3].map(id => SQL`(${id})`)
const sql = SQL`INSERT INTO users (id) VALUES `
sql.append(SQL.glue(ids, ' , '))
t.equal(sql.text, 'INSERT INTO users (id) VALUES ($1) , ($2) , ($3)')
t.equal(sql.sql, 'INSERT INTO users (id) VALUES (?) , (?) , (?)')
t.equal(sql.debug, `INSERT INTO users (id) VALUES (1) , (2) , (3)`)
t.deepEqual(sql.values, [1, 2, 3])
t.end()
})
test('SQL helper - build complex query with append and glue', (t) => {
const updates = []
const v1 = 'v1'
const v2 = 'v2'
const v3 = 'v3'
const v4 = 'v4'
const v5 = 'v5'
const v6 = 'v6'
const v7 = 'v7'
const sql = SQL`TEST QUERY glue pieces FROM `
updates.push(SQL`v1 = ${v1}`)
updates.push(SQL`v2 = ${v2}`)
updates.push(SQL`v3 = ${v3}`)
updates.push(SQL`v4 = ${v4}`)
updates.push(SQL`v5 = ${v5}`)
sql.append(sql.glue(updates, ' , '))
sql.append(SQL`WHERE v6 = ${v6} `)
sql.append(SQL`AND v7 = ${v7}`)
t.equal(sql.text, 'TEST QUERY glue pieces FROM v1 = $1 , v2 = $2 , v3 = $3 , v4 = $4 , v5 = $5 WHERE v6 = $6 AND v7 = $7')
t.equal(sql.sql, 'TEST QUERY glue pieces FROM v1 = ? , v2 = ? , v3 = ? , v4 = ? , v5 = ? WHERE v6 = ? AND v7 = ?')
t.equal(sql.debug, `TEST QUERY glue pieces FROM v1 = 'v1' , v2 = 'v2' , v3 = 'v3' , v4 = 'v4' , v5 = 'v5' WHERE v6 = 'v6' AND v7 = 'v7'`)
t.deepEqual(sql.values, [v1, v2, v3, v4, v5, v6, v7])
t.end()
})
test('SQL helper - build complex query with append', (t) => {
const v1 = 'v1'
const v2 = 'v2'
const v3 = 'v3'
const v4 = 'v4'
const v5 = 'v5'
const v6 = 'v6'
const v7 = 'v7'
const sql = SQL`TEST QUERY glue pieces FROM `
sql.append(SQL`v1 = ${v1}, `)
sql.append(SQL`v2 = ${v2}, `)
sql.append(SQL`v3 = ${v3}, `)
sql.append(SQL`v4 = ${v4}, `)
sql.append(SQL`v5 = ${v5} `)
sql.append(SQL`WHERE v6 = ${v6} `)
sql.append(SQL`AND v7 = ${v7}`)
t.equal(sql.text, 'TEST QUERY glue pieces FROM v1 = $1, v2 = $2, v3 = $3, v4 = $4, v5 = $5 WHERE v6 = $6 AND v7 = $7')
t.equal(sql.sql, 'TEST QUERY glue pieces FROM v1 = ?, v2 = ?, v3 = ?, v4 = ?, v5 = ? WHERE v6 = ? AND v7 = ?')
t.equal(sql.debug, `TEST QUERY glue pieces FROM v1 = 'v1', v2 = 'v2', v3 = 'v3', v4 = 'v4', v5 = 'v5' WHERE v6 = 'v6' AND v7 = 'v7'`)
t.deepEqual(sql.values, [v1, v2, v3, v4, v5, v6, v7])
t.end()
})
test('SQL helper - build complex query with append passing simple strings and template strings', (t) => {
const v1 = 'v1'
const v2 = 'v2'
const v3 = 'v3'
const v4 = 'v4'
const v5 = 'v5'
const v6 = 'v6'
const v7 = 'v7'
const sql = SQL`TEST QUERY glue pieces FROM `
sql.append(SQL`v1 = ${v1}, `)
sql.append(SQL`v2 = ${v2}, `)
sql.append(SQL`v3 = ${v3}, `)
sql.append(SQL`v4 = ${v4}, `)
sql.append(SQL`v5 = ${v5}, `)
sql.append(SQL`v6 = v6 `)
sql.append(SQL`WHERE v6 = ${v6} `)
sql.append(SQL`AND v7 = ${v7} `)
sql.append(SQL`AND v8 = v8`)
t.equal(sql.text, 'TEST QUERY glue pieces FROM v1 = $1, v2 = $2, v3 = $3, v4 = $4, v5 = $5, v6 = v6 WHERE v6 = $6 AND v7 = $7 AND v8 = v8')
t.equal(sql.debug, `TEST QUERY glue pieces FROM v1 = 'v1', v2 = 'v2', v3 = 'v3', v4 = 'v4', v5 = 'v5', v6 = v6 WHERE v6 = 'v6' AND v7 = 'v7' AND v8 = v8`)
t.deepEqual(sql.values, [v1, v2, v3, v4, v5, v6, v7])
t.end()
})
test('SQL helper - will throw an error if append is called without using SQL', (t) => {
const sql = SQL`TEST QUERY glue pieces FROM `
try {
sql.append(`v1 = v1`)
t.fail('showld throw an error when passing strings not prefixed with SQL')
} catch (e) {
t.equal(e.message, '"append" accepts only template string prefixed with SQL (SQL`...`)')
}
t.end()
})
test('SQL helper - build string using append with and without unsafe flag', (t) => {
const v2 = 'v2'
const longName = 'whateverThisIs'
const sql = SQL`TEST QUERY glue pieces FROM test WHERE test1 == test2`
sql.append(SQL` AND v1 = v1,`)
sql.append(SQL` AND v2 = ${v2}, `)
sql.append(SQL` AND v3 = ${longName}`, { unsafe: true })
sql.append(SQL` AND v4 = v4`, { unsafe: true })
t.equal(sql.text, 'TEST QUERY glue pieces FROM test WHERE test1 == test2 AND v1 = v1, AND v2 = $1, AND v3 = whateverThisIs AND v4 = v4')
t.equal(sql.debug, `TEST QUERY glue pieces FROM test WHERE test1 == test2 AND v1 = v1, AND v2 = 'v2', AND v3 = whateverThisIs AND v4 = v4`)
t.equal(sql.values.length, 1)
t.true(sql.values.includes(v2))
t.end()
})
test('SQL helper - build string using append and only unsafe', (t) => {
const v2 = 'v2'
const longName = 'whateverThisIs'
const sql = SQL`TEST QUERY glue pieces FROM test WHERE test1 == test2`
t.equal(sql.text, 'TEST QUERY glue pieces FROM test WHERE test1 == test2')
sql.append(SQL` AND v1 = v1,`, { unsafe: true })
t.equal(sql.text, 'TEST QUERY glue pieces FROM test WHERE test1 == test2 AND v1 = v1,')
sql.append(SQL` AND v2 = ${v2} AND v3 = ${longName} AND v4 = 'v4'`, { unsafe: true })
t.equal(sql.text, 'TEST QUERY glue pieces FROM test WHERE test1 == test2 AND v1 = v1, AND v2 = v2 AND v3 = whateverThisIs AND v4 = \'v4\'')
t.equal(sql.debug, `TEST QUERY glue pieces FROM test WHERE test1 == test2 AND v1 = v1, AND v2 = v2 AND v3 = whateverThisIs AND v4 = 'v4'`)
t.end()
})
test('SQL helper - handles js null values as valid `null` sql values', (t) => {
const name = null
const id = 123
const sql = SQL`UPDATE teams SET name = ${name} WHERE id = ${id}`
t.equal(sql.text, 'UPDATE teams SET name = $1 WHERE id = $2')
t.equal(sql.sql, 'UPDATE teams SET name = ? WHERE id = ?')
t.equal(sql.debug, `UPDATE teams SET name = null WHERE id = ${id}`)
t.deepEqual(sql.values, [name, id])
t.end()
})
test('SQL helper - throws when building an sql string with an `undefined` value', (t) => {
t.throws(() => SQL`UPDATE teams SET name = ${undefined}`)
t.end()
})