Add unit tests for RedisWrapper

This commit is contained in:
Shane Kilkelly 2016-12-19 15:12:22 +00:00
parent d428f9adbc
commit 822f76a883
2 changed files with 67 additions and 1 deletions

View file

@ -21,7 +21,7 @@ module.exports = Redis =
if redisFeatureSettings?.cluster?
logger.log {feature}, "creating redis-cluster client"
rclient = new ioredis.Cluster(redisFeatureSettings.cluster)
rclient._is_redis_cluster = true
rclient.__is_redis_cluster = true
else
logger.log {feature}, "creating redis client"
rclient = redis.createClient(redisFeatureSettings)

View file

@ -0,0 +1,66 @@
assert = require("chai").assert
sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/infrastructure/RedisWrapper.js"
SandboxedModule = require('sandboxed-module')
describe 'RedisWrapper', ->
beforeEach ->
@featureName = 'somefeature'
@settings =
redis:
web:
port:"1234"
host:"somewhere"
password: "password"
somefeature: {}
@normalRedisInstance =
thisIsANormalRedisInstance: true
n: 1
@clusterRedisInstance =
thisIsAClusterRedisInstance: true
n: 2
@redis =
createClient: sinon.stub().returns(@normalRedisInstance)
@ioredis =
Cluster: sinon.stub().returns(@clusterRedisInstance)
@logger = {log: sinon.stub()}
@RedisWrapper = SandboxedModule.require modulePath, requires:
'logger-sharelatex': @logger
'settings-sharelatex': @settings
'redis-sharelatex': @redis
'ioredis': @ioredis
describe 'client', ->
beforeEach ->
@call = () =>
@RedisWrapper.client(@featureName)
describe 'when feature uses cluster', ->
beforeEach ->
@settings.redis.somefeature =
cluster: [1, 2, 3]
it 'should return a cluster client', ->
client = @call()
expect(client).to.equal @clusterRedisInstance
expect(client.__is_redis_cluster).to.equal true
describe 'when feature uses normal redis', ->
beforeEach ->
@settings.redis.somefeature =
port:"1234"
host:"somewhere"
password: "password"
it 'should return a regular redis client', ->
client = @call()
expect(client).to.equal @normalRedisInstance
expect(client.__is_redis_cluster).to.equal undefined