read docs from doc collection first

- removes include deleted flag as it is irrelivant now
This commit is contained in:
Henry Oswald 2015-01-22 15:05:48 +00:00
parent ad77bdd9c4
commit 5d2c09bf75
6 changed files with 56 additions and 60 deletions

View file

@ -5,24 +5,19 @@ _ = require "underscore"
async = require "async" async = require "async"
module.exports = DocManager = module.exports = DocManager =
getDoc: (project_id, doc_id, options, callback = (error, doc, mongoPath) ->) -> getDoc: (project_id, doc_id, callback = (error, doc, mongoPath) ->) ->
if typeof(options) == "function"
callback = options
options.include_deleted = false
MongoManager.findProject project_id, (error, project) -> MongoManager.findDoc doc_id, (err, docFromDocCollection)->
return callback(error) if error? return callback(err) if err?
return callback new Errors.NotFoundError("No such project: #{project_id}") if !project? MongoManager.findProject project_id, (error, project) ->
DocManager.findDocInProject project, doc_id, (error, doc, mongoPath) ->
return callback(error) if error? return callback(error) if error?
if doc? return callback new Errors.NotFoundError("No such project: #{project_id}") if !project?
return callback null, doc, mongoPath DocManager.findDocInProject project, doc_id, (error, doc, mongoPath) ->
else return callback(error) if error?
if options.include_deleted if docFromDocCollection?
MongoManager.findDoc doc_id, (error, doc) -> return callback null, docFromDocCollection, mongoPath
return callback(error) if error? else if doc?
return callback new Errors.NotFoundError("No such doc: #{project_id}") if !doc? return callback null, doc, mongoPath
return callback null, doc
else else
return callback new Errors.NotFoundError("No such doc: #{project_id}") return callback new Errors.NotFoundError("No such doc: #{project_id}")
@ -37,7 +32,7 @@ module.exports = DocManager =
updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) -> updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) ->
DocManager.getDoc project_id, doc_id, (error, doc, mongoPath) -> DocManager.getDoc project_id, doc_id, (error, doc, mongoPath) ->
return callback(error) if error? return callback(error) if error?
return callback new Errors.NotFoundError("No such project/doc: #{project_id}/#{doc_id}") if !doc? or !mongoPath? return callback new Errors.NotFoundError("No such project/doc to update: #{project_id}/#{doc_id}") if !doc? or !mongoPath?
if _.isEqual(doc.lines, lines) if _.isEqual(doc.lines, lines)
logger.log { logger.log {
@ -65,7 +60,7 @@ module.exports = DocManager =
deleteDoc: (project_id, doc_id, callback = (error) ->) -> deleteDoc: (project_id, doc_id, callback = (error) ->) ->
DocManager.getDoc project_id, doc_id, (error, doc) -> DocManager.getDoc project_id, doc_id, (error, doc) ->
return callback(error) if error? 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 to delete: #{project_id}/#{doc_id}") if !doc?
MongoManager.upsertIntoDocCollection project_id, doc_id, doc.lines, doc.rev, (error) -> MongoManager.upsertIntoDocCollection project_id, doc_id, doc.lines, doc.rev, (error) ->
return callback(error) if error? return callback(error) if error?
MongoManager.markDocAsDeleted doc_id, (error) -> MongoManager.markDocAsDeleted doc_id, (error) ->

View file

@ -5,9 +5,8 @@ module.exports = HttpController =
getDoc: (req, res, next = (error) ->) -> getDoc: (req, res, next = (error) ->) ->
project_id = req.params.project_id project_id = req.params.project_id
doc_id = req.params.doc_id doc_id = req.params.doc_id
include_deleted = req.query?.include_deleted == "true"
logger.log project_id: project_id, doc_id: doc_id, "getting doc" logger.log project_id: project_id, doc_id: doc_id, "getting doc"
DocManager.getDoc project_id, doc_id, include_deleted: include_deleted, (error, doc) -> DocManager.getDoc project_id, doc_id, (error, doc) ->
return next(error) if error? return next(error) if error?
logger.log doc: doc, "got doc" logger.log doc: doc, "got doc"
if !doc? if !doc?

View file

@ -34,19 +34,13 @@ describe "Getting a doc", ->
describe "when the doc is a deleted doc", -> describe "when the doc is a deleted doc", ->
beforeEach (done) -> beforeEach (done) ->
@deleted_doc_id = ObjectId() @deleted_doc_id = ObjectId()
DocstoreClient.createDeletedDoc @project_id, @deleted_doc_id, @lines, done DocstoreClient.createDeletedDoc @project_id, @deleted_doc_id, @lines, done
describe "with include_deleted=true", -> it "should return the doc", (done) ->
it "should return the doc", (done) -> DocstoreClient.getDoc @project_id, @deleted_doc_id, (error, res, doc) =>
DocstoreClient.getDoc @project_id, @deleted_doc_id, include_deleted: true, (error, res, doc) => doc.lines.should.deep.equal @lines
doc.lines.should.deep.equal @lines doc.deleted.should.equal true
doc.deleted.should.equal true done()
done()
describe "without include_deleted=true", ->
it "should return 404", (done) ->
DocstoreClient.getDoc @project_id, @deleted_doc_id, (error, res, doc) =>
res.statusCode.should.equal 404
done()

View file

@ -31,12 +31,9 @@ module.exports = DocstoreClient =
deleteProject: (project_id, callback = (error, res, body) ->) -> deleteProject: (project_id, callback = (error, res, body) ->) ->
db.projects.remove _id: project_id, callback db.projects.remove _id: project_id, callback
getDoc: (project_id, doc_id, options, callback = (error, res, body) ->) -> getDoc: (project_id, doc_id, callback = (error, res, body) ->) ->
if typeof(options) == "function"
callback = options
options = { include_deleted: false }
request.get { request.get {
url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}?include_deleted=#{options.include_deleted}" url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}"
json: true json: true
}, callback }, callback

View file

@ -15,14 +15,38 @@ describe "DocManager", ->
@doc_id = ObjectId().toString() @doc_id = ObjectId().toString()
@project_id = ObjectId().toString() @project_id = ObjectId().toString()
@callback = sinon.stub() @callback = sinon.stub()
@stubbedError = new Error("blew up")
describe "getDoc", -> describe "getDoc", ->
beforeEach ->
@project = { name: "mock-project" }
@doc = { _id: @doc_id, lines: ["mock-lines"] }
@docCollectionDoc = { _id: @doc_id, lines: ["mock-lines"] }
describe "when the doc is in the doc collection not projects collection", ->
beforeEach ->
@MongoManager.findDoc = sinon.stub()
@MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
@DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, @doc, @mongoPath)
it "should get the doc from the doc collection when it is present there", (done)->
@MongoManager.findDoc.callsArgWith(1, 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)
@DocManager.getDoc @project_id, @doc_id, (err, doc)=>
err.should.equal @stubbedError
done()
describe "when the project exists and the doc is in it", -> describe "when the project exists and the doc is in it", ->
beforeEach -> beforeEach ->
@project = { name: "mock-project" }
@doc = { _id: @doc_id, lines: ["mock-lines"] }
@mongoPath = "mock.mongo.path" @mongoPath = "mock.mongo.path"
@MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project) @MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
@MongoManager.findDoc = sinon.stub().callsArg(1)
@DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, @doc, @mongoPath) @DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, @doc, @mongoPath)
@DocManager.getDoc @project_id, @doc_id, @callback @DocManager.getDoc @project_id, @doc_id, @callback
@ -42,6 +66,7 @@ describe "DocManager", ->
describe "when the project does not exist", -> describe "when the project does not exist", ->
beforeEach -> beforeEach ->
@MongoManager.findProject = sinon.stub().callsArgWith(1, null, null) @MongoManager.findProject = sinon.stub().callsArgWith(1, null, null)
@MongoManager.findDoc = sinon.stub().callsArg(1)
@DocManager.findDocInProject = sinon.stub() @DocManager.findDocInProject = sinon.stub()
@DocManager.getDoc @project_id, @doc_id, @callback @DocManager.getDoc @project_id, @doc_id, @callback
@ -59,9 +84,7 @@ describe "DocManager", ->
@DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, null, null) @DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, null, null)
@MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc) @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc)
describe "when include_deleted = true", -> @DocManager.getDoc @project_id, @doc_id, @callback
beforeEach ->
@DocManager.getDoc @project_id, @doc_id, include_deleted: true, @callback
it "should try to find the doc in the docs collection", -> it "should try to find the doc in the docs collection", ->
@MongoManager.findDoc @MongoManager.findDoc
@ -73,17 +96,7 @@ describe "DocManager", ->
.calledWith(null, @doc) .calledWith(null, @doc)
.should.equal true .should.equal true
describe "when include_deleted is not set", ->
beforeEach ->
@DocManager.getDoc @project_id, @doc_id, @callback
it "should not try to find the doc in the docs collection", ->
@MongoManager.findDoc.called.should.equal false
it "should return a NotFoundError", ->
@callback
.calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
.should.equal true
describe "when the doc does not exist anywhere", -> describe "when the doc does not exist anywhere", ->
beforeEach -> beforeEach ->
@ -124,7 +137,7 @@ describe "DocManager", ->
beforeEach -> beforeEach ->
@MongoManager.findProject = sinon.stub().callsArgWith(1, null, null) @MongoManager.findProject = sinon.stub().callsArgWith(1, null, null)
@DocManager.findAllDocsInProject = sinon.stub() @DocManager.findAllDocsInProject = sinon.stub()
@DocManager.getDoc @project_id, @doc_id, @callback @DocManager.getAllDocs @project_id, @callback
it "should not try to find the doc in the project", -> it "should not try to find the doc in the project", ->
@DocManager.findAllDocsInProject.called.should.equal false @DocManager.findAllDocsInProject.called.should.equal false

View file

@ -29,7 +29,7 @@ describe "HttpController", ->
@req.params = @req.params =
project_id: @project_id project_id: @project_id
doc_id: @doc_id doc_id: @doc_id
@DocManager.getDoc = sinon.stub().callsArgWith(3, null, @doc) @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc)
describe "without deleted docs", -> describe "without deleted docs", ->
beforeEach -> beforeEach ->
@ -37,7 +37,7 @@ describe "HttpController", ->
it "should get the document (including deleted)", -> it "should get the document (including deleted)", ->
@DocManager.getDoc @DocManager.getDoc
.calledWith(@project_id, @doc_id, include_deleted: false) .calledWith(@project_id, @doc_id)
.should.equal true .should.equal true
it "should return the doc as JSON", -> it "should return the doc as JSON", ->
@ -52,13 +52,11 @@ describe "HttpController", ->
describe "with deleted docs", -> describe "with deleted docs", ->
beforeEach -> beforeEach ->
@req.query =
include_deleted: 'true'
@HttpController.getDoc @req, @res, @next @HttpController.getDoc @req, @res, @next
it "should get the document (without deleted)", -> it "should get the document (without deleted)", ->
@DocManager.getDoc @DocManager.getDoc
.calledWith(@project_id, @doc_id, include_deleted: true) .calledWith(@project_id, @doc_id)
.should.equal true .should.equal true
describe "getRawDoc", -> describe "getRawDoc", ->