flushProjectTests.js 4.3 KB

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