Skip to content

Commit

Permalink
one shouldn't mock the database
Browse files Browse the repository at this point in the history
  • Loading branch information
TBonnin committed Mar 18, 2024
1 parent cbb7fde commit 2018920
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 204 deletions.
24 changes: 0 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

207 changes: 207 additions & 0 deletions packages/persist/lib/server.integration.test.ts
@@ -0,0 +1,207 @@
import { expect, describe, it, beforeAll, afterAll } from 'vitest';
import { server } from './server.js';
import fetch from 'node-fetch';
import {
multipleMigrations,
createActivityLog,
environmentService,
connectionService,
createSync,
createSyncJob,
AuthCredentials,
Connection,
Sync,
Job as SyncJob,
SyncType,
SyncStatus,
Environment,
db
} from '@nangohq/shared';

describe('Persist API', () => {
const port = 3096;
const serverUrl = `http://localhost:${port}`;
let seed: {
env: Environment;
activityLogId: number;
connection: Connection;
sync: Sync;
syncJob: SyncJob;
};

beforeAll(async () => {
await multipleMigrations();
seed = await initDb();
server.listen(port);
});

afterAll(async () => {
await clearDb();
});

it('should server /health', async () => {
const response = await fetch(`${serverUrl}/health`);
expect(response.status).toEqual(200);
expect(await response.json()).toEqual({ status: 'ok' });
});

it('should log', async () => {
const response = await fetch(`${serverUrl}/environment/${seed.env.id}/log`, {
method: 'POST',
body: JSON.stringify({ activityLogId: seed.activityLogId, level: 'info', msg: 'Hello, world!' }),
headers: {
'Content-Type': 'application/json'
}
});
expect(response.status).toEqual(201);
});

describe('save records', () => {
it('should error if no records', async () => {
const response = await fetch(
`${serverUrl}/environment/${seed.env.id}/connection/${seed.connection.id}/sync/${seed.sync.id}/job/${seed.syncJob.id}/records`,
{
method: 'POST',
body: JSON.stringify({
model: 'MyModel',
records: [],
providerConfigKey: seed.connection.provider_config_key,
connectionId: seed.connection.connection_id,
lastSyncDate: new Date(),
trackDeletes: false,
softDelete: true
}),
headers: {
'Content-Type': 'application/json'
}
}
);
expect(response.status).toEqual(400);
const respBody = (await response.json()) as any[];
expect(respBody[0]['errors']['issues'][0]['path'][0]).toEqual('records');
expect(respBody[0]['errors']['issues'][0]['message']).toEqual('Array must contain at least 1 element(s)');
});

it('should save records', async () => {
const model = 'MyModel';
const records = [
{ id: 1, name: 'r1' },
{ id: 2, name: 'r2' }
];
const response = await fetch(
`${serverUrl}/environment/${seed.env.id}/connection/${seed.connection.id}/sync/${seed.sync.id}/job/${seed.syncJob.id}/records`,
{
method: 'POST',
body: JSON.stringify({
model,
records: records,
providerConfigKey: seed.connection.provider_config_key,
connectionId: seed.connection.connection_id,
activityLogId: seed.activityLogId,
lastSyncDate: new Date(),
trackDeletes: false
}),
headers: {
'Content-Type': 'application/json'
}
}
);
expect(response.status).toEqual(201);
});
});

it('should delete records ', async () => {
const model = 'MyModel';
const records = [
{ id: 1, name: 'r1' },
{ id: 2, name: 'r2' }
];
const response = await fetch(
`${serverUrl}/environment/${seed.env.id}/connection/${seed.connection.id}/sync/${seed.sync.id}/job/${seed.syncJob.id}/records`,
{
method: 'DELETE',
body: JSON.stringify({
model,
records: records,
providerConfigKey: seed.connection.provider_config_key,
connectionId: seed.connection.connection_id,
activityLogId: seed.activityLogId,
lastSyncDate: new Date(),
trackDeletes: false
}),
headers: {
'Content-Type': 'application/json'
}
}
);
expect(response.status).toEqual(201);
});

it('should update records ', async () => {
const model = 'MyModel';
const records = [
{ id: 1, name: 'new1' },
{ id: 2, name: 'new2' }
];
const response = await fetch(
`${serverUrl}/environment/${seed.env.id}/connection/${seed.connection.id}/sync/${seed.sync.id}/job/${seed.syncJob.id}/records`,
{
method: 'PUT',
body: JSON.stringify({
model,
records: records,
providerConfigKey: seed.connection.provider_config_key,
connectionId: seed.connection.connection_id,
activityLogId: seed.activityLogId,
lastSyncDate: new Date(),
trackDeletes: false,
softDelete: true
}),
headers: {
'Content-Type': 'application/json'
}
}
);
expect(response.status).toEqual(201);
});
});

const initDb = async () => {
const env = await environmentService.createEnvironment(0, 'testEnv');
if (!env) throw new Error('Environment not created');
const activityLogId = await createActivityLog({
environment_id: env.id,
level: 'info',
action: 'sync',
success: null,
timestamp: Date.now(),
start: Date.now(),
connection_id: null,
provider_config_key: null
});
if (!activityLogId) throw new Error('Activity log not created');
const connectionRes = await connectionService.upsertConnection(`conn-test`, `provider-test`, 'google', {} as AuthCredentials, {}, env.id, 0);
const connectionId = connectionRes[0]?.id;
if (!connectionId) throw new Error('Connection not created');

const connection = (await connectionService.getConnectionById(connectionId)) as Connection;
if (!connection) throw new Error('Connection not found');

const sync = await createSync(connectionId, 'sync-test');
if (!sync?.id) throw new Error('Sync not created');

const syncJob = (await createSyncJob(sync.id, SyncType.INITIAL, SyncStatus.RUNNING, `job-test`, connection)) as SyncJob;
if (!syncJob) throw new Error('Sync job not created');

return {
env,
activityLogId,
connection,
sync,
syncJob
};
};

const clearDb = async () => {
await db.knex.raw(`DROP SCHEMA nango CASCADE`);
};

0 comments on commit 2018920

Please sign in to comment.