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

refactor: migrate ga, ga360, gcs_datalake, google adwords destinations to component tests #2675

Merged
merged 10 commits into from
Oct 25, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const moment = require('moment-timezone');

module.exports = {
formatTimestamp: (timestamp) => {
const tsMomentInstance = moment(timestamp);
const offsetFromUtc = tsMomentInstance.utcOffset();
return tsMomentInstance.utcOffset(offsetFromUtc).format('YYYY-MM-DD HH:mm:ssZ');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const moment = require('moment-timezone');
const { formatTimestamp } = require('./helper');

describe('google adwords offline conversions - helper', () => {
it('should correctly format to IST', () => {
moment.tz.setDefault('Asia/Calcutta');
expect(formatTimestamp('2019-10-14 11:15:18.299Z')).toEqual('2019-10-14 16:45:18+05:30');
});
it('should correctly format to UTC', () => {
moment.tz.setDefault('UTC');
expect(formatTimestamp('2019-10-14 11:15:18.299Z')).toEqual('2019-10-14 11:15:18+00:00');
});
it('should return "Invalid date" when a string not in date-format is sent as argument', () => {
expect(formatTimestamp('abc')).toEqual('Invalid date');
});
it('should return offset value correctly when number is passed', () => {
moment.tz.setDefault('Asia/Tokyo');
expect(formatTimestamp(11245)).toEqual('1970-01-01 09:00:11+09:00');
});
it('should return current date when a value not string is sent as argument', () => {
moment.tz.setDefault('UTC');
const spy = jest.spyOn(Date, 'now').mockReturnValue('2023-10-22 12:51:30+00:00');
expect(formatTimestamp([])).toEqual('2023-10-22 12:51:30+00:00');
expect(formatTimestamp({})).toEqual('2023-10-22 12:51:30+00:00');
expect(formatTimestamp(undefined)).toEqual('2023-10-22 12:51:30+00:00');
spy.mockClear();
});
it('should return "Invalid date" when null value is passed as argument', () => {
expect(formatTimestamp(null)).toEqual('Invalid date');
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { set, get } = require('lodash');
const moment = require('moment');
const { EventType } = require('../../../constants');
const {
getHashFromArrayWithDuplicate,
Expand All @@ -23,6 +22,7 @@ const {
getClickConversionPayloadAndEndpoint,
} = require('./utils');
const { InstrumentationError, ConfigurationError } = require('../../util/errorTypes');
const helper = require('./helper');

/**
* get conversions depending on the type set from dashboard
Expand Down Expand Up @@ -65,9 +65,7 @@ const getConversions = (message, metadata, { Config }, event, conversionType) =>
// eslint-disable-next-line unicorn/consistent-destructuring
if (!properties.conversionDateTime && (timestamp || originalTimestamp)) {
const conversionTimestamp = timestamp || originalTimestamp;
const conversionDateTime = moment(conversionTimestamp)
.utcOffset(moment(conversionTimestamp).utcOffset())
.format('YYYY-MM-DD HH:mm:ssZ');
const conversionDateTime = helper.formatTimestamp(conversionTimestamp);
set(payload, 'conversions[0].conversionDateTime', conversionDateTime);
}
payload.partialFailure = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const sha256 = require('sha256');
const { get, set, cloneDeep } = require('lodash');
const moment = require('moment');
const { httpPOST } = require('../../../adapters/network');
const {
isHttpStatusSuccess,
Expand All @@ -26,6 +25,7 @@ const {
const { processAxiosResponse } = require('../../../adapters/utils/networkUtils');
const Cache = require('../../util/cache');
const { AbortedError, ConfigurationError, InstrumentationError } = require('../../util/errorTypes');
const helper = require('./helper');

const conversionActionIdCache = new Cache(CONVERSION_ACTION_ID_CACHE_TTL);

Expand Down Expand Up @@ -224,9 +224,7 @@ const getAddConversionPayload = (message, Config) => {
// transform originalTimestamp to format (yyyy-mm-dd hh:mm:ss+|-hh:mm)
// e.g 2019-10-14T11:15:18.299Z -> 2019-10-14 16:10:29+0530
const timestamp = payload.operations.create.transaction_attribute.transaction_date_time;
const convertedDateTime = moment(timestamp)
.utcOffset(moment(timestamp).utcOffset())
.format('YYYY-MM-DD HH:mm:ssZ');
const convertedDateTime = helper.formatTimestamp(timestamp);
payload.operations.create.transaction_attribute.transaction_date_time = convertedDateTime;
// mapping custom_key that should be predefined in google Ui and mentioned when new job is created
if (properties.custom_key && properties[properties.custom_key]) {
Expand Down
Loading
Loading