flushProjectTests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 chai = require('chai');
  19. const should = chai.should();
  20. const modulePath = "../../../../app/js/ProjectManager.js";
  21. const SandboxedModule = require('sandboxed-module');
  22. describe("ProjectManager - flushProject", function() {
  23. beforeEach(function() {
  24. let Timer;
  25. this.ProjectManager = SandboxedModule.require(modulePath, { requires: {
  26. "./RedisManager": (this.RedisManager = {}),
  27. "./ProjectHistoryRedisManager": (this.ProjectHistoryRedisManager = {}),
  28. "./DocumentManager": (this.DocumentManager = {}),
  29. "logger-sharelatex": (this.logger = { log: sinon.stub(), error: sinon.stub() }),
  30. "./HistoryManager": (this.HistoryManager = {}),
  31. "./Metrics": (this.Metrics = {
  32. Timer: (Timer = (function() {
  33. Timer = class Timer {
  34. static initClass() {
  35. this.prototype.done = sinon.stub();
  36. }
  37. };
  38. Timer.initClass();
  39. return Timer;
  40. })())
  41. })
  42. }
  43. }
  44. );
  45. this.project_id = "project-id-123";
  46. return this.callback = sinon.stub();
  47. });
  48. describe("successfully", function() {
  49. beforeEach(function(done) {
  50. this.doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"];
  51. this.RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, this.doc_ids);
  52. this.DocumentManager.flushDocIfLoadedWithLock = sinon.stub().callsArg(2);
  53. return this.ProjectManager.flushProjectWithLocks(this.project_id, error => {
  54. this.callback(error);
  55. return done();
  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((doc_id) =>
  65. this.DocumentManager.flushDocIfLoadedWithLock
  66. .calledWith(this.project_id, doc_id)
  67. .should.equal(true));
  68. });
  69. it("should call the callback without error", function() {
  70. return this.callback.calledWith(null).should.equal(true);
  71. });
  72. return it("should time the execution", function() {
  73. return this.Metrics.Timer.prototype.done.called.should.equal(true);
  74. });
  75. });
  76. return describe("when a doc errors", function() {
  77. beforeEach(function(done) {
  78. this.doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"];
  79. this.RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, this.doc_ids);
  80. this.DocumentManager.flushDocIfLoadedWithLock = sinon.spy((project_id, doc_id, callback) => {
  81. if (callback == null) { callback = function(error) {}; }
  82. if (doc_id === "doc-id-1") {
  83. return callback(this.error = new Error("oops, something went wrong"));
  84. } else {
  85. return callback();
  86. }
  87. });
  88. return this.ProjectManager.flushProjectWithLocks(this.project_id, error => {
  89. this.callback(error);
  90. return done();
  91. });
  92. });
  93. it("should still flush each doc in the project", function() {
  94. return Array.from(this.doc_ids).map((doc_id) =>
  95. this.DocumentManager.flushDocIfLoadedWithLock
  96. .calledWith(this.project_id, doc_id)
  97. .should.equal(true));
  98. });
  99. it("should record the error", function() {
  100. return this.logger.error
  101. .calledWith({err: this.error, project_id: this.project_id, doc_id: "doc-id-1"}, "error flushing doc")
  102. .should.equal(true);
  103. });
  104. it("should call the callback with an error", function() {
  105. return this.callback.calledWith(new Error()).should.equal(true);
  106. });
  107. return it("should time the execution", function() {
  108. return this.Metrics.Timer.prototype.done.called.should.equal(true);
  109. });
  110. });
  111. });