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

test: unit test for cache.js #6

Merged
merged 1 commit into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aicore/libcache",
"version": "1.0.4",
"version": "1.0.10",
"description": "Cache library for core.ai services",
"main": "index.js",
"type": "module",
Expand All @@ -20,7 +20,7 @@
"lint:fix": "eslint --quiet --fix src test",
"prepare": "husky install",
"test": "npm run test:unit && npm run test:integ",
"test:unit": "mocha test/unit/setup-mocks.js test/unit/**/*.spec.js --timeout=3000",
"test:unit": "mocha test/unit/setup-mocks.js test/unit/*.spec.js test/unit/**/*.spec.js --timeout=3000",
"test:integ": "mocha test/integration/**/*.spec.js --timeout=3000",
"printReportsLink": "echo Detailed unit test coverage report: file:///$(pwd)/coverage-unit/index.html && echo Detailed integration test coverage report: file:///$(pwd)/coverage-integration/index.html",
"cover": "npm run cover:unit && npm run cover:integ",
Expand All @@ -30,7 +30,7 @@
"bumpPatchVersion": "npm --no-git-tag-version version patch",
"bumpPatchVersionWithGitTag": "npm version patch",
"release": "npm run bumpPatchVersionWithGitTag",
"start" : "MEMCACHIER_SERVERS=172.30.0.99 node src/index.js"
"start": "MEMCACHIER_SERVERS=172.30.0.99 node src/index.js"
},
"files": [
"src"
Expand Down
6 changes: 4 additions & 2 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export async function putToCache(key, value, ttl) {
if (isValidPutArguments(key, value, ttl)) {
return put(key, value, ttl);
}
return Promise.reject(`Please provide valid key ${key} valid value ${value} and ttl ${ttl}`);
return Promise.reject(`Please Verify parameters and its types. key is ${key}` +
` and type of key is ${typeof key} value is ${value} type of value is ${typeof value}.` +
` ttl is ${ttl} and typeof ttl is ${typeof ttl}`);
}

/** This is a description of the getValueFromCache function.
Expand All @@ -25,7 +27,7 @@ export async function getValueFromCache(key) {
if (isString(key)) {
return get(key);
}
return Promise.reject(`Please provide valid string as key`);
return Promise.reject(`Please provide valid string as key`);
}

/** This is a description of the getValueFromCache function.
Expand Down
3 changes: 2 additions & 1 deletion test/integration/hello-test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// remove integration tests if you don't have them.
// jshint ignore: start
/*global describe, it*/

/*
import helloWorld from "../../src/index.js";
import * as assert from 'assert';
import * as chai from 'chai';
Expand All @@ -32,3 +32,4 @@ describe('Integration: Hello world Tests', function () {
});
});
});
*/
237 changes: 237 additions & 0 deletions test/unit/cache-test.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/*global describe, it*/
import mockedFunctions from './setup-mocks.js';
import * as chai from 'chai';
import {deleteKeyFromCache, getValueFromCache, putToCache} from "../../src/cache.js";

let expect = chai.expect;

describe('This will test src/cache.js', function () {

it('getValueFromCache should pass', async function () {
mockedFunctions.memjs.client.get = function (key, callback) {
callback(null, '"world"');
};
const retVal = await getValueFromCache('hello');
expect(retVal).to.eql('world');
});
it('getValueFromCache should fail for null',
async function () {

let isExceptionOccurred = false;
try {
await getValueFromCache(null);
} catch (e) {
expect(e).to.eql('Please provide valid string as key');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});

it('getValueFromCache should fail for Number as key',
async function () {

let isExceptionOccurred = false;
try {
await getValueFromCache(1);
} catch (e) {
expect(e).to.eql('Please provide valid string as key');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
it('getValueFromCache should fail for boolean as key',
async function () {

let isExceptionOccurred = false;
try {
await getValueFromCache(true);
} catch (e) {
expect(e).to.eql('Please provide valid string as key');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
it('getValueFromCache should fail for Object as key',
async function () {

let isExceptionOccurred = false;
try {
await getValueFromCache({});
} catch (e) {
expect(e).to.eql('Please provide valid string as key');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});

it('deleteKeyFromCache should pass', async function () {
mockedFunctions.memjs.client.delete = function (key, callback) {
callback(null, true);
};
const retVal = await deleteKeyFromCache('hello');
expect(retVal).to.eql(true);
});
it('deleteKeyFromCache should fail for null as key',
async function () {

let isExceptionOccurred = false;
try {
await deleteKeyFromCache(null);
} catch (e) {
expect(e).to.eql('Please provide valid string as key');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
it('deleteKeyFromCache should fail for Number as key',
async function () {

let isExceptionOccurred = false;
try {
await deleteKeyFromCache(1);
} catch (e) {
expect(e).to.eql('Please provide valid string as key');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
it('deleteKeyFromCache should fail for boolean as key',
async function () {

let isExceptionOccurred = false;
try {
await deleteKeyFromCache(true);
} catch (e) {
expect(e).to.eql('Please provide valid string as key');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
it('deleteKeyFromCache should fail for object as key',
async function () {

let isExceptionOccurred = false;
try {
await deleteKeyFromCache({});
} catch (e) {
expect(e).to.eql('Please provide valid string as key');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});

it('putToCache should pass for string value', async function () {
mockedFunctions.memjs.client.set = function (key, value, ttl, callback) {
callback(null, true);
};
const retVal = await putToCache('hello', 'world', 1000);
expect(retVal).to.eql(true);
});
it('putToCache should pass for Object value', async function () {
mockedFunctions.memjs.client.set = function (key, value, ttl, callback) {
callback(null, true);
};
const retVal = await putToCache('hello', {}, 1000);
expect(retVal).to.eql(true);
});

it('putToCache should pass for boolean value', async function () {
mockedFunctions.memjs.client.set = function (key, value, ttl, callback) {
callback(null, true);
};
const retVal = await putToCache('hello', true, 1000);
expect(retVal).to.eql(true);
});
it('putToCache should fail for null as key',
async function () {

let isExceptionOccurred = false;
try {
await putToCache(null, 'world', 1000);
} catch (e) {
expect(e).to.eql('Please Verify parameters and its types. key is null and type of key is object' +
' value is world type of value is string. ttl is 1000 and typeof ttl is number');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});

it('putToCache should fail for number as key',
async function () {

let isExceptionOccurred = false;
try {
await putToCache(1, 'world', 1000);
} catch (e) {
expect(e).to.eql('Please Verify parameters and its types. key is 1 and type of key is number' +
' value is world type of value is string. ttl is 1000 and typeof ttl is number');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
it('putToCache should fail for boolean as key',
async function () {

let isExceptionOccurred = false;
try {
await putToCache(true, 'world', 1000);
} catch (e) {
expect(e).to.eql('Please Verify parameters and its types. key is true and type of key is boolean' +
' value is world type of value is string. ttl is 1000 and typeof ttl is number');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
it('putToCache should fail for null as Object',
async function () {

let isExceptionOccurred = false;
try {
await putToCache('hello', null, 1000);
} catch (e) {
expect(e).to.eql('Please Verify parameters and its types. key is hello and type of key is string'+
' value is null type of value is object. ttl is 1000 and typeof ttl is number');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
it('putToCache should fail for string as ttl',
async function () {

let isExceptionOccurred = false;
try {
await putToCache('hello', 'world', "1000");
} catch (e) {
expect(e).to.eql('Please Verify parameters and its types. key is hello and type of key is string'+
' value is world type of value is string. ttl is 1000 and typeof ttl is string');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
it('putToCache should fail for Object as ttl',
async function () {

let isExceptionOccurred = false;
try {
await putToCache('hello', 'world', {});
} catch (e) {
expect(e).to.eql('Please Verify parameters and its types. key is hello and type of key is string'+
' value is world type of value is string. ttl is [object Object] and typeof ttl is object');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
it('putToCache should fail for boolean as ttl',
async function () {

let isExceptionOccurred = false;
try {
await putToCache('hello', 'world', true);
} catch (e) {
expect(e).to.eql('Please Verify parameters and its types. key is hello and type of key is string'+
' value is world type of value is string. ttl is true and typeof ttl is boolean');
isExceptionOccurred = true;
}
expect(isExceptionOccurred).to.eql(true);
});
});
25 changes: 0 additions & 25 deletions test/unit/unit-test.spec.js

This file was deleted.