MongoManagerTests.coffee 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/MongoManager.js"
  6. packModulePath = "../../../../app/js/PackManager.js"
  7. SandboxedModule = require('sandboxed-module')
  8. {ObjectId} = require("mongojs")
  9. tk = require "timekeeper"
  10. describe "MongoManager", ->
  11. beforeEach ->
  12. tk.freeze(new Date())
  13. @MongoManager = SandboxedModule.require modulePath, requires:
  14. "./mongojs" : { db: @db = {}, ObjectId: ObjectId }
  15. "./PackManager" : @PackManager = {}
  16. 'metrics-sharelatex': {timeAsyncMethod: ()->}
  17. 'logger-sharelatex': {log: ()->}
  18. @callback = sinon.stub()
  19. @doc_id = ObjectId().toString()
  20. @project_id = ObjectId().toString()
  21. afterEach ->
  22. tk.reset()
  23. describe "getLastCompressedUpdate", ->
  24. beforeEach ->
  25. @update = "mock-update"
  26. @db.docHistory = {}
  27. @db.docHistory.find = sinon.stub().returns @db.docHistory
  28. @db.docHistory.findOne = sinon.stub().returns @db.docHistory
  29. @db.docHistory.sort = sinon.stub().returns @db.docHistory
  30. @db.docHistory.limit = sinon.stub().returns @db.docHistory
  31. @db.docHistory.toArray = sinon.stub().callsArgWith(0, null, [@update])
  32. @MongoManager.getLastCompressedUpdate @doc_id, @callback
  33. it "should find the updates for the doc", ->
  34. @db.docHistory.find
  35. .calledWith(doc_id: ObjectId(@doc_id))
  36. .should.equal true
  37. it "should limit to one result", ->
  38. @db.docHistory.limit
  39. .calledWith(1)
  40. .should.equal true
  41. it "should sort in descending version order", ->
  42. @db.docHistory.sort
  43. .calledWith(v: -1)
  44. .should.equal true
  45. it "should call the call back with the update", ->
  46. @callback.calledWith(null, @update).should.equal true
  47. describe "peekLastCompressedUpdate", ->
  48. describe "when there is no last update", ->
  49. beforeEach ->
  50. @PackManager.getLastPackFromIndex = sinon.stub().callsArgWith(1, null, null)
  51. @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null)
  52. @MongoManager.peekLastCompressedUpdate @doc_id, @callback
  53. it "should get the last update", ->
  54. @MongoManager.getLastCompressedUpdate
  55. .calledWith(@doc_id)
  56. .should.equal true
  57. it "should call the callback with no update", ->
  58. @callback.calledWith(null, null).should.equal true
  59. describe "when there is an update", ->
  60. beforeEach ->
  61. @update = { _id: Object() }
  62. @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @update)
  63. @MongoManager.peekLastCompressedUpdate @doc_id, @callback
  64. it "should get the last update", ->
  65. @MongoManager.getLastCompressedUpdate
  66. .calledWith(@doc_id)
  67. .should.equal true
  68. it "should call the callback with the update", ->
  69. @callback.calledWith(null, @update).should.equal true
  70. describe "when there is a last update in S3", ->
  71. beforeEach ->
  72. @update = { _id: Object(), v: 12345, v_end: 12345, inS3:true}
  73. @PackManager.getLastPackFromIndex = sinon.stub().callsArgWith(1, null, @update)
  74. @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null)
  75. @MongoManager.peekLastCompressedUpdate @doc_id, @callback
  76. it "should get the last update", ->
  77. @MongoManager.getLastCompressedUpdate
  78. .calledWith(@doc_id)
  79. .should.equal true
  80. it "should call the callback with a null update and the correct version", ->
  81. @callback.calledWith(null, null, @update.v_end).should.equal true
  82. describe "backportProjectId", ->
  83. beforeEach ->
  84. @db.docHistory =
  85. update: sinon.stub().callsArg(3)
  86. @MongoManager.backportProjectId @project_id, @doc_id, @callback
  87. it "should insert the project_id into all entries for the doc_id which don't have it set", ->
  88. @db.docHistory.update
  89. .calledWith({
  90. doc_id: ObjectId(@doc_id)
  91. project_id: { $exists: false }
  92. }, {
  93. $set: { project_id: ObjectId(@project_id) }
  94. }, {
  95. multi: true
  96. })
  97. .should.equal true
  98. it "should call the callback", ->
  99. @callback.called.should.equal true
  100. describe "getProjectMetaData", ->
  101. beforeEach ->
  102. @metadata = { "mock": "metadata" }
  103. @db.projectHistoryMetaData =
  104. find: sinon.stub().callsArgWith(1, null, [@metadata])
  105. @MongoManager.getProjectMetaData @project_id, @callback
  106. it "should look up the meta data in the db", ->
  107. @db.projectHistoryMetaData.find
  108. .calledWith({ project_id: ObjectId(@project_id) })
  109. .should.equal true
  110. it "should return the metadata", ->
  111. @callback.calledWith(null, @metadata).should.equal true
  112. describe "setProjectMetaData", ->
  113. beforeEach ->
  114. @metadata = { "mock": "metadata" }
  115. @db.projectHistoryMetaData =
  116. update: sinon.stub().callsArgWith(3, null, [@metadata])
  117. @MongoManager.setProjectMetaData @project_id, @metadata, @callback
  118. it "should upsert the metadata into the DB", ->
  119. @db.projectHistoryMetaData.update
  120. .calledWith({
  121. project_id: ObjectId(@project_id)
  122. }, {
  123. $set: @metadata
  124. }, {
  125. upsert: true
  126. })
  127. .should.equal true
  128. it "should call the callback", ->
  129. @callback.called.should.equal true