flushAndDeleteProjectTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS206: Consider reworking classes to avoid initClass
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const modulePath = '../../../../app/js/ProjectManager.js'
  16. const SandboxedModule = require('sandboxed-module')
  17. describe('ProjectManager - flushAndDeleteProject', function () {
  18. beforeEach(function () {
  19. let Timer
  20. this.LockManager = {
  21. getLock: sinon.stub().yields(),
  22. releaseLock: sinon.stub().yields(),
  23. }
  24. this.ProjectManager = SandboxedModule.require(modulePath, {
  25. requires: {
  26. './RedisManager': (this.RedisManager = {}),
  27. './ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
  28. './DocumentManager': (this.DocumentManager = {}),
  29. './HistoryManager': (this.HistoryManager = {
  30. flushProjectChanges: sinon.stub().callsArg(2),
  31. }),
  32. './LockManager': this.LockManager,
  33. './Metrics': (this.Metrics = {
  34. Timer: (Timer = (function () {
  35. Timer = class Timer {
  36. static initClass() {
  37. this.prototype.done = sinon.stub()
  38. }
  39. }
  40. Timer.initClass()
  41. return Timer
  42. })()),
  43. }),
  44. },
  45. })
  46. this.project_id = 'project-id-123'
  47. return (this.callback = sinon.stub())
  48. })
  49. describe('successfully', function () {
  50. beforeEach(function (done) {
  51. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  52. this.RedisManager.getDocIdsInProject = sinon
  53. .stub()
  54. .callsArgWith(1, null, this.doc_ids)
  55. this.DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArg(3)
  56. return this.ProjectManager.flushAndDeleteProjectWithLocks(
  57. this.project_id,
  58. {},
  59. error => {
  60. this.callback(error)
  61. return done()
  62. }
  63. )
  64. })
  65. it('should get the doc ids in the project', function () {
  66. return this.RedisManager.getDocIdsInProject
  67. .calledWith(this.project_id)
  68. .should.equal(true)
  69. })
  70. it('should delete each doc in the project', function () {
  71. return Array.from(this.doc_ids).map(docId =>
  72. this.DocumentManager.flushAndDeleteDocWithLock
  73. .calledWith(this.project_id, docId, {})
  74. .should.equal(true)
  75. )
  76. })
  77. it('should flush project history', function () {
  78. return this.HistoryManager.flushProjectChanges
  79. .calledWith(this.project_id, {})
  80. .should.equal(true)
  81. })
  82. it('should call the callback without error', function () {
  83. return this.callback.calledWith(null).should.equal(true)
  84. })
  85. return it('should time the execution', function () {
  86. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  87. })
  88. })
  89. return describe('when a doc errors', function () {
  90. beforeEach(function (done) {
  91. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  92. this.RedisManager.getDocIdsInProject = sinon
  93. .stub()
  94. .callsArgWith(1, null, this.doc_ids)
  95. this.DocumentManager.flushAndDeleteDocWithLock = sinon.spy(
  96. (projectId, docId, options, callback) => {
  97. if (docId === 'doc-id-1') {
  98. return callback(
  99. (this.error = new Error('oops, something went wrong'))
  100. )
  101. } else {
  102. return callback()
  103. }
  104. }
  105. )
  106. return this.ProjectManager.flushAndDeleteProjectWithLocks(
  107. this.project_id,
  108. {},
  109. error => {
  110. this.callback(error)
  111. return done()
  112. }
  113. )
  114. })
  115. it('should still flush each doc in the project', function () {
  116. return Array.from(this.doc_ids).map(docId =>
  117. this.DocumentManager.flushAndDeleteDocWithLock
  118. .calledWith(this.project_id, docId, {})
  119. .should.equal(true)
  120. )
  121. })
  122. it('should still flush project history', function () {
  123. return this.HistoryManager.flushProjectChanges
  124. .calledWith(this.project_id, {})
  125. .should.equal(true)
  126. })
  127. it('should record the error', function () {
  128. return this.logger.error
  129. .calledWith(
  130. { err: this.error, projectId: this.project_id, docId: 'doc-id-1' },
  131. 'error deleting doc'
  132. )
  133. .should.equal(true)
  134. })
  135. it('should call the callback with an error', function () {
  136. return this.callback
  137. .calledWith(sinon.match.instanceOf(Error))
  138. .should.equal(true)
  139. })
  140. return it('should time the execution', function () {
  141. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  142. })
  143. })
  144. })