RedisManagerTests.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/RedisManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "RedisManager", ->
  8. beforeEach ->
  9. @RedisManager = SandboxedModule.require modulePath, requires:
  10. "redis-sharelatex" :
  11. createClient: () => @rclient =
  12. auth: sinon.stub()
  13. multi: () => @rclient
  14. "settings-sharelatex":
  15. redis:
  16. web:{}
  17. @doc_id = "doc-id-123"
  18. @project_id = "project-id-123"
  19. @batchSize = 100
  20. @callback = sinon.stub()
  21. describe "getOldestDocUpdates", ->
  22. beforeEach ->
  23. @rawUpdates = [ {v: 42, op: "mock-op-42"}, { v: 45, op: "mock-op-45" }]
  24. @jsonUpdates = (JSON.stringify(update) for update in @rawUpdates)
  25. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonUpdates)
  26. @RedisManager.getOldestDocUpdates @doc_id, @batchSize, @callback
  27. it "should read the updates from redis", ->
  28. @rclient.lrange
  29. .calledWith("UncompressedHistoryOps:#{@doc_id}", 0, @batchSize - 1)
  30. .should.equal true
  31. it "should call the callback with the unparsed ops", ->
  32. @callback.calledWith(null, @jsonUpdates).should.equal true
  33. describe "expandDocUpdates", ->
  34. beforeEach ->
  35. @RedisManager.expandDocUpdates @jsonUpdates, @callback
  36. it "should call the callback with the parsed ops", ->
  37. @callback.calledWith(null, @rawUpdates).should.equal true
  38. describe "deleteAppliedDocUpdates", ->
  39. beforeEach ->
  40. @rclient.lrem = sinon.stub()
  41. @rclient.srem = sinon.stub()
  42. @rclient.exec = sinon.stub().callsArgWith(0)
  43. @RedisManager.deleteAppliedDocUpdates @project_id, @doc_id, @jsonUpdates, @callback
  44. it "should delete the first update from redis", ->
  45. @rclient.lrem
  46. .calledWith("UncompressedHistoryOps:#{@doc_id}", 0, @jsonUpdates[0])
  47. .should.equal true
  48. it "should delete the second update from redis", ->
  49. @rclient.lrem
  50. .calledWith("UncompressedHistoryOps:#{@doc_id}", 0, @jsonUpdates[1])
  51. .should.equal true
  52. it "should delete the doc from the set of docs with history ops", ->
  53. @rclient.srem
  54. .calledWith("DocsWithHistoryOps:#{@project_id}", @doc_id)
  55. .should.equal true
  56. it "should call the callback ", ->
  57. @callback.called.should.equal true
  58. describe "getDocIdsWithHistoryOps", ->
  59. beforeEach ->
  60. @doc_ids = ["mock-id-1", "mock-id-2"]
  61. @rclient.smembers = sinon.stub().callsArgWith(1, null, @doc_ids)
  62. @RedisManager.getDocIdsWithHistoryOps @project_id, @callback
  63. it "should read the doc_ids from redis", ->
  64. @rclient.smembers
  65. .calledWith("DocsWithHistoryOps:#{@project_id}")
  66. .should.equal true
  67. it "should call the callback with the doc_ids", ->
  68. @callback.calledWith(null, @doc_ids).should.equal true