Skip to content

Commit

Permalink
Updated Storage Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AliRKat committed Oct 1, 2024
1 parent f370c78 commit 7837491
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 6 deletions.
24 changes: 21 additions & 3 deletions lib/countly-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ const memoryStorage = {
* @param {Function} callback - callback to call when done storing
*/
storeSet: function(key, value, callback) {
__data[key] = value;
if (typeof callback === "function") {
callback(null); // No errors for memory storage
if (key) {
cc.log(cc.logLevelEnums.DEBUG, `storeSet, Setting key: [${key}] & value: [${value}]!`);
__data[key] = value;
if (typeof callback === "function") {
callback(null);
}
}
else {
cc.log(cc.logLevelEnums.WARNING, `storeSet, Provioded key: [${key}] is null!`);
}
},
/**
Expand Down Expand Up @@ -263,6 +269,17 @@ var storeRemove = function(key) {
storageMethod.storeRemove(key);
};

/**
* Disclaimer: This method is mainly for testing purposes.
* @returns {StorageTypes} Returns the active storage type for the SDK
*/
var getStorageType = function() {
if (storageMethod === memoryStorage) {
return StorageTypes.MEMORY;
}
return StorageTypes.FILE;
};

module.exports = {
initStorage,
storeSet,
Expand All @@ -274,4 +291,5 @@ module.exports = {
setStoragePath,
resetStorage,
readFile,
getStorageType,
};
113 changes: 110 additions & 3 deletions test/tests_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var storage = require("../lib/countly-storage");
var cc = require("../lib/countly-common");
var hp = require("./helpers/helper_functions");

const StorageTypes = cc.storageTypeEnums;

// example event object to use
var eventObj = {
key: "storage_check",
Expand Down Expand Up @@ -294,14 +296,119 @@ describe("Storage Tests", () => {

it("17- Setting storage method to memory only and checking storage path /no-init", (done) => {
storage.resetStorage();
storage.initStorage(null, cc.storageTypeEnums.MEMORY);
storage.initStorage(null, StorageTypes.MEMORY);
assert.equal(storage.getStoragePath(), undefined);
done();
});

it("18- Memory only storage Device-Id", (done) => {
hp.clearStorage();
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://test.url.ly",
device_id: "Test-Device-Id",
clear_stored_device_id: true,
storage_type: StorageTypes.MEMORY,
});
assert.equal(storage.getStoragePath(), undefined);
assert.equal(storage.storeGet("cly_id", null), "Test-Device-Id");
assert.equal(storage.storeGet("cly_id_type", null), cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED);
done();
});

it("18- Recording to storage with memory only storage /no-init", (done) => {
it("19- Record event in memory only mode and validate the record", (done) => {
hp.clearStorage();
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://test.url.ly",
device_id: "Test-Device-Id",
clear_stored_device_id: true,
storage_type: StorageTypes.MEMORY,
});
Countly.add_event(eventObj);
setTimeout(() => {
const storedData = storage.storeGet("cly_queue", null);
const eventArray = JSON.parse(storedData[0].events);
const eventFromQueue = eventArray[0];
hp.eventValidator(eventObj, eventFromQueue);
done();
}, hp.mWait);
});

it("20- Record and validate user details in memory only mode", (done) => {
hp.clearStorage();
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://test.url.ly",
device_id: "Test-Device-Id",
clear_stored_device_id: true,
storage_type: StorageTypes.MEMORY,
});
Countly.user_details(userDetailObj);
const storedData = storage.storeGet("cly_queue", null);
const userDetailsReq = storedData[0];
hp.userDetailRequestValidator(userDetailObj, userDetailsReq);
done();
});

it("21- Memory only storage, change SDK Generated Device-Id", (done) => {
hp.clearStorage();
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://test.url.ly",
clear_stored_device_id: true,
storage_type: StorageTypes.MEMORY,
});
assert.equal(storage.getStoragePath(), undefined);
assert.equal(storage.storeGet("cly_id", null), Countly.get_device_id());
assert.equal(storage.storeGet("cly_id_type", null), Countly.get_device_id_type());

Countly.change_id("Test-Id-2");
assert.equal(storage.storeGet("cly_id", null), "Test-Id-2");
assert.equal(storage.storeGet("cly_id_type", null), cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED);
done();
});

it("22- Switch to file storage after init", (done) => {
hp.clearStorage();
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://test.url.ly",
clear_stored_device_id: true,
storage_type: StorageTypes.MEMORY,
});
assert.equal(storage.getStoragePath(), undefined);
assert.equal(storage.getStorageType(), StorageTypes.MEMORY);

storage.initStorage();
assert.equal(storage.getStoragePath(), "../data/");
assert.equal(storage.getStorageType(), StorageTypes.FILE);
done();
});

it("23- storeRemove Memory Only /no-init", (done) => {
storage.resetStorage();
recordValuesToStorageAndValidate(null, cc.storageTypeEnums.MEMORY);
storage.initStorage(null, StorageTypes.MEMORY);
assert.equal(storage.getStoragePath(), undefined);
assert.equal(storage.getStorageType(), StorageTypes.MEMORY);
storage.storeSet("keyToStore", "valueToStore");
assert.equal(storage.storeGet("keyToStore", null), "valueToStore");

storage.storeRemove("keyToStore");
assert.equal(storage.storeGet("keyToStore", null), null);
done();
});

it("24- storeRemove File Storage /no-init", (done) => {
storage.resetStorage();
storage.initStorage();
assert.equal(storage.getStoragePath(), "../data/");
assert.equal(storage.getStorageType(), StorageTypes.FILE);
storage.storeSet("keyToStore", "valueToStore");
assert.equal(storage.storeGet("keyToStore", null), "valueToStore");

storage.storeRemove("keyToStore");
assert.equal(storage.storeGet("keyToStore", null), null);
done();
});
});

0 comments on commit 7837491

Please sign in to comment.