MongoManagerTests.coffee 4.8 KB

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