test.coffee 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "./index.coffee"
  7. expect = require("chai").expect
  8. describe "index", ->
  9. beforeEach ->
  10. @settings = {}
  11. @sentinelClient = set:->
  12. @normalRedisClient = get: ->
  13. @sentinel =
  14. createClient: sinon.stub().returns(@sentinelClient)
  15. @normalRedis =
  16. createClient: sinon.stub().returns(@normalRedisClient)
  17. @redis = SandboxedModule.require modulePath, requires:
  18. "redis-sentinel":@sentinel
  19. "redis":@normalRedis
  20. @auth_pass = "1234 pass"
  21. describe "sentinel", ->
  22. beforeEach ->
  23. @endpoints = [
  24. {host: '127.0.0.1', port: 26379},
  25. {host: '127.0.0.1', port: 26380}
  26. ]
  27. @masterName = "my master"
  28. @sentinelOptions =
  29. endpoints:@endpoints
  30. masterName:@masterName
  31. auth_pass:@auth_pass
  32. it "should use sentinal if the first argument in an array", ->
  33. client = @redis.createClient @sentinelOptions
  34. @sentinel.createClient.called.should.equal true
  35. @normalRedis.createClient.called.should.equal false
  36. client.should.equal @sentinelClient
  37. it "should pass the options correctly though", ->
  38. client = @redis.createClient @sentinelOptions
  39. @sentinel.createClient.calledWith(@endpoints, @masterName, auth_pass:@auth_pass).should.equal true
  40. client.should.equal @sentinelClient
  41. describe "normal redis", ->
  42. beforeEach ->
  43. @standardOpts =
  44. auth_pass: @auth_pass
  45. port: 1234
  46. host: "redis.mysite.env"
  47. it "should use the normal redis driver if a non array is passed", ->
  48. client = @redis.createClient @standardOpts
  49. @sentinel.createClient.called.should.equal false
  50. @normalRedis.createClient.called.should.equal true
  51. client.should.equal @normalRedisClient
  52. it "should use the normal redis driver if a non array is passed", ->
  53. client = @redis.createClient @standardOpts
  54. @normalRedis.createClient.calledWith(@standardOpts.port, @standardOpts.host, auth_pass:@auth_pass).should.equal true