-
Notifications
You must be signed in to change notification settings - Fork 0
/
query2_test.ts
264 lines (260 loc) · 6.42 KB
/
query2_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
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
import Q from "./mod.ts";
import { test, assertStrictEq } from "./test_deps.ts";
test("query static method - select", () => {
const sql = Q.select("a", "b").from("hello").where({ a: 1 }).build();
assertStrictEq(sql, "SELECT `a`, `b` FROM `hello` WHERE `a`=1");
});
test("query static method - fields", () => {
const sql = Q.select()
.fields("a", "b")
.table("hello")
.where({ a: 1 })
.build();
assertStrictEq(sql, "SELECT `a`, `b` FROM `hello` WHERE `a`=1");
});
test("query static method - insert", () => {
const sql = Q.insert({ a: 123, b: 456 }).into("hello").build();
assertStrictEq(sql, "INSERT INTO `hello` (`a`, `b`) VALUES (123, 456)");
});
test("query static method - insert into", () => {
const sql = Q.insert([
{ a: 123, b: 456 },
{ a: 789, b: 111 },
])
.into("hello")
.build();
assertStrictEq(
sql,
"INSERT INTO `hello` (`a`, `b`) VALUES (123, 456),\n(789, 111)",
);
});
test("query static method - update", () => {
const sql = Q.update()
.table("abc")
.set({ a: 123, b: 456 })
.where({ c: 789 })
.build();
assertStrictEq(sql, "UPDATE `abc` SET `a`=123, `b`=456 WHERE `c`=789");
});
test("query static method - delete", () => {
const sql = Q.delete().from("abc").where({ a: 666 }).limit(10).build();
assertStrictEq(sql, "DELETE FROM `abc` WHERE `a`=666 LIMIT 10");
});
test("query leftJoin - on", () => {
const sql = Q.select("*")
.from("hello")
.as("A")
.leftJoin("world")
.as("B")
.on("A.id=B.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `A`.* FROM `hello` AS `A` LEFT JOIN `world` AS `B` ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query leftJoin - `A`.*, `B`.*", () => {
const sql = Q.select("*")
.from("hello")
.as("A")
.leftJoin("world", ["*"])
.as("B")
.on("A.id=B.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `A`.*, `B`.* FROM `hello` AS `A` LEFT JOIN `world` AS `B` ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query leftJoin - `B`.*", () => {
const sql = Q.select()
.from("hello")
.as("A")
.leftJoin("world", ["*"])
.as("B")
.on("A.id=B.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `B`.* FROM `hello` AS `A` LEFT JOIN `world` AS `B` ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query leftJoin - with field", () => {
const sql = Q.select("x", "y")
.from("hello")
.as("A")
.leftJoin("world", ["z"])
.as("B")
.on("A.id=B.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `A`.`x`, `A`.`y`, `B`.`z` FROM `hello` AS `A` LEFT JOIN `world` AS `B` ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query leftJoin - without as", () => {
const sql = Q.select("x", "y")
.from("hello")
.leftJoin("world", ["z"])
.on("hello.id=world.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `hello`.`x`, `hello`.`y`, `world`.`z` FROM `hello` LEFT JOIN `world` ON hello.id=world.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query rightJoin - on", () => {
const sql = Q.select("*")
.from("hello")
.as("A")
.rightJoin("world", ["*"])
.as("B")
.on("A.id=B.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `A`.*, `B`.* FROM `hello` AS `A` RIGHT JOIN `world` AS `B` ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query rightJoin - as", () => {
const sql = Q.select("x", "y")
.from("hello")
.as("A")
.rightJoin("world", ["z"])
.as("B")
.on("A.id=B.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `A`.`x`, `A`.`y`, `B`.`z` FROM `hello` AS `A` RIGHT JOIN `world` AS `B` ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query rightJoin - without as", () => {
const sql = Q.select("x", "y")
.from("hello")
.rightJoin("world", ["z"])
.on("hello.id=world.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `hello`.`x`, `hello`.`y`, `world`.`z` FROM `hello` RIGHT JOIN `world` ON hello.id=world.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query join - `A`.*, `B`.*", () => {
const sql = Q.select("*")
.from("hello")
.as("A")
.join("world", ["*"])
.as("B")
.on("A.id=B.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `A`.*, `B`.* FROM `hello` AS `A` JOIN `world` AS `B` ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query join - as", () => {
const sql = Q.select("x", "y")
.from("hello")
.as("A")
.join("world", ["z"])
.as("B")
.on("A.id=B.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `A`.`x`, `A`.`y`, `B`.`z` FROM `hello` AS `A` JOIN `world` AS `B` ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query join - without as", () => {
const sql = Q.select("x", "y")
.from("hello")
.join("world", ["z"])
.on("hello.id=world.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `hello`.`x`, `hello`.`y`, `world`.`z` FROM `hello` JOIN `world` ON hello.id=world.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query join - as tow table", () => {
const sql = Q.select("x", "y")
.from("hello")
.as("A")
.leftJoin("world", ["z"])
.as("B")
.on("A.id=B.id")
.leftJoin("world", ["k"])
.as("C")
.on("B.uid=C.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `A`.`x`, `A`.`y`, `B`.`z`, `C`.`k` FROM `hello` AS `A` LEFT JOIN `world` AS `B` ON A.id=B.id LEFT JOIN `world` AS `C` ON B.uid=C.id WHERE 1 AND 2 LIMIT 2,3",
);
});
test("query join - count()", () => {
const sql = Q.select("x", "y", "count(y) AS c1")
.from("hello")
.as("A")
.join("world", ["z", "count(z) as c2"])
.as("B")
.on("A.id=B.id")
.where("1")
.and("2")
.offset(2)
.limit(3)
.build();
assertStrictEq(
sql,
"SELECT `A`.`x`, `A`.`y`, count(y) AS c1, `B`.`z`, count(z) as c2 FROM `hello` AS `A` JOIN `world` AS `B` ON A.id=B.id WHERE 1 AND 2 LIMIT 2,3",
);
});