2014-04-28 11:45:59 -04:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
|
|
|
require('chai').should()
|
|
|
|
modulePath = require('path').join __dirname, '../../../app/js/MongoManager'
|
|
|
|
ObjectId = require("mongojs").ObjectId
|
2015-01-20 12:09:51 -05:00
|
|
|
assert = require("chai").assert
|
2014-04-28 11:45:59 -04:00
|
|
|
|
|
|
|
describe "MongoManager", ->
|
|
|
|
beforeEach ->
|
|
|
|
@MongoManager = SandboxedModule.require modulePath, requires:
|
|
|
|
"./mongojs":
|
2016-11-28 09:55:16 -05:00
|
|
|
db: @db = { docs: {}, docOps: {} }
|
2014-04-28 11:45:59 -04:00
|
|
|
ObjectId: ObjectId
|
2017-03-16 12:46:58 -04:00
|
|
|
'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
|
2017-03-31 06:25:23 -04:00
|
|
|
'logger-sharelatex': {log: ()->}
|
2014-04-28 11:45:59 -04:00
|
|
|
@project_id = ObjectId().toString()
|
2014-06-05 08:29:50 -04:00
|
|
|
@doc_id = ObjectId().toString()
|
2014-04-28 11:45:59 -04:00
|
|
|
@callback = sinon.stub()
|
2015-01-20 12:09:51 -05:00
|
|
|
@stubbedErr = new Error("hello world")
|
2014-04-28 11:45:59 -04:00
|
|
|
|
2014-06-05 08:29:50 -04:00
|
|
|
describe "findDoc", ->
|
|
|
|
beforeEach ->
|
2016-09-02 08:32:50 -04:00
|
|
|
@doc = { name: "mock-doc"}
|
2014-06-05 08:29:50 -04:00
|
|
|
@db.docs.find = sinon.stub().callsArgWith(2, null, [@doc])
|
2016-12-05 12:27:31 -05:00
|
|
|
@filter = { lines: true }
|
|
|
|
@MongoManager.findDoc @project_id, @doc_id, @filter, @callback
|
2014-06-05 08:29:50 -04:00
|
|
|
|
|
|
|
it "should find the doc", ->
|
2017-08-22 03:26:30 -04:00
|
|
|
@db.docs.find.lastCall.args.slice(0,2).should.deep.equal([
|
|
|
|
{_id: ObjectId(@doc_id), project_id: ObjectId(@project_id)},
|
|
|
|
@filter
|
|
|
|
])
|
2014-06-05 08:29:50 -04:00
|
|
|
|
|
|
|
it "should call the callback with the doc", ->
|
|
|
|
@callback.calledWith(null, @doc).should.equal true
|
|
|
|
|
2015-02-20 09:28:16 -05:00
|
|
|
describe "getProjectsDocs", ->
|
2014-04-28 12:43:19 -04:00
|
|
|
beforeEach ->
|
2016-12-05 12:27:31 -05:00
|
|
|
@filter = {lines: true}
|
2015-02-20 09:28:16 -05:00
|
|
|
@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])
|
2016-12-05 11:31:51 -05:00
|
|
|
|
|
|
|
describe "with included_deleted = false", ->
|
|
|
|
beforeEach ->
|
2016-12-05 12:27:31 -05:00
|
|
|
@MongoManager.getProjectsDocs @project_id, include_deleted: false, @filter, @callback
|
2015-02-20 09:28:16 -05:00
|
|
|
|
2016-12-05 11:31:51 -05:00
|
|
|
it "should find the non-deleted docs via the project_id", ->
|
2017-08-22 03:26:30 -04:00
|
|
|
@db.docs.find.lastCall.args.slice(0,1).should.deep.equal([
|
|
|
|
{project_id: ObjectId(@project_id), deleted: {$ne: true}}
|
|
|
|
])
|
2016-12-05 11:31:51 -05:00
|
|
|
|
|
|
|
it "should call the callback with the docs", ->
|
|
|
|
@callback.calledWith(null, [@doc, @doc3, @doc4]).should.equal true
|
|
|
|
|
|
|
|
describe "with included_deleted = true", ->
|
2016-12-05 12:27:31 -05:00
|
|
|
beforeEach ->
|
|
|
|
@MongoManager.getProjectsDocs @project_id, include_deleted: true, @filter, @callback
|
2016-12-05 11:31:51 -05:00
|
|
|
|
|
|
|
it "should find all via the project_id", ->
|
2017-08-22 03:26:30 -04:00
|
|
|
@db.docs.find.lastCall.args.slice(0,2).should.deep.equal([
|
|
|
|
{project_id: ObjectId(@project_id)},
|
|
|
|
@filter
|
|
|
|
])
|
2014-04-28 12:43:19 -04:00
|
|
|
|
2016-12-05 11:31:51 -05:00
|
|
|
it "should call the callback with the docs", ->
|
|
|
|
@callback.calledWith(null, [@doc, @doc3, @doc4]).should.equal true
|
2015-01-20 12:09:51 -05:00
|
|
|
|
|
|
|
describe "upsertIntoDocCollection", ->
|
2014-04-29 10:07:22 -04:00
|
|
|
beforeEach ->
|
2015-01-20 12:09:51 -05:00
|
|
|
@db.docs.update = sinon.stub().callsArgWith(3, @stubbedErr)
|
|
|
|
@oldRev = 77
|
2014-04-29 10:07:22 -04:00
|
|
|
|
2015-01-20 12:09:51 -05:00
|
|
|
it "should upsert the document", (done)->
|
2016-12-02 10:22:08 -05:00
|
|
|
@MongoManager.upsertIntoDocCollection @project_id, @doc_id, {@lines}, (err)=>
|
2015-01-20 12:09:51 -05:00
|
|
|
args = @db.docs.update.args[0]
|
|
|
|
assert.deepEqual args[0], {_id: ObjectId(@doc_id)}
|
|
|
|
assert.equal args[1]["$set"]["lines"], @lines
|
2015-02-26 11:01:10 -05:00
|
|
|
assert.equal args[1]["$inc"]["rev"], 1
|
2015-01-20 12:09:51 -05:00
|
|
|
assert.deepEqual args[1]["$set"]["project_id"], ObjectId(@project_id)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return the error", (done)->
|
2016-12-02 10:22:08 -05:00
|
|
|
@MongoManager.upsertIntoDocCollection @project_id, @doc_id, {@lines}, (err)=>
|
2015-01-20 12:09:51 -05:00
|
|
|
err.should.equal @stubbedErr
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "markDocAsDeleted", ->
|
|
|
|
beforeEach ->
|
|
|
|
@db.docs.update = sinon.stub().callsArgWith(2, @stubbedErr)
|
|
|
|
@oldRev = 77
|
|
|
|
|
2016-12-02 10:02:54 -05:00
|
|
|
it "should process the update", (done) ->
|
|
|
|
@MongoManager.markDocAsDeleted @project_id, @doc_id, (err)=>
|
2015-01-20 12:09:51 -05:00
|
|
|
args = @db.docs.update.args[0]
|
2016-12-02 10:02:54 -05:00
|
|
|
assert.deepEqual args[0], {_id: ObjectId(@doc_id), project_id: ObjectId(@project_id)}
|
2015-01-20 12:09:51 -05:00
|
|
|
assert.equal args[1]["$set"]["deleted"], true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return the error", (done)->
|
2016-12-02 10:02:54 -05:00
|
|
|
@MongoManager.markDocAsDeleted @project_id, @doc_id, (err)=>
|
2015-01-20 12:09:51 -05:00
|
|
|
err.should.equal @stubbedErr
|
|
|
|
done()
|
2014-04-29 10:07:22 -04:00
|
|
|
|
2016-11-28 09:55:16 -05:00
|
|
|
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", ->
|
2017-08-22 03:26:30 -04:00
|
|
|
@db.docOps.find.lastCall.args.slice(0,2).should.deep.equal([
|
|
|
|
{ doc_id: ObjectId(@doc_id) },
|
|
|
|
{version: 1}
|
|
|
|
])
|
2016-11-28 09:55:16 -05:00
|
|
|
|
|
|
|
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", ->
|
2017-08-22 03:26:30 -04:00
|
|
|
@db.docOps.update.lastCall.args.slice(0,3).should.deep.equal([
|
|
|
|
{doc_id: ObjectId(@doc_id)},
|
|
|
|
{$set: {version: @version}},
|
|
|
|
{upsert: true}
|
|
|
|
])
|
2016-11-28 09:55:16 -05:00
|
|
|
|
|
|
|
it "should call the callback", ->
|
2017-03-16 12:46:58 -04:00
|
|
|
@callback.called.should.equal true
|