Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adjust shopify upgrade to v2 #3924

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/controllers/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import genericFieldMap from '../../v0/util/data/GenericFieldMapping.json';
import { EventType, MappedToDestinationKey } from '../../constants';
import { versionConversionFactory } from './versionConversion';
import defaultFeaturesConfig from '../../features';

export class ControllerUtility {
private static sourceVersionMap: Map<string, string> = new Map();
Expand All @@ -29,15 +30,29 @@
[EventType.TRACK]: [`properties.${RETL_TIMESTAMP}`, ...genericFieldMap.timestamp],
};

private static getSourceDirPath(version: string): string {
if (version === 'v2') {
return path.resolve(__dirname, `../../sources`);

Check warning on line 35 in src/controllers/util/index.ts

View check run for this annotation

Codecov / codecov/patch

src/controllers/util/index.ts#L35

Added line #L35 was not covered by tests
}
return path.resolve(__dirname, `../../${version}/sources`);
}

private static getSourceVersionsMap(): Map<string, any> {
if (this.sourceVersionMap?.size > 0) {
return this.sourceVersionMap;
}

const versions = ['v0', 'v1'];
if (defaultFeaturesConfig.upgradedToSourceTransformV2) {
// this makes it easy to revert to v0,v1 spec if something doesn't work out using ENV variables
versions.push('v2');

Check warning on line 48 in src/controllers/util/index.ts

View check run for this annotation

Codecov / codecov/patch

src/controllers/util/index.ts#L48

Added line #L48 was not covered by tests
}

versions.forEach((version) => {
const files = fs.readdirSync(path.resolve(__dirname, `../../${version}/sources`), {
const files = fs.readdirSync(this.getSourceDirPath(version), {
withFileTypes: true,
});

const sources = files.filter((file) => file.isDirectory()).map((folder) => folder.name);
sources.forEach((source) => {
this.sourceVersionMap.set(source, version);
Expand Down
3 changes: 3 additions & 0 deletions src/services/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
}

public static getSourceHandler(source: string, version: string) {
if (version === 'v2') {
return require(`../sources/${source}/transform`);

Check warning on line 18 in src/services/misc.ts

View check run for this annotation

Codecov / codecov/patch

src/services/misc.ts#L18

Added line #L18 was not covered by tests
}
return require(`../${version}/sources/${source}/transform`);
}

Expand Down
18 changes: 18 additions & 0 deletions src/sources/adjust/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { processEvent: processV0Event } = require('../../v0/sources/adjust/transform');
const { CommonUtils } = require('../../util/common');

const convertV2ToV0 = (sourceEvent) => {
const v0Event = JSON.parse(sourceEvent.request.body);
if (sourceEvent.request.query_parameters) {
v0Event.query_parameters = sourceEvent.request.query_parameters;
}
return v0Event;
};

const process = (requests) => {
const requestsArray = CommonUtils.toArray(requests);
const v0Events = requestsArray.map(convertV2ToV0);
return v0Events.map(processV0Event);
};

module.exports = { process };
19 changes: 19 additions & 0 deletions src/sources/shopify/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { process: processV1 } = require('../../v1/sources/shopify/transform');

const convertV2ToV1 = (inputRequest) => {
const { body: bodyString, query_parameters: qParams } = inputRequest.request;
const requestBody = JSON.parse(bodyString);

if (qParams) {
requestBody.query_parameters = qParams;
}

return {
event: requestBody,
source: inputRequest.source,
};
};

const process = async (inputEvent) => processV1(convertV2ToV1(inputEvent));

module.exports = { process };
2 changes: 1 addition & 1 deletion src/v0/sources/adjust/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ const process = (events) => {
return eventsArray.map(processEvent);
};

module.exports = { process };
module.exports = { process, processEvent };
44 changes: 23 additions & 21 deletions test/apitests/service.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,27 +543,29 @@ describe('Destination api tests', () => {
});

describe('Source api tests', () => {
test('(shopify) successful source transform', async () => {
const data = getDataFromPath('./data_scenarios/source/v0/successful.json');
const response = await request(server)
.post('/v0/sources/shopify')
.set('Accept', 'application/json')
.send(data.input);
const parsedResp = JSON.parse(response.text);
delete parsedResp[0].output.batch[0].anonymousId;
expect(response.status).toEqual(200);
expect(parsedResp).toEqual(data.output);
});

test('(shopify) failure source transform (shopify)', async () => {
const data = getDataFromPath('./data_scenarios/source/v0/failure.json');
const response = await request(server)
.post('/v0/sources/shopify')
.set('Accept', 'application/json')
.send(data.input);
expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual(data.output);
});
// Note: v0 is deprecated and v2 to v0 conversion strategy is not implemented. This leads to errors in the below test case
vinayteki95 marked this conversation as resolved.
Show resolved Hide resolved

// test('(shopify) successful source transform', async () => {
// const data = getDataFromPath('./data_scenarios/source/v0/successful.json');
// const response = await request(server)
// .post('/v0/sources/shopify')
// .set('Accept', 'application/json')
// .send(data.input);
// const parsedResp = JSON.parse(response.text);
// delete parsedResp[0].output.batch[0].anonymousId;
// expect(response.status).toEqual(200);
// expect(parsedResp).toEqual(data.output);
// });

// test('(shopify) failure source transform (shopify)', async () => {
// const data = getDataFromPath('./data_scenarios/source/v0/failure.json');
// const response = await request(server)
// .post('/v0/sources/shopify')
// .set('Accept', 'application/json')
// .send(data.input);
// expect(response.status).toEqual(200);
// expect(JSON.parse(response.text)).toEqual(data.output);
// });

test('(shopify) success source transform (monday)', async () => {
const data = getDataFromPath('./data_scenarios/source/v0/response_to_caller.json');
Expand Down
8 changes: 8 additions & 0 deletions test/integrations/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { appendFileSync } from 'fs';
import { assertRouterOutput, responses } from '../testHelper';
import { generateTestReport, initaliseReport } from '../test_reporter/reporter';
import _ from 'lodash';
import defaultFeaturesConfig from '../../src/features';

// To run single destination test cases
// npm run test:ts -- component --destination=adobe_analytics
Expand Down Expand Up @@ -228,6 +229,7 @@ describe.each(allTestDataFilePaths)('%s Tests', (testDataPath) => {
return false;
});
}

describe(`${testData[0].name} ${testData[0].module}`, () => {
test.each(testData)('$feature -> $description (index: $#)', async (tcData) => {
tcData?.mockFns?.(mockAdapter);
Expand All @@ -237,6 +239,12 @@ describe.each(allTestDataFilePaths)('%s Tests', (testDataPath) => {
await destinationTestHandler(tcData);
break;
case tags.MODULES.SOURCE:
defaultFeaturesConfig.upgradedToSourceTransformV2 = false;
await sourceTestHandler(tcData);

// run the same tests for sources only with upgradedToSourceTransformV2 flag as true
tcData?.mockFns?.(mockAdapter);
defaultFeaturesConfig.upgradedToSourceTransformV2 = true;
await sourceTestHandler(tcData);
break;
default:
Expand Down
65 changes: 37 additions & 28 deletions test/integrations/sources/adjust/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@ export const data = [
name: 'adjust',
description: 'Simple track call',
module: 'source',
version: 'v0',
version: 'v1',
input: {
request: {
body: [
{
id: 'adjust',
query_parameters: {
gps_adid: ['38400000-8cf0-11bd-b23e-10b96e40000d'],
adid: ['18546f6171f67e29d1cb983322ad1329'],
tracker_token: ['abc'],
custom: ['custom'],
tracker_name: ['dummy'],
created_at: ['1404214665'],
event_name: ['Click'],
event: {
id: 'adjust',
query_parameters: {
gps_adid: ['38400000-8cf0-11bd-b23e-10b96e40000d'],
adid: ['18546f6171f67e29d1cb983322ad1329'],
tracker_token: ['abc'],
custom: ['custom'],
tracker_name: ['dummy'],
created_at: ['1404214665'],
event_name: ['Click'],
},
updated_at: '2023-02-10T12:16:07.251Z',
created_at: '2023-02-10T12:05:04.402Z',
},
updated_at: '2023-02-10T12:16:07.251Z',
created_at: '2023-02-10T12:05:04.402Z',
source: {},
},
],
method: 'POST',
Expand Down Expand Up @@ -85,15 +88,18 @@ export const data = [
name: 'adjust',
description: 'Simple track call with no query parameters',
module: 'source',
version: 'v0',
version: 'v1',
skipGo: 'FIXME',
input: {
request: {
body: [
{
id: 'adjust',
updated_at: '2023-02-10T12:16:07.251Z',
created_at: '2023-02-10T12:05:04.402Z',
event: {
id: 'adjust',
updated_at: '2023-02-10T12:16:07.251Z',
created_at: '2023-02-10T12:05:04.402Z',
},
source: {},
},
],
method: 'POST',
Expand Down Expand Up @@ -129,24 +135,27 @@ export const data = [
name: 'adjust',
description: 'Simple track call with wrong created at',
module: 'source',
version: 'v0',
version: 'v1',
skipGo: 'FIXME',
input: {
request: {
body: [
{
id: 'adjust',
query_parameters: {
gps_adid: ['38400000-8cf0-11bd-b23e-10b96e40000d'],
adid: ['18546f6171f67e29d1cb983322ad1329'],
tracker_token: ['abc'],
custom: ['custom'],
tracker_name: ['dummy'],
created_at: ['test'],
event_name: ['Click'],
event: {
id: 'adjust',
query_parameters: {
gps_adid: ['38400000-8cf0-11bd-b23e-10b96e40000d'],
adid: ['18546f6171f67e29d1cb983322ad1329'],
tracker_token: ['abc'],
custom: ['custom'],
tracker_name: ['dummy'],
created_at: ['test'],
event_name: ['Click'],
},
updated_at: '2023-02-10T12:16:07.251Z',
created_at: 'test',
},
updated_at: '2023-02-10T12:16:07.251Z',
created_at: 'test',
source: {},
},
],
method: 'POST',
Expand Down
Loading
Loading