From 7870b4fb55225800065104d1fb35277f681ab3d9 Mon Sep 17 00:00:00 2001 From: James Allen Date: Thu, 5 Jun 2014 13:29:50 +0100 Subject: [PATCH] Return deleted docs --- .../docstore/app/coffee/DocManager.coffee | 12 +++++-- .../docstore/app/coffee/HttpController.coffee | 2 ++ .../docstore/app/coffee/MongoManager.coffee | 4 +++ .../test/unit/coffee/DocManagerTests.coffee | 31 +++++++++++++++++-- .../unit/coffee/HttpControllerTests.coffee | 5 +++ .../test/unit/coffee/MongoManagerTests.coffee | 20 ++++++++++-- 6 files changed, 67 insertions(+), 7 deletions(-) diff --git a/services/docstore/app/coffee/DocManager.coffee b/services/docstore/app/coffee/DocManager.coffee index dfbad5f6a2..14ab13bf02 100644 --- a/services/docstore/app/coffee/DocManager.coffee +++ b/services/docstore/app/coffee/DocManager.coffee @@ -10,8 +10,14 @@ module.exports = DocManager = return callback new Errors.NotFoundError("No such project: #{project_id}") if !project? DocManager.findDocInProject project, doc_id, (error, doc, mongoPath) -> return callback(error) if error? - return callback new Errors.NotFoundError("No such doc: #{project_id}") if !doc? - return callback null, doc, mongoPath + if doc? + return callback null, doc, mongoPath + else + # Perhaps it's a deleted doc + MongoManager.findDoc doc_id, (error, doc) -> + return callback(error) if error? + return callback new Errors.NotFoundError("No such doc: #{project_id}") if !doc? + return callback null, doc getAllDocs: (project_id, callback = (error, docs) ->) -> MongoManager.findProject project_id, (error, project) -> @@ -24,7 +30,7 @@ module.exports = DocManager = updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) -> DocManager.getDoc project_id, doc_id, (error, doc, mongoPath) -> return callback(error) if error? - return callback new Errors.NotFoundError("No such project/doc: #{project_id}/#{doc_id}") if !doc? + return callback new Errors.NotFoundError("No such project/doc: #{project_id}/#{doc_id}") if !doc? or !mongoPath? if _.isEqual(doc.lines, lines) logger.log { diff --git a/services/docstore/app/coffee/HttpController.coffee b/services/docstore/app/coffee/HttpController.coffee index a14c9047a5..7a4976a1f3 100644 --- a/services/docstore/app/coffee/HttpController.coffee +++ b/services/docstore/app/coffee/HttpController.coffee @@ -8,6 +8,7 @@ module.exports = HttpController = logger.log project_id: project_id, doc_id: doc_id, "getting doc" DocManager.getDoc project_id, doc_id, (error, doc) -> return next(error) if error? + logger.log doc: doc, "got doc" if !doc? res.send 404 else @@ -69,6 +70,7 @@ module.exports = HttpController = _id: doc._id?.toString() lines: doc.lines rev: doc.rev + deleted: !!doc.deleted } _buildRawDocView: (doc)-> diff --git a/services/docstore/app/coffee/MongoManager.coffee b/services/docstore/app/coffee/MongoManager.coffee index 23e98e69c3..82db106d96 100644 --- a/services/docstore/app/coffee/MongoManager.coffee +++ b/services/docstore/app/coffee/MongoManager.coffee @@ -5,6 +5,10 @@ module.exports = MongoManager = db.projects.find _id: ObjectId(project_id.toString()), {}, (error, projects = []) -> callback error, projects[0] + findDoc: (doc_id, callback = (error, doc) ->) -> + db.docs.find _id: ObjectId(doc_id.toString()), {}, (error, docs = []) -> + callback error, docs[0] + updateDoc: (project_id, docPath, lines, callback = (error) ->) -> update = $set: {} diff --git a/services/docstore/test/unit/coffee/DocManagerTests.coffee b/services/docstore/test/unit/coffee/DocManagerTests.coffee index e9d914f711..339d082c8a 100644 --- a/services/docstore/test/unit/coffee/DocManagerTests.coffee +++ b/services/docstore/test/unit/coffee/DocManagerTests.coffee @@ -53,11 +53,11 @@ describe "DocManager", -> .calledWith(new Errors.NotFoundError("No such project: #{@project_id}")) .should.equal true - describe "when the doc does not exist", -> + describe "when the doc does not exist in the project tree", -> beforeEach -> - @project = { name: "mock-project" } @MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project) @DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, null, null) + @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc) @DocManager.getDoc @project_id, @doc_id, @callback it "should try to find the doc in the project", -> @@ -65,6 +65,33 @@ describe "DocManager", -> .calledWith(@project, @doc_id) .should.equal true + it "should try to find the doc in the docs collection", -> + @MongoManager.findDoc + .calledWith(@doc_id) + .should.equal true + + it "should return the doc", -> + @callback + .calledWith(null, @doc) + .should.equal true + + describe "when the doc does not exist anywhere", -> + beforeEach -> + @MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project) + @DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, null, null) + @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, null) + @DocManager.getDoc @project_id, @doc_id, @callback + + it "should try to find the doc in the project", -> + @DocManager.findDocInProject + .calledWith(@project, @doc_id) + .should.equal true + + it "should try to find the doc in the docs collection", -> + @MongoManager.findDoc + .calledWith(@doc_id) + .should.equal true + it "should return a NotFoundError", -> @callback .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}")) diff --git a/services/docstore/test/unit/coffee/HttpControllerTests.coffee b/services/docstore/test/unit/coffee/HttpControllerTests.coffee index 7c7ff8c103..4343d8120a 100644 --- a/services/docstore/test/unit/coffee/HttpControllerTests.coffee +++ b/services/docstore/test/unit/coffee/HttpControllerTests.coffee @@ -43,6 +43,7 @@ describe "HttpController", -> _id: @doc_id lines: @doc.lines rev: @doc.rev + deleted: false }) .should.equal true @@ -93,10 +94,12 @@ describe "HttpController", -> _id: @docs[0]._id.toString() lines: @docs[0].lines rev: @docs[0].rev + deleted: false }, { _id: @docs[1]._id.toString() lines: @docs[1].lines rev: @docs[1].rev + deleted: false }]) .should.equal true @@ -124,10 +127,12 @@ describe "HttpController", -> _id: @docs[0]._id.toString() lines: @docs[0].lines rev: @docs[0].rev + deleted: false }, { _id: @docs[2]._id.toString() lines: @docs[2].lines rev: @docs[2].rev + deleted: false }]) .should.equal true diff --git a/services/docstore/test/unit/coffee/MongoManagerTests.coffee b/services/docstore/test/unit/coffee/MongoManagerTests.coffee index d8c7411eec..c8e8478f58 100644 --- a/services/docstore/test/unit/coffee/MongoManagerTests.coffee +++ b/services/docstore/test/unit/coffee/MongoManagerTests.coffee @@ -11,6 +11,7 @@ describe "MongoManager", -> db: @db = { projects: {}, docs: {} } ObjectId: ObjectId @project_id = ObjectId().toString() + @doc_id = ObjectId().toString() @callback = sinon.stub() describe "findProject", -> @@ -19,7 +20,7 @@ describe "MongoManager", -> @db.projects.find = sinon.stub().callsArgWith(2, null, [@project]) @MongoManager.findProject @project_id, @callback - it "should find the project without the doc lines", -> + it "should find the project", -> @db.projects.find .calledWith({ _id: ObjectId(@project_id) @@ -29,6 +30,22 @@ describe "MongoManager", -> it "should call the callback with the project", -> @callback.calledWith(null, @project).should.equal true + 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 + describe "updateDoc", -> beforeEach -> @lines = ["mock-lines"] @@ -53,7 +70,6 @@ describe "MongoManager", -> describe "insertDoc", -> beforeEach -> - @doc_id = ObjectId().toString() @lines = ["mock-lines"] @db.docs.insert = sinon.stub().callsArg(1) @MongoManager.insertDoc @project_id, @doc_id, lines: @lines, @callback