Skip to content

Commit

Permalink
chore: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandra shekar Varkala committed Feb 13, 2024
1 parent 65a816d commit e2a0938
Showing 1 changed file with 132 additions and 121 deletions.
253 changes: 132 additions & 121 deletions test/apitests/service.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,138 @@ describe('features tests', () => {
});
});

describe('Api tests with a mock destination', () => {
test('(mock destination) Processor transformation - success scenario with single event', async () => {
const destType = '__rudder_test__';
const version = 'v0';
const getInputData = () => {
return [
{ message: { a: 'b1' }, destination: {}, metadata: { jobId: 1 } },
{ message: { a: 'b2' }, destination: {}, metadata: { jobId: 2 } },
];
};
const tevent = { version: 'v0', endpoint: 'http://abc' };

const getDestHandlerSpy = jest
.spyOn(FetchHandler, 'getDestHandler')
.mockImplementationOnce((d, v) => {
expect(d).toEqual(destType);
expect(v).toEqual(version);
return {
process: jest.fn(() => {
return tevent;
}),
};
});

const expected = [
{
output: { version: 'v0', endpoint: 'http://abc', userId: '' },
metadata: { jobId: 1 },
statusCode: 200,
},
{
output: { version: 'v0', endpoint: 'http://abc', userId: '' },
metadata: { jobId: 2 },
statusCode: 200,
},
];

const response = await request(server)
.post('/v0/destinations/__rudder_test__')
.set('Accept', 'application/json')
.send(getInputData());

expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual(expected);
expect(getDestHandlerSpy).toHaveBeenCalledTimes(1);
});

test('(mock destination) Batching - success', async () => {
const destType = '__rudder_test__';
const version = 'v0';
const getBatchInputData = () => {
return {
input: [
{ message: { a: 'b1' }, destination: {}, metadata: { jobId: 1 } },
{ message: { a: 'b2' }, destination: {}, metadata: { jobId: 2 } },
],
destType: destType,
};
};
const tevent = [
{
batchedRequest: { version: 'v0', endpoint: 'http://abc' },
metadata: [{ jobId: 1 }, { jobId: 2 }],
statusCode: 200,
},
];

const getDestHandlerSpy = jest
.spyOn(FetchHandler, 'getDestHandler')
.mockImplementationOnce((d, v) => {
expect(d).toEqual(destType);
expect(v).toEqual(version);
return {
batch: jest.fn(() => {
return tevent;
}),
};
});

const response = await request(server)
.post('/batch')
.set('Accept', 'application/json')
.send(getBatchInputData());

expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual(tevent);
expect(getDestHandlerSpy).toHaveBeenCalledTimes(1);
});

test('(mock destination) Router transformation - success', async () => {
const destType = '__rudder_test__';
const version = 'v0';
const getRouterTransformInputData = () => {
return {
input: [
{ message: { a: 'b1' }, destination: {}, metadata: { jobId: 1 } },
{ message: { a: 'b2' }, destination: {}, metadata: { jobId: 2 } },
],
destType: destType,
};
};
const tevent = [
{
batchedRequest: { version: 'v0', endpoint: 'http://abc' },
metadata: [{ jobId: 1 }, { jobId: 2 }],
statusCode: 200,
},
];

const getDestHandlerSpy = jest
.spyOn(FetchHandler, 'getDestHandler')
.mockImplementationOnce((d, v) => {
expect(d).toEqual(destType);
expect(v).toEqual(version);
return {
processRouterDest: jest.fn(() => {
return tevent;
}),
};
});

const response = await request(server)
.post('/routerTransform')
.set('Accept', 'application/json')
.send(getRouterTransformInputData());

expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual({ output: tevent });
expect(getDestHandlerSpy).toHaveBeenCalledTimes(1);
});
});

describe('Destination api tests', () => {
describe('Processor transform tests', () => {
test('(webhook) success scenario with single event', async () => {
Expand Down Expand Up @@ -154,49 +286,6 @@ describe('Destination api tests', () => {
expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual(data.output);
});

test('(mock destination) success scenario with single event', async () => {
const destType = '__rudder_test__';
const version = 'v0';
const getInputData = () => {
return [
{ message: { a: 'b1' }, destination: {}, metadata: { jobId: 1 } },
{ message: { a: 'b2' }, destination: {}, metadata: { jobId: 2 } },
];
};
const tevent = { version: 'v0', endpoint: 'http://abc' };

FetchHandler.getDestHandler = jest.fn().mockImplementation((d, v) => {
expect(d).toEqual(destType);
expect(v).toEqual(version);
return {
process: jest.fn(() => {
return tevent;
}),
};
});

const expected = [
{
output: { version: 'v0', endpoint: 'http://abc', userId: '' },
metadata: { jobId: 1 },
statusCode: 200,
},
{
output: { version: 'v0', endpoint: 'http://abc', userId: '' },
metadata: { jobId: 2 },
statusCode: 200,
},
];

const response = await request(server)
.post('/v0/destinations/__rudder_test__')
.set('Accept', 'application/json')
.send(getInputData());

expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual(expected);
});
});

describe('Batch transform tests', () => {
Expand All @@ -219,45 +308,6 @@ describe('Destination api tests', () => {
expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual(data.output);
});

test('(mock destination) successful batch transform', async () => {
const destType = '__rudder_test__';
const version = 'v0';
const getBatchInputData = () => {
return {
input: [
{ message: { a: 'b1' }, destination: {}, metadata: { jobId: 1 } },
{ message: { a: 'b2' }, destination: {}, metadata: { jobId: 2 } },
],
destType: destType,
};
};
const tevent = [
{
batchedRequest: { version: 'v0', endpoint: 'http://abc' },
metadata: [{ jobId: 1 }, { jobId: 2 }],
statusCode: 200,
},
];

FetchHandler.getDestHandler = jest.fn().mockImplementation((d, v) => {
expect(d).toEqual(destType);
expect(v).toEqual(version);
return {
batch: jest.fn(() => {
return tevent;
}),
};
});

const response = await request(server)
.post('/batch')
.set('Accept', 'application/json')
.send(getBatchInputData());

expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual(tevent);
});
});

describe('Router transform tests', () => {
Expand Down Expand Up @@ -290,45 +340,6 @@ describe('Destination api tests', () => {
expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual(data.output);
});

test('(mock destination) successful router transform', async () => {
const destType = '__rudder_test__';
const version = 'v0';
const getRouterTransformInputData = () => {
return {
input: [
{ message: { a: 'b1' }, destination: {}, metadata: { jobId: 1 } },
{ message: { a: 'b2' }, destination: {}, metadata: { jobId: 2 } },
],
destType: destType,
};
};
const tevent = [
{
batchedRequest: { version: 'v0', endpoint: 'http://abc' },
metadata: [{ jobId: 1 }, { jobId: 2 }],
statusCode: 200,
},
];

FetchHandler.getDestHandler = jest.fn().mockImplementation((d, v) => {
expect(d).toEqual(destType);
expect(v).toEqual(version);
return {
processRouterDest: jest.fn(() => {
return tevent;
}),
};
});

const response = await request(server)
.post('/routerTransform')
.set('Accept', 'application/json')
.send(getRouterTransformInputData());

expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual({ output: tevent });
});
});
});

Expand Down

0 comments on commit e2a0938

Please sign in to comment.