Skip to content

Commit

Permalink
Add redis testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
sdvcrx committed Dec 12, 2017
1 parent 8621996 commit 012a011
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cacheItem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const CacheDriver = require('./cacheDriver')

class CacheItem {
constructor(name = '', data = '', timeout = 0) {
constructor(name = '', data, timeout = 0) {
this.name = name
this.data = data
this.timeout = timeout
Expand Down
1 change: 1 addition & 0 deletions test/cacheItem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('CacheItem', () => {
it('expect return true if cache is empty', () => {
const item = new CacheItem()
expect(item.invalid()).to.be.true
expect(item.data).to.be.undefined
})
})

Expand Down
49 changes: 49 additions & 0 deletions test/redisCacheDriver.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const chai = require('chai')
const chaiAsPromised = require("chai-as-promised")
chai.use(chaiAsPromised)
const expect = chai.expect

const RedisCacheDriver = require('../redisCacheDriver')

const data = { data: 'test' }

function Timeout (secs) {
return new Promise((resolve) => {
setTimeout(resolve, secs * 1000)
})
}

function testRedisGet (keyName, data, done) {
const promise = RedisCacheDriver.get(keyName, data)
.then(d => d.data)

const chain = expect(promise)
.to.eventually.deep.equal(data)

if (done) {
chain.notify(done)
}
}

describe('RedisCacheDriver', () => {
describe('#get', function () {
this.timeout(2000)

it('expect get key from redis', (done) => {
RedisCacheDriver.set('key1', data).then(() => {
testRedisGet('key1', data, done)
}).catch(done)
})

it('expect get key from redis with timeout', (done) => {
RedisCacheDriver.set('key2', data, 1).then(() => {

testRedisGet('key2', data)

return Timeout(1)
}).then(() => {
testRedisGet('key2', undefined, done)
}).catch(done)
})
})
})

0 comments on commit 012a011

Please sign in to comment.