flushProjectTests.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS206: Consider reworking classes to avoid initClass
  14. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. const sinon = require('sinon')
  18. const modulePath = '../../../../app/js/ProjectManager.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('ProjectManager - flushProject', function () {
  21. beforeEach(function () {
  22. let Timer
  23. this.ProjectManager = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './RedisManager': (this.RedisManager = {}),
  26. './ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
  27. './DocumentManager': (this.DocumentManager = {}),
  28. './HistoryManager': (this.HistoryManager = {}),
  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.flushDocIfLoadedWithLock = sinon.stub().callsArg(2)
  52. return this.ProjectManager.flushProjectWithLocks(
  53. this.project_id,
  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 flush each doc in the project', function () {
  66. return Array.from(this.doc_ids).map(doc_id =>
  67. this.DocumentManager.flushDocIfLoadedWithLock
  68. .calledWith(this.project_id, doc_id)
  69. .should.equal(true)
  70. )
  71. })
  72. it('should call the callback without error', function () {
  73. return this.callback.calledWith(null).should.equal(true)
  74. })
  75. return it('should time the execution', function () {
  76. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  77. })
  78. })
  79. return describe('when a doc errors', function () {
  80. beforeEach(function (done) {
  81. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  82. this.RedisManager.getDocIdsInProject = sinon
  83. .stub()
  84. .callsArgWith(1, null, this.doc_ids)
  85. this.DocumentManager.flushDocIfLoadedWithLock = sinon.spy(
  86. (project_id, doc_id, callback) => {
  87. if (callback == null) {
  88. callback = function (error) {}
  89. }
  90. if (doc_id === 'doc-id-1') {
  91. return callback(
  92. (this.error = new Error('oops, something went wrong'))
  93. )
  94. } else {
  95. return callback()
  96. }
  97. }
  98. )
  99. return this.ProjectManager.flushProjectWithLocks(
  100. this.project_id,
  101. error => {
  102. this.callback(error)
  103. return done()
  104. }
  105. )
  106. })
  107. it('should still flush each doc in the project', function () {
  108. return Array.from(this.doc_ids).map(doc_id =>
  109. this.DocumentManager.flushDocIfLoadedWithLock
  110. .calledWith(this.project_id, doc_id)
  111. .should.equal(true)
  112. )
  113. })
  114. it('should record the error', function () {
  115. return this.logger.error
  116. .calledWith(
  117. { err: this.error, projectId: this.project_id, docId: 'doc-id-1' },
  118. 'error flushing doc'
  119. )
  120. .should.equal(true)
  121. })
  122. it('should call the callback with an error', function () {
  123. return this.callback
  124. .calledWith(sinon.match.instanceOf(Error))
  125. .should.equal(true)
  126. })
  127. return it('should time the execution', function () {
  128. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  129. })
  130. })
  131. })