MongoManagerTests.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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