Skip to content

Commit

Permalink
chore: api test first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandra shekar Varkala committed Feb 12, 2024
1 parent 69c8348 commit 8ae49ce
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 289 deletions.
70 changes: 70 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@pyroscope/nodejs": "^0.2.6",
"@rudderstack/integrations-lib": "^0.2.2",
"@rudderstack/workflow-engine": "^0.6.9",
"@shopify/jest-koa-mocks": "^5.1.1",
"ajv": "^8.12.0",
"ajv-draft-04": "^1.0.0",
"ajv-formats": "^2.1.1",
Expand Down
40 changes: 40 additions & 0 deletions src/services/destination/nativeIntegration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { NativeIntegrationDestinationService } from './nativeIntegration';
import { DestinationPostTransformationService } from './postTransformation';
import {
ProcessorTransformationRequest,
ProcessorTransformationOutput,
ProcessorTransformationResponse,
} from '../../types/index';

jest.mock('../../v0/destinations/__rudder_test__/transform');
let __rudder_test__ = require('../../v0/destinations/__rudder_test__/transform');

describe('PostTransformation Service', () => {
test('should handleProcessorTransformFailureEvents', async () => {
const destType = '__rudder_test__';
const version = 'v0';
const requestMetadata = {};
const event = { message: { a: 'b' } } as ProcessorTransformationRequest;
const events: ProcessorTransformationRequest[] = [event];
const expected: ProcessorTransformationOutput[] = [
{
version: 'v0',
} as ProcessorTransformationOutput,
];

const expectedResp: ProcessorTransformationResponse[] = [];
__rudder_test__.process = jest.fn(() => expected);
const postTransformSpy = jest
.spyOn(DestinationPostTransformationService, 'handleProcessorTransformSucessEvents')
.mockImplementation((e, t, d) => {
console.log('handleProcessorTransformSucessEvents xyz');
return expectedResp;
});

const service = new NativeIntegrationDestinationService();
service.doProcessorTransformation(events, destType, version, requestMetadata);

//expect(postTransformSpy).toHaveBeenCalledTimes(1);
//expect(postTransformSpy).toHaveBeenCalledWith(event, expected, __rudder_test__);
});
});
23 changes: 23 additions & 0 deletions src/services/destination/postTransformation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createMockContext } from '@shopify/jest-koa-mocks';
import { MetaTransferObject, ProcessorTransformationRequest } from '../../types/index';
import { DestinationPostTransformationService } from './postTransformation';
import { ProcessorTransformationResponse } from '../../types';

describe('PostTransformation Service', () => {
test('should handleProcessorTransformFailureEvents', async () => {
const e = new Error('test error');
const metaTo = { errorContext: 'error Context' } as MetaTransferObject;
const resp = DestinationPostTransformationService.handleProcessorTransformFailureEvents(
e,
metaTo,
);

const expected = {
statusCode: 500,
error: 'test error',
statTags: { errorCategory: 'transformation' },
} as ProcessorTransformationResponse;

expect(resp).toEqual(expected);
});
});
23 changes: 23 additions & 0 deletions src/services/destination/preTransformation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createMockContext } from '@shopify/jest-koa-mocks';
import { ProcessorTransformationRequest } from '../../types/index';
import { DestinationPreTransformationService } from './preTransformation';

describe('PreTransformation Service', () => {
test('should enhance events with query params', async () => {
const ctx = createMockContext();
ctx.request.query = { cycle: 'true', x: 'y' };

const events: ProcessorTransformationRequest[] = [
{ message: { a: 'b' } } as ProcessorTransformationRequest,
];
const expected: ProcessorTransformationRequest[] = [
{
message: { a: 'b' },
request: { query: { cycle: 'true', x: 'y' } },
} as ProcessorTransformationRequest,
];

const resp = DestinationPreTransformationService.preProcess(events, ctx);
expect(resp).toEqual(expected);
});
});
61 changes: 61 additions & 0 deletions src/v0/destinations/__rudder_test__/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable no-restricted-syntax */
const groupBy = require('lodash/groupBy');
const lodash = require('lodash');
const {
removeUndefinedAndNullValues,
getSuccessRespEvents,
getErrorRespEvents,
} = require('../../util');


const batch = (destEvents) => {
const respList = [];
if (!Array.isArray(destEvents) || destEvents.length <= 0) {
const respEvents = getErrorRespEvents(null, 400, 'Invalid event array');
return [respEvents];
}

// Grouping the events by topic
const groupedEvents = groupBy(destEvents, (event) => event.message.topic);

// Creating a batched request for each topic
// we are grouping the events based on topics
// and creating a batched request for each topic
// example: input events = [{event1,topic1},{event2,topic1},{event3,topic2}]
// out from transformer: {batchedRequest:[{event1},{event2}]}, {batchedRequest:[{event3}]} (2 multilexed responses)
for (const events of Object.values(groupedEvents)) {
const response = {
batchedRequest: events.map((event) => event.message),
metadata: events.map((event) => event.metadata),
destination: events[0].destination,
};
respList.push(
getSuccessRespEvents(response.batchedRequest, response.metadata, response.destination, true),
);
}
return respList;
};

const process = (event) => {
console.log("called __rudder_test__ transform.js");

Check warning on line 40 in src/v0/destinations/__rudder_test__/transform.js

View workflow job for this annotation

GitHub Actions / Code Coverage

Unexpected console statement

const { message, destination } = event;

const userId = message.userId || message.anonymousId;
const outputEvent = {
message,
userId,
destination
};
return removeUndefinedAndNullValues(outputEvent);
};
const processRouterDest = async (inputs, reqMetadata) => {

Check failure on line 52 in src/v0/destinations/__rudder_test__/transform.js

View workflow job for this annotation

GitHub Actions / Code Coverage

'reqMetadata' is defined but never used
// group events by userId or anonymousId and then call process
const groupedInputs = lodash.groupBy(
inputs,
(input) => input.message.userId || input.message.anonymousId,
);
return batch(groupedInputs);
};

module.exports = { process, batch, processRouterDest };
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@
}
}
],
"destType": "am"
"destType": "__rudder_test__"
},
"output": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@
}
}
],
"destType": "am"
"destType": "__rudder_test__"
},
"output": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@
}
}
],
"destType": "pinterest_tag"
"destType": "__rudder_test__"
},
"output": {
"output": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@
}
}
],
"destType": "webhook"
"destType": "__rudder_test__"
},
"output": {
"output": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@
}
}
],
"destType": "webhook"
"destType": "__rudder_test__"
},
"output": {
"output": [
Expand Down
Loading

0 comments on commit 8ae49ce

Please sign in to comment.