Skip to content

Commit

Permalink
Merge pull request #101 from Countly/BulkTests
Browse files Browse the repository at this point in the history
Bulk Tests
  • Loading branch information
turtledreams authored Sep 19, 2024
2 parents cd6c767 + 1c2e05b commit 97a4ccc
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 19 deletions.
60 changes: 41 additions & 19 deletions test/helpers/helper_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,54 @@ var dir = path.resolve(__dirname, "../../");
var idDir = (`${dir}/data/__cly_id.json`);
var eventDir = (`${dir}/data/__cly_event.json`);
var reqDir = (`${dir}/data/__cly_queue.json`);
var bulkEventDir = (`${dir}/bulk_data/__cly_bulk_event.json`);
var bulkQueueDir = (`${dir}/bulk_data/__cly_req_queue.json`);
// timeout variables
var sWait = 50;
var mWait = 3000;
var lWait = 10000;
// parsing event queue
function readEventQueue() {
var a = JSON.parse(fs.readFileSync(eventDir, "utf-8")).cly_event;
function readEventQueue(givenPath = null, isBulk = false) {
var destination = eventDir;
if (givenPath !== null) {
destination = givenPath;
}
var a = JSON.parse(fs.readFileSync(destination, "utf-8")).cly_event;
if (isBulk) {
a = JSON.parse(fs.readFileSync(destination, "utf-8")).cly_bulk_event;
}
return a;
}
// parsing request queue
function readRequestQueue() {
var a = JSON.parse(fs.readFileSync(reqDir, "utf-8")).cly_queue;
function readRequestQueue(givenPath = null, isBulk = false) {
var destination = reqDir;
if (givenPath !== null) {
destination = givenPath;
}
var a = JSON.parse(fs.readFileSync(destination, "utf-8")).cly_queue;
if (isBulk) {
a = JSON.parse(fs.readFileSync(destination, "utf-8")).cly_req_queue;
}
return a;
}

// queue files clearing logic
function clearStorage(keepID) {
keepID = keepID || false;
function clearStorage(keepID = false, isBulk = false, customDir = '') {
// Resets Countly
Countly.halt(true);
// clean storages
if (fs.existsSync(eventDir)) {
fs.unlinkSync(eventDir);
// Determine the directory based on isBulk or customDir
const eventDirectory = customDir || (isBulk ? bulkEventDir : eventDir);
const reqDirectory = customDir || (isBulk ? bulkQueueDir : reqDir);
// Remove event directory if it exists
if (fs.existsSync(eventDirectory)) {
fs.rmSync(eventDirectory, { recursive: true, force: true });
}
if (fs.existsSync(reqDir)) {
fs.unlinkSync(reqDir);
// Remove request directory if it exists
if (fs.existsSync(reqDirectory)) {
fs.rmSync(reqDirectory, { recursive: true, force: true });
}
if (!keepID) {
if (fs.existsSync(idDir)) {
fs.unlinkSync(idDir);
}
// Optionally keep the ID directory
if (!keepID && fs.existsSync(idDir)) {
fs.rmSync(idDir, { recursive: true, force: true });
}
}
/**
Expand Down Expand Up @@ -67,11 +84,16 @@ function eventValidator(eventObject, eventQueue, time) {
}
assert.equal(eventObject.dur, eventQueue.dur);
}
// check if segmentation exists. If it is add test(s)
// check if segmentation exists. If it does, add tests
if (typeof eventObject.segmentation !== 'undefined') {
// loop through segmentation keys and create tets
// loop through segmentation keys and create tests
for (var key in eventObject.segmentation) {
assert.equal(eventObject.segmentation[key], eventQueue.segmentation[key]);
if (Array.isArray(eventObject.segmentation[key]) || typeof eventObject.segmentation[key] === 'object') {
assert.deepStrictEqual(eventObject.segmentation[key], eventQueue.segmentation[key]);
}
else {
assert.equal(eventObject.segmentation[key], eventQueue.segmentation[key]);
}
}
}
// common parameter validation
Expand Down
214 changes: 214 additions & 0 deletions test/tests_bulk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/* eslint-disable no-console */
/* global runthis */
var path = require("path");
const assert = require("assert");
const CountlyBulk = require("../lib/countly-bulk");
var hp = require("./helpers/helper_functions");
var storage = require("../lib/countly-storage");

// default paths
var dir = path.resolve(__dirname, "../");
var bulkEventDir = (`${dir}/bulk_data/__cly_bulk_event.json`);
var bulkQueueDir = (`${dir}/bulk_data/__cly_req_queue.json`);

function createBulk(storagePath) {
var bulk = new CountlyBulk({
app_key: "YOUR_APP_KEY",
url: "https://try.count.ly",
storage_path: storagePath,
});
return bulk;
}

function validateCrash(validator, nonfatal) {
assert.ok(validator.crash._os);
assert.ok(validator.crash._os_version);
assert.ok(validator.crash._error);
assert.ok(validator.crash._app_version);
assert.ok(typeof validator.crash._run !== 'undefined');
assert.ok(typeof validator.crash._custom !== 'undefined');
assert.equal(nonfatal, validator.crash._nonfatal);
assert.equal(true, validator.crash._javascript);
assert.equal(true, validator.crash._not_os_specific);
}

// note: this can replace the current one in the helper functions
function validateUserDetails(actual, expected) {
const keys = ['name', 'username', 'email', 'organization', 'phone', 'picture', 'gender', 'byear', 'custom'];
let isValid = true;

keys.forEach((key) => {
if (typeof actual[key] === 'object' && actual[key] !== null) {
if (Array.isArray(actual[key])) {
if (!Array.isArray(expected[key]) || JSON.stringify(actual[key]) !== JSON.stringify(expected[key])) {
console.error(`Mismatch for key "${key}": expected "${JSON.stringify(expected[key])}", but got "${JSON.stringify(actual[key])}"`);
isValid = false;
}
}
else {
if (JSON.stringify(actual[key]) !== JSON.stringify(expected[key])) {
console.error(`Mismatch for key "${key}": expected "${JSON.stringify(expected[key])}", but got "${JSON.stringify(actual[key])}"`);
isValid = false;
}
}
}
else if (actual[key] !== expected[key]) {
console.error(`Mismatch for key "${key}": expected "${expected[key]}", but got "${actual[key]}"`);
isValid = false;
}
});
// Validate nested custom object separately
if (expected.custom && actual.custom) {
const customKeys = Object.keys(expected.custom);
customKeys.forEach((key) => {
if (typeof actual.custom[key] === 'object' && actual.custom[key] !== null) {
if (Array.isArray(actual.custom[key])) {
if (!Array.isArray(expected.custom[key]) || JSON.stringify(actual.custom[key]) !== JSON.stringify(expected.custom[key])) {
console.error(`Mismatch in custom object for key "${key}": expected "${JSON.stringify(expected.custom[key])}", but got "${JSON.stringify(actual.custom[key])}"`);
isValid = false;
}
}
else {
if (JSON.stringify(actual.custom[key]) !== JSON.stringify(expected.custom[key])) {
console.error(`Mismatch in custom object for key "${key}": expected "${JSON.stringify(expected.custom[key])}", but got "${JSON.stringify(actual.custom[key])}"`);
isValid = false;
}
}
}
else if (actual.custom[key] !== expected.custom[key]) {
console.error(`Mismatch in custom object for key "${key}": expected "${expected.custom[key]}", but got "${actual.custom[key]}"`);
isValid = false;
}
});
}
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,
},
};

describe("Bulk Tests", () => {
it("1- Bulk with Default Storage Path", (done) => {
hp.clearStorage(false, true);
createBulk();
assert.equal(storage.getStoragePath(), "../bulk_data/");
done();
});

it("2- Bulk with Custom Storage Path", (done) => {
hp.clearStorage(false, true);
createBulk("../test/customStorageDirectory/");
assert.equal(storage.getStoragePath(), "../test/customStorageDirectory/");
done();
});

it("3- Bulk add_user with Record Event", (done) => {
hp.clearStorage(false, true);
var bulk = createBulk();
var user = bulk.add_user({ device_id: "testUser1" });
user.add_event(eventObj);
setTimeout(() => {
var events = hp.readEventQueue(bulkEventDir, true);
var deviceEvents = events.testUser1; // Access the events for the specific device
var recordedEvent = deviceEvents[0]; // Access the first event
hp.eventValidator(eventObj, recordedEvent);
done();
}, hp.mWait);
});

it("4- Bulk add_user with User Details", (done) => {
hp.clearStorage(false, true);
var bulk = createBulk();
var user = bulk.add_user({ device_id: "testUser2" });
user.user_details(userDetailObj);

// read event queue
setTimeout(() => {
var reqQueue = hp.readRequestQueue(bulkQueueDir, true);
var req = reqQueue[0];
// Extract the user_details from the actual request
const actualUserDetails = req.user_details || {};
// Validate the user details
const isValid = validateUserDetails(actualUserDetails, userDetailObj);
assert.equal(true, isValid);
done();
}, hp.sWait);
});

it("5- Bulk add_request", (done) => {
hp.clearStorage(false, true);
var bulk = createBulk();
bulk.add_request({ device_id: "TestUser3" });
setTimeout(() => {
var reqQueue = hp.readRequestQueue(bulkQueueDir, true);
var testUser3Request = reqQueue.find((req) => req.device_id === "TestUser3");
assert.ok(testUser3Request);
assert.strictEqual(testUser3Request.device_id, "TestUser3");
assert.strictEqual(testUser3Request.app_key, "YOUR_APP_KEY");
assert.strictEqual(testUser3Request.sdk_name, "javascript_native_nodejs_bulk");
done();
}, hp.sWait);
});

it("6- Bulk add_user Report Crash", (done) => {
hp.clearStorage(false, true);
var bulk = createBulk();
var user = bulk.add_user({ device_id: "TestUser4" });
try {
runthis();
}
catch (ex) {
user.report_crash({
_os: "Android",
_os_version: "7",
_error: "Stack trace goes here",
_app_version: "1.0",
_run: 12345,
_custom: {},
_nonfatal: true,
_javascript: true,
_not_os_specific: true,
}, 1500645200);
}
// read event queue
setTimeout(() => {
var reqQueue = hp.readRequestQueue(bulkQueueDir, true);
var testUser4Request = reqQueue.find((req) => req.device_id === "TestUser4");
validateCrash(testUser4Request, true);
done();
}, hp.sWait);
});
});

0 comments on commit 97a4ccc

Please sign in to comment.