Add in DocManager.deleteDoc method

This commit is contained in:
James Allen 2014-04-29 15:07:22 +01:00
parent 9be5265f32
commit 885d5de191
4 changed files with 71 additions and 1 deletions

View file

@ -31,6 +31,14 @@ module.exports = DocManager =
return callback(error) if error?
callback null, true
deleteDoc: (project_id, doc_id, callback = (error) ->) ->
DocManager.getDoc project_id, doc_id, (error, doc) ->
return callback(error) if error?
return callback new Errors.NotFoundError("No such project/doc: #{project_id}/#{doc_id}") if !doc?
MongoManager.insertDoc project_id, doc_id, { lines: doc.lines, deleted: true }, (error) ->
return callback(error) if error?
callback()
findDocInProject: (project, doc_id, callback = (error, doc, mongoPath) ->) ->
result = @_findDocInFolder project.rootFolder[0], doc_id, "rootFolder.0"
if result?

View file

@ -12,4 +12,9 @@ module.exports = MongoManager =
update.$set["#{docPath}.lines"] = lines
update.$inc["#{docPath}.rev"] = 1
db.projects.update _id: ObjectId(project_id), update, callback
db.projects.update _id: ObjectId(project_id), update, callback
insertDoc: (project_id, doc_id, attributes, callback = (error) ->) ->
attributes._id = ObjectId(doc_id)
attributes.project_id = ObjectId(project_id)
db.projects.insert attributes, callback

View file

@ -70,6 +70,44 @@ describe "DocManager", ->
.calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
.should.equal true
describe "deleteDoc", ->
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)
@DocManager.deleteDoc @project_id, @doc_id, @callback
it "should get the doc", ->
@DocManager.getDoc
.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
})
.should.equal true
it "should return the callback", ->
@callback.called.should.equal true
describe "when the doc does not exist", ->
beforeEach ->
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, null)
@MongoManager.insertDoc = sinon.stub().callsArg(3)
@DocManager.deleteDoc @project_id, @doc_id, @callback
it "should not try to insert a deleted doc", ->
@MongoManager.insertDoc.called.should.equal false
it "should return a NotFoundError", ->
@callback
.calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
.should.equal true
describe "updateDoc", ->
beforeEach ->
@oldDocLines = ["old", "doc", "lines"]

View file

@ -50,3 +50,22 @@ describe "MongoManager", ->
it "should call the callback with the project", ->
@callback.called.should.equal true
describe "insertDoc", ->
beforeEach ->
@doc_id = ObjectId().toString()
@lines = ["mock-lines"]
@db.projects.insert = sinon.stub().callsArg(1)
@MongoManager.insertDoc @project_id, @doc_id, lines: @lines, @callback
it "should insert the attributes with the given doc and project id", ->
@db.projects.insert
.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