-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmemory-analytics.test.ts
More file actions
346 lines (303 loc) · 10.6 KB
/
Copy pathmemory-analytics.test.ts
File metadata and controls
346 lines (303 loc) · 10.6 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, beforeEach } from 'vitest';
import { InMemoryDriver } from './memory-driver.js';
import { MemoryAnalyticsService } from './memory-analytics.js';
import type { Cube } from '@objectstack/spec/data';
describe('MemoryAnalyticsService', () => {
let driver: InMemoryDriver;
let service: MemoryAnalyticsService;
beforeEach(async () => {
// Initialize driver with sample data
driver = new InMemoryDriver({
initialData: {
orders: [
{ id: 1, customer: 'Alice', status: 'completed', amount: 100, created_at: new Date('2024-01-15') },
{ id: 2, customer: 'Bob', status: 'completed', amount: 200, created_at: new Date('2024-01-16') },
{ id: 3, customer: 'Alice', status: 'pending', amount: 150, created_at: new Date('2024-01-17') },
{ id: 4, customer: 'Charlie', status: 'completed', amount: 300, created_at: new Date('2024-01-18') },
{ id: 5, customer: 'Bob', status: 'cancelled', amount: 50, created_at: new Date('2024-01-19') },
],
products: [
{ id: 1, name: 'Laptop', category: 'electronics', price: 999, stock: 10 },
{ id: 2, name: 'Mouse', category: 'electronics', price: 25, stock: 100 },
{ id: 3, name: 'Desk', category: 'furniture', price: 299, stock: 5 },
{ id: 4, name: 'Chair', category: 'furniture', price: 199, stock: 8 },
]
}
});
// Connect the driver to load initial data
await driver.connect();
// Define cubes
const cubes: Cube[] = [
{
name: 'orders',
title: 'Orders',
sql: 'orders',
measures: {
count: {
name: 'count',
label: 'Order Count',
type: 'count',
sql: 'id'
},
totalAmount: {
name: 'total_amount',
label: 'Total Amount',
type: 'sum',
sql: 'amount'
},
avgAmount: {
name: 'avg_amount',
label: 'Average Amount',
type: 'avg',
sql: 'amount'
}
},
dimensions: {
customer: {
name: 'customer',
label: 'Customer',
type: 'string',
sql: 'customer'
},
status: {
name: 'status',
label: 'Status',
type: 'string',
sql: 'status'
},
createdAt: {
name: 'created_at',
label: 'Created At',
type: 'time',
sql: 'created_at',
granularities: ['day', 'week', 'month']
}
},
public: true
},
{
name: 'products',
title: 'Products',
sql: 'products',
measures: {
count: {
name: 'count',
label: 'Product Count',
type: 'count',
sql: 'id'
},
avgPrice: {
name: 'avg_price',
label: 'Average Price',
type: 'avg',
sql: 'price'
},
totalStock: {
name: 'total_stock',
label: 'Total Stock',
type: 'sum',
sql: 'stock'
}
},
dimensions: {
category: {
name: 'category',
label: 'Category',
type: 'string',
sql: 'category'
},
name: {
name: 'name',
label: 'Product Name',
type: 'string',
sql: 'name'
}
},
public: true
}
];
service = new MemoryAnalyticsService({ driver, cubes });
});
describe('getMeta', () => {
it('should return metadata for all cubes', async () => {
const meta = await service.getMeta();
expect(meta).toHaveLength(2);
expect(meta[0].name).toBe('orders');
expect(meta[1].name).toBe('products');
});
it('should return metadata for a specific cube', async () => {
const meta = await service.getMeta('orders');
expect(meta).toHaveLength(1);
expect(meta[0].name).toBe('orders');
expect(meta[0].measures).toHaveLength(3);
expect(meta[0].dimensions).toHaveLength(3);
});
it('should include measure and dimension details', async () => {
const meta = await service.getMeta('orders');
const cube = meta[0];
const countMeasure = cube.measures.find(m => m.name === 'orders.count');
expect(countMeasure).toBeDefined();
expect(countMeasure?.type).toBe('count');
const statusDim = cube.dimensions.find(d => d.name === 'orders.status');
expect(statusDim).toBeDefined();
expect(statusDim?.type).toBe('string');
});
});
describe('query', () => {
it('should execute a simple count query', async () => {
const result = await service.query({
cube: 'orders',
measures: ['orders.count']
});
expect(result.rows).toHaveLength(1);
expect(result.rows[0]['orders.count']).toBe(5);
expect(result.fields).toHaveLength(1);
expect(result.fields[0].name).toBe('orders.count');
expect(result.fields[0].type).toBe('number');
});
it('should group by a dimension', async () => {
const result = await service.query({
cube: 'orders',
measures: ['orders.count'],
dimensions: ['orders.status']
});
expect(result.rows).toHaveLength(3); // completed, pending, cancelled
const completedRow = result.rows.find(r => r['orders.status'] === 'completed');
expect(completedRow).toBeDefined();
expect(completedRow!['orders.count']).toBe(3);
});
it('should calculate sum aggregation', async () => {
const result = await service.query({
cube: 'orders',
measures: ['orders.totalAmount'],
dimensions: ['orders.customer']
});
const aliceRow = result.rows.find(r => r['orders.customer'] === 'Alice');
expect(aliceRow).toBeDefined();
expect(aliceRow!['orders.totalAmount']).toBe(250); // 100 + 150
});
it('should calculate average aggregation', async () => {
const result = await service.query({
cube: 'products',
measures: ['products.avgPrice'],
dimensions: ['products.category']
});
const electronicsRow = result.rows.find(r => r['products.category'] === 'electronics');
expect(electronicsRow).toBeDefined();
expect(electronicsRow!['products.avgPrice']).toBe(512); // (999 + 25) / 2
});
it('should support multiple measures', async () => {
const result = await service.query({
cube: 'orders',
measures: ['orders.count', 'orders.totalAmount', 'orders.avgAmount']
});
expect(result.rows).toHaveLength(1);
expect(result.rows[0]['orders.count']).toBe(5);
expect(result.rows[0]['orders.totalAmount']).toBe(800); // 100+200+150+300+50
expect(result.rows[0]['orders.avgAmount']).toBe(160); // 800/5
});
it('should apply filters', async () => {
const result = await service.query({
cube: 'orders',
measures: ['orders.count', 'orders.totalAmount'],
filters: [
{ member: 'orders.status', operator: 'equals', values: ['completed'] }
]
});
expect(result.rows).toHaveLength(1);
expect(result.rows[0]['orders.count']).toBe(3);
expect(result.rows[0]['orders.totalAmount']).toBe(600); // 100+200+300
});
it('should support sorting', async () => {
const result = await service.query({
cube: 'orders',
measures: ['orders.totalAmount'],
dimensions: ['orders.customer'],
order: { 'orders.totalAmount': 'desc' }
});
expect(result.rows[0]['orders.customer']).toBe('Charlie'); // 300
expect(result.rows[1]['orders.customer']).toBe('Alice'); // 250
expect(result.rows[2]['orders.customer']).toBe('Bob'); // 250
});
it('should support limit and offset', async () => {
const result = await service.query({
cube: 'orders',
measures: ['orders.count'],
dimensions: ['orders.customer'],
order: { 'orders.customer': 'asc' },
limit: 2,
offset: 1
});
expect(result.rows).toHaveLength(2);
expect(result.rows[0]['orders.customer']).toBe('Bob');
expect(result.rows[1]['orders.customer']).toBe('Charlie');
});
it('should throw error for unknown cube', async () => {
await expect(async () => {
await service.query({
cube: 'unknown',
measures: ['unknown.count']
});
}).rejects.toThrow('Cube not found: unknown');
});
it('should include SQL in result for debugging', async () => {
const result = await service.query({
cube: 'orders',
measures: ['orders.count']
});
expect(result.sql).toBeDefined();
expect(result.sql).toContain('orders');
});
});
describe('generateSql', () => {
it('should generate SQL for a simple query', async () => {
const result = await service.generateSql({
cube: 'orders',
measures: ['orders.count']
});
expect(result.sql).toContain('SELECT');
expect(result.sql).toContain('COUNT(*)');
expect(result.sql).toContain('FROM orders');
});
it('should generate SQL with GROUP BY', async () => {
const result = await service.generateSql({
cube: 'orders',
measures: ['orders.count'],
dimensions: ['orders.status']
});
expect(result.sql).toContain('GROUP BY status');
});
it('should generate SQL with WHERE clause', async () => {
const result = await service.generateSql({
cube: 'orders',
measures: ['orders.count'],
filters: [
{ member: 'orders.status', operator: 'equals', values: ['completed'] }
]
});
expect(result.sql).toContain('WHERE');
expect(result.sql).toContain('status');
});
it('should generate SQL with ORDER BY', async () => {
const result = await service.generateSql({
cube: 'orders',
measures: ['orders.count'],
dimensions: ['orders.status'],
order: { 'orders.status': 'asc' }
});
expect(result.sql).toContain('ORDER BY');
expect(result.sql).toContain('ASC');
});
it('should generate SQL with LIMIT and OFFSET', async () => {
const result = await service.generateSql({
cube: 'orders',
measures: ['orders.count'],
limit: 10,
offset: 5
});
expect(result.sql).toContain('LIMIT 10');
expect(result.sql).toContain('OFFSET 5');
});
});
});