| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- SandboxedModule = require('sandboxed-module')
- sinon = require('sinon')
- require('chai').should()
- modulePath = require('path').join __dirname, '../../../app/js/MongoManager'
- ObjectId = require("mongojs").ObjectId
- assert = require("chai").assert
- describe "MongoManager", ->
- beforeEach ->
- @MongoManager = SandboxedModule.require modulePath, requires:
- "./mongojs":
- db: @db = { docs: {}, docOps: {} }
- ObjectId: ObjectId
- 'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
- 'logger-sharelatex': {log: ()->}
- @project_id = ObjectId().toString()
- @doc_id = ObjectId().toString()
- @callback = sinon.stub()
- @stubbedErr = new Error("hello world")
- describe "findDoc", ->
- beforeEach ->
- @doc = { name: "mock-doc"}
- @db.docs.find = sinon.stub().callsArgWith(2, null, [@doc])
- @filter = { lines: true }
- @MongoManager.findDoc @project_id, @doc_id, @filter, @callback
- it "should find the doc", ->
- @db.docs.find
- .calledWith({
- _id: ObjectId(@doc_id)
- project_id: ObjectId(@project_id)
- }, @filter)
- .should.equal true
- it "should call the callback with the doc", ->
- @callback.calledWith(null, @doc).should.equal true
- describe "getProjectsDocs", ->
- beforeEach ->
- @filter = {lines: true}
- @doc1 = { name: "mock-doc1" }
- @doc2 = { name: "mock-doc2" }
- @doc3 = { name: "mock-doc3" }
- @doc4 = { name: "mock-doc4" }
- @db.docs.find = sinon.stub().callsArgWith(2, null, [@doc, @doc3, @doc4])
-
- describe "with included_deleted = false", ->
- beforeEach ->
- @MongoManager.getProjectsDocs @project_id, include_deleted: false, @filter, @callback
- it "should find the non-deleted docs via the project_id", ->
- @db.docs.find
- .calledWith({
- project_id: ObjectId(@project_id)
- deleted: { $ne: true }
- }, @filter)
- .should.equal true
- it "should call the callback with the docs", ->
- @callback.calledWith(null, [@doc, @doc3, @doc4]).should.equal true
-
- describe "with included_deleted = true", ->
- beforeEach ->
- @MongoManager.getProjectsDocs @project_id, include_deleted: true, @filter, @callback
- it "should find all via the project_id", ->
- @db.docs.find
- .calledWith({
- project_id: ObjectId(@project_id)
- }, @filter)
- .should.equal true
- it "should call the callback with the docs", ->
- @callback.calledWith(null, [@doc, @doc3, @doc4]).should.equal true
- describe "upsertIntoDocCollection", ->
- beforeEach ->
- @db.docs.update = sinon.stub().callsArgWith(3, @stubbedErr)
- @oldRev = 77
- it "should upsert the document", (done)->
- @MongoManager.upsertIntoDocCollection @project_id, @doc_id, {@lines}, (err)=>
- args = @db.docs.update.args[0]
- assert.deepEqual args[0], {_id: ObjectId(@doc_id)}
- assert.equal args[1]["$set"]["lines"], @lines
- assert.equal args[1]["$inc"]["rev"], 1
- assert.deepEqual args[1]["$set"]["project_id"], ObjectId(@project_id)
- done()
- it "should return the error", (done)->
- @MongoManager.upsertIntoDocCollection @project_id, @doc_id, {@lines}, (err)=>
- err.should.equal @stubbedErr
- done()
- describe "markDocAsDeleted", ->
- beforeEach ->
- @db.docs.update = sinon.stub().callsArgWith(2, @stubbedErr)
- @oldRev = 77
- it "should process the update", (done) ->
- @MongoManager.markDocAsDeleted @project_id, @doc_id, (err)=>
- args = @db.docs.update.args[0]
- assert.deepEqual args[0], {_id: ObjectId(@doc_id), project_id: ObjectId(@project_id)}
- assert.equal args[1]["$set"]["deleted"], true
- done()
- it "should return the error", (done)->
- @MongoManager.markDocAsDeleted @project_id, @doc_id, (err)=>
- err.should.equal @stubbedErr
- done()
- describe "destroyDoc", ->
- beforeEach (done) ->
- @db.docs.remove = sinon.stub().yields()
- @db.docOps.remove = sinon.stub().yields()
- @MongoManager.destroyDoc '123456789012', done
- it "should destroy the doc", ->
- sinon.assert.calledWith(@db.docs.remove, {_id: ObjectId('123456789012')})
- it "should destroy the docOps", ->
- sinon.assert.calledWith(@db.docOps.remove, {doc_id: ObjectId('123456789012')})
- describe "getDocVersion", ->
- describe "when the doc exists", ->
- beforeEach ->
- @doc =
- version: @version = 42
- @db.docOps.find = sinon.stub().callsArgWith(2, null, [@doc])
- @MongoManager.getDocVersion @doc_id, @callback
- it "should look for the doc in the database", ->
- @db.docOps.find
- .calledWith({ doc_id: ObjectId(@doc_id) }, {version: 1})
- .should.equal true
- it "should call the callback with the version", ->
- @callback.calledWith(null, @version).should.equal true
- describe "when the doc doesn't exist", ->
- beforeEach ->
- @db.docOps.find = sinon.stub().callsArgWith(2, null, [])
- @MongoManager.getDocVersion @doc_id, @callback
- it "should call the callback with 0", ->
- @callback.calledWith(null, 0).should.equal true
- describe "setDocVersion", ->
- beforeEach ->
- @version = 42
- @db.docOps.update = sinon.stub().callsArg(3)
- @MongoManager.setDocVersion @doc_id, @version, @callback
- it "should update the doc version", ->
- @db.docOps.update
- .calledWith({
- doc_id: ObjectId(@doc_id)
- }, {
- $set:
- version: @version
- }, {
- upsert: true
- })
- .should.equal true
- it "should call the callback", ->
- @callback.called.should.equal true
|