flushProjectTests.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/ProjectManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "ProjectManager - flushProject", ->
  7. beforeEach ->
  8. @ProjectManager = SandboxedModule.require modulePath, requires:
  9. "./RedisManager": @RedisManager = {}
  10. "./ProjectHistoryRedisManager": @ProjectHistoryRedisManager = {}
  11. "./DocumentManager": @DocumentManager = {}
  12. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  13. "./HistoryManager": @HistoryManager = {}
  14. "./Metrics": @Metrics =
  15. Timer: class Timer
  16. done: sinon.stub()
  17. @project_id = "project-id-123"
  18. @callback = sinon.stub()
  19. describe "successfully", ->
  20. beforeEach (done) ->
  21. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  22. @RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, @doc_ids)
  23. @DocumentManager.flushDocIfLoadedWithLock = sinon.stub().callsArg(2)
  24. @ProjectManager.flushProjectWithLocks @project_id, (error) =>
  25. @callback(error)
  26. done()
  27. it "should get the doc ids in the project", ->
  28. @RedisManager.getDocIdsInProject
  29. .calledWith(@project_id)
  30. .should.equal true
  31. it "should flush each doc in the project", ->
  32. for doc_id in @doc_ids
  33. @DocumentManager.flushDocIfLoadedWithLock
  34. .calledWith(@project_id, doc_id)
  35. .should.equal true
  36. it "should call the callback without error", ->
  37. @callback.calledWith(null).should.equal true
  38. it "should time the execution", ->
  39. @Metrics.Timer::done.called.should.equal true
  40. describe "when a doc errors", ->
  41. beforeEach (done) ->
  42. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  43. @RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, @doc_ids)
  44. @DocumentManager.flushDocIfLoadedWithLock = sinon.spy (project_id, doc_id, callback = (error) ->) =>
  45. if doc_id == "doc-id-1"
  46. callback(@error = new Error("oops, something went wrong"))
  47. else
  48. callback()
  49. @ProjectManager.flushProjectWithLocks @project_id, (error) =>
  50. @callback(error)
  51. done()
  52. it "should still flush each doc in the project", ->
  53. for doc_id in @doc_ids
  54. @DocumentManager.flushDocIfLoadedWithLock
  55. .calledWith(@project_id, doc_id)
  56. .should.equal true
  57. it "should record the error", ->
  58. @logger.error
  59. .calledWith(err: @error, project_id: @project_id, doc_id: "doc-id-1", "error flushing doc")
  60. .should.equal true
  61. it "should call the callback with an error", ->
  62. @callback.calledWith(new Error()).should.equal true
  63. it "should time the execution", ->
  64. @Metrics.Timer::done.called.should.equal true