bunsane 0.2.7 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +26 -0
- package/docs/SCALABILITY_PLAN.md +3 -3
- package/package.json +1 -1
- package/query/ComponentInclusionNode.ts +14 -4
- package/query/FilterBuilder.ts +25 -0
- package/query/Query.ts +5 -1
- package/query/builders/JsonbArrayBuilder.ts +116 -0
- package/query/index.ts +28 -2
- package/tests/benchmark/query-lateral-benchmark.test.ts +372 -0
- package/tests/integration/query/Query.edgeCases.test.ts +595 -0
- package/tests/integration/query/Query.exec.test.ts +67 -14
- package/tests/integration/query/Query.jsonbArray.test.ts +214 -0
- package/tests/pglite-setup.ts +1 -0
- package/tests/unit/query/JsonbArrayBuilder.test.ts +178 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Edge case tests for Query SQL generation
|
|
3
|
+
* These tests verify that various edge cases don't produce invalid SQL
|
|
4
|
+
*/
|
|
5
|
+
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test';
|
|
6
|
+
import { Entity } from '../../../core/Entity';
|
|
7
|
+
import { Query, FilterOp } from '../../../query/Query';
|
|
8
|
+
import { TestUser, TestProduct, TestOrder } from '../../fixtures/components';
|
|
9
|
+
import { createTestContext, ensureComponentsRegistered } from '../../utils';
|
|
10
|
+
|
|
11
|
+
describe('Query Edge Cases', () => {
|
|
12
|
+
const ctx = createTestContext();
|
|
13
|
+
|
|
14
|
+
beforeAll(async () => {
|
|
15
|
+
await ensureComponentsRegistered(TestUser, TestProduct, TestOrder);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('IN/NOT IN operator edge cases', () => {
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
// Create test data
|
|
21
|
+
const entity = ctx.tracker.create();
|
|
22
|
+
entity.add(TestUser, { name: 'InTest', email: 'in@test.com', age: 25 });
|
|
23
|
+
await entity.save();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('IN with single element array works', async () => {
|
|
27
|
+
const results = await new Query()
|
|
28
|
+
.with(TestUser, {
|
|
29
|
+
filters: [Query.filter('name', FilterOp.IN, ['InTest'])]
|
|
30
|
+
})
|
|
31
|
+
.exec();
|
|
32
|
+
|
|
33
|
+
expect(results.length).toBeGreaterThanOrEqual(1);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('IN with multiple elements works', async () => {
|
|
37
|
+
const results = await new Query()
|
|
38
|
+
.with(TestUser, {
|
|
39
|
+
filters: [Query.filter('name', FilterOp.IN, ['InTest', 'Other', 'Another'])]
|
|
40
|
+
})
|
|
41
|
+
.exec();
|
|
42
|
+
|
|
43
|
+
expect(results.length).toBeGreaterThanOrEqual(1);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('IN with empty array returns no results (not SQL error)', async () => {
|
|
47
|
+
const results = await new Query()
|
|
48
|
+
.with(TestUser, {
|
|
49
|
+
filters: [Query.filter('name', FilterOp.IN, [])]
|
|
50
|
+
})
|
|
51
|
+
.exec();
|
|
52
|
+
|
|
53
|
+
// Empty IN should return no results, not throw SQL error
|
|
54
|
+
expect(results.length).toBe(0);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('NOT IN with empty array returns all results (not SQL error)', async () => {
|
|
58
|
+
const allResults = await new Query()
|
|
59
|
+
.with(TestUser)
|
|
60
|
+
.exec();
|
|
61
|
+
|
|
62
|
+
const notInResults = await new Query()
|
|
63
|
+
.with(TestUser, {
|
|
64
|
+
filters: [Query.filter('name', FilterOp.NOT_IN, [])]
|
|
65
|
+
})
|
|
66
|
+
.exec();
|
|
67
|
+
|
|
68
|
+
// Empty NOT IN should return all results (nothing excluded)
|
|
69
|
+
expect(notInResults.length).toBe(allResults.length);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('NOT IN with single element works', async () => {
|
|
73
|
+
const results = await new Query()
|
|
74
|
+
.with(TestUser, {
|
|
75
|
+
filters: [Query.filter('name', FilterOp.NOT_IN, ['InTest'])]
|
|
76
|
+
})
|
|
77
|
+
.exec();
|
|
78
|
+
|
|
79
|
+
// Should not include 'InTest'
|
|
80
|
+
const hasInTest = results.some(e => e.getInMemory(TestUser)?.name === 'InTest');
|
|
81
|
+
expect(hasInTest).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('IN combined with other filters works', async () => {
|
|
85
|
+
const results = await new Query()
|
|
86
|
+
.with(TestUser, {
|
|
87
|
+
filters: [
|
|
88
|
+
Query.filter('name', FilterOp.IN, ['InTest', 'Other']),
|
|
89
|
+
Query.filter('age', FilterOp.GTE, 18)
|
|
90
|
+
]
|
|
91
|
+
})
|
|
92
|
+
.exec();
|
|
93
|
+
|
|
94
|
+
expect(Array.isArray(results)).toBe(true);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('Multi-component query edge cases', () => {
|
|
99
|
+
beforeEach(async () => {
|
|
100
|
+
// Create entity with multiple components
|
|
101
|
+
const entity = ctx.tracker.create();
|
|
102
|
+
entity.add(TestUser, { name: 'MultiComp', email: 'multi@test.com', age: 30 });
|
|
103
|
+
entity.add(TestProduct, { sku: 'MULTI-SKU', name: 'Multi Product', price: 100, inStock: true });
|
|
104
|
+
await entity.save();
|
|
105
|
+
|
|
106
|
+
// Create entity with only user
|
|
107
|
+
const userOnly = ctx.tracker.create();
|
|
108
|
+
userOnly.add(TestUser, { name: 'UserOnly', email: 'useronly@test.com', age: 25 });
|
|
109
|
+
await userOnly.save();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('two components with no filters works', async () => {
|
|
113
|
+
const results = await new Query()
|
|
114
|
+
.with(TestUser)
|
|
115
|
+
.with(TestProduct)
|
|
116
|
+
.exec();
|
|
117
|
+
|
|
118
|
+
expect(Array.isArray(results)).toBe(true);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('two components with filter on first works', async () => {
|
|
122
|
+
const results = await new Query()
|
|
123
|
+
.with(TestUser, {
|
|
124
|
+
filters: [Query.filter('name', FilterOp.EQ, 'MultiComp')]
|
|
125
|
+
})
|
|
126
|
+
.with(TestProduct)
|
|
127
|
+
.exec();
|
|
128
|
+
|
|
129
|
+
expect(Array.isArray(results)).toBe(true);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('two components with filter on second works', async () => {
|
|
133
|
+
const results = await new Query()
|
|
134
|
+
.with(TestUser)
|
|
135
|
+
.with(TestProduct, {
|
|
136
|
+
filters: [Query.filter('sku', FilterOp.EQ, 'MULTI-SKU')]
|
|
137
|
+
})
|
|
138
|
+
.exec();
|
|
139
|
+
|
|
140
|
+
expect(Array.isArray(results)).toBe(true);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('two components with filters on both works', async () => {
|
|
144
|
+
const results = await new Query()
|
|
145
|
+
.with(TestUser, {
|
|
146
|
+
filters: [Query.filter('name', FilterOp.EQ, 'MultiComp')]
|
|
147
|
+
})
|
|
148
|
+
.with(TestProduct, {
|
|
149
|
+
filters: [Query.filter('price', FilterOp.GTE, 50)]
|
|
150
|
+
})
|
|
151
|
+
.exec();
|
|
152
|
+
|
|
153
|
+
expect(Array.isArray(results)).toBe(true);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test('three components with mixed filters works', async () => {
|
|
157
|
+
// Add order to existing entity
|
|
158
|
+
const entity = ctx.tracker.create();
|
|
159
|
+
entity.add(TestUser, { name: 'TripleComp', email: 'triple@test.com', age: 35 });
|
|
160
|
+
entity.add(TestProduct, { sku: 'TRIPLE-SKU', name: 'Triple Product', price: 200, inStock: true });
|
|
161
|
+
entity.add(TestOrder, { orderNumber: 'ORD-TRIPLE', status: 'pending', total: 200 });
|
|
162
|
+
await entity.save();
|
|
163
|
+
|
|
164
|
+
const results = await new Query()
|
|
165
|
+
.with(TestUser, {
|
|
166
|
+
filters: [Query.filter('age', FilterOp.GTE, 18)]
|
|
167
|
+
})
|
|
168
|
+
.with(TestProduct)
|
|
169
|
+
.with(TestOrder, {
|
|
170
|
+
filters: [Query.filter('status', FilterOp.EQ, 'pending')]
|
|
171
|
+
})
|
|
172
|
+
.exec();
|
|
173
|
+
|
|
174
|
+
expect(Array.isArray(results)).toBe(true);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe('Sorting edge cases', () => {
|
|
179
|
+
beforeEach(async () => {
|
|
180
|
+
// Create entities with various ages
|
|
181
|
+
for (const age of [20, 30, 40]) {
|
|
182
|
+
const entity = ctx.tracker.create();
|
|
183
|
+
entity.add(TestUser, {
|
|
184
|
+
name: `SortEdge${age}`,
|
|
185
|
+
email: `sortedge${age}@test.com`,
|
|
186
|
+
age
|
|
187
|
+
});
|
|
188
|
+
await entity.save();
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('sort with no filters works', async () => {
|
|
193
|
+
const results = await new Query()
|
|
194
|
+
.with(TestUser)
|
|
195
|
+
.sortBy(TestUser, 'age', 'ASC')
|
|
196
|
+
.exec();
|
|
197
|
+
|
|
198
|
+
expect(Array.isArray(results)).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test('sort with filter on same component works', async () => {
|
|
202
|
+
const results = await new Query()
|
|
203
|
+
.with(TestUser, {
|
|
204
|
+
filters: [Query.filter('email', FilterOp.LIKE, 'sortedge%')]
|
|
205
|
+
})
|
|
206
|
+
.sortBy(TestUser, 'age', 'DESC')
|
|
207
|
+
.populate()
|
|
208
|
+
.exec();
|
|
209
|
+
|
|
210
|
+
expect(Array.isArray(results)).toBe(true);
|
|
211
|
+
// Verify sort order
|
|
212
|
+
for (let i = 1; i < results.length; i++) {
|
|
213
|
+
const prevAge = results[i - 1]!.getInMemory(TestUser)?.age ?? 0;
|
|
214
|
+
const currAge = results[i]!.getInMemory(TestUser)?.age ?? 0;
|
|
215
|
+
expect(currAge).toBeLessThanOrEqual(prevAge);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test('sort on component different from filter works', async () => {
|
|
220
|
+
// Create entity with both components
|
|
221
|
+
const entity = ctx.tracker.create();
|
|
222
|
+
entity.add(TestUser, { name: 'SortDiffComp', email: 'sortdiff@test.com', age: 50 });
|
|
223
|
+
entity.add(TestProduct, { sku: 'SORT-DIFF', name: 'Sort Product', price: 75, inStock: true });
|
|
224
|
+
await entity.save();
|
|
225
|
+
|
|
226
|
+
const results = await new Query()
|
|
227
|
+
.with(TestUser)
|
|
228
|
+
.with(TestProduct, {
|
|
229
|
+
filters: [Query.filter('price', FilterOp.GTE, 50)]
|
|
230
|
+
})
|
|
231
|
+
.sortBy(TestUser, 'age', 'ASC')
|
|
232
|
+
.exec();
|
|
233
|
+
|
|
234
|
+
expect(Array.isArray(results)).toBe(true);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test('multi-component query with sort works', async () => {
|
|
238
|
+
const entity = ctx.tracker.create();
|
|
239
|
+
entity.add(TestUser, { name: 'MultiSort', email: 'multisort@test.com', age: 28 });
|
|
240
|
+
entity.add(TestProduct, { sku: 'MULTI-SORT', name: 'MultiSort Product', price: 150, inStock: true });
|
|
241
|
+
await entity.save();
|
|
242
|
+
|
|
243
|
+
const results = await new Query()
|
|
244
|
+
.with(TestUser)
|
|
245
|
+
.with(TestProduct)
|
|
246
|
+
.sortBy(TestProduct, 'price', 'DESC')
|
|
247
|
+
.exec();
|
|
248
|
+
|
|
249
|
+
expect(Array.isArray(results)).toBe(true);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe('Exclusion edge cases', () => {
|
|
254
|
+
test('without() with no matching entities works', async () => {
|
|
255
|
+
const entity = ctx.tracker.create();
|
|
256
|
+
entity.add(TestUser, { name: 'WithoutTest', email: 'without@test.com', age: 22 });
|
|
257
|
+
await entity.save();
|
|
258
|
+
|
|
259
|
+
const results = await new Query()
|
|
260
|
+
.with(TestUser)
|
|
261
|
+
.without(TestOrder) // This entity doesn't have TestOrder
|
|
262
|
+
.exec();
|
|
263
|
+
|
|
264
|
+
expect(results.some(e => e.id === entity.id)).toBe(true);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test('excludeEntityId with non-existent ID works', async () => {
|
|
268
|
+
const results = await new Query()
|
|
269
|
+
.with(TestUser)
|
|
270
|
+
.excludeEntityId('00000000-0000-0000-0000-000000000000')
|
|
271
|
+
.exec();
|
|
272
|
+
|
|
273
|
+
expect(Array.isArray(results)).toBe(true);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test('multiple excludeEntityId calls work', async () => {
|
|
277
|
+
const entity1 = ctx.tracker.create();
|
|
278
|
+
entity1.add(TestUser, { name: 'Exclude1', email: 'exclude1@test.com', age: 30 });
|
|
279
|
+
await entity1.save();
|
|
280
|
+
|
|
281
|
+
const entity2 = ctx.tracker.create();
|
|
282
|
+
entity2.add(TestUser, { name: 'Exclude2', email: 'exclude2@test.com', age: 31 });
|
|
283
|
+
await entity2.save();
|
|
284
|
+
|
|
285
|
+
const results = await new Query()
|
|
286
|
+
.with(TestUser)
|
|
287
|
+
.excludeEntityId(entity1.id)
|
|
288
|
+
.excludeEntityId(entity2.id)
|
|
289
|
+
.exec();
|
|
290
|
+
|
|
291
|
+
expect(results.some(e => e.id === entity1.id)).toBe(false);
|
|
292
|
+
expect(results.some(e => e.id === entity2.id)).toBe(false);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test('without() combined with filters works', async () => {
|
|
296
|
+
const results = await new Query()
|
|
297
|
+
.with(TestUser, {
|
|
298
|
+
filters: [Query.filter('age', FilterOp.GTE, 18)]
|
|
299
|
+
})
|
|
300
|
+
.without(TestProduct)
|
|
301
|
+
.exec();
|
|
302
|
+
|
|
303
|
+
expect(Array.isArray(results)).toBe(true);
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
describe('Pagination edge cases', () => {
|
|
308
|
+
test('limit 0 returns empty array', async () => {
|
|
309
|
+
const results = await new Query()
|
|
310
|
+
.with(TestUser)
|
|
311
|
+
.take(0)
|
|
312
|
+
.exec();
|
|
313
|
+
|
|
314
|
+
expect(results.length).toBe(0);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test('offset larger than result set returns empty', async () => {
|
|
318
|
+
const count = await new Query().with(TestUser).count();
|
|
319
|
+
|
|
320
|
+
const results = await new Query()
|
|
321
|
+
.with(TestUser)
|
|
322
|
+
.offset(count + 100)
|
|
323
|
+
.take(10)
|
|
324
|
+
.exec();
|
|
325
|
+
|
|
326
|
+
expect(results.length).toBe(0);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test('pagination with filters works', async () => {
|
|
330
|
+
const results = await new Query()
|
|
331
|
+
.with(TestUser, {
|
|
332
|
+
filters: [Query.filter('age', FilterOp.GTE, 18)]
|
|
333
|
+
})
|
|
334
|
+
.take(5)
|
|
335
|
+
.offset(0)
|
|
336
|
+
.exec();
|
|
337
|
+
|
|
338
|
+
expect(results.length).toBeLessThanOrEqual(5);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
test('pagination with sort works', async () => {
|
|
342
|
+
const results = await new Query()
|
|
343
|
+
.with(TestUser)
|
|
344
|
+
.sortBy(TestUser, 'age', 'ASC')
|
|
345
|
+
.take(5)
|
|
346
|
+
.offset(0)
|
|
347
|
+
.exec();
|
|
348
|
+
|
|
349
|
+
expect(results.length).toBeLessThanOrEqual(5);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
test('pagination with multi-component query works', async () => {
|
|
353
|
+
const results = await new Query()
|
|
354
|
+
.with(TestUser)
|
|
355
|
+
.with(TestProduct)
|
|
356
|
+
.take(5)
|
|
357
|
+
.offset(0)
|
|
358
|
+
.exec();
|
|
359
|
+
|
|
360
|
+
expect(Array.isArray(results)).toBe(true);
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
describe('Boolean filter edge cases', () => {
|
|
365
|
+
beforeEach(async () => {
|
|
366
|
+
const inStock = ctx.tracker.create();
|
|
367
|
+
inStock.add(TestProduct, { sku: 'BOOL-IN', name: 'In Stock', price: 50, inStock: true });
|
|
368
|
+
await inStock.save();
|
|
369
|
+
|
|
370
|
+
const outOfStock = ctx.tracker.create();
|
|
371
|
+
outOfStock.add(TestProduct, { sku: 'BOOL-OUT', name: 'Out of Stock', price: 50, inStock: false });
|
|
372
|
+
await outOfStock.save();
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test('boolean true filter works', async () => {
|
|
376
|
+
const results = await new Query()
|
|
377
|
+
.with(TestProduct, {
|
|
378
|
+
filters: [Query.filter('inStock', FilterOp.EQ, true)]
|
|
379
|
+
})
|
|
380
|
+
.populate()
|
|
381
|
+
.exec();
|
|
382
|
+
|
|
383
|
+
for (const entity of results) {
|
|
384
|
+
const product = entity.getInMemory(TestProduct);
|
|
385
|
+
if (product) {
|
|
386
|
+
expect(product.inStock).toBe(true);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
test('boolean false filter works', async () => {
|
|
392
|
+
const results = await new Query()
|
|
393
|
+
.with(TestProduct, {
|
|
394
|
+
filters: [Query.filter('inStock', FilterOp.EQ, false)]
|
|
395
|
+
})
|
|
396
|
+
.populate()
|
|
397
|
+
.exec();
|
|
398
|
+
|
|
399
|
+
for (const entity of results) {
|
|
400
|
+
const product = entity.getInMemory(TestProduct);
|
|
401
|
+
if (product) {
|
|
402
|
+
expect(product.inStock).toBe(false);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
describe('Numeric filter edge cases', () => {
|
|
409
|
+
test('zero value filter works', async () => {
|
|
410
|
+
const entity = ctx.tracker.create();
|
|
411
|
+
entity.add(TestProduct, { sku: 'ZERO-PRICE', name: 'Free Product', price: 0, inStock: true });
|
|
412
|
+
await entity.save();
|
|
413
|
+
|
|
414
|
+
const results = await new Query()
|
|
415
|
+
.with(TestProduct, {
|
|
416
|
+
filters: [Query.filter('price', FilterOp.EQ, 0)]
|
|
417
|
+
})
|
|
418
|
+
.exec();
|
|
419
|
+
|
|
420
|
+
expect(results.length).toBeGreaterThanOrEqual(1);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
test('negative value filter works', async () => {
|
|
424
|
+
const entity = ctx.tracker.create();
|
|
425
|
+
entity.add(TestUser, { name: 'NegAge', email: 'neg@test.com', age: -1 });
|
|
426
|
+
await entity.save();
|
|
427
|
+
|
|
428
|
+
const results = await new Query()
|
|
429
|
+
.with(TestUser, {
|
|
430
|
+
filters: [Query.filter('age', FilterOp.LT, 0)]
|
|
431
|
+
})
|
|
432
|
+
.exec();
|
|
433
|
+
|
|
434
|
+
expect(results.length).toBeGreaterThanOrEqual(1);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
test('large number filter works', async () => {
|
|
438
|
+
const entity = ctx.tracker.create();
|
|
439
|
+
entity.add(TestProduct, { sku: 'LARGE-PRICE', name: 'Expensive', price: 999999999, inStock: true });
|
|
440
|
+
await entity.save();
|
|
441
|
+
|
|
442
|
+
const results = await new Query()
|
|
443
|
+
.with(TestProduct, {
|
|
444
|
+
filters: [Query.filter('price', FilterOp.GTE, 999999999)]
|
|
445
|
+
})
|
|
446
|
+
.exec();
|
|
447
|
+
|
|
448
|
+
expect(results.length).toBeGreaterThanOrEqual(1);
|
|
449
|
+
});
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
describe('String filter edge cases', () => {
|
|
453
|
+
test('LIKE with special characters works', async () => {
|
|
454
|
+
const entity = ctx.tracker.create();
|
|
455
|
+
entity.add(TestUser, { name: 'Test%User', email: 'special@test.com', age: 25 });
|
|
456
|
+
await entity.save();
|
|
457
|
+
|
|
458
|
+
// Note: % in LIKE is a wildcard, so this tests the pattern matching
|
|
459
|
+
const results = await new Query()
|
|
460
|
+
.with(TestUser, {
|
|
461
|
+
filters: [Query.filter('email', FilterOp.LIKE, '%special@test.com')]
|
|
462
|
+
})
|
|
463
|
+
.exec();
|
|
464
|
+
|
|
465
|
+
expect(results.length).toBeGreaterThanOrEqual(1);
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
test('ILIKE case-insensitive filter works', async () => {
|
|
469
|
+
const entity = ctx.tracker.create();
|
|
470
|
+
entity.add(TestUser, { name: 'CaseTest', email: 'UPPERCASE@TEST.COM', age: 25 });
|
|
471
|
+
await entity.save();
|
|
472
|
+
|
|
473
|
+
const results = await new Query()
|
|
474
|
+
.with(TestUser, {
|
|
475
|
+
filters: [Query.filter('email', FilterOp.ILIKE, '%uppercase@test.com%')]
|
|
476
|
+
})
|
|
477
|
+
.exec();
|
|
478
|
+
|
|
479
|
+
expect(results.length).toBeGreaterThanOrEqual(1);
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
describe('Bug reproduction: 2 components, 1 filter, LATERAL joins', () => {
|
|
484
|
+
// This is the exact pattern from the bug report:
|
|
485
|
+
// new Query().with(UserTag).with(PhoneComponent, Query.filters(...)).take(1).exec()
|
|
486
|
+
|
|
487
|
+
test('exact bug pattern: 2 components, filter on second, take(1)', async () => {
|
|
488
|
+
const entity = ctx.tracker.create();
|
|
489
|
+
entity.add(TestUser, { name: 'BugRepro', email: 'bug@test.com', age: 30 });
|
|
490
|
+
entity.add(TestProduct, { sku: 'BUG-SKU', name: 'Bug Product', price: 100, inStock: true });
|
|
491
|
+
await entity.save();
|
|
492
|
+
|
|
493
|
+
// This is the exact failing pattern
|
|
494
|
+
const results = await new Query()
|
|
495
|
+
.with(TestUser)
|
|
496
|
+
.with(TestProduct, {
|
|
497
|
+
filters: [Query.filter('sku', FilterOp.EQ, 'BUG-SKU')]
|
|
498
|
+
})
|
|
499
|
+
.take(1)
|
|
500
|
+
.exec();
|
|
501
|
+
|
|
502
|
+
expect(Array.isArray(results)).toBe(true);
|
|
503
|
+
expect(results.length).toBeLessThanOrEqual(1);
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
test('verify SQL is valid for bug pattern', async () => {
|
|
507
|
+
const entity = ctx.tracker.create();
|
|
508
|
+
entity.add(TestUser, { name: 'SQLVerify', email: 'sql@test.com', age: 25 });
|
|
509
|
+
entity.add(TestProduct, { sku: 'SQL-SKU', name: 'SQL Product', price: 50, inStock: true });
|
|
510
|
+
await entity.save();
|
|
511
|
+
|
|
512
|
+
// Should not throw SQL syntax error
|
|
513
|
+
const results = await new Query()
|
|
514
|
+
.with(TestUser)
|
|
515
|
+
.with(TestProduct, {
|
|
516
|
+
filters: [Query.filter('name', FilterOp.LIKE, '%SQL%')]
|
|
517
|
+
})
|
|
518
|
+
.take(10)
|
|
519
|
+
.exec();
|
|
520
|
+
|
|
521
|
+
expect(Array.isArray(results)).toBe(true);
|
|
522
|
+
});
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
describe('Combined complex queries', () => {
|
|
526
|
+
test('all features combined works', async () => {
|
|
527
|
+
// Create test data
|
|
528
|
+
const entity = ctx.tracker.create();
|
|
529
|
+
entity.add(TestUser, { name: 'ComplexTest', email: 'complex@test.com', age: 30 });
|
|
530
|
+
entity.add(TestProduct, { sku: 'COMPLEX-SKU', name: 'Complex Product', price: 100, inStock: true });
|
|
531
|
+
await entity.save();
|
|
532
|
+
|
|
533
|
+
// Query with everything
|
|
534
|
+
const results = await new Query()
|
|
535
|
+
.with(TestUser, {
|
|
536
|
+
filters: [
|
|
537
|
+
Query.filter('age', FilterOp.GTE, 18),
|
|
538
|
+
Query.filter('name', FilterOp.LIKE, 'Complex%')
|
|
539
|
+
]
|
|
540
|
+
})
|
|
541
|
+
.with(TestProduct, {
|
|
542
|
+
filters: [Query.filter('inStock', FilterOp.EQ, true)]
|
|
543
|
+
})
|
|
544
|
+
.without(TestOrder)
|
|
545
|
+
.sortBy(TestUser, 'age', 'DESC')
|
|
546
|
+
.take(10)
|
|
547
|
+
.offset(0)
|
|
548
|
+
.populate()
|
|
549
|
+
.exec();
|
|
550
|
+
|
|
551
|
+
expect(Array.isArray(results)).toBe(true);
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
test('multi-component with IN filter and sort works', async () => {
|
|
555
|
+
const entity = ctx.tracker.create();
|
|
556
|
+
entity.add(TestUser, { name: 'InSortTest', email: 'insort@test.com', age: 35 });
|
|
557
|
+
entity.add(TestProduct, { sku: 'IN-SORT-SKU', name: 'InSort Product', price: 75, inStock: true });
|
|
558
|
+
await entity.save();
|
|
559
|
+
|
|
560
|
+
const results = await new Query()
|
|
561
|
+
.with(TestUser, {
|
|
562
|
+
filters: [Query.filter('name', FilterOp.IN, ['InSortTest', 'Other'])]
|
|
563
|
+
})
|
|
564
|
+
.with(TestProduct)
|
|
565
|
+
.sortBy(TestProduct, 'price', 'ASC')
|
|
566
|
+
.exec();
|
|
567
|
+
|
|
568
|
+
expect(Array.isArray(results)).toBe(true);
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
test('filters on all components with pagination works', async () => {
|
|
572
|
+
const entity = ctx.tracker.create();
|
|
573
|
+
entity.add(TestUser, { name: 'AllFilters', email: 'allfilters@test.com', age: 40 });
|
|
574
|
+
entity.add(TestProduct, { sku: 'ALL-FILTERS', name: 'All Filters Product', price: 200, inStock: true });
|
|
575
|
+
entity.add(TestOrder, { orderNumber: 'ORD-ALL', status: 'completed', total: 200 });
|
|
576
|
+
await entity.save();
|
|
577
|
+
|
|
578
|
+
const results = await new Query()
|
|
579
|
+
.with(TestUser, {
|
|
580
|
+
filters: [Query.filter('age', FilterOp.GTE, 30)]
|
|
581
|
+
})
|
|
582
|
+
.with(TestProduct, {
|
|
583
|
+
filters: [Query.filter('price', FilterOp.GTE, 100)]
|
|
584
|
+
})
|
|
585
|
+
.with(TestOrder, {
|
|
586
|
+
filters: [Query.filter('status', FilterOp.EQ, 'completed')]
|
|
587
|
+
})
|
|
588
|
+
.take(5)
|
|
589
|
+
.offset(0)
|
|
590
|
+
.exec();
|
|
591
|
+
|
|
592
|
+
expect(Array.isArray(results)).toBe(true);
|
|
593
|
+
});
|
|
594
|
+
});
|
|
595
|
+
});
|