MongoManagerTests.coffee 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "getDocVersion", ->
  96. describe "when the doc exists", ->
  97. beforeEach ->
  98. @doc =
  99. version: @version = 42
  100. @db.docOps.find = sinon.stub().callsArgWith(2, null, [@doc])
  101. @MongoManager.getDocVersion @doc_id, @callback
  102. it "should look for the doc in the database", ->
  103. @db.docOps.find
  104. .calledWith({ doc_id: ObjectId(@doc_id) }, {version: 1})
  105. .should.equal true
  106. it "should call the callback with the version", ->
  107. @callback.calledWith(null, @version).should.equal true
  108. describe "when the doc doesn't exist", ->
  109. beforeEach ->
  110. @db.docOps.find = sinon.stub().callsArgWith(2, null, [])
  111. @MongoManager.getDocVersion @doc_id, @callback
  112. it "should call the callback with 0", ->
  113. @callback.calledWith(null, 0).should.equal true
  114. describe "setDocVersion", ->
  115. beforeEach ->
  116. @version = 42
  117. @db.docOps.update = sinon.stub().callsArg(3)
  118. @MongoManager.setDocVersion @doc_id, @version, @callback
  119. it "should update the doc version", ->
  120. @db.docOps.update
  121. .calledWith({
  122. doc_id: ObjectId(@doc_id)
  123. }, {
  124. $set:
  125. version: @version
  126. }, {
  127. upsert: true
  128. })
  129. .should.equal true
  130. it "should call the callback", ->
  131. @callback.called.should.equal true