flushAndDeleteProjectTests.js 4.7 KB

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