flushAndDeleteProjectTests.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.ProjectManager = SandboxedModule.require(modulePath, {
  21. requires: {
  22. './RedisManager': (this.RedisManager = {}),
  23. './ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
  24. './DocumentManager': (this.DocumentManager = {}),
  25. './HistoryManager': (this.HistoryManager = {
  26. flushProjectChanges: sinon.stub().callsArg(2),
  27. }),
  28. './Metrics': (this.Metrics = {
  29. Timer: (Timer = (function () {
  30. Timer = class Timer {
  31. static initClass() {
  32. this.prototype.done = sinon.stub()
  33. }
  34. }
  35. Timer.initClass()
  36. return Timer
  37. })()),
  38. }),
  39. },
  40. })
  41. this.project_id = 'project-id-123'
  42. return (this.callback = sinon.stub())
  43. })
  44. describe('successfully', function () {
  45. beforeEach(function (done) {
  46. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  47. this.RedisManager.getDocIdsInProject = sinon
  48. .stub()
  49. .callsArgWith(1, null, this.doc_ids)
  50. this.DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArg(3)
  51. return this.ProjectManager.flushAndDeleteProjectWithLocks(
  52. this.project_id,
  53. {},
  54. error => {
  55. this.callback(error)
  56. return done()
  57. }
  58. )
  59. })
  60. it('should get the doc ids in the project', function () {
  61. return this.RedisManager.getDocIdsInProject
  62. .calledWith(this.project_id)
  63. .should.equal(true)
  64. })
  65. it('should delete each doc in the project', function () {
  66. return Array.from(this.doc_ids).map(docId =>
  67. this.DocumentManager.flushAndDeleteDocWithLock
  68. .calledWith(this.project_id, docId, {})
  69. .should.equal(true)
  70. )
  71. })
  72. it('should flush project history', function () {
  73. return this.HistoryManager.flushProjectChanges
  74. .calledWith(this.project_id, {})
  75. .should.equal(true)
  76. })
  77. it('should call the callback without error', function () {
  78. return this.callback.calledWith(null).should.equal(true)
  79. })
  80. return it('should time the execution', function () {
  81. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  82. })
  83. })
  84. return describe('when a doc errors', function () {
  85. beforeEach(function (done) {
  86. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  87. this.RedisManager.getDocIdsInProject = sinon
  88. .stub()
  89. .callsArgWith(1, null, this.doc_ids)
  90. this.DocumentManager.flushAndDeleteDocWithLock = sinon.spy(
  91. (projectId, docId, options, callback) => {
  92. if (docId === 'doc-id-1') {
  93. return callback(
  94. (this.error = new Error('oops, something went wrong'))
  95. )
  96. } else {
  97. return callback()
  98. }
  99. }
  100. )
  101. return this.ProjectManager.flushAndDeleteProjectWithLocks(
  102. this.project_id,
  103. {},
  104. error => {
  105. this.callback(error)
  106. return done()
  107. }
  108. )
  109. })
  110. it('should still flush each doc in the project', function () {
  111. return Array.from(this.doc_ids).map(docId =>
  112. this.DocumentManager.flushAndDeleteDocWithLock
  113. .calledWith(this.project_id, docId, {})
  114. .should.equal(true)
  115. )
  116. })
  117. it('should still flush project history', function () {
  118. return this.HistoryManager.flushProjectChanges
  119. .calledWith(this.project_id, {})
  120. .should.equal(true)
  121. })
  122. it('should record the error', function () {
  123. return this.logger.error
  124. .calledWith(
  125. { err: this.error, projectId: this.project_id, docId: 'doc-id-1' },
  126. 'error deleting doc'
  127. )
  128. .should.equal(true)
  129. })
  130. it('should call the callback with an error', function () {
  131. return this.callback
  132. .calledWith(sinon.match.instanceOf(Error))
  133. .should.equal(true)
  134. })
  135. return it('should time the execution', function () {
  136. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  137. })
  138. })
  139. })