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":
|
2014-04-29 11:36:10 -04:00
|
|
|
db: @db = { projects: {}, docs: {} }
|
2014-04-28 11:45:59 -04:00
|
|
|
ObjectId: ObjectId
|
|
|
|
@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
|
|
|
|
|
|
|
describe "findProject", ->
|
|
|
|
beforeEach ->
|
|
|
|
@project = { name: "mock-project" }
|
|
|
|
@db.projects.find = sinon.stub().callsArgWith(2, null, [@project])
|
|
|
|
@MongoManager.findProject @project_id, @callback
|
|
|
|
|
2014-06-05 08:29:50 -04:00
|
|
|
it "should find the project", ->
|
2014-04-28 11:45:59 -04:00
|
|
|
@db.projects.find
|
|
|
|
.calledWith({
|
|
|
|
_id: ObjectId(@project_id)
|
|
|
|
}, {})
|
|
|
|
.should.equal true
|
2014-04-28 12:43:19 -04:00
|
|
|
|
|
|
|
it "should call the callback with the project", ->
|
|
|
|
@callback.calledWith(null, @project).should.equal true
|
|
|
|
|
2014-06-05 08:29:50 -04:00
|
|
|
describe "findDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@doc = { name: "mock-doc" }
|
|
|
|
@db.docs.find = sinon.stub().callsArgWith(2, null, [@doc])
|
|
|
|
@MongoManager.findDoc @doc_id, @callback
|
|
|
|
|
|
|
|
it "should find the doc", ->
|
|
|
|
@db.docs.find
|
|
|
|
.calledWith({
|
|
|
|
_id: ObjectId(@doc_id)
|
|
|
|
}, {})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with the doc", ->
|
|
|
|
@callback.calledWith(null, @doc).should.equal true
|
|
|
|
|
2014-04-28 12:43:19 -04:00
|
|
|
describe "updateDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@lines = ["mock-lines"]
|
|
|
|
@docPath = "rootFolder.0.folders.1.docs.0"
|
|
|
|
@db.projects.update = sinon.stub().callsArg(2)
|
2014-05-16 08:06:35 -04:00
|
|
|
@MongoManager.updateDoc @project_id, @docPath, @lines, @callback
|
2014-04-28 12:43:19 -04:00
|
|
|
|
|
|
|
it "should update the doc lines and increment the TPDS rev", ->
|
|
|
|
@db.projects.update
|
|
|
|
.calledWith({
|
|
|
|
_id: ObjectId(@project_id)
|
|
|
|
}, {
|
|
|
|
$set:
|
|
|
|
"rootFolder.0.folders.1.docs.0.lines": @lines
|
|
|
|
$inc:
|
|
|
|
"rootFolder.0.folders.1.docs.0.rev": 1
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with the project", ->
|
|
|
|
@callback.called.should.equal true
|
2014-04-29 10:07:22 -04:00
|
|
|
|
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)->
|
|
|
|
@MongoManager.upsertIntoDocCollection @project_id, @doc_id, @lines, @oldRev, (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]["$set"]["rev"], 78
|
|
|
|
assert.deepEqual args[1]["$set"]["project_id"], ObjectId(@project_id)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return the error", (done)->
|
|
|
|
@MongoManager.upsertIntoDocCollection @project_id, @doc_id, @lines, @oldRev, (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 @doc_id, (err)=>
|
|
|
|
args = @db.docs.update.args[0]
|
|
|
|
assert.deepEqual args[0], {_id: ObjectId(@doc_id)}
|
|
|
|
assert.equal args[1]["$set"]["deleted"], true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return the error", (done)->
|
|
|
|
@MongoManager.markDocAsDeleted @doc_id, (err)=>
|
|
|
|
err.should.equal @stubbedErr
|
|
|
|
done()
|
2014-04-29 10:07:22 -04:00
|
|
|
|
2015-01-20 12:09:51 -05:00
|
|
|
|