This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
146 lines (128 loc) · 3.14 KB
/
index.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
const {
placeholderGenerator,
where,
sql,
limit,
offset,
and,
or
} = require("./index")
describe("placeholderGenerator.gen", () => {
it("should return placeholder", () => {
const $ = placeholderGenerator()
expect($.gen('a')).toEqual('$1')
expect($.gen('b')).toEqual('$2')
})
})
describe("placeholderGenerator.getValues", () => {
it("should return values", () => {
const $ = placeholderGenerator()
$.gen('a')
$.gen('b')
expect($.getValues()).toEqual(['a', 'b'])
})
})
describe("where", () => {
it("works", () => {
const $ = placeholderGenerator()
const params = {
name: 'aname',
id: ['aid', 'bid'],
date: undefined
}
const got = where(
and(
['name =', params.name],
or(
['id IN', params.id],
"address = 'somewhere'"
),
['date =', params.date],
() => {
return "category like 'hoooman'"
}
)
)($.gen)
const expected = "WHERE ( name = $1 AND ( id IN ( $2, $3 ) OR address = 'somewhere' ) AND category like 'hoooman' )"
expect(got).toEqual(expected)
expect($.getValues()).toEqual(['aname', 'aid', 'bid'])
})
})
describe("sql", () => {
it("works", () => {
const params = {
id: 1,
name: 'apple',
limit: 10,
offset: 20
}
const result = sql`
SELECT * FROM users
${where(
and(
['id =', params.id],
['name like', params.name],
['location =', params.location]
)
)}
${limit(params.limit)}
${offset(params.offset)};
`
const expectedSql = `
SELECT * FROM users
WHERE ( id = $1 AND name like $2 )
LIMIT $3
OFFSET $4;
`
const expected = [expectedSql, [1, "apple", 10, 20]]
expect(result).toEqual(expected)
})
})
describe("limit", () => {
it("works", () => {
const $ = placeholderGenerator()
const params = {
limit: 10
}
const expected = 'LIMIT $1'
const got = limit(params.limit)($.gen)
expect(got).toEqual(expected)
expect($.getValues()).toEqual([params.limit])
})
it("returns empty string if falsy value is passed", () => {
const $ = placeholderGenerator()
const expected = ''
const got1 = limit(undefined)($.gen)
expect(got1).toEqual('')
const got2 = limit(null)($.gen)
expect(got2).toEqual('')
const got3 = limit(0)($.gen)
expect(got3).toEqual('')
const got4 = limit(false)($.gen)
expect(got4).toEqual('')
})
})
describe("offset", () => {
it("works", () => {
const $ = placeholderGenerator()
const params = {
offset: 50
}
const expected = 'OFFSET $1'
const got = offset(params.offset)($.gen)
expect(got).toEqual(expected)
expect($.getValues()).toEqual([params.offset])
})
it("returns empty string if falsy value is passed", () => {
const $ = placeholderGenerator()
const expected = ''
const got1 = offset(undefined)($.gen)
expect(got1).toEqual('')
const got2 = offset(null)($.gen)
expect(got2).toEqual('')
const got3 = offset(0)($.gen)
expect(got3).toEqual('')
const got4 = offset(false)($.gen)
expect(got4).toEqual('')
})
})