flushAndDeleteProjectTests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 - flushAndDeleteProject', 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.DocumentManager = {
  15. promises: {
  16. flushAndDeleteDocWithLock: sinon.stub().resolves(),
  17. },
  18. }
  19. this.HistoryManager = {
  20. promises: {
  21. flushProjectChanges: sinon.stub().resolves(),
  22. },
  23. }
  24. this.Metrics = {
  25. Timer: class Timer {},
  26. }
  27. this.Metrics.Timer.prototype.done = sinon.stub()
  28. this.ProjectManager = SandboxedModule.require(MODULE_PATH, {
  29. requires: {
  30. './RedisManager': this.RedisManager,
  31. './ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
  32. './DocumentManager': this.DocumentManager,
  33. './HistoryManager': this.HistoryManager,
  34. './Metrics': this.Metrics,
  35. },
  36. })
  37. })
  38. describe('successfully', function () {
  39. beforeEach(async function () {
  40. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  41. this.RedisManager.promises.getDocIdsInProject.resolves(this.doc_ids)
  42. await this.ProjectManager.promises.flushAndDeleteProjectWithLocks(
  43. this.project_id,
  44. {}
  45. )
  46. })
  47. it('should get the doc ids in the project', function () {
  48. this.RedisManager.promises.getDocIdsInProject.should.have.been.calledWith(
  49. this.project_id
  50. )
  51. })
  52. it('should delete each doc in the project', function () {
  53. for (const docId of this.doc_ids) {
  54. this.DocumentManager.promises.flushAndDeleteDocWithLock.should.have.been.calledWith(
  55. this.project_id,
  56. docId,
  57. {}
  58. )
  59. }
  60. })
  61. it('should flush project history', function () {
  62. this.HistoryManager.promises.flushProjectChanges.should.have.been.calledWith(
  63. this.project_id,
  64. {}
  65. )
  66. })
  67. it('should time the execution', function () {
  68. this.Metrics.Timer.prototype.done.called.should.equal(true)
  69. })
  70. })
  71. describe('when a doc errors', function () {
  72. beforeEach(async function () {
  73. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  74. this.RedisManager.promises.getDocIdsInProject.resolves(this.doc_ids)
  75. this.DocumentManager.promises.flushAndDeleteDocWithLock.callsFake(
  76. async (projectId, docId, options) => {
  77. if (docId === 'doc-id-1') {
  78. throw new Error('oops, something went wrong')
  79. }
  80. }
  81. )
  82. await expect(
  83. this.ProjectManager.promises.flushAndDeleteProjectWithLocks(
  84. this.project_id,
  85. {}
  86. )
  87. ).to.be.rejected
  88. })
  89. it('should still flush each doc in the project', function () {
  90. for (const docId of this.doc_ids) {
  91. this.DocumentManager.promises.flushAndDeleteDocWithLock.should.have.been.calledWith(
  92. this.project_id,
  93. docId,
  94. {}
  95. )
  96. }
  97. })
  98. it('should still flush project history', function () {
  99. this.HistoryManager.promises.flushProjectChanges.should.have.been.calledWith(
  100. this.project_id,
  101. {}
  102. )
  103. })
  104. it('should time the execution', function () {
  105. this.Metrics.Timer.prototype.done.called.should.equal(true)
  106. })
  107. })
  108. })