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

fix: event fix and added utility #3142

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/v0/destinations/adobe_analytics/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
getIntegrationsObj,
removeUndefinedAndNullValues,
simpleProcessRouterDest,
validateEventAndLowerCaseConversion,
} = require('../../util');

const {
Expand Down Expand Up @@ -307,7 +308,7 @@ const processTrackEvent = (message, adobeEventName, destinationConfig, extras =
destinationConfig;
const { event: rawMessageEvent, properties } = message;
const { overrideEventString, overrideProductString } = properties;
const event = rawMessageEvent.toLowerCase();
const event = validateEventAndLowerCaseConversion(rawMessageEvent, true, true);
const adobeEventArr = adobeEventName ? adobeEventName.split(',') : [];
// adobeEventArr is an array of events which is defined as
// ["eventName", "mapped Adobe Event=mapped merchproperty's value", "mapped Adobe Event=mapped merchproperty's value", . . .]
Expand Down
20 changes: 20 additions & 0 deletions src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,25 @@
return combineBatches(combineBatches(inputBatches));
};

/**
* This function validates the event and return it as string.
* @param {*} isMandatory The event is a required field.
* @param {*} convertToLowerCase The event should be converted to lower-case.
* @returns {string} Event name converted to string.
*/
const validateEventAndLowerCaseConversion = (event, isMandatory, convertToLowerCase) => {
if (!isDefined(event) || typeof event === 'object' || typeof event === 'boolean') {
throw new InstrumentationError('Event should not be a object, NaN, boolean or undefined');

Check warning on line 2212 in src/v0/util/index.js

View check run for this annotation

Codecov / codecov/patch

src/v0/util/index.js#L2212

Added line #L2212 was not covered by tests
}

// handling 0 as it is a valid value
if (isMandatory && !event && event !== 0) {
throw new InstrumentationError('Event is a required field');

Check warning on line 2217 in src/v0/util/index.js

View check run for this annotation

Codecov / codecov/patch

src/v0/util/index.js#L2217

Added line #L2217 was not covered by tests
}

return convertToLowerCase ? event.toString().toLowerCase() : event.toString();
};

// ========================================================================
// EXPORTS
// ========================================================================
Expand Down Expand Up @@ -2317,4 +2336,5 @@
findExistingBatch,
removeDuplicateMetadata,
combineBatchRequestsWithSameJobIds,
validateEventAndLowerCaseConversion,
};
54 changes: 52 additions & 2 deletions src/v0/util/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { TAG_NAMES } = require('@rudderstack/integrations-lib');
const { TAG_NAMES, InstrumentationError } = require('@rudderstack/integrations-lib');
const utilities = require('.');
const { getFuncTestData } = require('../../../test/testHelper');
const { FilteredEventsError } = require('./errorTypes');
Expand All @@ -7,6 +7,7 @@ const {
flattenJson,
generateExclusionList,
combineBatchRequestsWithSameJobIds,
validateEventAndLowerCaseConversion,
} = require('./index');

// Names of the utility functions to test
Expand Down Expand Up @@ -36,7 +37,6 @@ describe('Utility Functions Tests', () => {
test.each(funcTestData)('$description', async ({ description, input, output }) => {
try {
let result;

// This is to allow sending multiple arguments to the function
if (Array.isArray(input)) {
result = utilities[funcName](...input);
Expand Down Expand Up @@ -456,3 +456,53 @@ describe('Unit test cases for combineBatchRequestsWithSameJobIds', () => {
expect(combineBatchRequestsWithSameJobIds(input)).toEqual(expectedOutput);
});
});

describe('validateEventAndLowerCaseConversion Tests', () => {
it('should return string conversion of number types', () => {
aashishmalik marked this conversation as resolved.
Show resolved Hide resolved
const ev = 0;
expect(validateEventAndLowerCaseConversion(ev, false, true)).toBe('0');
expect(validateEventAndLowerCaseConversion(ev, true, false)).toBe('0');
});

it('should convert string types to lowercase', () => {
const ev = 'Abc';
expect(validateEventAndLowerCaseConversion(ev, true, true)).toBe('abc');
});

it('should throw error if event is object type', () => {
expect(() => {
validateEventAndLowerCaseConversion({}, true, true);
}).toThrow(InstrumentationError);
expect(() => {
validateEventAndLowerCaseConversion([1, 2], false, true);
}).toThrow(InstrumentationError);
expect(() => {
validateEventAndLowerCaseConversion({ a: 1 }, true, true);
}).toThrow(InstrumentationError);
});

it('should convert string to lowercase', () => {
expect(validateEventAndLowerCaseConversion('Abc', true, true)).toBe('abc');
expect(validateEventAndLowerCaseConversion('ABC', true, false)).toBe('ABC');
expect(validateEventAndLowerCaseConversion('abc55', false, true)).toBe('abc55');
expect(validateEventAndLowerCaseConversion(123, false, true)).toBe('123');
});

it('should throw error for null and undefined', () => {
expect(() => {
validateEventAndLowerCaseConversion(null, true, true);
}).toThrow(InstrumentationError);
expect(() => {
validateEventAndLowerCaseConversion(undefined, false, true);
}).toThrow(InstrumentationError);
});

it('should throw error for boolean values', () => {
expect(() => {
validateEventAndLowerCaseConversion(true, true, true);
}).toThrow(InstrumentationError);
expect(() => {
validateEventAndLowerCaseConversion(false, false, false);
}).toThrow(InstrumentationError);
});
});