-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handling invalid timestamp for adjust source (#3866)
* fix: handling invalid timestamp for adjust source * fix: small change in logic * fix: unnecessary line removal * fix: unnecessary line removal * fix: review comment addressed * fix: review comment addressed * chore: add edge testcase - update error messages * fix: testcase error message --------- Co-authored-by: Sai Sankeerth <[email protected]>
- Loading branch information
1 parent
f3ff409
commit d57f48e
Showing
4 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { TransformationError } = require('@rudderstack/integrations-lib'); | ||
|
||
const convertToISODate = (rawTimestamp) => { | ||
if (typeof rawTimestamp !== 'number' && typeof rawTimestamp !== 'string') { | ||
throw new TransformationError( | ||
`Invalid timestamp type: expected number or string, received ${typeof rawTimestamp}`, | ||
); | ||
} | ||
|
||
const createdAt = Number(rawTimestamp); | ||
|
||
if (Number.isNaN(createdAt)) { | ||
throw new TransformationError(`Failed to parse timestamp: "${rawTimestamp}"`); | ||
} | ||
|
||
const date = new Date(createdAt * 1000); | ||
|
||
if (Number.isNaN(date.getTime())) { | ||
throw new TransformationError(`Failed to create valid date for timestamp "${rawTimestamp}"`); | ||
} | ||
|
||
return date.toISOString(); | ||
}; | ||
|
||
module.exports = { | ||
convertToISODate, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const { convertToISODate } = require('./utils'); | ||
const { TransformationError } = require('@rudderstack/integrations-lib'); | ||
|
||
describe('convertToISODate', () => { | ||
// Converts valid numeric timestamp to ISO date string | ||
it('should return ISO date string when given a valid numeric timestamp', () => { | ||
const timestamp = 1633072800; // Example timestamp for 2021-10-01T00:00:00.000Z | ||
const result = convertToISODate(timestamp); | ||
expect(result).toBe('2021-10-01T07:20:00.000Z'); | ||
}); | ||
|
||
// Throws error for non-numeric string input | ||
it('should throw TransformationError when given a non-numeric string', () => { | ||
const invalidTimestamp = 'invalid'; | ||
expect(() => convertToISODate(invalidTimestamp)).toThrow(TransformationError); | ||
}); | ||
|
||
// Converts valid numeric string timestamp to ISO date string | ||
it('should convert valid numeric string timestamp to ISO date string', () => { | ||
const rawTimestamp = '1633072800'; // Corresponds to 2021-10-01T00:00:00.000Z | ||
const result = convertToISODate(rawTimestamp); | ||
expect(result).toBe('2021-10-01T07:20:00.000Z'); | ||
}); | ||
|
||
// Throws error for non-number and non-string input | ||
it('should throw error for non-number and non-string input', () => { | ||
expect(() => convertToISODate({})).toThrow(TransformationError); | ||
expect(() => convertToISODate([])).toThrow(TransformationError); | ||
expect(() => convertToISODate(null)).toThrow(TransformationError); | ||
expect(() => convertToISODate(undefined)).toThrow(TransformationError); | ||
}); | ||
|
||
it('should throw error for timestamp that results in invalid date when multiplied', () => { | ||
const hugeTimestamp = 999999999999999; // This will become invalid when multiplied by 1000 | ||
expect(() => convertToISODate(hugeTimestamp)).toThrow(TransformationError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters