-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsql-driver-queryast.test.ts
More file actions
243 lines (201 loc) · 8.05 KB
/
Copy pathsql-driver-queryast.test.ts
File metadata and controls
243 lines (201 loc) · 8.05 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { SqlDriver } from '../src/index.js';
/**
* QueryAST format tests — verifies compatibility with @objectstack/spec QueryAST.
*/
describe('SqlDriver (QueryAST Format)', () => {
let driver: SqlDriver;
beforeEach(async () => {
driver = new SqlDriver({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
const k = (driver as any).knex;
await k.schema.createTable('products', (t: any) => {
t.string('id').primary();
t.string('name');
t.float('price');
t.string('category');
});
await k('products').insert([
{ id: '1', name: 'Laptop', price: 1200, category: 'Electronics' },
{ id: '2', name: 'Mouse', price: 25, category: 'Electronics' },
{ id: '3', name: 'Desk', price: 350, category: 'Furniture' },
{ id: '4', name: 'Chair', price: 200, category: 'Furniture' },
{ id: '5', name: 'Monitor', price: 400, category: 'Electronics' },
]);
});
afterEach(async () => {
await driver.disconnect();
});
describe('Driver Metadata', () => {
it('should expose driver metadata for ObjectStack compatibility', () => {
expect(driver.name).toBe('com.objectstack.driver.sql');
expect(driver.version).toBeDefined();
expect(driver.supports).toBeDefined();
expect(driver.supports.transactions).toBe(true);
expect(driver.supports.joins).toBe(true);
});
});
describe('Lifecycle Methods', () => {
it('should support connect method', async () => {
await expect(driver.connect()).resolves.toBeUndefined();
});
it('should support checkHealth method', async () => {
const healthy = await driver.checkHealth();
expect(healthy).toBe(true);
});
it('should support disconnect method', async () => {
const testDriver = new SqlDriver({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
await expect(testDriver.disconnect()).resolves.toBeUndefined();
const healthy = await testDriver.checkHealth();
expect(healthy).toBe(false);
});
});
describe('QueryAST Format Support', () => {
it('should support QueryAST with limit and orderBy', async () => {
const results = await driver.find('products', {
fields: ['name', 'price'],
limit: 2,
orderBy: [{ field: 'price', order: 'asc' as const }],
} as any);
expect(results.length).toBe(2);
expect(results[0].name).toBe('Mouse');
expect(results[1].name).toBe('Chair');
});
it('should support QueryAST orderBy with object notation', async () => {
const results = await driver.find('products', {
fields: ['name'],
orderBy: [
{ field: 'category', order: 'asc' as const },
{ field: 'price', order: 'desc' as const },
],
} as any);
expect(results.length).toBe(5);
expect(results[0].name).toBe('Laptop');
expect(results[3].name).toBe('Desk');
});
it('should support QueryAST with where, offset, limit, and orderBy', async () => {
const results = await driver.find('products', {
where: [['category', '=', 'Electronics']],
offset: 1,
limit: 1,
orderBy: [{ field: 'price', order: 'asc' as const }],
} as any);
expect(results.length).toBe(1);
expect(results[0].name).toBe('Monitor');
});
it('should support aggregations in QueryAST format', async () => {
const results = await driver.aggregate('products', {
aggregations: [
{ function: 'sum' as const, field: 'price', alias: 'total_price' },
{ function: 'count' as const, field: '*', alias: 'count' },
],
groupBy: ['category'],
});
expect(results.length).toBe(2);
const electronics = results.find((r: any) => r.category === 'Electronics');
const furniture = results.find((r: any) => r.category === 'Furniture');
expect(electronics).toBeDefined();
expect(electronics.total_price).toBe(1625);
expect(furniture).toBeDefined();
expect(furniture.total_price).toBe(550);
});
it('should support count with QueryAST where clause', async () => {
const count = await driver.count('products', {
where: [['price', '>', 300]],
} as any);
expect(count).toBe(3);
});
});
describe('Standard QueryAST Pagination', () => {
it('should support limit with orderBy using standard keys', async () => {
const results = await driver.find('products', {
fields: ['name'],
limit: 2,
orderBy: [{ field: 'price', order: 'asc' }],
} as any);
expect(results.length).toBe(2);
expect(results[0].name).toBe('Mouse');
});
it('should still support legacy aggregate format', async () => {
const results = await driver.aggregate('products', {
aggregate: [{ func: 'avg', field: 'price', alias: 'avg_price' }],
groupBy: ['category'],
});
expect(results.length).toBe(2);
const electronics = results.find((r: any) => r.category === 'Electronics');
expect(electronics.avg_price).toBeCloseTo(541.67, 1);
});
it('should support offset and limit with orderBy', async () => {
const results = await driver.find('products', {
limit: 3,
offset: 2,
orderBy: [{ field: 'name', order: 'asc' as const }],
} as any);
expect(results.length).toBe(3);
expect(results[0].name).toBe('Laptop');
expect(results[1].name).toBe('Monitor');
expect(results[2].name).toBe('Mouse');
});
});
describe('Legacy Keys Are Ignored', () => {
it('should ignore legacy "filters" key — only "where" is recognized', async () => {
const results = await driver.find('products', {
filters: [['category', '=', 'Furniture']],
} as any);
// "filters" is not recognized, so no WHERE clause is applied — returns all rows
expect(results.length).toBe(5);
});
it('should use "where" and ignore "filters" when both are present', async () => {
const results = await driver.find('products', {
where: [['category', '=', 'Electronics']],
filters: [['category', '=', 'Furniture']],
} as any);
// Only "where" is applied — returns Electronics, not Furniture
expect(results.every((r: any) => r.category === 'Electronics')).toBe(true);
expect(results.length).toBe(3);
});
it('should ignore legacy "sort" key — only "orderBy" is recognized', async () => {
const results = await driver.find('products', {
fields: ['name'],
limit: 5,
orderBy: [{ field: 'price', order: 'desc' as const }],
sort: [{ field: 'price', order: 'asc' as const }],
} as any);
// "sort" is ignored; "orderBy" (desc) is applied — most expensive first
expect(results.length).toBe(5);
expect(results[0].name).toBe('Laptop');
expect(results[4].name).toBe('Mouse');
});
it('should ignore legacy "skip" key — only "offset" is recognized', async () => {
const results = await driver.find('products', {
skip: 3,
orderBy: [{ field: 'name', order: 'asc' as const }],
} as any);
// "skip" is not recognized — no offset applied, returns all 5 rows
expect(results.length).toBe(5);
});
it('should ignore legacy "top" key — only "limit" is recognized', async () => {
const results = await driver.find('products', {
top: 2,
orderBy: [{ field: 'name', order: 'asc' as const }],
} as any);
// "top" is not recognized — no limit applied, returns all 5 rows
expect(results.length).toBe(5);
});
it('should ignore legacy "filters" in count — only "where" is recognized', async () => {
const count = await driver.count('products', {
filters: [['price', '>', 300]],
} as any);
// "filters" is not recognized — counts all rows
expect(count).toBe(5);
});
});
});