Skip to content

Commit

Permalink
chore: added mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauravudia committed Jan 4, 2024
1 parent 021aeac commit aec875a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 96 deletions.
3 changes: 1 addition & 2 deletions src/cdk/v2/destinations/the_trade_desk/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const SUPPORTED_EVENT_TYPE = 'record';
const ACTION_TYPES = ['insert', 'delete'];
const DATA_PROVIDER_ID = 'rudderstack';
const MAX_REQUEST_SIZE_IN_BYTES = 2500000;

// ref:- https://partner.thetradedesk.com/v3/portal/data/doc/DataEnvironments
const DATA_SERVERS_BASE_ENDPOINTS_MAP = {
Expand All @@ -17,6 +16,6 @@ module.exports = {
SUPPORTED_EVENT_TYPE,
ACTION_TYPES,
DATA_PROVIDER_ID,
MAX_REQUEST_SIZE_IN_BYTES,
MAX_REQUEST_SIZE_IN_BYTES: 2500000,
DATA_SERVERS_BASE_ENDPOINTS_MAP,
};
52 changes: 13 additions & 39 deletions src/cdk/v2/destinations/the_trade_desk/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const lodash = require('lodash');
const CryptoJS = require('crypto-js');
const jsonSize = require('json-size');
const { InstrumentationError, AbortedError } = require('@rudderstack/integrations-lib');
const { BatchUtils } = require('@rudderstack/workflow-engine');
const {
Expand All @@ -10,11 +9,9 @@ const {
removeUndefinedAndNullValues,
handleRtTfSingleEventError,
} = require('../../../../v0/util');
const {
DATA_PROVIDER_ID,
MAX_REQUEST_SIZE_IN_BYTES,
DATA_SERVERS_BASE_ENDPOINTS_MAP,
} = require('./config');
const tradeDeskConfig = require('./config');

const { DATA_PROVIDER_ID, DATA_SERVERS_BASE_ENDPOINTS_MAP } = tradeDeskConfig;

const ttlInMin = (ttl) => parseInt(ttl, 10) * 1440;
const getBaseEndpoint = (dataServer) => DATA_SERVERS_BASE_ENDPOINTS_MAP[dataServer];
Expand All @@ -41,40 +38,12 @@ const responseBuilder = (items, config) => {
return response;
};

const splitItemsBasedOnMaxSizeInBytes = (items, maxSize) => {
if (!items || items.length === 0) return [];

const itemsSize = jsonSize(items);

if (itemsSize <= maxSize) {
return [items];
}

const batches = [];
let currentBatch = [];

items.forEach((item) => {
const itemSize = jsonSize(item);

if (jsonSize(currentBatch) + itemSize <= maxSize) {
currentBatch.push(item);
} else {
batches.push([...currentBatch]);
currentBatch = [item];
}
});

if (currentBatch.length > 0) {
batches.push(currentBatch);
}

return batches;
};

const batchResponseBuilder = (items, config) => {
const response = [];
const itemsChunks = BatchUtils.chunkArrayBySizeAndLength(items, {
maxSizeInBytes: MAX_REQUEST_SIZE_IN_BYTES,
// TODO: use destructuring at the top of file once proper 'mocking' is implemented.
// eslint-disable-next-line unicorn/consistent-destructuring
maxSizeInBytes: tradeDeskConfig.MAX_REQUEST_SIZE_IN_BYTES,
});

itemsChunks.items.forEach((chunk) => {
Expand Down Expand Up @@ -104,8 +73,13 @@ const processRecordInputs = (inputs, destination) => {
TTLInMinutes: action === 'insert' ? ttlInMin(Config.ttlInDays) : 0,
},
];

Object.keys(fields).forEach((id) => {
items.push({ [id]: fields[id], Data: data });
const value = fields[id];
if (value) {
// adding only non empty ID's
items.push({ [id]: value, Data: data });
}
});
} else {
errorResponseList.push(handleRtTfSingleEventError(input, error, {}));

Check warning on line 85 in src/cdk/v2/destinations/the_trade_desk/utils.js

View check run for this annotation

Codecov / codecov/patch

src/cdk/v2/destinations/the_trade_desk/utils.js#L84-L85

Added lines #L84 - L85 were not covered by tests
Expand All @@ -130,4 +104,4 @@ const processRouterDest = (inputs) => {
return respList;
};

module.exports = { getSignatureHeader, splitItemsBasedOnMaxSizeInBytes, processRouterDest };
module.exports = { getSignatureHeader, processRouterDest };
44 changes: 1 addition & 43 deletions src/cdk/v2/destinations/the_trade_desk/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { AbortedError } = require('@rudderstack/integrations-lib');
const { getSignatureHeader, splitItemsBasedOnMaxSizeInBytes } = require('./utils');
const { getSignatureHeader } = require('./utils');

describe('getSignatureHeader', () => {
it('should calculate the signature header for a valid request and secret key', () => {
Expand Down Expand Up @@ -47,45 +47,3 @@ describe('getSignatureHeader', () => {
}).toThrow(AbortedError);
});
});

describe('splitItemsBasedOnMaxSizeInBytes', () => {
it('should return an array with a single element when the total size of the items is less than or equal to the maximum size', () => {
const items = [
{ id: 1, name: 'item1' },
{ id: 2, name: 'item2' },
{ id: 3, name: 'item3' },
];
const maxSize = 100;
const result = splitItemsBasedOnMaxSizeInBytes(items, maxSize);
expect(result).toEqual([items]);
});

it('should split the items into batches of maximum size and return an array of batches', () => {
// size of this items array is 121 bytes
const items = [
{ id: 1, name: 'item1' },
{ id: 2, name: 'item2' },
{ id: 3, name: 'item3' },
{ id: 4, name: 'item4' },
{ id: 5, name: 'item5' },
];
const maxSize = 100;
const result = splitItemsBasedOnMaxSizeInBytes(items, maxSize);
expect(result).toEqual([
[
{ id: 1, name: 'item1' },
{ id: 2, name: 'item2' },
{ id: 3, name: 'item3' },
{ id: 4, name: 'item4' },
],
[{ id: 5, name: 'item5' }],
]);
});

it('should return an empty array when the items array is empty', () => {
const items = [];
const maxSize = 100;
const result = splitItemsBasedOnMaxSizeInBytes(items, maxSize);
expect(result).toEqual([]);
});
});
2 changes: 1 addition & 1 deletion src/features.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@
"INTERCOM": true
},
"supportSourceTransformV1": true,
"supportTransformerProxyV1": true
"supportTransformerProxyV1": false
}
5 changes: 5 additions & 0 deletions test/integrations/destinations/the_trade_desk/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import config from '../../../../src/cdk/v2/destinations/the_trade_desk/config';

export const defaultMockFns = () => {
jest.replaceProperty(config, 'MAX_REQUEST_SIZE_IN_BYTES', 250);
};
37 changes: 26 additions & 11 deletions test/integrations/destinations/the_trade_desk/router/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { overrideDestination } from '../../../testUtils';
import { defaultMockFns } from '../mocks';
import {
destType,
destTypeInUpperCase,
Expand Down Expand Up @@ -43,7 +44,7 @@ export const data = [
action: 'insert',
fields: {
DAID: 'test-daid-2',
UID2: 'test-uid2-2',
UID2: null,
},
channel: 'sources',
context: sampleContext,
Expand Down Expand Up @@ -106,15 +107,6 @@ export const data = [
},
],
},
{
UID2: 'test-uid2-2',
Data: [
{
Name: segmentName,
TTLInMinutes: 43200,
},
],
},
],
},
JSON_ARRAY: {},
Expand All @@ -140,10 +132,12 @@ export const data = [
},
},
},
mockFns: defaultMockFns,
},
{
name: destType,
description: 'Add/Remove IDs to/from the segment',
description:
'Add/Remove IDs to/from the segment and split into multiple requests based on size',
feature: 'router',
module: 'destination',
version: 'v0',
Expand Down Expand Up @@ -237,6 +231,26 @@ export const data = [
},
],
},
],
},
JSON_ARRAY: {},
XML: {},
FORM: {},
},
files: {},
},
{
version: '1',
type: 'REST',
method: 'POST',
endpoint: 'https://sin-data.adsrvr.org/data/advertiser',
headers: {},
params: {},
body: {
JSON: {
DataProviderId: dataProviderId,
AdvertiserId: advertiserId,
Items: [
{
UID2: 'test-uid2-2',
Data: [
Expand Down Expand Up @@ -271,6 +285,7 @@ export const data = [
},
},
},
mockFns: defaultMockFns,
},
{
name: destType,
Expand Down

0 comments on commit aec875a

Please sign in to comment.