Write docs to do collection and project collection
- docs are written to project collection then doc collection - deletes used to insert into doc collection, now its the same upsert with deleted = true - does not set delted = false for new docs as it is not needed, save space - changed delete to be seperate update on mongo
This commit is contained in:
@@ -138,8 +138,10 @@ describe "DocManager", ->
|
||||
describe "when the doc exists", ->
|
||||
beforeEach ->
|
||||
@lines = ["mock", "doc", "lines"]
|
||||
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, lines: @lines)
|
||||
@MongoManager.insertDoc = sinon.stub().callsArg(3)
|
||||
@rev = 77
|
||||
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, {lines: @lines, rev:@rev})
|
||||
@MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(4)
|
||||
@MongoManager.markDocAsDeleted = sinon.stub().callsArg(1)
|
||||
@DocManager.deleteDoc @project_id, @doc_id, @callback
|
||||
|
||||
it "should get the doc", ->
|
||||
@@ -147,12 +149,14 @@ describe "DocManager", ->
|
||||
.calledWith(@project_id, @doc_id)
|
||||
.should.equal true
|
||||
|
||||
it "insert the doc as a deleted doc", ->
|
||||
@MongoManager.insertDoc
|
||||
.calledWith(@project_id, @doc_id, {
|
||||
lines: @lines
|
||||
deleted: true
|
||||
})
|
||||
it "should update the doc lines", ->
|
||||
@MongoManager.upsertIntoDocCollection
|
||||
.calledWith(@project_id, @doc_id, @lines, @rev)
|
||||
.should.equal true
|
||||
|
||||
it "should mark doc as deleted", ->
|
||||
@MongoManager.markDocAsDeleted
|
||||
.calledWith(@doc_id)
|
||||
.should.equal true
|
||||
|
||||
it "should return the callback", ->
|
||||
@@ -161,11 +165,11 @@ describe "DocManager", ->
|
||||
describe "when the doc does not exist", ->
|
||||
beforeEach ->
|
||||
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, null)
|
||||
@MongoManager.insertDoc = sinon.stub().callsArg(3)
|
||||
@MongoManager.upsertIntoDocCollection = sinon.stub()
|
||||
@DocManager.deleteDoc @project_id, @doc_id, @callback
|
||||
|
||||
it "should not try to insert a deleted doc", ->
|
||||
@MongoManager.insertDoc.called.should.equal false
|
||||
@MongoManager.upsertIntoDocCollection.called.should.equal false
|
||||
|
||||
it "should return a NotFoundError", ->
|
||||
@callback
|
||||
@@ -180,6 +184,7 @@ describe "DocManager", ->
|
||||
@mongoPath = "mock.mongo.path"
|
||||
|
||||
@MongoManager.updateDoc = sinon.stub().callsArg(3)
|
||||
@MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(4)
|
||||
|
||||
describe "when the doc lines have changed", ->
|
||||
beforeEach ->
|
||||
@@ -196,6 +201,11 @@ describe "DocManager", ->
|
||||
.calledWith(@project_id, @mongoPath, @newDocLines)
|
||||
.should.equal true
|
||||
|
||||
it "should upsert the document to the doc collection", ->
|
||||
@MongoManager.upsertIntoDocCollection
|
||||
.calledWith(@project_id, @doc_id, @newDocLines, @rev)
|
||||
.should.equal true
|
||||
|
||||
it "should log out the old and new doc lines", ->
|
||||
@logger.log
|
||||
.calledWith(
|
||||
@@ -218,6 +228,7 @@ describe "DocManager", ->
|
||||
|
||||
it "should not update the doc", ->
|
||||
@MongoManager.updateDoc.called.should.equal false
|
||||
@MongoManager.upsertIntoDocCollection.called.should.equal false
|
||||
|
||||
it "should return the callback with the existing rev", ->
|
||||
@callback.calledWith(null, false, @rev).should.equal true
|
||||
@@ -229,6 +240,7 @@ describe "DocManager", ->
|
||||
|
||||
it "should not try to update the doc", ->
|
||||
@MongoManager.updateDoc.called.should.equal false
|
||||
@MongoManager.upsertIntoDocCollection.called.should.equal false
|
||||
|
||||
it "should return a NotFoundError", ->
|
||||
@callback
|
||||
|
||||
@@ -3,6 +3,7 @@ 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 ->
|
||||
@@ -13,6 +14,7 @@ describe "MongoManager", ->
|
||||
@project_id = ObjectId().toString()
|
||||
@doc_id = ObjectId().toString()
|
||||
@callback = sinon.stub()
|
||||
@stubbedErr = new Error("hello world")
|
||||
|
||||
describe "findProject", ->
|
||||
beforeEach ->
|
||||
@@ -68,20 +70,41 @@ describe "MongoManager", ->
|
||||
it "should call the callback with the project", ->
|
||||
@callback.called.should.equal true
|
||||
|
||||
describe "insertDoc", ->
|
||||
|
||||
describe "upsertIntoDocCollection", ->
|
||||
beforeEach ->
|
||||
@lines = ["mock-lines"]
|
||||
@db.docs.insert = sinon.stub().callsArg(1)
|
||||
@MongoManager.insertDoc @project_id, @doc_id, lines: @lines, @callback
|
||||
@db.docs.update = sinon.stub().callsArgWith(3, @stubbedErr)
|
||||
@oldRev = 77
|
||||
|
||||
it "should insert the attributes with the given doc and project id", ->
|
||||
@db.docs.insert
|
||||
.calledWith({
|
||||
_id: ObjectId(@doc_id)
|
||||
project_id: ObjectId(@project_id)
|
||||
lines: @lines
|
||||
})
|
||||
.should.equal true
|
||||
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 call the callback", ->
|
||||
@callback.called.should.equal true
|
||||
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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user