getPreviousDocOpsTests.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/RedisManager"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "RedisManager.getPreviousDocOpsTests", ->
  7. beforeEach ->
  8. @callback = sinon.stub()
  9. @RedisManager = SandboxedModule.require modulePath, requires:
  10. "redis-sharelatex" : createClient: () =>
  11. @rclient ?=
  12. auth: ->
  13. multi: => @rclient
  14. "logger-sharelatex": @logger = { error: sinon.stub(), log: sinon.stub() }
  15. @doc_id = "doc-id-123"
  16. describe "with a start and an end value", ->
  17. beforeEach ->
  18. @first_version_in_redis = 30
  19. @version = 70
  20. @length = @version - @first_version_in_redis
  21. @start = 50
  22. @end = 60
  23. @ops = [
  24. { "mock": "op-1" },
  25. { "mock": "op-2" }
  26. ]
  27. @jsonOps = @ops.map (op) -> JSON.stringify op
  28. @rclient.llen = sinon.stub().callsArgWith(1, null, @length)
  29. @rclient.get = sinon.stub().callsArgWith(1, null, @version.toString())
  30. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonOps)
  31. @RedisManager.getPreviousDocOps(@doc_id, @start, @end, @callback)
  32. it "should get the length of the existing doc ops", ->
  33. @rclient.llen
  34. .calledWith("DocOps:#{@doc_id}")
  35. .should.equal true
  36. it "should get the current version of the doc", ->
  37. @rclient.get
  38. .calledWith("DocVersion:#{@doc_id}")
  39. .should.equal true
  40. it "should get the appropriate docs ops", ->
  41. @rclient.lrange
  42. .calledWith("DocOps:#{@doc_id}", @start - @first_version_in_redis, @end - @first_version_in_redis)
  43. .should.equal true
  44. it "should return the docs with the doc ops deserialized", ->
  45. @callback.calledWith(null, @ops).should.equal true
  46. describe "with an end value of -1", ->
  47. beforeEach ->
  48. @first_version_in_redis = 30
  49. @version = 70
  50. @length = @version - @first_version_in_redis
  51. @start = 50
  52. @end = -1
  53. @ops = [
  54. { "mock": "op-1" },
  55. { "mock": "op-2" }
  56. ]
  57. @jsonOps = @ops.map (op) -> JSON.stringify op
  58. @rclient.llen = sinon.stub().callsArgWith(1, null, @length)
  59. @rclient.get = sinon.stub().callsArgWith(1, null, @version.toString())
  60. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonOps)
  61. @RedisManager.getPreviousDocOps(@doc_id, @start, @end, @callback)
  62. it "should get the appropriate docs ops to the end of list", ->
  63. @rclient.lrange
  64. .calledWith("DocOps:#{@doc_id}", @start - @first_version_in_redis, -1)
  65. .should.equal true
  66. it "should return the docs with the doc ops deserialized", ->
  67. @callback.calledWith(null, @ops).should.equal true
  68. describe "when the requested range is not in Redis", ->
  69. beforeEach ->
  70. @first_version_in_redis = 30
  71. @version = 70
  72. @length = @version - @first_version_in_redis
  73. @start = 20
  74. @end = -1
  75. @ops = [
  76. { "mock": "op-1" },
  77. { "mock": "op-2" }
  78. ]
  79. @jsonOps = @ops.map (op) -> JSON.stringify op
  80. @rclient.llen = sinon.stub().callsArgWith(1, null, @length)
  81. @rclient.get = sinon.stub().callsArgWith(1, null, @version.toString())
  82. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonOps)
  83. @RedisManager.getPreviousDocOps(@doc_id, @start, @end, @callback)
  84. it "should return an error", ->
  85. @callback.calledWith(new Error("range is not loaded in redis")).should.equal true
  86. it "should log out the problem", ->
  87. @logger.error.called.should.equal true