flushProjectTests.coffee 2.5 KB

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