changed findElementByPath to not call cb more than once & deal with nulls

This commit is contained in:
Henry Oswald 2014-11-11 14:44:44 +00:00
parent 948183b685
commit f5d3801bfb
2 changed files with 52 additions and 5 deletions

View file

@ -69,13 +69,13 @@ module.exports =
return cb null, haystackFolder return cb null, haystackFolder
needleFolderName = foldersList[level] needleFolderName = foldersList[level]
found = false found = false
_.each haystackFolder.folders, (folder)-> for folder in haystackFolder.folders
if folder.name.toLowerCase() == needleFolderName.toLowerCase() if folder.name.toLowerCase() == needleFolderName.toLowerCase()
found = true found = true
if level == foldersList.length-1 if level == foldersList.length-1
cb null, folder return cb null, folder
else else
getParentFolder(folder, foldersList, ++level, cb) return getParentFolder(folder, foldersList, level+1, cb)
if !found if !found
cb("not found project_or_id: #{project_or_id} search path: #{needlePath}, folder #{foldersList[level]} could not be found") cb("not found project_or_id: #{project_or_id} search path: #{needlePath}, folder #{foldersList[level]} could not be found")
@ -84,7 +84,7 @@ module.exports =
return cb null, folder return cb null, folder
enteties = _.union folder.fileRefs, folder.docs, folder.folders enteties = _.union folder.fileRefs, folder.docs, folder.folders
result = _.find enteties, (entity)-> result = _.find enteties, (entity)->
entity.name.toLowerCase() == entityName.toLowerCase() entity?.name.toLowerCase() == entityName.toLowerCase()
if result? if result?
cb null, result cb null, result
else else

View file

@ -160,7 +160,7 @@ describe 'project model', ->
doc._id.should.equal rootDoc._id doc._id.should.equal rootDoc._id
done() done()
describe 'finding an entity by path', (done)-> describe 'findElementByPath', ->
it 'should take a doc path and return the element for a root level document', (done)-> it 'should take a doc path and return the element for a root level document', (done)->
path = "#{doc1.name}" path = "#{doc1.name}"
@ -221,6 +221,53 @@ describe 'project model', ->
assert.equal element, undefined assert.equal element, undefined
done() done()
describe "where duplicate folder exists", ->
beforeEach ->
@duplicateFolder = {name:"duplicate1", _id:"1234", folders:[{
name: "1"
docs:[{name:"main.tex", _id:"456"}]
folders: []
fileRefs: []
}], docs:[@doc = {name:"main.tex", _id:"456"}], fileRefs:[]}
@project =
rootFolder:[
folders: [@duplicateFolder, @duplicateFolder]
fileRefs: []
docs: []
]
Project.getProject = sinon.stub()
Project.getProject.callsArgWith(2, null, @project)
it "should not call the callback more than once", (done)->
@locator.findElementByPath project._id, "#{@duplicateFolder.name}/#{@doc.name}", ->
done() #mocha will throw exception if done called multiple times
it "should not call the callback more than once when the path is longer than 1 level below the duplicate level", (done)->
@locator.findElementByPath project._id, "#{@duplicateFolder.name}/1/main.tex", ->
done() #mocha will throw exception if done called multiple times
describe "with a null doc", ->
beforeEach ->
@project =
rootFolder:[
folders: []
fileRefs: []
docs: [{name:"main.tex"}, null, {name:"other.tex"}]
]
Project.getProject = sinon.stub()
Project.getProject.callsArgWith(2, null, @project)
it "should not crash with a null", (done)->
callback = sinon.stub()
@locator.findElementByPath project._id, "/other.tex", (err, element)->
element.name.should.equal "other.tex"
done()
describe 'finding a project by user_id and project name', ()-> describe 'finding a project by user_id and project name', ()->
it 'should return the projet from an array case insenstive', (done)-> it 'should return the projet from an array case insenstive', (done)->