MongoManagerTests.coffee 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/MongoManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. {ObjectId} = require("mongojs")
  8. describe "MongoManager", ->
  9. beforeEach ->
  10. @MongoManager = SandboxedModule.require modulePath, requires:
  11. "./mongojs" : { db: @db = {}, ObjectId: ObjectId }
  12. @callback = sinon.stub()
  13. @doc_id = ObjectId().toString()
  14. describe "getLastCompressedUpdate", ->
  15. beforeEach ->
  16. @update = "mock-update"
  17. @db.docHistory = {}
  18. @db.docHistory.find = sinon.stub().returns @db.docHistory
  19. @db.docHistory.sort = sinon.stub().returns @db.docHistory
  20. @db.docHistory.limit = sinon.stub().returns @db.docHistory
  21. @db.docHistory.toArray = sinon.stub().callsArgWith(0, null, [@update])
  22. @MongoManager.getLastCompressedUpdate @doc_id, @callback
  23. it "should find the updates for the doc", ->
  24. @db.docHistory.find
  25. .calledWith(doc_id: ObjectId(@doc_id))
  26. .should.equal true
  27. it "should limit to one result", ->
  28. @db.docHistory.limit
  29. .calledWith(1)
  30. .should.equal true
  31. it "should sort in descending timestamp order", ->
  32. @db.docHistory.sort
  33. .calledWith("meta.end_ts": -1)
  34. .should.equal true
  35. it "should call the call back with the update", ->
  36. @callback.calledWith(null, @update).should.equal true
  37. describe "deleteCompressedUpdate", ->
  38. beforeEach ->
  39. @update_id = ObjectId().toString()
  40. @db.docHistory =
  41. remove: sinon.stub().callsArg(1)
  42. @MongoManager.deleteCompressedUpdate(@update_id, @callback)
  43. it "should remove the update", ->
  44. @db.docHistory.remove
  45. .calledWith(_id: ObjectId(@update_id))
  46. .should.equal true
  47. it "should call the callback", ->
  48. @callback.called.should.equal true
  49. describe "popLastCompressedUpdate", ->
  50. describe "when there is no last update", ->
  51. beforeEach ->
  52. @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null)
  53. @MongoManager.deleteCompressedUpdate = sinon.stub()
  54. @MongoManager.popLastCompressedUpdate @doc_id, @callback
  55. it "should get the last update", ->
  56. @MongoManager.getLastCompressedUpdate
  57. .calledWith(@doc_id)
  58. .should.equal true
  59. it "should not try to delete the last update", ->
  60. @MongoManager.deleteCompressedUpdate.called.should.equal false
  61. it "should call the callback with no update", ->
  62. @callback.calledWith(null, null).should.equal true
  63. describe "when there is an update", ->
  64. beforeEach ->
  65. @update = { _id: Object() }
  66. @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @update)
  67. @MongoManager.deleteCompressedUpdate = sinon.stub().callsArgWith(1, null)
  68. @MongoManager.popLastCompressedUpdate @doc_id, @callback
  69. it "should get the last update", ->
  70. @MongoManager.getLastCompressedUpdate
  71. .calledWith(@doc_id)
  72. .should.equal true
  73. it "should delete the last update", ->
  74. @MongoManager.deleteCompressedUpdate
  75. .calledWith(@update._id)
  76. .should.equal true
  77. it "should call the callback with the update", ->
  78. @callback.calledWith(null, @update).should.equal true
  79. describe "insertCompressedUpdate", ->
  80. beforeEach ->
  81. @update = { op: "op", meta: "meta", v: "v"}
  82. @db.docHistory =
  83. insert: sinon.stub().callsArg(1)
  84. @MongoManager.insertCompressedUpdate @doc_id, @update, @callback
  85. it "should insert the update", ->
  86. @db.docHistory.insert
  87. .calledWith({
  88. doc_id: ObjectId(@doc_id),
  89. op: @update.op,
  90. meta: @update.meta,
  91. v: @update.v
  92. })
  93. .should.equal true
  94. it "should call the callback", ->
  95. @callback.called.should.equal true
  96. describe "insertCompressedUpdates", ->
  97. beforeEach (done) ->
  98. @updates = [ "mock-update-1", "mock-update-2" ]
  99. @MongoManager.insertCompressedUpdate = sinon.stub().callsArg(2)
  100. @MongoManager.insertCompressedUpdates @doc_id, @updates, (args...) =>
  101. @callback(args...)
  102. done()
  103. it "should insert each update", ->
  104. for update in @updates
  105. @MongoManager.insertCompressedUpdate
  106. .calledWith(@doc_id, update)
  107. .should.equal true
  108. it "should call the callback", ->
  109. @callback.called.should.equal true
  110. describe "getUpdates", ->
  111. beforeEach ->
  112. @updates = ["mock-update"]
  113. @db.docHistory = {}
  114. @db.docHistory.find = sinon.stub().returns @db.docHistory
  115. @db.docHistory.sort = sinon.stub().returns @db.docHistory
  116. @db.docHistory.limit = sinon.stub().returns @db.docHistory
  117. @db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @updates)
  118. @from = 42
  119. @to = 55
  120. describe "with a to version", ->
  121. beforeEach ->
  122. @MongoManager.getUpdates @doc_id, from: @from, to: @to, @callback
  123. it "should find the all updates between the to and from versions", ->
  124. @db.docHistory.find
  125. .calledWith({
  126. doc_id: ObjectId(@doc_id)
  127. v: { $gte: @from, $lte: @to }
  128. })
  129. .should.equal true
  130. it "should sort in descending version order", ->
  131. @db.docHistory.sort
  132. .calledWith("v": -1)
  133. .should.equal true
  134. it "should not limit the results", ->
  135. @db.docHistory.limit
  136. .called.should.equal false
  137. it "should call the call back with the updates", ->
  138. @callback.calledWith(null, @updates).should.equal true
  139. describe "without a to version", ->
  140. beforeEach ->
  141. @MongoManager.getUpdates @doc_id, from: @from, @callback
  142. it "should find the all updates after the from version", ->
  143. @db.docHistory.find
  144. .calledWith({
  145. doc_id: ObjectId(@doc_id)
  146. v: { $gte: @from }
  147. })
  148. .should.equal true
  149. it "should call the call back with the updates", ->
  150. @callback.calledWith(null, @updates).should.equal true
  151. describe "with a limit", ->
  152. beforeEach ->
  153. @MongoManager.getUpdates @doc_id, from: @from, limit: @limit = 10, @callback
  154. it "should limit the results", ->
  155. @db.docHistory.limit
  156. .calledWith(@limit)
  157. .should.equal true