Skip to content

Commit

Permalink
tests: fix flaky test (NangoHQ#1853)
Browse files Browse the repository at this point in the history
## Describe your changes

Fixes NAN-565

This test has been throwing a lot of errors

- Fix inserting to the same activityLog
- Fix object matching
  • Loading branch information
bodinsamuel authored Mar 14, 2024
1 parent 1b5a295 commit 2a9ddda
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
9 changes: 6 additions & 3 deletions packages/shared/lib/db/seeders/config.seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { schema } from '../database.js';
import configService from '../../services/config.service.js';
import environmentService from '../../services/environment.service.js';
import type { Config as ProviderConfig } from '../../models/Provider.js';
import type { Environment } from '../../models/Environment.js';

export const createConfigSeeds = async (environmentName = ''): Promise<void> => {
let result;
export const createConfigSeeds = async (environmentName = ''): Promise<Environment[]> => {
let result: Environment[];
if (environmentName) {
result = [await environmentService.createEnvironment(0, environmentName)];
result = [(await environmentService.createEnvironment(0, environmentName))!];
} else {
result = await schema().select('*').from('_nango_environments');
}
Expand Down Expand Up @@ -34,4 +35,6 @@ export const createConfigSeeds = async (environmentName = ''): Promise<void> =>
environment_id
} as ProviderConfig);
}

return result;
};
5 changes: 3 additions & 2 deletions packages/shared/lib/services/sync/data/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { markRecordsForDeletion, syncCreatedAtForAddedRecords, syncUpdateAtForCh
import type { UpsertResponse } from '../../../models/Data.js';
import type { DataRecord } from '../../../models/Sync.js';
import encryptionManager from '../../../utils/encryption.manager.js';
import { logger } from '../../../index.js';

/**
* Upsert
Expand Down Expand Up @@ -87,12 +88,12 @@ export async function upsert(

if (error.code) errorMessage += `Error code: ${error.code}.\n`;

console.log(`${errorMessage}${error}`);
logger.error(`${errorMessage}${error}`);

let errorDetail = '';
switch (error.code) {
case '22001': {
errorDetail = 'String length exceeds the columns maximum length (string_data_right_truncation)';
errorDetail = "String length exceeds the column's maximum length (string_data_right_truncation)";
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,40 @@ import { getAllDataRecords, formatDataRecords } from './records.service.js';
import { createConfigSeeds } from '../../../db/seeders/config.seeder.js';
import type { DataRecord } from '../../../models/Sync.js';
import { generateInsertableJson, createRecords } from './mocks.js';
import { createActivityLog } from '../../activity/activity.service.js';
import type { Environment } from '../../../models/Environment.js';

const environmentName = 'delete-service';

async function getActivity(env: Environment): Promise<number> {
const activityLogId = await createActivityLog({
action: 'full sync',
connection_id: '1234',
environment_id: env.id,
level: 'debug',
provider_config_key: 'test',
start: Date.now(),
success: false,
timestamp: Date.now()
});
if (!activityLogId) {
throw new Error('failed_to_create_activitylogid');
}

return activityLogId;
}

describe('Data delete service integration tests', () => {
let envs: Environment[];
beforeAll(async () => {
await multipleMigrations();
await createConfigSeeds(environmentName);
envs = await createConfigSeeds(environmentName);
});

it('Should insert records properly and retrieve a full record', async () => {
const env = envs[0]!;
const activityLogId = await getActivity(env);

const duplicateRecords = [
{
id: '1',
Expand Down Expand Up @@ -55,8 +79,8 @@ describe('Data delete service integration tests', () => {
'external_id',
nangoConnectionId,
modelName,
1,
1
activityLogId,
env.id
);
expect(success).toBe(true);
expect(error).toBe(undefined);
Expand Down Expand Up @@ -85,6 +109,8 @@ describe('Data delete service integration tests', () => {
});

it('Should take a snapshot of the inserted records', async () => {
const env = envs[0]!;
const activityLogId = await getActivity(env);
const records = generateInsertableJson(100);
const { response, meta } = await createRecords(records, environmentName);
const { response: formattedResults } = response;
Expand All @@ -95,8 +121,8 @@ describe('Data delete service integration tests', () => {
'external_id',
nangoConnectionId,
modelName,
1,
1
activityLogId,
env.id
);
expect(success).toBe(true);
expect(error).toBe(undefined);
Expand All @@ -107,10 +133,12 @@ describe('Data delete service integration tests', () => {
return rest;
});
const snapshotFullRecords = await getFullSnapshotRecords(nangoConnectionId, modelName);
expect(fullRecordsWithoutPendingDelete).toEqual(snapshotFullRecords);
expect(fullRecordsWithoutPendingDelete).toMatchObject(snapshotFullRecords);
});

it('Given a snapshot, the next insert with less records should show as deleted if track deletes is true', async () => {
const env = envs[0]!;
const activityLogId = await getActivity(env);
const records = generateInsertableJson(100);
const { response, meta } = await createRecords(records, environmentName);
const { response: formattedResults } = response;
Expand All @@ -121,8 +149,8 @@ describe('Data delete service integration tests', () => {
'external_id',
nangoConnectionId,
modelName,
1,
1,
activityLogId,
env.id,
true // track_deletes
);
expect(success).toBe(true);
Expand All @@ -137,8 +165,8 @@ describe('Data delete service integration tests', () => {
'external_id',
nangoConnectionId,
modelName,
1,
1,
activityLogId,
env.id,
true // track_deletes
);
expect(slimSuccess).toBe(true);
Expand All @@ -164,6 +192,8 @@ describe('Data delete service integration tests', () => {
});

it('Given a snapshot, the next insert with less records should not show as deleted if track deletes is false', async () => {
const env = envs[0]!;
const activityLogId = await getActivity(env);
const records = generateInsertableJson(100);
const { response, meta } = await createRecords(records, environmentName);
const { response: formattedResults } = response;
Expand All @@ -174,8 +204,8 @@ describe('Data delete service integration tests', () => {
'external_id',
nangoConnectionId,
modelName,
1,
1,
activityLogId,
env.id,
false // track_deletes
);
expect(success).toBe(true);
Expand All @@ -194,8 +224,8 @@ describe('Data delete service integration tests', () => {
'external_id',
nangoConnectionId,
modelName,
1,
1,
activityLogId,
env.id,
false // track_deletes
);
expect(slimSuccess).toBe(true);
Expand All @@ -217,7 +247,10 @@ describe('Data delete service integration tests', () => {

expect(recordResponse?.records?.length).toEqual(0);
});

it('When track deletes is true and an entry is updated it only that record should show as updated when getAllDataRecords is called', async () => {
const env = envs[0]!;
const activityLogId = await getActivity(env);
const records = generateInsertableJson(100);
const { response, meta } = await createRecords(records, environmentName);
const { response: rawRecords } = response;
Expand All @@ -237,16 +270,16 @@ describe('Data delete service integration tests', () => {
'external_id',
nangoConnectionId,
modelName,
1,
1,
activityLogId,
env.id,
true // track_deletes
);
expect(success).toBe(true);
expect(error).toBe(undefined);
await takeSnapshot(nangoConnectionId, modelName);

if (formattedResults) {
// @ts-expect-error
// @ts-expect-error untyped rawRecords
rawRecords[0]!['json']['updatedAt'] = new Date().toISOString();
}

Expand All @@ -266,8 +299,8 @@ describe('Data delete service integration tests', () => {
'external_id',
nangoConnectionId,
modelName,
1,
1,
activityLogId,
env.id,
true // track_deletes
);

Expand Down

0 comments on commit 2a9ddda

Please sign in to comment.