test.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. @ioredisConstructor = ioredisConstructor = sinon.stub()
  12. @ioredis = class IoRedis
  13. constructor: ioredisConstructor
  14. on: sinon.stub()
  15. @ioredis.Cluster = class Cluster
  16. constructor: (@config, @options) ->
  17. on: sinon.stub()
  18. @redis = SandboxedModule.require modulePath, requires:
  19. "ioredis": @ioredis
  20. @auth_pass = "1234 pass"
  21. describe "single node redis", ->
  22. beforeEach ->
  23. @standardOpts =
  24. auth_pass: @auth_pass
  25. port: 1234
  26. host: "redis.mysite.env"
  27. it "should use the ioredis driver in single-instance mode if a non array is passed", ->
  28. client = @redis.createClient @standardOpts
  29. assert.equal(client.constructor, @ioredis)
  30. it "should call createClient for the ioredis driver in single-instance mode if a non array is passed", ->
  31. client = @redis.createClient @standardOpts
  32. @ioredisConstructor.calledWith(@standardOpts).should.equal true
  33. describe "cluster", ->
  34. beforeEach ->
  35. @cluster = [{"mock": "cluster"}, { "mock": "cluster2"}]
  36. @extraOptions = {keepAlive:100}
  37. @settings =
  38. cluster: @cluster
  39. redisOptions: @extraOptions
  40. key_schema: {foo: (x) -> "#{x}"}
  41. it "should pass the options correctly though with no options", ->
  42. client = @redis.createClient cluster: @cluster
  43. assert(client instanceof @ioredis.Cluster)
  44. client.config.should.deep.equal @cluster
  45. it "should not pass the key_schema through to the driver", ->
  46. client = @redis.createClient cluster: @cluster, key_schema: "foobar"
  47. assert(client instanceof @ioredis.Cluster)
  48. client.config.should.deep.equal @cluster
  49. expect(client.options).to.deep.equal {retry_max_delay: 5000}
  50. it "should pass the options correctly though with additional options", ->
  51. client = @redis.createClient @settings
  52. assert(client instanceof @ioredis.Cluster)
  53. client.config.should.deep.equal @cluster
  54. # need to use expect here because of _.clone in sandbox
  55. expect(client.options).to.deep.equal {redisOptions: @extraOptions, retry_max_delay: 5000}
  56. describe "monkey patch ioredis exec", ->
  57. beforeEach ->
  58. @callback = sinon.stub()
  59. @results = []
  60. @multiOrig = { exec: sinon.stub().yields(null, @results)}
  61. @client = { multi: sinon.stub().returns(@multiOrig) }
  62. @redis._monkeyPatchIoredisExec(@client)
  63. @multi = @client.multi()
  64. it "should return the old redis format for an array", ->
  65. @results[0] = [null, 42]
  66. @results[1] = [null, "foo"]
  67. @multi.exec @callback
  68. @callback.calledWith(null, [42, "foo"]).should.equal true
  69. it "should return the old redis format when there is an error", ->
  70. @results[0] = [null, 42]
  71. @results[1] = ["error", "foo"]
  72. @multi.exec @callback
  73. @callback.calledWith("error").should.equal true