-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsql.test.ts
112 lines (93 loc) · 3.18 KB
/
sql.test.ts
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
import sql = require('../lib/sql')
const trimSpaces = str => str.trim().replace(/\s+/g, ' ')
test('basic usage', () => {
const yearRange = [1983, 1992]
const query = sql`
select * from movies
where
year >= ${yearRange[0]}
and year <= ${yearRange[1]}
`
expect(trimSpaces(query.text)).toBe(
'select * from movies where year >= $1 and year <= $2'
)
expect(query.values).toHaveLength(2)
expect(query.values[0]).toBe(yearRange[0])
expect(query.values[1]).toBe(yearRange[1])
})
test('ignore expressions returning `undefined`', () => {
const query = sql`
select * from books
${undefined}
`
expect(trimSpaces(query.text)).toBe('select * from books')
expect(query.values).toHaveLength(0)
})
test('add expressions returning `false`', () => {
const query = sql`select * from books where read = ${false}`
expect(trimSpaces(query.text)).toBe('select * from books where read = $1')
expect(query.values).toHaveLength(1)
expect(query.values[0]).toBe(false)
})
test('add expressions returning `null`', () => {
const query = sql`select * from books where author = ${null}`
expect(trimSpaces(query.text)).toBe('select * from books where author = $1')
expect(query.values).toHaveLength(1)
expect(query.values[0]).toBe(null)
})
test('imbricated sql tags', () => {
const author = 'steinbeck'
const query = sql`
select * from books
${author && sql`where author = ${author}`}
`
expect(trimSpaces(query.text)).toBe('select * from books where author = $1')
expect(query.values).toHaveLength(1)
expect(query.values[0]).toBe(author)
})
test('imbricated sql tags (2 levels)', () => {
const [expr0, expr1, expr2] = ['i', 'like', 'sql']
const query = sql`
level 0 ${expr0}
${sql`
level 1 ${expr1}
${sql`
level 2 ${expr2}
`}
`}
`
expect(trimSpaces(query.text)).toBe('level 0 $1 level 1 $2 level 2 $3')
expect(query.values).toHaveLength(3)
expect(query.values[0]).toBe(expr0)
expect(query.values[1]).toBe(expr1)
expect(query.values[2]).toBe(expr2)
})
test('imbricated sql tags (parallel)', () => {
const [expr0, expr1, expr2, expr3, expr4] = ['i', 'like', 'sql', 'a', 'lot']
const query = sql`
level 0 ${expr0}
${sql`a ${expr1} ${expr2}`}
${sql`b ${expr3} ${expr4}`}
`
expect(trimSpaces(query.text)).toBe('level 0 $1 a $2 $3 b $4 $5')
expect(query.values).toHaveLength(5)
expect(query.values[0]).toBe(expr0)
expect(query.values[1]).toBe(expr1)
expect(query.values[2]).toBe(expr2)
expect(query.values[3]).toBe(expr3)
expect(query.values[4]).toBe(expr4)
})
test('json as query parameter', () => {
const jsonValue = { _sql: { some: 'data' }, item: 'value' }
const query = sql`select obj from movies where obj = ${jsonValue}`
expect(trimSpaces(query.text)).toBe('select obj from movies where obj = $1')
expect(query.values).toHaveLength(1)
expect(query.values[0]).toBe(jsonValue)
})
test('query with raw data', () => {
const tableName = 'books'
const query = sql`select * from ${sql.raw(tableName)} where read = ${false}`
expect(trimSpaces(query.text)).toBe('select * from books where read = $1')
expect(query.values).toHaveLength(1)
expect(query.values[0]).toBe(false)
})