diff --git a/package-lock.json b/package-lock.json index 5e9e61e..428d3ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@aicore/template-nodejs", - "version": "1.0.4", + "version": "1.0.10", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@aicore/template-nodejs", - "version": "1.0.4", + "version": "1.0.10", "license": "AGPL-3.0-or-later", "dependencies": { "memjs": "^1.3.0" diff --git a/package.json b/package.json index b0c878b..ed92356 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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" diff --git a/src/cache.js b/src/cache.js index c4afc7a..4910448 100644 --- a/src/cache.js +++ b/src/cache.js @@ -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. @@ -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. diff --git a/test/integration/hello-test.spec.js b/test/integration/hello-test.spec.js index 62c0563..6b89999 100644 --- a/test/integration/hello-test.spec.js +++ b/test/integration/hello-test.spec.js @@ -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'; @@ -32,3 +32,4 @@ describe('Integration: Hello world Tests', function () { }); }); }); +*/ diff --git a/test/unit/cache-test.spec.js b/test/unit/cache-test.spec.js new file mode 100644 index 0000000..ab26312 --- /dev/null +++ b/test/unit/cache-test.spec.js @@ -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); + }); +}); diff --git a/test/unit/unit-test.spec.js b/test/unit/unit-test.spec.js deleted file mode 100644 index b207cbf..0000000 --- a/test/unit/unit-test.spec.js +++ /dev/null @@ -1,25 +0,0 @@ -// Testing framework: Mocha , assertion style: chai -// See https://mochajs.org/#getting-started on how to write tests -// Use chai for BDD style assertions (expect, should etc..). See move here: https://www.chaijs.com/guide/styles/#expect - -// Mocks and spies: sinon -// if you want to mock/spy on fn() for unit tests, use sinon. refer docs: https://sinonjs.org/ - -// Note on coverage suite used here: -// we use c8 for coverage https://github.com/bcoe/c8. Its reporting is based on nyc, so detailed docs can be found -// here: https://github.com/istanbuljs/nyc ; We didn't use nyc as it do not yet have ES module support -// see: https://github.com/digitalbazaar/bedrock-test/issues/16 . c8 is drop replacement for nyc coverage reporting tool -/*global describe, it*/ - -import helloWorld from "../../src/index.js"; -import * as assert from 'assert'; -import * as chai from 'chai'; - -let expect = chai.expect; - -describe('unit Tests', function () { - it('should return Hello World', function () { - expect(helloWorld('yo')).to.equal('Hello World yo'); - }); - -});