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
|
|
|
|
|
|
|
|
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()
|
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
|
|
describe "findProject", ->
|
|
|
|
beforeEach ->
|
|
|
|
@project = { name: "mock-project" }
|
|
|
|
@db.projects.find = sinon.stub().callsArgWith(2, null, [@project])
|
|
|
|
@MongoManager.findProject @project_id, @callback
|
|
|
|
|
|
|
|
it "should find the project without the doc lines", ->
|
|
|
|
@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
|
|
|
|
|
|
|
|
describe "updateDoc", ->
|
|
|
|
beforeEach ->
|
2014-05-08 10:43:08 -04:00
|
|
|
@version = 42
|
2014-04-28 12:43:19 -04:00
|
|
|
@lines = ["mock-lines"]
|
|
|
|
@docPath = "rootFolder.0.folders.1.docs.0"
|
|
|
|
@db.projects.update = sinon.stub().callsArg(2)
|
2014-05-08 10:43:08 -04:00
|
|
|
@MongoManager.updateDoc @project_id, @docPath, @lines, @version, @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
|
2014-05-08 10:43:08 -04:00
|
|
|
"rootFolder.0.folders.1.docs.0.version": @version
|
2014-04-28 12:43:19 -04:00
|
|
|
$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
|
|
|
|
|
|
|
describe "insertDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@doc_id = ObjectId().toString()
|
|
|
|
@lines = ["mock-lines"]
|
2014-04-29 11:36:10 -04:00
|
|
|
@db.docs.insert = sinon.stub().callsArg(1)
|
2014-04-29 10:07:22 -04:00
|
|
|
@MongoManager.insertDoc @project_id, @doc_id, lines: @lines, @callback
|
|
|
|
|
|
|
|
it "should insert the attributes with the given doc and project id", ->
|
2014-04-29 11:36:10 -04:00
|
|
|
@db.docs.insert
|
2014-04-29 10:07:22 -04:00
|
|
|
.calledWith({
|
|
|
|
_id: ObjectId(@doc_id)
|
|
|
|
project_id: ObjectId(@project_id)
|
|
|
|
lines: @lines
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|