MongoManagerTests.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/MongoManager'
  5. ObjectId = require("mongojs").ObjectId
  6. assert = require("chai").assert
  7. describe "MongoManager", ->
  8. beforeEach ->
  9. @MongoManager = SandboxedModule.require modulePath, requires:
  10. "./mongojs":
  11. db: @db = { docs: {}, docOps: {} }
  12. ObjectId: ObjectId
  13. 'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
  14. 'logger-sharelatex': {log: ()->}
  15. @project_id = ObjectId().toString()
  16. @doc_id = ObjectId().toString()
  17. @callback = sinon.stub()
  18. @stubbedErr = new Error("hello world")
  19. describe "findDoc", ->
  20. beforeEach ->
  21. @doc = { name: "mock-doc"}
  22. @db.docs.find = sinon.stub().callsArgWith(2, null, [@doc])
  23. @filter = { lines: true }
  24. @MongoManager.findDoc @project_id, @doc_id, @filter, @callback
  25. it "should find the doc", ->
  26. @db.docs.find
  27. .calledWith({
  28. _id: ObjectId(@doc_id)
  29. project_id: ObjectId(@project_id)
  30. }, @filter)
  31. .should.equal true
  32. it "should call the callback with the doc", ->
  33. @callback.calledWith(null, @doc).should.equal true
  34. describe "getProjectsDocs", ->
  35. beforeEach ->
  36. @filter = {lines: true}
  37. @doc1 = { name: "mock-doc1" }
  38. @doc2 = { name: "mock-doc2" }
  39. @doc3 = { name: "mock-doc3" }
  40. @doc4 = { name: "mock-doc4" }
  41. @db.docs.find = sinon.stub().callsArgWith(2, null, [@doc, @doc3, @doc4])
  42. describe "with included_deleted = false", ->
  43. beforeEach ->
  44. @MongoManager.getProjectsDocs @project_id, include_deleted: false, @filter, @callback
  45. it "should find the non-deleted docs via the project_id", ->
  46. @db.docs.find
  47. .calledWith({
  48. project_id: ObjectId(@project_id)
  49. deleted: { $ne: true }
  50. }, @filter)
  51. .should.equal true
  52. it "should call the callback with the docs", ->
  53. @callback.calledWith(null, [@doc, @doc3, @doc4]).should.equal true
  54. describe "with included_deleted = true", ->
  55. beforeEach ->
  56. @MongoManager.getProjectsDocs @project_id, include_deleted: true, @filter, @callback
  57. it "should find all via the project_id", ->
  58. @db.docs.find
  59. .calledWith({
  60. project_id: ObjectId(@project_id)
  61. }, @filter)
  62. .should.equal true
  63. it "should call the callback with the docs", ->
  64. @callback.calledWith(null, [@doc, @doc3, @doc4]).should.equal true
  65. describe "upsertIntoDocCollection", ->
  66. beforeEach ->
  67. @db.docs.update = sinon.stub().callsArgWith(3, @stubbedErr)
  68. @oldRev = 77
  69. it "should upsert the document", (done)->
  70. @MongoManager.upsertIntoDocCollection @project_id, @doc_id, {@lines}, (err)=>
  71. args = @db.docs.update.args[0]
  72. assert.deepEqual args[0], {_id: ObjectId(@doc_id)}
  73. assert.equal args[1]["$set"]["lines"], @lines
  74. assert.equal args[1]["$inc"]["rev"], 1
  75. assert.deepEqual args[1]["$set"]["project_id"], ObjectId(@project_id)
  76. done()
  77. it "should return the error", (done)->
  78. @MongoManager.upsertIntoDocCollection @project_id, @doc_id, {@lines}, (err)=>
  79. err.should.equal @stubbedErr
  80. done()
  81. describe "markDocAsDeleted", ->
  82. beforeEach ->
  83. @db.docs.update = sinon.stub().callsArgWith(2, @stubbedErr)
  84. @oldRev = 77
  85. it "should process the update", (done) ->
  86. @MongoManager.markDocAsDeleted @project_id, @doc_id, (err)=>
  87. args = @db.docs.update.args[0]
  88. assert.deepEqual args[0], {_id: ObjectId(@doc_id), project_id: ObjectId(@project_id)}
  89. assert.equal args[1]["$set"]["deleted"], true
  90. done()
  91. it "should return the error", (done)->
  92. @MongoManager.markDocAsDeleted @project_id, @doc_id, (err)=>
  93. err.should.equal @stubbedErr
  94. done()
  95. describe "destroyDoc", ->
  96. beforeEach (done) ->
  97. @db.docs.remove = sinon.stub().yields()
  98. @db.docOps.remove = sinon.stub().yields()
  99. @MongoManager.destroyDoc '123456789012', done
  100. it "should destroy the doc", ->
  101. sinon.assert.calledWith(@db.docs.remove, {_id: ObjectId('123456789012')})
  102. it "should destroy the docOps", ->
  103. sinon.assert.calledWith(@db.docOps.remove, {doc_id: ObjectId('123456789012')})
  104. describe "getDocVersion", ->
  105. describe "when the doc exists", ->
  106. beforeEach ->
  107. @doc =
  108. version: @version = 42
  109. @db.docOps.find = sinon.stub().callsArgWith(2, null, [@doc])
  110. @MongoManager.getDocVersion @doc_id, @callback
  111. it "should look for the doc in the database", ->
  112. @db.docOps.find
  113. .calledWith({ doc_id: ObjectId(@doc_id) }, {version: 1})
  114. .should.equal true
  115. it "should call the callback with the version", ->
  116. @callback.calledWith(null, @version).should.equal true
  117. describe "when the doc doesn't exist", ->
  118. beforeEach ->
  119. @db.docOps.find = sinon.stub().callsArgWith(2, null, [])
  120. @MongoManager.getDocVersion @doc_id, @callback
  121. it "should call the callback with 0", ->
  122. @callback.calledWith(null, 0).should.equal true
  123. describe "setDocVersion", ->
  124. beforeEach ->
  125. @version = 42
  126. @db.docOps.update = sinon.stub().callsArg(3)
  127. @MongoManager.setDocVersion @doc_id, @version, @callback
  128. it "should update the doc version", ->
  129. @db.docOps.update
  130. .calledWith({
  131. doc_id: ObjectId(@doc_id)
  132. }, {
  133. $set:
  134. version: @version
  135. }, {
  136. upsert: true
  137. })
  138. .should.equal true
  139. it "should call the callback", ->
  140. @callback.called.should.equal true