Skip to content

Commit

Permalink
test cases added for source endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
anantjain45823 committed Sep 27, 2023
1 parent 830dfa3 commit 16a00d8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
36 changes: 22 additions & 14 deletions src/v0/destinations/am/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
isAppleFamily,
isDefinedAndNotNullAndNotEmpty,
simpleProcessRouterDest,
isValidInteger,
} = require('../../util');
const {
BASE_URL,
Expand Down Expand Up @@ -80,28 +81,35 @@ const aliasEndpoint = (destConfig) => {
return retVal;
};

const handleSessionIdUnderRoot = (message) => {
const sessionId = get(message, 'session_id');
const handleSessionIdUnderRoot = (sessionId) => {
if (typeof sessionId === 'string') {
const extractedPart = sessionId.split(':').reverse();
if (!isValidInteger(extractedPart[0])) return -1;
return extractedPart[0];
}
return sessionId;
return Number(sessionId);
};

const handleSessionIdUnderContext = (message) => {
let sessionId = get(message, 'context.sessionId');
sessionId = Number(sessionId);
if (Number.isNaN(sessionId)) return -1;
return sessionId;
const handleSessionIdUnderContext = (sessionId) => {
if (!isValidInteger(sessionId)) return -1;
return Number(sessionId);
};

const getSessionId = (message) =>
get(message, 'session_id')
? handleSessionIdUnderRoot(message)
: get(message, 'context.sessionId')
? handleSessionIdUnderContext(message)
: -1;
const getSessionId = (message) => {
let sessionId = -1;
const rootSessionId = get(message, 'session_id');
if (rootSessionId) {
sessionId = handleSessionIdUnderRoot(rootSessionId);
if (sessionId !== -1) {
return sessionId;
}
}
const contextSessionId = get(message, 'context.sessionId');
if (contextSessionId) {
sessionId = handleSessionIdUnderContext(contextSessionId);
}
return sessionId;
};

const addMinIdlength = () => ({ min_id_length: 1 });

Expand Down
10 changes: 9 additions & 1 deletion src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1992,7 +1992,14 @@ const getAuthErrCategoryFromStCode = (status) => {
}
return '';
};

const isValidInteger = (value) => {
if (Number.isNaN(value) || !isDefinedAndNotNull(value)) {
return false;
}
// const valueToString = `${value}`;
// Use a regular expression to check if the string is a valid integer or a valid floating-point number
return /^-?\d+(\.\d+)?$/.test(`${value}`);
};
// ========================================================================
// EXPORTS
// ========================================================================
Expand Down Expand Up @@ -2096,4 +2103,5 @@ module.exports = {
groupEventsByType,
getAuthErrCategoryFromErrDetailsAndStCode,
getAuthErrCategoryFromStCode,
isValidInteger,
};
30 changes: 15 additions & 15 deletions test/__tests__/data/am_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"app_name": "RudderLabs JavaScript SDK",
"app_version": "1.0.0",
"language": "en-US",
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"insert_id": "84e26acc-56a5-4835-8233-591137fca468",
"ip": "0.0.0.0",
"user_properties": {
Expand Down Expand Up @@ -205,7 +205,7 @@
"app_name": "RudderLabs JavaScript SDK",
"app_version": "1.0.0",
"language": "en-US",
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"insert_id": "84e26acc-56a5-4835-8233-591137fca468",
"ip": "0.0.0.0",
"user_properties": {
Expand Down Expand Up @@ -265,7 +265,7 @@
"app_name": "RudderLabs JavaScript SDK",
"app_version": "1.0.0",
"language": "en-US",
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"event_type": "$identify",
"user_properties": {
"anonymousId": "123456",
Expand Down Expand Up @@ -330,7 +330,7 @@
"initial_referring_domain": "docs.rudderstack.com",
"initial_referrer": "https://docs.rudderstack.com"
},
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"ip": "1.1.1.1",
"time": 1571051718299,
"user_id": "12345",
Expand Down Expand Up @@ -389,7 +389,7 @@
"initial_referring_domain": "docs.rudderstack.com",
"initial_referrer": "https://docs.rudderstack.com"
},
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"ip": "1.1.1.1",
"groups": {
"Company": "ABC"
Expand Down Expand Up @@ -440,7 +440,7 @@
"app_version": "1.0.0",
"language": "en-US",
"event_type": "test track event",
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"event_properties": {
"user_actual_role": "system_admin",
"user_actual_id": 12345,
Expand Down Expand Up @@ -494,7 +494,7 @@
"app_name": "RudderLabs JavaScript SDK",
"app_version": "1.0.0",
"language": "en-US",
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"event_type": "$identify",
"user_properties": {
"anonymousId": "123456",
Expand Down Expand Up @@ -3014,7 +3014,7 @@
"app_name": "RudderLabs JavaScript SDK",
"app_version": "1.0.0",
"language": "en-US",
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"event_type": "$identify",
"device_brand": "testBrand",
"device_manufacturer": "testManufacturer",
Expand Down Expand Up @@ -3159,7 +3159,7 @@
"app_name": "RudderLabs JavaScript SDK",
"app_version": "1.0.0",
"language": "en-US",
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"insert_id": "84e26acc-56a5-4835-8233-591137fca468",
"ip": "0.0.0.0",
"user_properties": {
Expand Down Expand Up @@ -3731,7 +3731,7 @@
"initial_referring_domain": "docs.rudderstack.com",
"name": "ApplicationLoaded"
},
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"insert_id": "5e10d13a-bf9a-44bf-b884-43a9e591ea71",
"ip": "1.1.1.1",
"event_id": 2,
Expand Down Expand Up @@ -3869,7 +3869,7 @@
"event_type": "$identify",
"time": 1571043797562,
"user_id": "123456",
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": 1662393792,
"country": "India",
"city": "kolkata",
"library": "rudderstack"
Expand Down Expand Up @@ -3975,7 +3975,7 @@
"initial_referring_domain": "docs.rudderstack.com",
"name": "ApplicationLoaded"
},
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"insert_id": "5e10d13a-bf9a-44bf-b884-43a9e591ea71",
"ip": "1.1.1.1",
"event_id": 2,
Expand Down Expand Up @@ -4035,7 +4035,7 @@
"initial_referring_domain": "docs.rudderstack.com",
"name": "ApplicationLoaded"
},
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"insert_id": "5e10d13a-bf9a-44bf-b884-43a9e591ea71",
"ip": "1.1.1.1",
"event_id": 2,
Expand Down Expand Up @@ -4095,7 +4095,7 @@
"initial_referring_domain": "docs.rudderstack.com",
"name": "ApplicationLoaded"
},
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"insert_id": "5e10d13a-bf9a-44bf-b884-43a9e591ea71",
"ip": "1.1.1.1",
"event_id": 2,
Expand Down Expand Up @@ -4155,7 +4155,7 @@
"initial_referring_domain": "docs.rudderstack.com",
"name": "ApplicationLoaded"
},
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"insert_id": "5e10d13a-bf9a-44bf-b884-43a9e591ea71",
"ip": "1.1.1.1",
"event_id": 2,
Expand Down
4 changes: 2 additions & 2 deletions test/__tests__/data/am_router_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"app_name": "RudderLabs JavaScript SDK",
"app_version": "1.0.0",
"language": "en-US",
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"insert_id": "84e26acc-56a5-4835-8233-591137fca468",
"city": "kolkata",
"country": "India",
Expand Down Expand Up @@ -109,7 +109,7 @@
"initial_referring_domain": "docs.rudderstack.com",
"name": "ApplicationLoaded"
},
"session_id": "3049dc4c-5a95-4ccd-a3e7-d74a7e411f22",
"session_id": -1,
"insert_id": "5e10d13a-bf9a-44bf-b884-43a9e591ea71",
"ip": "1.1.1.1",
"user_properties": {
Expand Down

0 comments on commit 16a00d8

Please sign in to comment.