Skip to content

Commit

Permalink
chore: contract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandra shekar Varkala committed Aug 15, 2023
1 parent 8956ba2 commit cbe8872
Show file tree
Hide file tree
Showing 2 changed files with 784 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createHttpTerminator } from 'http-terminator';
import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import { applicationRoutes } from '../../src/routes';
import min from 'lodash/min';

let server: any;
const OLD_ENV = process.env;
Expand Down Expand Up @@ -95,14 +96,72 @@ const integrations = {
};

const features = getDataFromPath('../../src/features.json');
const allIntegrations: string[] = [];
let allIntegrations: string[] = [];
Object.keys(features.routerTransform).forEach((feature) => {
allIntegrations.push(integrations[feature]);
});

console.log(allIntegrations);

//Only google sheets failing. Rest all passing.
const assertRouterOutput = (routerOutput, inputData) => {
const returnedJobids = {};
routerOutput.forEach((outEvent) => {
//Assert that metadata is present and is an array
const metadata = outEvent.metadata;
expect(Array.isArray(metadata)).toEqual(true);

//Assert that statusCode is present and is a number between 200 and 600
const statusCode = outEvent.statusCode;
expect(statusCode).toBeDefined();
expect(typeof statusCode === 'number').toEqual(true);
const validStatusCode = statusCode >= 200 && statusCode < 600;
expect(validStatusCode).toEqual(true);

//Assert that every job_id in the input is present in the output one and only one time.
metadata.forEach((meta) => {
const jobId = meta.jobId;
expect(returnedJobids[jobId]).toBeUndefined();
returnedJobids[jobId] = true;
});
});

const inputJobids = {};
inputData.input.forEach((input) => {
const jobId = input.metadata.jobId;
inputJobids[jobId] = true;
});

expect(returnedJobids).toEqual(inputJobids);

if (inputData.order) {
routerOutput.sort((a, b) => {
const aMin = min(a.metadata.map((meta) => meta.jobId));
const bMin = min(b.metadata.map((meta) => meta.jobId));
return aMin - bMin;
});

let userIdJobIdMap = {};
routerOutput.forEach((outEvent) => {
const metadata = outEvent.metadata;
metadata.forEach((meta) => {
const jobId = meta.jobId;
const userId = meta.userId;
let arr = userIdJobIdMap[userId] || [];
arr.push(jobId);
userIdJobIdMap[userId] = arr;
});
});

//The jobids for a user should be in order. If not, there is an issue.
Object.keys(userIdJobIdMap).forEach((userId) => {
const jobIds = userIdJobIdMap[userId];
for (let i = 0; i < jobIds.length - 1; i++) {
expect(jobIds[i] < jobIds[i + 1]).toEqual(true);
}
});
}
};

describe('Router transform tests', () => {
allIntegrations.forEach((integration) => {
const data = getDataFromPath('./data/generic_router_input.json');
Expand All @@ -115,46 +174,18 @@ describe('Router transform tests', () => {
.set('Accept', 'application/json')
.send(inputData);
expect(response.status).toEqual(200);

const routerOutput = JSON.parse(response.text).output;

const returnedJobids = {};
routerOutput.forEach((outEvent) => {
console.log(outEvent);

//Assert that metadata is present and is an array
const metadata = outEvent.metadata;
expect(Array.isArray(metadata)).toEqual(true);

//Assert that statusCode is present and is a number between 200 and 600
const statusCode = outEvent.statusCode;
expect(statusCode).toBeDefined();
expect(typeof statusCode === 'number').toEqual(true);
const validStatusCode = statusCode >= 200 && statusCode < 600;
expect(validStatusCode).toEqual(true);

//Assert that every job_id in the input is present in the output one and only one time.
metadata.forEach((meta) => {
const jobId = meta.jobId;
console.log(jobId);
expect(returnedJobids[jobId]).toBeUndefined();
returnedJobids[jobId] = true;
});
});

const inputJobids = {};
inputData.input.forEach((input) => {
const jobId = input.metadata.jobId;
inputJobids[jobId] = true;
});

expect(returnedJobids).toEqual(inputJobids);
assertRouterOutput(routerOutput, inputData);
});
});
});
});

const batchIntegrations = ['am', 'kafka'];
describe('Batch tests', () => {
allIntegrations.forEach((integration) => {
batchIntegrations.forEach((integration) => {
const data = getDataFromPath('./data/generic_router_input.json');
data.input.forEach((inputData) => {
test(`${integration} batch`, async () => {
Expand All @@ -165,39 +196,10 @@ describe('Batch tests', () => {
.set('Accept', 'application/json')
.send(inputData);
expect(response.status).toEqual(200);
const routerOutput = JSON.parse(response.text).output;

const returnedJobids = {};
routerOutput.forEach((outEvent) => {
console.log(outEvent);

//Assert that metadata is present and is an array
const metadata = outEvent.metadata;
expect(Array.isArray(metadata)).toEqual(true);

//Assert that statusCode is present and is a number between 200 and 600
const statusCode = outEvent.statusCode;
expect(statusCode).toBeDefined();
expect(typeof statusCode === 'number').toEqual(true);
const validStatusCode = statusCode >= 200 && statusCode < 600;
expect(validStatusCode).toEqual(true);

//Assert that every job_id in the input is present in the output one and only one time.
metadata.forEach((meta) => {
const jobId = meta.jobId;
console.log(jobId);
expect(returnedJobids[jobId]).toBeUndefined();
returnedJobids[jobId] = true;
});
});

const inputJobids = {};
inputData.input.forEach((input) => {
const jobId = input.metadata.jobId;
inputJobids[jobId] = true;
});

expect(returnedJobids).toEqual(inputJobids);
const routerOutput = JSON.parse(response.text);

assertRouterOutput(routerOutput, inputData);
});
});
});
Expand Down
Loading

0 comments on commit cbe8872

Please sign in to comment.