Skip to content

Commit

Permalink
Added tests, moved expiry time in config
Browse files Browse the repository at this point in the history
  • Loading branch information
pallavi2209 committed Jan 11, 2017
1 parent b8c0283 commit 456aeec
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 6 deletions.
4 changes: 2 additions & 2 deletions api/v1/helpers/verbs/doFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const fu = require('./findUtils');
const COUNT_HEADER_NAME = require('../../constants').COUNT_HEADER_NAME;
const httpStatus = require('../../constants').httpStatus;
const redisCache = require('../../../../cache/redisCache').client;
const SECS_IN_MIN = 60;
const cacheExpiry = require('../../../../config').CACHE_EXPIRY_IN_SECS;

/**
* Finds all matching records but only returns a subset of the results for
Expand Down Expand Up @@ -134,7 +134,7 @@ function doFindResponse(reqResNext, props, opts, cacheKey) {
// cache the object by cacheKey. Store the key-value pair in cache
// with an expiry of 1 minute (60s)
const strObj = JSON.stringify(retval);
redisCache.setex(cacheKey, SECS_IN_MIN, strObj);
redisCache.setex(cacheKey, cacheExpiry, strObj);
}
})
.catch((err) => u.handleError(reqResNext.next, err, props.modelName));
Expand Down
7 changes: 4 additions & 3 deletions api/v1/helpers/verbs/doGet.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
const u = require('./utils');
const httpStatus = require('../../constants').httpStatus;
const redisCache = require('../../../../cache/redisCache').client;

const SECS_IN_MIN = 60;
const cacheExpiry = require('../../../../config').CACHE_EXPIRY_IN_SECS;

/**
* Retrieves a record and sends it back in the json response with status code
Expand All @@ -41,6 +40,8 @@ function doGet(req, res, next, props) {
cacheKey += reqParams.fields.value;
}

console.log(cacheKey);

redisCache.get(cacheKey, (cacheErr, reply) => {
if (cacheErr || !reply) {
// if err or no reply, get from db and set redis cache
Expand All @@ -51,7 +52,7 @@ function doGet(req, res, next, props) {
// cache the object by cacheKey. Store the key-value pair in cache
// with an expiry of 1 minute (60s)
const strObj = JSON.stringify(o);
redisCache.setex(cacheKey, SECS_IN_MIN, strObj);
redisCache.setex(cacheKey, cacheExpiry, strObj);
})
.catch((err) => u.handleError(next, err, props.modelName));
} else {
Expand Down
3 changes: 3 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const auditSubjects = pe.AUDIT_SUBJECTS || 'NONE';
const auditSamples = pe.AUDIT_SAMPLES || 'NONE';
const auditAspects = pe.AUDIT_ASPECTS || 'NONE';

const CACHE_EXPIRY_IN_SECS = 60;

module.exports = {

api: {
Expand Down Expand Up @@ -162,4 +164,5 @@ module.exports = {
auditSubjects,
auditSamples,
auditAspects,
CACHE_EXPIRY_IN_SECS,
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
"test-api": "mocha -R dot --recursive tests/api",
"test-disablehttp": "DISABLE_HTTP=true mocha -R dot --recursive tests/disableHttp",
"test-enforced": "USE_ACCESS_TOKEN=true mocha -R dot --recursive tests/enforceToken",
"test-cache-persp": "ENABLE_CACHE_PERSPECTIVE=true mocha -R dot --recursive tests/enableCache",
"test-db": "npm run checkdb && mocha -R dot --recursive tests/db",
"test-view": "NODE_ENV=build mocha -R dot --recursive --compilers js:babel-core/register --require ./tests/view/setup.js tests/view",
"test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R dot --recursive tests/api tests/clock tests/config tests/db tests/jobQueue tests/realtime tests/tokenNotReq && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage && npm run test-view && npm run test-disablehttp && npm run test-enforced",
"test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R dot --recursive tests/api tests/clock tests/config tests/db tests/jobQueue tests/realtime tests/tokenNotReq && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage && npm run test-view && npm run test-disablehttp && npm run test-enforced && npm run test-cache-persp",
"undo-migratedb": "node db/migrateUndo.js",
"view": "NODE_ENV=production gulp browserifyViews && npm start"
},
Expand Down
161 changes: 161 additions & 0 deletions tests/enableCache/perspectives.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/

/**
* tests/enableCache/perspectives.js
*/
'use strict'; // eslint-disable-line strict

const supertest = require('supertest');
const api = supertest(require('../../index').app);
const constants = require('../../api/v1/constants');
const tu = require('../testUtils');
const u = require('./utils');
const path = '/v1/perspectives';
const expect = require('chai').expect;
const redisCache = require('../../cache/redisCache').client;
const ZERO = 0;
const ONE = 1;

describe(`api: GET ${path}`, () => {
let lensId;
let token;
let perspectiveId;
// let perspectiveName;

before((done) => {
redisCache.flushdb();
tu.createToken()
.then((returnedToken) => {
token = returnedToken;
done();
})
.catch(done);
});

before((done) => {
u.doSetup()
.then((createdLens) => tu.db.Perspective.create({
name: `${tu.namePrefix}testPersp`,
lensId: createdLens.id,
rootSubject: 'myMainSubject',
aspectFilter: ['temperature', 'humidity'],
aspectTagFilter: ['temp', 'hum'],
subjectTagFilter: ['ea', 'na'],
statusFilter: ['Critical', '-OK'],
}))
.then((createdPersp) => {
lensId = createdPersp.lensId;
perspectiveId = createdPersp.id;
// perspectiveName = createdPersp.name;
done();
})
.catch(done);
});

after(u.forceDelete);
after(tu.forceDeleteUser);

it('basic get', (done) => {
api.get(path)
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.end((err, res) => {
if (err) {
done(err);
}

expect(res.body).to.have.length(ONE);
expect(res.body[ZERO].name).to.be.equal('___testPersp');
expect(res.body).to.have.deep.property('[0].lensId', lensId);

done();
});
});

it('basic get, response present in cache', (done) => {
redisCache.get('{\"where\":{}}', (cacheErr, reply) => {
if (reply) {
const jsonReply = JSON.parse(reply);
expect(jsonReply).to.have.length(ONE);
expect(jsonReply[ZERO].name).to.be.equal('___testPersp');
expect(jsonReply).to.have.deep.property('[0].lensId', lensId);

done();
} else {
throw new Error('Expected response value in cache');
}
});
});

it('get with limit and sort', (done) => {
api.get(path + '?limit=10&sort=name')
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.end((err, res) => {
if (err) {
done(err);
}

expect(res.body).to.have.length(ONE);
expect(res.body[ZERO].name).to.be.equal('___testPersp');
expect(res.body).to.have.deep.property('[0].lensId', lensId);

done();
});
});

it('get with limit and sort, response present in cache', (done) => {
redisCache.get('{"order":["name"],"limit":10,"where":{}}',
(cacheErr, reply) => {
if (reply) {
const jsonReply = JSON.parse(reply);
expect(jsonReply).to.have.length(ONE);
expect(jsonReply[ZERO].name).to.be.equal('___testPersp');
expect(jsonReply).to.have.deep.property('[0].lensId', lensId);

done();
} else {
throw new Error('Expected response value in cache');
}
});
});

it('basic get by id', (done) => {
api.get(`${path}/${perspectiveId}`)
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.end((err, res) => {
if (err) {
done(err);
}

expect(res.body.name).to.equal(`${tu.namePrefix}testPersp`);
expect(res.body.rootSubject).to.equal('myMainSubject');
expect(res.body.lensId).to.equal(lensId);

done();
});
});

it('basic get by id, response present in cache', (done) => {
redisCache.get(perspectiveId,
(cacheErr, reply) => {
if (reply) {
const jsonReply = JSON.parse(reply);
expect(jsonReply.name).to.equal(`${tu.namePrefix}testPersp`);
expect(jsonReply.rootSubject).to.equal('myMainSubject');
expect(jsonReply.lensId).to.equal(lensId);

done();
} else {
throw new Error('Expected response value in cache');
}
});
});
});
47 changes: 47 additions & 0 deletions tests/enableCache/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/

/**
* tests/enableCache/utils.js
*/
'use strict'; // eslint-disable-line strict

const tu = require('../testUtils');
const path = require('path');
const fs = require('fs');

const testStartTime = new Date();

module.exports = {
doSetup() {
return new tu.db.Sequelize.Promise((resolve, reject) => {
const willSendthis = fs.readFileSync(
path.join(__dirname,
'../api/v1/apiTestsUtils/lens.zip')
);
const lens = {
name: `${tu.namePrefix}testLensName`,
sourceName: 'testSourceLensName',
description: 'test Description',
sourceDescription: 'test Source Description',
isPublished: true,
library: willSendthis,
};
tu.db.Lens.create(lens)
.then(resolve)
.catch(reject);
});
},

forceDelete(done) {
tu.forceDelete(tu.db.Perspective, testStartTime)
.then(() => tu.forceDelete(tu.db.Lens, testStartTime))
.then(() => done())
.catch(done);
},
};

0 comments on commit 456aeec

Please sign in to comment.