-
Notifications
You must be signed in to change notification settings - Fork 368
/
records.service.integration.test.ts
130 lines (104 loc) · 4.46 KB
/
records.service.integration.test.ts
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
import { expect, describe, it, beforeAll } from 'vitest';
import dayjs from 'dayjs';
import { multipleMigrations } from '../../../db/database.js';
import * as RecordsService from './records.service.js';
import { createConfigSeeds } from '../../../db/seeders/config.seeder.js';
import { upsertRecords } from './mocks.js';
const environmentName = 'records-service';
describe('Records service', () => {
beforeAll(async () => {
await multipleMigrations();
await createConfigSeeds(environmentName);
});
it('Should retrieve records', async () => {
const n = 10;
const { connection, model } = await upsertRecords(n);
const { success, response, error } = await RecordsService.getAllDataRecords(
connection.connection_id,
connection.provider_config_key,
connection.environment_id,
model
);
expect(success).toBe(true);
expect(error).toBe(null);
expect(response?.records.length).toBe(n);
expect(response?.records[0]?.['_nango_metadata']).toMatchObject({
first_seen_at: expect.toBeIsoDateTimezone(),
last_modified_at: expect.toBeIsoDateTimezone(),
last_action: 'ADDED',
deleted_at: null,
cursor: expect.stringMatching(/^[A-Za-z0-9+/]+={0,2}$/) // base64 encoded string
});
expect(response?.next_cursor).toBe(null); // no next page
});
it('Should paginate the records to retrieve all records', async () => {
const numOfRecords = 3000;
const limit = 100;
const { connection, model } = await upsertRecords(numOfRecords);
let cursor = null;
const allFetchedRecords = [];
do {
const result = await RecordsService.getAllDataRecords(
connection.connection_id,
connection.provider_config_key,
connection.environment_id,
model,
undefined, // delta
limit, // limit
undefined, // filter
cursor // cursor
);
if (!result.response) {
throw new Error('Response is undefined');
}
const { response: recordsResponse, error } = result;
expect(error).toBe(null);
expect(recordsResponse).not.toBe(undefined);
const { records, next_cursor } = recordsResponse;
allFetchedRecords.push(...records);
cursor = next_cursor;
expect(records).not.toBe(undefined);
expect(records?.length).toBeLessThanOrEqual(limit);
} while (cursor);
for (let i = 1; i < allFetchedRecords.length; i++) {
const currentRecordDate = dayjs(allFetchedRecords[i]?._nango_metadata.first_seen_at);
const previousRecordDate = dayjs(allFetchedRecords[i - 1]?._nango_metadata.first_seen_at);
expect(currentRecordDate.isAfter(previousRecordDate) || currentRecordDate.isSame(previousRecordDate)).toBe(true);
}
expect(allFetchedRecords.length).toBe(numOfRecords);
});
it('Should be able to retrieve 20K records in under 5s with a cursor', async () => {
const numOfRecords = 20000;
const limit = 1000;
const { connection, model } = await upsertRecords(numOfRecords);
let cursor: string | undefined | null = null;
let allRecordsLength = 0;
const startTime = Date.now();
do {
const { response, error } = await RecordsService.getAllDataRecords(
connection.connection_id,
connection.provider_config_key,
connection.environment_id,
model, // model
undefined, // delta
limit, // limit
undefined, // filter
cursor // cursor
);
if (!response) {
throw new Error('Response is undefined');
}
expect(error).toBe(null);
expect(response).not.toBe(undefined);
const { records, next_cursor } = response;
allRecordsLength += records.length;
cursor = next_cursor;
expect(records).not.toBe(undefined);
expect(records?.length).toBeLessThanOrEqual(limit);
} while (cursor);
const endTime = Date.now();
const runTime = endTime - startTime;
expect(runTime).toBeLessThan(5000);
expect(allRecordsLength).toBe(numOfRecords);
});
});