Skip to content

Commit 05e1832

Browse files
tests for postgres query build and a small bug in mongo cursor
1 parent c57efb3 commit 05e1832

File tree

5 files changed

+236
-9
lines changed

5 files changed

+236
-9
lines changed

modules/module-mongodb-storage/src/utils/util.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,12 @@ export const createPaginatedConnectionQuery = async <T extends mongo.Document>(
125125
if (!cursor) {
126126
return query;
127127
}
128+
const connected_at = query.connected_at
129+
? { $lt: new Date(cursor), $gte: query.connected_at.$gte }
130+
: { $lt: new Date(cursor) };
128131
return {
129-
$and: [
130-
query,
131-
{
132-
connected_at: {
133-
$lt: new Date(cursor)
134-
}
135-
}
136-
]
132+
...query,
133+
connected_at
137134
} as mongo.Filter<T>;
138135
};
139136

modules/module-mongodb-storage/test/src/__snapshots__/connection-report-storage.test.ts.snap

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,76 @@ exports[`Report storage tests > Should show paginated response of all connection
301301
}
302302
`;
303303

304+
exports[`Report storage tests > Should show paginated response of all connections with a limit with date range 1`] = `
305+
{
306+
"count": 4,
307+
"cursor": "<removed-for-snapshot>",
308+
"items": [
309+
{
310+
"client_id": "",
311+
"sdk": "unknown",
312+
"user_agent": "Dart (flutter-web) Chrome/128 android",
313+
"user_id": "user_one",
314+
},
315+
{
316+
"client_id": "client_two",
317+
"sdk": "powersync-js/1.21.1",
318+
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
319+
"user_id": "user_two",
320+
},
321+
{
322+
"client_id": "client_three",
323+
"sdk": "powersync-js/1.21.2",
324+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
325+
"user_id": "user_three",
326+
},
327+
{
328+
"client_id": "client_one",
329+
"sdk": "powersync-js/1.24.5",
330+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
331+
"user_id": "user_week",
332+
},
333+
],
334+
"more": true,
335+
"total": 0,
336+
}
337+
`;
338+
339+
exports[`Report storage tests > Should show paginated response of all connections with a limit with date range 2`] = `
340+
{
341+
"count": 2,
342+
"cursor": undefined,
343+
"items": [
344+
{
345+
"client_id": "",
346+
"sdk": "unknown",
347+
"user_agent": "Dart (flutter-web) Chrome/128 android",
348+
"user_id": "user_one",
349+
},
350+
{
351+
"client_id": "client_two",
352+
"sdk": "powersync-js/1.21.1",
353+
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
354+
"user_id": "user_two",
355+
},
356+
{
357+
"client_id": "client_three",
358+
"sdk": "powersync-js/1.21.2",
359+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
360+
"user_id": "user_three",
361+
},
362+
{
363+
"client_id": "client_one",
364+
"sdk": "powersync-js/1.24.5",
365+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
366+
"user_id": "user_week",
367+
},
368+
],
369+
"more": false,
370+
"total": 0,
371+
}
372+
`;
373+
304374
exports[`Report storage tests > Should show paginated response of connections of specified user_id 1`] = `
305375
{
306376
"count": 2,
@@ -370,3 +440,20 @@ exports[`Report storage tests > Should show paginated response of connections ov
370440
"total": 0,
371441
}
372442
`;
443+
444+
exports[`Report storage tests > Should show paginated response of connections over a date range of specified client_id and user_id 1`] = `
445+
{
446+
"count": 1,
447+
"cursor": undefined,
448+
"items": [
449+
{
450+
"client_id": "client_one",
451+
"sdk": "powersync-dart/1.6.4",
452+
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
453+
"user_id": "user_one",
454+
},
455+
],
456+
"more": false,
457+
"total": 0,
458+
}
459+
`;

modules/module-postgres-storage/src/storage/PostgresReportStorage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export class PostgresReportStorage implements storage.ReportStorage {
160160
/** Order in descending connected at range to match Mongo sort=-1*/
161161
intermediateQuery += ` ORDER BY connected_at DESC`;
162162
query += intermediateQuery;
163+
163164
return {
164165
mainQuery: {
165166
statement: query,

modules/module-postgres-storage/test/src/__snapshots__/connection-report-storage.test.ts.snap

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,84 @@ exports[`Report storage tests > Should show paginated response of all connection
310310
}
311311
`;
312312

313+
exports[`Report storage tests > Should show paginated response of all connections with a limit with date range 1`] = `
314+
{
315+
"count": 4,
316+
"cursor": "<removed-for-snapshot>",
317+
"items": [
318+
{
319+
"client_id": "client_two",
320+
"id": "2",
321+
"sdk": "powersync-js/1.21.1",
322+
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
323+
"user_id": "user_two",
324+
},
325+
{
326+
"client_id": "",
327+
"id": "5",
328+
"sdk": "unknown",
329+
"user_agent": "Dart (flutter-web) Chrome/128 android",
330+
"user_id": "user_one",
331+
},
332+
{
333+
"client_id": "client_three",
334+
"id": "3",
335+
"sdk": "powersync-js/1.21.2",
336+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
337+
"user_id": "user_three",
338+
},
339+
{
340+
"client_id": "client_one",
341+
"id": "week",
342+
"sdk": "powersync-js/1.24.5",
343+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
344+
"user_id": "user_week",
345+
},
346+
],
347+
"more": true,
348+
"total": 0,
349+
}
350+
`;
351+
352+
exports[`Report storage tests > Should show paginated response of all connections with a limit with date range 2`] = `
353+
{
354+
"count": 2,
355+
"cursor": undefined,
356+
"items": [
357+
{
358+
"client_id": "client_two",
359+
"id": "2",
360+
"sdk": "powersync-js/1.21.1",
361+
"user_agent": "powersync-js/1.21.0 powersync-web Chromium/138 linux",
362+
"user_id": "user_two",
363+
},
364+
{
365+
"client_id": "",
366+
"id": "5",
367+
"sdk": "unknown",
368+
"user_agent": "Dart (flutter-web) Chrome/128 android",
369+
"user_id": "user_one",
370+
},
371+
{
372+
"client_id": "client_three",
373+
"id": "3",
374+
"sdk": "powersync-js/1.21.2",
375+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
376+
"user_id": "user_three",
377+
},
378+
{
379+
"client_id": "client_one",
380+
"id": "week",
381+
"sdk": "powersync-js/1.24.5",
382+
"user_agent": "powersync-js/1.21.0 powersync-web Firefox/141 linux",
383+
"user_id": "user_week",
384+
},
385+
],
386+
"more": false,
387+
"total": 0,
388+
}
389+
`;
390+
313391
exports[`Report storage tests > Should show paginated response of connections of specified user_id 1`] = `
314392
{
315393
"count": 2,
@@ -387,3 +465,21 @@ exports[`Report storage tests > Should show paginated response of connections ov
387465
"total": 0,
388466
}
389467
`;
468+
469+
exports[`Report storage tests > Should show paginated response of connections over a date range of specified client_id and user_id 1`] = `
470+
{
471+
"count": 1,
472+
"cursor": undefined,
473+
"items": [
474+
{
475+
"client_id": "client_one",
476+
"id": "1",
477+
"sdk": "powersync-dart/1.6.4",
478+
"user_agent": "powersync-dart/1.6.4 Dart (flutter-web) Chrome/128 android",
479+
"user_id": "user_one",
480+
},
481+
],
482+
"more": false,
483+
"total": 0,
484+
}
485+
`;

packages/service-core-tests/src/tests/register-report-tests.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,30 @@ export async function registerReportTests(factory: storage.ReportStorage) {
182182
expect(cleaned).toMatchSnapshot();
183183
});
184184

185+
it('Should show paginated response of connections over a date range of specified client_id and user_id', async () => {
186+
const connections = await factory.getGeneralClientConnectionAnalytics({
187+
client_id: user_one.client_id,
188+
user_id: user_one.user_id,
189+
date_range: {
190+
start: weekAgo,
191+
end: now
192+
}
193+
});
194+
195+
const cleaned = {
196+
...connections,
197+
items: removeVolatileFields(connections.items)
198+
};
199+
expect(cleaned).toMatchSnapshot();
200+
});
201+
185202
it('Should show paginated response of all connections with a limit', async () => {
186203
const initial = await factory.getGeneralClientConnectionAnalytics({
187204
limit: 4
188205
});
189206

190207
const cursor = initial.cursor;
191208
expect(cursor).toBeDefined();
192-
193209
const cleanedInitial = {
194210
...initial,
195211
cursor: '<removed-for-snapshot>',
@@ -199,6 +215,36 @@ export async function registerReportTests(factory: storage.ReportStorage) {
199215
const connections = await factory.getGeneralClientConnectionAnalytics({
200216
cursor
201217
});
218+
const cleaned = {
219+
...connections,
220+
items: removeVolatileFields(initial.items)
221+
};
222+
expect(cleaned).toMatchSnapshot();
223+
});
224+
225+
it('Should show paginated response of all connections with a limit with date range', async () => {
226+
const date_range = {
227+
start: monthAgo,
228+
end: nowLess5minutes
229+
};
230+
const initial = await factory.getGeneralClientConnectionAnalytics({
231+
limit: 4,
232+
date_range
233+
});
234+
235+
const cursor = initial.cursor;
236+
expect(cursor).toBeDefined();
237+
238+
const cleanedInitial = {
239+
...initial,
240+
cursor: '<removed-for-snapshot>',
241+
items: removeVolatileFields(initial.items)
242+
};
243+
expect(cleanedInitial).toMatchSnapshot();
244+
const connections = await factory.getGeneralClientConnectionAnalytics({
245+
cursor,
246+
date_range
247+
});
202248

203249
const cleaned = {
204250
...connections,

0 commit comments

Comments
 (0)