Skip to content

Commit

Permalink
Updated HelperFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Rıza Kat committed Sep 17, 2024
1 parent 160f4c7 commit 1c2e05b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 70 deletions.
91 changes: 31 additions & 60 deletions test/helpers/helper_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,47 @@ 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;
return a;
}
function readBulkEventQueue() {
var a = JSON.parse(fs.readFileSync(bulkEventDir, "utf-8")).cly_bulk_event;
return a;
}
function readBulkReqQueue() {
var a = JSON.parse(fs.readFileSync(bulkQueueDir, "utf-8")).cly_req_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;
}

function clearStorageJsonFiles(directory, files) {
files.forEach((file) => {
const filePath = path.join(directory, file);
// Check if the file exists before attempting to clear it
fs.access(filePath, fs.constants.F_OK, (err) => {
if (!err) {
// Write an empty JSON object to the file
// logs can be added here
fs.writeFile(filePath, JSON.stringify({}), 'utf8', (er) => { });
}
});
});
}

function clearJsonFiles(isBulk = true, customDir = '') {
// Determine the directory and files based on isBulk
const directory = customDir || (isBulk ? path.join(__dirname, 'bulk_data') : path.join(__dirname, 'data'));
const files = isBulk ? [
'__cly_id.json',
'__cly_req_queue.json',
'__cly_bulk_event.json',
] : [
'__cly_event.json',
'__cly_id_type.json',
'__cly_id.json',
'__cly_queue.json',
'__cly_remote_configs.json',
];

// Clear files in the specified directory
clearStorageJsonFiles(directory, files);
}

// 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 @@ -236,11 +210,8 @@ module.exports = {
lWait,
readEventQueue,
readRequestQueue,
readBulkEventQueue,
readBulkReqQueue,
eventValidator,
crashRequestValidator,
clearJsonFiles,
sessionRequestValidator,
userDetailRequestValidator,
viewEventValidator,
Expand Down
26 changes: 16 additions & 10 deletions test/tests_bulk.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/* 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",
Expand Down Expand Up @@ -116,26 +122,26 @@ var userDetailObj = {

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

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

it("3- Bulk add_user with Record Event", (done) => {
hp.clearJsonFiles(true);
hp.clearStorage(false, true);
var bulk = createBulk();
var user = bulk.add_user({ device_id: "testUser1" });
user.add_event(eventObj);
setTimeout(() => {
var events = hp.readBulkEventQueue();
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);
Expand All @@ -144,14 +150,14 @@ describe("Bulk Tests", () => {
});

it("4- Bulk add_user with User Details", (done) => {
hp.clearJsonFiles(true);
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.readBulkReqQueue();
var reqQueue = hp.readRequestQueue(bulkQueueDir, true);
var req = reqQueue[0];
// Extract the user_details from the actual request
const actualUserDetails = req.user_details || {};
Expand All @@ -163,11 +169,11 @@ describe("Bulk Tests", () => {
});

it("5- Bulk add_request", (done) => {
hp.clearJsonFiles(true);
hp.clearStorage(false, true);
var bulk = createBulk();
bulk.add_request({ device_id: "TestUser3" });
setTimeout(() => {
var reqQueue = hp.readBulkReqQueue();
var reqQueue = hp.readRequestQueue(bulkQueueDir, true);
var testUser3Request = reqQueue.find((req) => req.device_id === "TestUser3");
assert.ok(testUser3Request);
assert.strictEqual(testUser3Request.device_id, "TestUser3");
Expand All @@ -178,7 +184,7 @@ describe("Bulk Tests", () => {
});

it("6- Bulk add_user Report Crash", (done) => {
hp.clearJsonFiles(true);
hp.clearStorage(false, true);
var bulk = createBulk();
var user = bulk.add_user({ device_id: "TestUser4" });
try {
Expand All @@ -199,7 +205,7 @@ describe("Bulk Tests", () => {
}
// read event queue
setTimeout(() => {
var reqQueue = hp.readBulkReqQueue();
var reqQueue = hp.readRequestQueue(bulkQueueDir, true);
var testUser4Request = reqQueue.find((req) => req.device_id === "TestUser4");
validateCrash(testUser4Request, true);
done();
Expand Down

0 comments on commit 1c2e05b

Please sign in to comment.