mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
clean up mongo query
This commit is contained in:
parent
0a0a8e23fe
commit
7a28fb58e7
4 changed files with 25 additions and 23 deletions
|
@ -7,7 +7,7 @@ DocArchive = require "./DocArchiveManager"
|
||||||
module.exports = DocManager =
|
module.exports = DocManager =
|
||||||
|
|
||||||
getDoc: (project_id, doc_id, callback = (error, doc) ->) ->
|
getDoc: (project_id, doc_id, callback = (error, doc) ->) ->
|
||||||
MongoManager.findDoc doc_id, (err, doc)->
|
MongoManager.findDoc project_id, doc_id, (err, doc)->
|
||||||
if err?
|
if err?
|
||||||
return callback(err)
|
return callback(err)
|
||||||
else if !doc?
|
else if !doc?
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
module.exports = MongoManager =
|
module.exports = MongoManager =
|
||||||
|
|
||||||
findDoc: (doc_id, callback = (error, doc) ->) ->
|
findDoc: (project_id, doc_id, callback = (error, doc) ->) ->
|
||||||
db.docs.find _id: ObjectId(doc_id.toString()), {}, (error, docs = []) ->
|
db.docs.find {_id: ObjectId(doc_id.toString()), project_id: ObjectId(project_id.toString())}, {}, (error, docs = []) ->
|
||||||
callback error, docs[0]
|
callback error, docs[0]
|
||||||
|
|
||||||
getProjectsDocs: (project_id, callback)->
|
getProjectsDocs: (project_id, callback)->
|
||||||
|
|
|
@ -18,14 +18,15 @@ describe "DocManager", ->
|
||||||
err:->
|
err:->
|
||||||
@doc_id = ObjectId().toString()
|
@doc_id = ObjectId().toString()
|
||||||
@project_id = ObjectId().toString()
|
@project_id = ObjectId().toString()
|
||||||
|
@another_project_id = ObjectId().toString()
|
||||||
@callback = sinon.stub()
|
@callback = sinon.stub()
|
||||||
@stubbedError = new Error("blew up")
|
@stubbedError = new Error("blew up")
|
||||||
|
|
||||||
describe "getDoc", ->
|
describe "getDoc", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@project = { name: "mock-project" }
|
@project = { name: "mock-project" }
|
||||||
@doc = { _id: @doc_id, lines: ["mock-lines"] }
|
@doc = { _id: @doc_id, project_id: @project_id, lines: ["mock-lines"] }
|
||||||
@docCollectionDoc = { _id: @doc_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", ->
|
describe "when the doc is in the doc collection not projects collection", ->
|
||||||
|
|
||||||
|
@ -33,24 +34,24 @@ describe "DocManager", ->
|
||||||
@MongoManager.findDoc = sinon.stub()
|
@MongoManager.findDoc = sinon.stub()
|
||||||
|
|
||||||
it "should get the doc from the doc collection when it is present there", (done)->
|
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)=>
|
@DocManager.getDoc @project_id, @doc_id, (err, doc)=>
|
||||||
doc.should.equal @docCollectionDoc
|
doc.should.equal @docCollectionDoc
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it "should return the error from find doc", (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)=>
|
@DocManager.getDoc @project_id, @doc_id, (err, doc)=>
|
||||||
err.should.equal @stubbedError
|
err.should.equal @stubbedError
|
||||||
done()
|
done()
|
||||||
|
|
||||||
describe "when the doc is deleted", ->
|
describe "when the doc is deleted", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@doc = { _id: @doc_id, lines: ["mock-lines"] }
|
@doc = { _id: @doc_id, project_id: @project_id, lines: ["mock-lines"] }
|
||||||
@s3doc = { _id: @doc_id, inS3: true }
|
@s3doc = { _id: @doc_id, project_id: @project_id, inS3: true }
|
||||||
@MongoManager.findDoc = sinon.stub()
|
@MongoManager.findDoc = sinon.stub()
|
||||||
@MongoManager.findDoc.callsArgWith(1, null, @s3doc)
|
@MongoManager.findDoc.callsArgWith(2, null, @s3doc)
|
||||||
@MongoManager.findDoc.callsArgWith(1, null, @doc)
|
@MongoManager.findDoc.callsArgWith(2, null, @doc)
|
||||||
spy = sinon.spy @DocManager.getDoc
|
spy = sinon.spy @DocManager.getDoc
|
||||||
@DocArchiveManager.unarchiveDoc = sinon.stub().callsArgWith(2)
|
@DocArchiveManager.unarchiveDoc = sinon.stub().callsArgWith(2)
|
||||||
@DocManager.getDoc @project_id, @doc_id, @callback
|
@DocManager.getDoc @project_id, @doc_id, @callback
|
||||||
|
@ -63,13 +64,13 @@ describe "DocManager", ->
|
||||||
|
|
||||||
describe "when the doc is in s3", ->
|
describe "when the doc is in s3", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc)
|
@MongoManager.findDoc = sinon.stub().callsArgWith(2, null, @doc)
|
||||||
|
|
||||||
@DocManager.getDoc @project_id, @doc_id, @callback
|
@DocManager.getDoc @project_id, @doc_id, @callback
|
||||||
|
|
||||||
describe "when the doc does not exist anywhere", ->
|
describe "when the doc does not exist anywhere", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@MongoManager.findDoc = sinon.stub().callsArgWith(1, null, null)
|
@MongoManager.findDoc = sinon.stub().callsArgWith(2, null, null)
|
||||||
@DocManager.getDoc @project_id, @doc_id, @callback
|
@DocManager.getDoc @project_id, @doc_id, @callback
|
||||||
|
|
||||||
it "should return a NotFoundError", ->
|
it "should return a NotFoundError", ->
|
||||||
|
@ -80,7 +81,7 @@ describe "DocManager", ->
|
||||||
describe "getAllDocs", ->
|
describe "getAllDocs", ->
|
||||||
describe "when the project exists", ->
|
describe "when the project exists", ->
|
||||||
beforeEach ->
|
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)
|
@MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, @docs)
|
||||||
@DocArchiveManager.unArchiveAllDocs = sinon.stub().callsArgWith(1, null, @docs)
|
@DocArchiveManager.unArchiveAllDocs = sinon.stub().callsArgWith(1, null, @docs)
|
||||||
@DocManager.getAllDocs @project_id, @callback
|
@DocManager.getAllDocs @project_id, @callback
|
||||||
|
@ -150,19 +151,19 @@ describe "DocManager", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@oldDocLines = ["old", "doc", "lines"]
|
@oldDocLines = ["old", "doc", "lines"]
|
||||||
@newDocLines = ["new", "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.upsertIntoDocCollection = sinon.stub().callsArg(3)
|
||||||
@MongoManager.findDoc = sinon.stub()
|
@MongoManager.findDoc = sinon.stub()
|
||||||
|
|
||||||
describe "when the doc lines have changed", ->
|
describe "when the doc lines have changed", ->
|
||||||
beforeEach ->
|
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
|
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
|
||||||
|
|
||||||
it "should get the existing doc", ->
|
it "should get the existing doc", ->
|
||||||
@MongoManager.findDoc
|
@MongoManager.findDoc
|
||||||
.calledWith(@doc_id)
|
.calledWith(@project_id, @doc_id)
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
||||||
it "should upsert the document to the doc collection", ->
|
it "should upsert the document to the doc collection", ->
|
||||||
|
@ -189,7 +190,7 @@ describe "DocManager", ->
|
||||||
|
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@error = new Error("doc could not be found")
|
@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
|
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
|
||||||
|
|
||||||
it "should not upsert the document to the doc collection", ->
|
it "should not upsert the document to the doc collection", ->
|
||||||
|
@ -200,7 +201,7 @@ describe "DocManager", ->
|
||||||
|
|
||||||
describe "when the doc lines have not changed", ->
|
describe "when the doc lines have not changed", ->
|
||||||
beforeEach ->
|
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
|
@DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @callback
|
||||||
|
|
||||||
it "should not update the doc", ->
|
it "should not update the doc", ->
|
||||||
|
@ -214,7 +215,7 @@ describe "DocManager", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
|
|
||||||
@doc.lines = []
|
@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
|
@DocManager.updateDoc @project_id, @doc_id, @doc.lines, @callback
|
||||||
|
|
||||||
it "should upsert the document to the doc collection", ->
|
it "should upsert the document to the doc collection", ->
|
||||||
|
@ -225,7 +226,7 @@ describe "DocManager", ->
|
||||||
|
|
||||||
describe "when the doc does not exist", ->
|
describe "when the doc does not exist", ->
|
||||||
beforeEach ->
|
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
|
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
|
||||||
|
|
||||||
it "should upsert the document to the doc collection", ->
|
it "should upsert the document to the doc collection", ->
|
||||||
|
|
|
@ -18,14 +18,15 @@ describe "MongoManager", ->
|
||||||
|
|
||||||
describe "findDoc", ->
|
describe "findDoc", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@doc = { name: "mock-doc" }
|
@doc = { name: "mock-doc"}
|
||||||
@db.docs.find = sinon.stub().callsArgWith(2, null, [@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", ->
|
it "should find the doc", ->
|
||||||
@db.docs.find
|
@db.docs.find
|
||||||
.calledWith({
|
.calledWith({
|
||||||
_id: ObjectId(@doc_id)
|
_id: ObjectId(@doc_id)
|
||||||
|
project_id: ObjectId(@project_id)
|
||||||
}, {})
|
}, {})
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue