Skip to content

Commit

Permalink
Refactoring Test Objects
Browse files Browse the repository at this point in the history
  • Loading branch information
AliRKat committed Oct 14, 2024
1 parent f8d3cbb commit ade4be6
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 114 deletions.
2 changes: 1 addition & 1 deletion test/helpers/helper_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function userDetailRequestValidator(originalDetails, details) {
assert.equal(originalDetails.byear, user.byear);
if (typeof originalDetails.custom !== 'undefined') {
for (var key in originalDetails.custom) {
assert.equal(originalDetails.custom[key], user.custom[key]);
assert.deepStrictEqual(originalDetails.custom[key], user.custom[key]);
}
}
}
Expand Down
62 changes: 62 additions & 0 deletions test/helpers/test_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var eventObj = {
key: "event_check",
count: 55,
sum: 3.14,
dur: 2000,
segmentation: {
string_value: "example",
number_value: 42,
boolean_value: true,
array_value: ["item1", "item2"],
object_value: { nested_key: "nested_value" },
null_value: null,
undefined_value: undefined,
},
};

var timedEventObj = {
key: "timed",
count: 1,
segmentation: {
app_version: "1.0",
country: "Turkey",
},
};

var userDetailObj = {
name: "Alexandrina Jovovich",
username: "alex_jov",
email: "[email protected]",
organization: "TechNova",
phone: "+987654321",
picture: "https://example.com/images/profile_alex.jpg",
gender: "Female",
byear: 1992, // birth year
custom: {
string_value: "example",
number_value: 42,
boolean_value: true,
array_value: ["item1", "item2"],
object_value: { nested_key: "nested_value" },
null_value: null,
undefined_value: undefined,
},
};

var getUserDetailsObj = function() {
return userDetailObj;
};

var getEventObj = function() {
return eventObj;
};

var getTimedEventObj = function() {
return timedEventObj;
};

module.exports = {
getEventObj,
getUserDetailsObj,
getTimedEventObj,
};
45 changes: 5 additions & 40 deletions test/tests_bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require("assert");
const CountlyBulk = require("../lib/countly-bulk");
var hp = require("./helpers/helper_functions");
var storage = require("../lib/countly-storage");
var testUtils = require("./helpers/test_utils");

const { StorageTypes } = CountlyBulk;

Expand Down Expand Up @@ -71,51 +72,15 @@ function validateUserDetails(actual, expected) {
return isValid;
}

var eventObj = {
key: "bulk_check",
count: 55,
sum: 3.14,
dur: 2000,
segmentation: {
string_value: "example",
number_value: 42,
boolean_value: true,
array_value: ["item1", "item2"],
object_value: { nested_key: "nested_value" },
null_value: null,
undefined_value: undefined,
},
};

var userDetailObj = {
name: "Alexandrina Jovovich",
username: "alex_jov",
email: "[email protected]",
organization: "TechNova",
phone: "+987654321",
picture: "https://example.com/images/profile_alex.jpg",
gender: "Female",
byear: 1992, // birth year
custom: {
string_value: "example",
number_value: 42,
boolean_value: true,
array_value: ["item1", "item2"],
object_value: { nested_key: "nested_value" },
null_value: null,
undefined_value: undefined,
},
};

// Create bulk data
function createBulkData(bulk) {
// Add an event
var user = bulk.add_user({ device_id: "testUser1" });
user.add_event(eventObj);
user.add_event(testUtils.getEventObj());

// add user details
var user2 = bulk.add_user({ device_id: "testUser2" });
user2.user_details(userDetailObj);
user2.user_details(testUtils.getUserDetailsObj());

// add request
bulk.add_request({ device_id: "TestUser3" });
Expand Down Expand Up @@ -152,11 +117,11 @@ function validateCreatedBulkData(bulk) {

var deviceEvents = events.testUser1; // Access the events for the specific device
var recordedEvent = deviceEvents[0]; // Access the first event
hp.eventValidator(eventObj, recordedEvent);
hp.eventValidator(testUtils.getEventObj(), recordedEvent);

var req = reqQueue[0]; // read user details queue
const actualUserDetails = req.user_details; // Extract the user_details from the actual request
const isValid = validateUserDetails(actualUserDetails, userDetailObj);
const isValid = validateUserDetails(actualUserDetails, testUtils.getUserDetailsObj());
assert.equal(true, isValid);

var testUser3Request = reqQueue.find((request) => request.device_id === "TestUser3");
Expand Down
30 changes: 6 additions & 24 deletions test/tests_events.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */
var Countly = require("../lib/countly");
var hp = require("./helpers/helper_functions");
var testUtils = require("./helpers/test_utils");

// init function
function initMain() {
Expand All @@ -11,26 +12,7 @@ function initMain() {
max_events: -1,
});
}
// an event object to use
var eventObj = {
key: "in_app_purchase",
count: 3,
sum: 2.97,
dur: 1000,
segmentation: {
app_version: "1.0",
country: "Turkey",
},
};
// a timed event object
var timedEventObj = {
key: "timed",
count: 1,
segmentation: {
app_version: "1.0",
country: "Turkey",
},
};

describe("Events tests", () => {
beforeEach(async() => {
await hp.clearStorage();
Expand All @@ -39,11 +21,11 @@ describe("Events tests", () => {
// initialize SDK
initMain();
// send custom event
Countly.add_event(eventObj);
Countly.add_event(testUtils.getEventObj());
// read event queue
setTimeout(() => {
var event = hp.readEventQueue()[0];
hp.eventValidator(eventObj, event);
hp.eventValidator(testUtils.getEventObj(), event);
done();
}, hp.mWait);
});
Expand All @@ -53,11 +35,11 @@ describe("Events tests", () => {
// send timed event
Countly.start_event("timed");
setTimeout(() => {
Countly.end_event(timedEventObj);
Countly.end_event(testUtils.getTimedEventObj());
// read event queue
setTimeout(() => {
var event = hp.readEventQueue()[0];
hp.eventValidator(timedEventObj, event, (hp.mWait / 1000));
hp.eventValidator(testUtils.getTimedEventObj(), event, (hp.mWait / 1000));
done();
}, hp.sWait);
}, hp.mWait);
Expand Down
40 changes: 7 additions & 33 deletions test/tests_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,10 @@ var Countly = require("../lib/countly");
var storage = require("../lib/countly-storage");
var cc = require("../lib/countly-common");
var hp = require("./helpers/helper_functions");
var testUtils = require("./helpers/test_utils");

const { StorageTypes } = Countly;

// example event object to use
var eventObj = {
key: "storage_check",
count: 5,
sum: 3.14,
dur: 2000,
segmentation: {
app_version: "1.0",
country: "Zambia",
},
};

var userDetailObj = {
name: "Akira Kurosawa",
username: "a_kurosawa",
email: "[email protected]",
organization: "Toho Studios",
phone: "+81312345678",
picture: "https://example.com/profile_images/akira_kurosawa.jpg",
gender: "Male",
byear: 1910,
custom: {
"known for": "Film Director",
"notable works": "Seven Samurai, Rashomon, Ran",
},
};

// init function
function initMain(device_id) {
Countly.init({
Expand Down Expand Up @@ -196,13 +170,13 @@ describe("Storage Tests", () => {
// event should be recorded stored and validated correctly
it("4- Record event and validate storage", (done) => {
initMain();
Countly.add_event(eventObj);
Countly.add_event(testUtils.getEventObj());
setTimeout(() => {
var storedEvents = hp.readEventQueue();
assert.equal(storedEvents.length, 1);

var event = storedEvents[0];
hp.eventValidator(eventObj, event);
hp.eventValidator(testUtils.getEventObj(), event);
done();
}, hp.mWait);
});
Expand Down Expand Up @@ -379,12 +353,12 @@ describe("Storage Tests", () => {
hp.doesFileStoragePathsExist((exists) => {
assert.equal(false, exists);
});
Countly.add_event(eventObj);
Countly.add_event(testUtils.getEventObj());
setTimeout(() => {
const storedData = storage.storeGet("cly_queue", null);
const eventArray = JSON.parse(storedData[0].events);
const eventFromQueue = eventArray[0];
hp.eventValidator(eventObj, eventFromQueue);
hp.eventValidator(testUtils.getEventObj(), eventFromQueue);
done();
}, hp.mWait);
});
Expand All @@ -402,10 +376,10 @@ describe("Storage Tests", () => {
hp.doesFileStoragePathsExist((exists) => {
assert.equal(false, exists);
});
Countly.user_details(userDetailObj);
Countly.user_details(testUtils.getUserDetailsObj);
const storedData = storage.storeGet("cly_queue", null);
const userDetailsReq = storedData[0];
hp.userDetailRequestValidator(userDetailObj, userDetailsReq);
hp.userDetailRequestValidator(testUtils.getUserDetailsObj, userDetailsReq);
done();
});

Expand Down
19 changes: 3 additions & 16 deletions test/tests_user_details.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
/* eslint-disable no-console */
var Countly = require("../lib/countly");
var hp = require("./helpers/helper_functions");
var testUtils = require("./helpers/test_utils");

var userDetailObj = {
name: "Barturiana Sosinsiava",
username: "bar2rawwen",
email: "[email protected]",
organization: "Dukely",
phone: "+123456789",
picture: "https://ps.timg.com/profile_images/52237/011_n_400x400.jpg",
gender: "Non-binary",
byear: 1987, // birth year
custom: {
"key1 segment": "value1 segment",
"key2 segment": "value2 segment",
},
};
// init function
function initMain() {
Countly.init({
Expand All @@ -34,11 +21,11 @@ describe("User details tests", () => {
// initialize SDK
initMain();
// send user details
Countly.user_details(userDetailObj);
Countly.user_details(testUtils.getUserDetailsObj());
// read event queue
setTimeout(() => {
var req = hp.readRequestQueue()[0];
hp.userDetailRequestValidator(userDetailObj, req);
hp.userDetailRequestValidator(testUtils.getUserDetailsObj(), req);
done();
}, hp.sWait);
});
Expand Down

0 comments on commit ade4be6

Please sign in to comment.