-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbasic.test.ts
More file actions
459 lines (393 loc) · 17.8 KB
/
basic.test.ts
File metadata and controls
459 lines (393 loc) · 17.8 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import { SqlParserImpl } from '../../src/parser';
import { SqlCompilerImpl } from '../../src/compiler';
import { QueryLeaf, DummyQueryLeaf } from '../../src/index';
import { MongoClient } from 'mongodb';
// Mock the MongoDB executor to avoid actual database connections during tests
jest.mock('../../src/executor', () => {
return {
MongoExecutor: jest.fn().mockImplementation(() => {
return {
execute: jest.fn().mockResolvedValue([{ id: 1, name: 'Test User', age: 25 }])
};
})
};
});
// Mock MongoClient
const mockMongoClient = {} as MongoClient;
describe('QueryLeaf', () => {
describe('SqlParserImpl', () => {
const parser = new SqlParserImpl();
test('should parse a SELECT statement', () => {
const sql = 'SELECT id, name, age FROM users WHERE age > 18';
const result = parser.parse(sql);
expect(result).toBeDefined();
expect(result.text).toBe(sql);
expect(result.ast).toBeDefined();
expect(result.ast.type).toBe('select');
});
test('should parse a SELECT with nested fields', () => {
const sql = 'SELECT address.zip, address FROM shipping_addresses';
const result = parser.parse(sql);
expect(result).toBeDefined();
expect(result.text).toBe(sql);
expect(result.ast).toBeDefined();
expect(result.ast.type).toBe('select');
});
test('should parse a SELECT with array indexing', () => {
const sql = 'SELECT items[0].id, items FROM orders';
const result = parser.parse(sql);
expect(result).toBeDefined();
expect(result.text).toBe(sql);
expect(result.ast).toBeDefined();
expect(result.ast.type).toBe('select');
});
test('should parse an INSERT statement', () => {
const sql = 'INSERT INTO users (id, name, age) VALUES (1, "John", 25)';
const result = parser.parse(sql);
expect(result).toBeDefined();
expect(result.text).toBe(sql);
expect(result.ast).toBeDefined();
expect(result.ast.type).toBe('insert');
});
test('should parse an UPDATE statement', () => {
const sql = 'UPDATE users SET name = "Jane" WHERE id = 1';
const result = parser.parse(sql);
expect(result).toBeDefined();
expect(result.text).toBe(sql);
expect(result.ast).toBeDefined();
expect(result.ast.type).toBe('update');
});
test('should parse a DELETE statement', () => {
const sql = 'DELETE FROM users WHERE id = 1';
const result = parser.parse(sql);
expect(result).toBeDefined();
expect(result.text).toBe(sql);
expect(result.ast).toBeDefined();
expect(result.ast.type).toBe('delete');
});
test('should throw on invalid SQL', () => {
const sql = 'INVALID SQL STATEMENT';
expect(() => parser.parse(sql)).toThrow();
});
});
describe('SqlCompilerImpl', () => {
const parser = new SqlParserImpl();
const compiler = new SqlCompilerImpl();
// Test different types of SELECT queries
test('should compile a SELECT statement', () => {
const sql = 'SELECT id, name, age FROM users WHERE age > 18';
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
expect(commands[0].type).toBe('FIND');
expect(commands[0].collection).toBe('users');
// Check if it's a FindCommand
if (commands[0].type === 'FIND') {
expect(commands[0].filter).toBeDefined();
}
});
test('should compile a SELECT with OFFSET', () => {
const sql = 'SELECT * FROM users OFFSET 10';
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
expect(commands[0].type).toBe('FIND');
expect(commands[0].collection).toBe('users');
// Check if it's a FindCommand with offset
if (commands[0].type === 'FIND') {
expect(commands[0].skip).toBe(10);
}
});
test('should compile a SELECT with LIMIT and OFFSET', () => {
const sql = 'SELECT * FROM users LIMIT 5 OFFSET 10';
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
expect(commands[0].type).toBe('FIND');
expect(commands[0].collection).toBe('users');
// Check if it's a FindCommand with limit and offset
if (commands[0].type === 'FIND') {
expect(commands[0].limit).toBe(5);
expect(commands[0].skip).toBe(10);
}
});
test('should compile a SELECT with nested fields', () => {
const sql = 'SELECT address.zip, address FROM shipping_addresses';
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
// Can be either a FIND or AGGREGATE command
expect(['FIND', 'AGGREGATE']).toContain(commands[0].type);
expect(commands[0].collection).toBe('shipping_addresses');
// Check if we're using FIND with projection
if (commands[0].type === 'FIND' && commands[0].projection) {
expect(commands[0].projection).toBeDefined();
expect(commands[0].projection['address.zip']).toBe(1);
expect(commands[0].projection['address']).toBe(1);
}
// Or AGGREGATE with pipeline including $project
else if (commands[0].type === 'AGGREGATE') {
expect(commands[0].pipeline).toBeDefined();
// Check that we have a $project stage with the right fields
const projectStage = commands[0].pipeline.find((stage: any) => '$project' in stage);
expect(projectStage).toBeDefined();
if (projectStage) {
// Check that address_zip field is included (from address.zip)
expect(projectStage.$project.address_zip).toBeDefined();
// Address field should be included too
expect(projectStage.$project.address).toBeDefined();
}
}
});
test('should compile a SELECT with array indexing', () => {
const sql = 'SELECT items[0].id, items FROM orders';
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
expect(commands[0].type).toBe('FIND');
expect(commands[0].collection).toBe('orders');
// Check if projection includes array element access
if (commands[0].type === 'FIND' && commands[0].projection) {
expect(commands[0].projection).toBeDefined();
expect(commands[0].projection['id']).toBeDefined();
expect(commands[0].projection['id']['$getField']).toBeDefined();
expect(commands[0].projection['id']['$getField']['field']).toBe('id');
expect(commands[0].projection['id']['$getField']['input']).toBeDefined();
expect(commands[0].projection['items']).toBe(1);
}
});
test('should compile an INSERT statement', () => {
const sql = 'INSERT INTO users (id, name, age) VALUES (1, "John", 25)';
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
expect(commands[0].type).toBe('INSERT');
expect(commands[0].collection).toBe('users');
// Check if it's an InsertCommand
if (commands[0].type === 'INSERT') {
expect(commands[0].documents).toHaveLength(1);
}
});
test('should compile an UPDATE statement', () => {
const sql = 'UPDATE users SET name = "Jane" WHERE id = 1';
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
expect(commands[0].type).toBe('UPDATE');
expect(commands[0].collection).toBe('users');
// Check if it's an UpdateCommand
if (commands[0].type === 'UPDATE') {
expect(commands[0].filter).toBeDefined();
expect(commands[0].update).toBeDefined();
}
});
test('should compile a DELETE statement', () => {
const sql = 'DELETE FROM users WHERE id = 1';
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
expect(commands[0].type).toBe('DELETE');
expect(commands[0].collection).toBe('users');
// Check if it's a DeleteCommand
if (commands[0].type === 'DELETE') {
expect(commands[0].filter).toBeDefined();
}
});
test('should compile queries with nested field conditions', () => {
const sql = "SELECT * FROM users WHERE address.city = 'New York'";
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
// Allow either FIND or AGGREGATE type
expect(['FIND', 'AGGREGATE']).toContain(commands[0].type);
if (commands[0].type === 'FIND' && commands[0].filter) {
// For FIND command
expect(commands[0].filter).toBeDefined();
expect(commands[0].filter['address.city']).toBe('New York');
} else if (commands[0].type === 'AGGREGATE') {
// For AGGREGATE command
expect(commands[0].pipeline).toBeDefined();
// Find the $match stage in pipeline
const matchStage = commands[0].pipeline.find((stage: any) => '$match' in stage);
expect(matchStage).toBeDefined();
if (matchStage) {
expect(matchStage.$match['address.city']).toBe('New York');
}
}
});
test('should compile queries with array element conditions', () => {
const sql = "SELECT * FROM orders WHERE items[0].price > 100";
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
// Allow either FIND or AGGREGATE type
expect(['FIND', 'AGGREGATE']).toContain(commands[0].type);
if (commands[0].type === 'FIND' && commands[0].filter) {
// For FIND command
expect(commands[0].filter).toBeDefined();
expect(commands[0].filter['items.0.price']).toBeDefined();
expect(commands[0].filter['items.0.price'].$gt).toBe(100);
} else if (commands[0].type === 'AGGREGATE') {
// For AGGREGATE command
expect(commands[0].pipeline).toBeDefined();
// Find the $match stage in pipeline
const matchStage = commands[0].pipeline.find((stage: any) => '$match' in stage);
expect(matchStage).toBeDefined();
if (matchStage) {
expect(matchStage.$match['items.0.price']).toBeDefined();
expect(matchStage.$match['items.0.price'].$gt).toBe(100);
}
}
});
test('should compile GROUP BY queries with aggregation', () => {
const sql = "SELECT category, COUNT(*) as count, AVG(price) as avg_price FROM products GROUP BY category";
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
// Allow either FIND or AGGREGATE type
expect(['FIND', 'AGGREGATE']).toContain(commands[0].type);
if (commands[0].type === 'FIND') {
// For FIND command
expect(commands[0].group).toBeDefined();
expect(commands[0].pipeline).toBeDefined();
// Check if the pipeline contains a $group stage
if (commands[0].pipeline) {
const groupStage = commands[0].pipeline.find(stage => '$group' in stage);
expect(groupStage).toBeDefined();
if (groupStage) {
// Just check the overall structure rather than specific field names
expect(groupStage.$group._id).toBeDefined();
// The property might be different based on the AST format
expect(groupStage.$group.count).toBeDefined();
expect(groupStage.$group.avg_price).toBeDefined();
// Check that the operations use the right aggregation operators
expect(groupStage.$group.count.$sum).toBeDefined();
expect(groupStage.$group.avg_price.$avg).toBeDefined();
}
}
} else if (commands[0].type === 'AGGREGATE') {
// For AGGREGATE command
expect(commands[0].pipeline).toBeDefined();
// Find the $group stage in the pipeline
const groupStage = commands[0].pipeline.find((stage: any) => '$group' in stage);
expect(groupStage).toBeDefined();
if (groupStage) {
// Just check the overall structure rather than specific field names
expect(groupStage.$group._id).toBeDefined();
// The property might be different based on the AST format
expect(groupStage.$group.count).toBeDefined();
expect(groupStage.$group.avg_price).toBeDefined();
// Check that the operations use the right aggregation operators
expect(groupStage.$group.count.$sum).toBeDefined();
expect(groupStage.$group.avg_price.$avg).toBeDefined();
}
}
});
test('should compile JOIN queries', () => {
const sql = "SELECT users.name, orders.total FROM users JOIN orders ON users._id = orders.userId";
const statement = parser.parse(sql);
const commands = compiler.compile(statement);
expect(commands).toHaveLength(1);
// Allow either FIND or AGGREGATE type
expect(['FIND', 'AGGREGATE']).toContain(commands[0].type);
if (commands[0].type === 'FIND') {
// For FIND command
expect(commands[0].lookup).toBeDefined();
expect(commands[0].pipeline).toBeDefined();
// Check if the pipeline contains a $lookup stage
if (commands[0].pipeline) {
const lookupStage = commands[0].pipeline.find(stage => '$lookup' in stage);
expect(lookupStage).toBeDefined();
if (lookupStage) {
expect(lookupStage.$lookup.from).toBe('orders');
expect(lookupStage.$lookup.localField).toBe('_id');
expect(lookupStage.$lookup.foreignField).toBe('userId');
}
// Check if it's followed by an $unwind stage
const unwindStage = commands[0].pipeline.find(stage => '$unwind' in stage);
expect(unwindStage).toBeDefined();
}
} else if (commands[0].type === 'AGGREGATE') {
// For AGGREGATE command
expect(commands[0].pipeline).toBeDefined();
// Check if the pipeline contains a $lookup stage
const lookupStage = commands[0].pipeline.find((stage: any) => '$lookup' in stage);
expect(lookupStage).toBeDefined();
if (lookupStage) {
expect(lookupStage.$lookup.from).toBe('orders');
expect(lookupStage.$lookup.localField).toBe('_id');
expect(lookupStage.$lookup.foreignField).toBe('userId');
}
// Check if it's followed by an $unwind stage
const unwindStage = commands[0].pipeline.find((stage: any) => '$unwind' in stage);
expect(unwindStage).toBeDefined();
}
});
});
describe('LIKE operator regex escaping', () => {
const parser = new SqlParserImpl();
const compiler = new SqlCompilerImpl();
function getLikeRegex(command: any, field: string): RegExp | undefined {
if (command.type === 'FIND' && command.filter) {
return command.filter[field]?.$regex;
}
if (command.type === 'AGGREGATE') {
const matchStage = command.pipeline.find((s: any) => '$match' in s);
return matchStage?.$match[field]?.$regex;
}
return undefined;
}
test('escapes dots so they match literally', () => {
const sql = "SELECT * FROM users WHERE email LIKE 'user.name@example.com'";
const cmds = compiler.compile(parser.parse(sql));
const re = getLikeRegex(cmds[0], 'email');
expect(re).toBeDefined();
expect(re!.test('user.name@example.com')).toBe(true);
expect(re!.test('userXname@example.com')).toBe(false);
});
test('escapes parentheses so they match literally', () => {
const sql = "SELECT * FROM books WHERE title LIKE 'Books (Fiction)'";
const cmds = compiler.compile(parser.parse(sql));
const re = getLikeRegex(cmds[0], 'title');
expect(re).toBeDefined();
expect(re!.test('Books (Fiction)')).toBe(true);
expect(re!.test('Books Fiction')).toBe(false);
});
test('escapes $ so pattern is not an invalid mid-string anchor', () => {
const sql = "SELECT * FROM items WHERE label LIKE 'Price: $10'";
const cmds = compiler.compile(parser.parse(sql));
const re = getLikeRegex(cmds[0], 'label');
expect(re).toBeDefined();
expect(re!.test('Price: $10')).toBe(true);
});
test('preserves % wildcard (zero or more chars)', () => {
const sql = "SELECT * FROM users WHERE name LIKE 'Jo%'";
const cmds = compiler.compile(parser.parse(sql));
const re = getLikeRegex(cmds[0], 'name');
expect(re).toBeDefined();
expect(re!.test('Jo')).toBe(true);
expect(re!.test('John')).toBe(true);
expect(re!.test('Josephine')).toBe(true);
expect(re!.test('Al')).toBe(false);
});
test('preserves _ wildcard (exactly one char)', () => {
const sql = "SELECT * FROM users WHERE code LIKE 'A_C'";
const cmds = compiler.compile(parser.parse(sql));
const re = getLikeRegex(cmds[0], 'code');
expect(re).toBeDefined();
expect(re!.test('ABC')).toBe(true);
expect(re!.test('AC')).toBe(false);
expect(re!.test('ABBC')).toBe(false);
});
});
describe('QueryLeaf', () => {
test('should execute a SQL query', async () => {
const queryLeaf = new QueryLeaf(mockMongoClient, 'test');
const result = await queryLeaf.execute('SELECT * FROM users WHERE age > 18');
expect(result).toBeDefined();
expect(Array.isArray(result)).toBe(true);
expect(result[0]).toHaveProperty('name');
expect(result[0]).toHaveProperty('age');
});
});
});