From 7a28fb58e7cbc30a3a6d3be98cddaf6ce22d2a61 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 2 Sep 2016 13:32:50 +0100 Subject: [PATCH] clean up mongo query --- .../docstore/app/coffee/DocManager.coffee | 2 +- .../docstore/app/coffee/MongoManager.coffee | 4 +- .../test/unit/coffee/DocManagerTests.coffee | 37 ++++++++++--------- .../test/unit/coffee/MongoManagerTests.coffee | 5 ++- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/services/docstore/app/coffee/DocManager.coffee b/services/docstore/app/coffee/DocManager.coffee index 62de6314ec..e09d156f22 100644 --- a/services/docstore/app/coffee/DocManager.coffee +++ b/services/docstore/app/coffee/DocManager.coffee @@ -7,7 +7,7 @@ DocArchive = require "./DocArchiveManager" module.exports = DocManager = getDoc: (project_id, doc_id, callback = (error, doc) ->) -> - MongoManager.findDoc doc_id, (err, doc)-> + MongoManager.findDoc project_id, doc_id, (err, doc)-> if err? return callback(err) else if !doc? diff --git a/services/docstore/app/coffee/MongoManager.coffee b/services/docstore/app/coffee/MongoManager.coffee index b454214ff1..00dd935632 100644 --- a/services/docstore/app/coffee/MongoManager.coffee +++ b/services/docstore/app/coffee/MongoManager.coffee @@ -2,8 +2,8 @@ module.exports = MongoManager = - findDoc: (doc_id, callback = (error, doc) ->) -> - db.docs.find _id: ObjectId(doc_id.toString()), {}, (error, docs = []) -> + findDoc: (project_id, doc_id, callback = (error, doc) ->) -> + db.docs.find {_id: ObjectId(doc_id.toString()), project_id: ObjectId(project_id.toString())}, {}, (error, docs = []) -> callback error, docs[0] getProjectsDocs: (project_id, callback)-> diff --git a/services/docstore/test/unit/coffee/DocManagerTests.coffee b/services/docstore/test/unit/coffee/DocManagerTests.coffee index 64551da527..c84f64b75b 100644 --- a/services/docstore/test/unit/coffee/DocManagerTests.coffee +++ b/services/docstore/test/unit/coffee/DocManagerTests.coffee @@ -18,14 +18,15 @@ describe "DocManager", -> err:-> @doc_id = ObjectId().toString() @project_id = ObjectId().toString() + @another_project_id = ObjectId().toString() @callback = sinon.stub() @stubbedError = new Error("blew up") describe "getDoc", -> beforeEach -> @project = { name: "mock-project" } - @doc = { _id: @doc_id, lines: ["mock-lines"] } - @docCollectionDoc = { _id: @doc_id, lines: ["mock-lines"] } + @doc = { _id: @doc_id, project_id: @project_id, lines: ["mock-lines"] } + @docCollectionDoc = { _id: @doc_id, project_id: @project_id, lines: ["mock-lines"] } describe "when the doc is in the doc collection not projects collection", -> @@ -33,24 +34,24 @@ describe "DocManager", -> @MongoManager.findDoc = sinon.stub() it "should get the doc from the doc collection when it is present there", (done)-> - @MongoManager.findDoc.callsArgWith(1, null, @docCollectionDoc) + @MongoManager.findDoc.callsArgWith(2, null, @docCollectionDoc) @DocManager.getDoc @project_id, @doc_id, (err, doc)=> doc.should.equal @docCollectionDoc done() it "should return the error from find doc", (done)-> - @MongoManager.findDoc.callsArgWith(1, @stubbedError) + @MongoManager.findDoc.callsArgWith(2, @stubbedError) @DocManager.getDoc @project_id, @doc_id, (err, doc)=> err.should.equal @stubbedError done() describe "when the doc is deleted", -> beforeEach -> - @doc = { _id: @doc_id, lines: ["mock-lines"] } - @s3doc = { _id: @doc_id, inS3: true } + @doc = { _id: @doc_id, project_id: @project_id, lines: ["mock-lines"] } + @s3doc = { _id: @doc_id, project_id: @project_id, inS3: true } @MongoManager.findDoc = sinon.stub() - @MongoManager.findDoc.callsArgWith(1, null, @s3doc) - @MongoManager.findDoc.callsArgWith(1, null, @doc) + @MongoManager.findDoc.callsArgWith(2, null, @s3doc) + @MongoManager.findDoc.callsArgWith(2, null, @doc) spy = sinon.spy @DocManager.getDoc @DocArchiveManager.unarchiveDoc = sinon.stub().callsArgWith(2) @DocManager.getDoc @project_id, @doc_id, @callback @@ -63,13 +64,13 @@ describe "DocManager", -> describe "when the doc is in s3", -> beforeEach -> - @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc) + @MongoManager.findDoc = sinon.stub().callsArgWith(2, null, @doc) @DocManager.getDoc @project_id, @doc_id, @callback describe "when the doc does not exist anywhere", -> beforeEach -> - @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, null) + @MongoManager.findDoc = sinon.stub().callsArgWith(2, null, null) @DocManager.getDoc @project_id, @doc_id, @callback it "should return a NotFoundError", -> @@ -80,7 +81,7 @@ describe "DocManager", -> describe "getAllDocs", -> describe "when the project exists", -> beforeEach -> - @docs = [{ _id: @doc_id, lines: ["mock-lines"] }] + @docs = [{ _id: @doc_id, project_id: @project_id, lines: ["mock-lines"] }] @MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, @docs) @DocArchiveManager.unArchiveAllDocs = sinon.stub().callsArgWith(1, null, @docs) @DocManager.getAllDocs @project_id, @callback @@ -150,19 +151,19 @@ describe "DocManager", -> beforeEach -> @oldDocLines = ["old", "doc", "lines"] @newDocLines = ["new", "doc", "lines"] - @doc = { _id: @doc_id, lines: @oldDocLines, rev: @rev = 5 } + @doc = { _id: @doc_id, project_id: @project_id, lines: @oldDocLines, rev: @rev = 5 } @MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(3) @MongoManager.findDoc = sinon.stub() describe "when the doc lines have changed", -> beforeEach -> - @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc) + @MongoManager.findDoc = sinon.stub().callsArgWith(2, null, @doc) @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback it "should get the existing doc", -> @MongoManager.findDoc - .calledWith(@doc_id) + .calledWith(@project_id, @doc_id) .should.equal true it "should upsert the document to the doc collection", -> @@ -189,7 +190,7 @@ describe "DocManager", -> beforeEach -> @error = new Error("doc could not be found") - @MongoManager.findDoc = sinon.stub().callsArgWith(1, @error, null, null) + @MongoManager.findDoc = sinon.stub().callsArgWith(2, @error, null, null) @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback it "should not upsert the document to the doc collection", -> @@ -200,7 +201,7 @@ describe "DocManager", -> describe "when the doc lines have not changed", -> beforeEach -> - @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc) + @MongoManager.findDoc = sinon.stub().callsArgWith(2, null, @doc) @DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @callback it "should not update the doc", -> @@ -214,7 +215,7 @@ describe "DocManager", -> beforeEach -> @doc.lines = [] - @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc) + @MongoManager.findDoc = sinon.stub().callsArgWith(2, null, @doc) @DocManager.updateDoc @project_id, @doc_id, @doc.lines, @callback it "should upsert the document to the doc collection", -> @@ -225,7 +226,7 @@ describe "DocManager", -> describe "when the doc does not exist", -> beforeEach -> - @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, null, null) + @MongoManager.findDoc = sinon.stub().callsArgWith(2, null, null, null) @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback it "should upsert the document to the doc collection", -> diff --git a/services/docstore/test/unit/coffee/MongoManagerTests.coffee b/services/docstore/test/unit/coffee/MongoManagerTests.coffee index b0e7c07e97..b64473b5ec 100644 --- a/services/docstore/test/unit/coffee/MongoManagerTests.coffee +++ b/services/docstore/test/unit/coffee/MongoManagerTests.coffee @@ -18,14 +18,15 @@ describe "MongoManager", -> describe "findDoc", -> beforeEach -> - @doc = { name: "mock-doc" } + @doc = { name: "mock-doc"} @db.docs.find = sinon.stub().callsArgWith(2, null, [@doc]) - @MongoManager.findDoc @doc_id, @callback + @MongoManager.findDoc @project_id, @doc_id, @callback it "should find the doc", -> @db.docs.find .calledWith({ _id: ObjectId(@doc_id) + project_id: ObjectId(@project_id) }, {}) .should.equal true