flushProjectTests.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const SandboxedModule = require('sandboxed-module')
  4. const MODULE_PATH = '../../../../app/js/ProjectManager.js'
  5. describe('ProjectManager - flushProject', function () {
  6. beforeEach(function () {
  7. this.project_id = 'project-id-123'
  8. this.RedisManager = {
  9. promises: {
  10. getDocIdsInProject: sinon.stub(),
  11. },
  12. }
  13. this.ProjectHistoryRedisManager = {}
  14. this.Metrics = {
  15. Timer: class Timer {},
  16. }
  17. this.DocumentManager = {
  18. promises: {
  19. flushDocIfLoadedWithLock: sinon.stub().resolves(),
  20. },
  21. }
  22. this.HistoryManager = {}
  23. this.Metrics.Timer.prototype.done = sinon.stub()
  24. this.ProjectManager = SandboxedModule.require(MODULE_PATH, {
  25. requires: {
  26. './RedisManager': this.RedisManager,
  27. './ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
  28. './DocumentManager': this.DocumentManager,
  29. './HistoryManager': this.HistoryManager,
  30. './Metrics': this.Metrics,
  31. },
  32. })
  33. })
  34. describe('successfully', function () {
  35. beforeEach(async function () {
  36. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  37. this.RedisManager.promises.getDocIdsInProject.resolves(this.doc_ids)
  38. await this.ProjectManager.promises.flushProjectWithLocks(this.project_id)
  39. })
  40. it('should get the doc ids in the project', function () {
  41. this.RedisManager.promises.getDocIdsInProject.should.have.been.calledWith(
  42. this.project_id
  43. )
  44. })
  45. it('should flush each doc in the project', function () {
  46. for (const docId of this.doc_ids) {
  47. this.DocumentManager.promises.flushDocIfLoadedWithLock.should.have.been.calledWith(
  48. this.project_id,
  49. docId
  50. )
  51. }
  52. })
  53. it('should time the execution', function () {
  54. this.Metrics.Timer.prototype.done.called.should.equal(true)
  55. })
  56. })
  57. describe('when a doc errors', function () {
  58. beforeEach(async function () {
  59. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  60. this.RedisManager.promises.getDocIdsInProject.resolves(this.doc_ids)
  61. this.DocumentManager.promises.flushDocIfLoadedWithLock.callsFake(
  62. async (projectId, docId) => {
  63. if (docId === 'doc-id-1') {
  64. throw new Error('oops, something went wrong')
  65. }
  66. }
  67. )
  68. await expect(
  69. this.ProjectManager.promises.flushProjectWithLocks(this.project_id)
  70. ).to.be.rejected
  71. })
  72. it('should still flush each doc in the project', function () {
  73. for (const docId of this.doc_ids) {
  74. this.DocumentManager.promises.flushDocIfLoadedWithLock.should.have.been.calledWith(
  75. this.project_id,
  76. docId
  77. )
  78. }
  79. })
  80. it('should time the execution', function () {
  81. this.Metrics.Timer.prototype.done.called.should.equal(true)
  82. })
  83. })
  84. })