moved count element to project entity handler and added tests

This commit is contained in:
Henry Oswald 2016-02-26 13:25:09 +00:00
parent d95526bd8b
commit 8aec86af4e
3 changed files with 81 additions and 19 deletions

View file

@ -424,6 +424,26 @@ module.exports = ProjectEntityHandler =
}
}, {}, callback
_countElements : (project, callback)->
countFolder = (folder, cb = (err, count)->)->
jobs = _.map folder?.folders, (folder)->
(asyncCb)-> countFolder folder, asyncCb
async.series jobs, (err, subfolderCounts)->
total = 0
if subfolderCounts?.length > 0
total = _.reduce subfolderCounts, (a, b)-> return a + b
if folder?.docs?.length?
total += folder?.docs?.length
if folder?.fileRefs?.length?
total += folder?.fileRefs?.length
cb(null, total)
countFolder project.rootFolder[0], callback
_putElement: (project_id, folder_id, element, type, callback = (err, path)->)->
sanitizeTypeOfElement = (elementType)->
lastChar = elementType.slice -1

View file

@ -66,26 +66,7 @@ ProjectSchema.statics.findAllUsersProjects = (user_id, requiredFields, callback)
this.find {readOnly_refs:user_id}, requiredFields, (err, readOnlyProjects)=>
callback(err, projects, collabertions, readOnlyProjects)
countElements = (project, callback)->
countFolder = (folder, cb)->
jobs = _.map folder?.folders, (folder)->
(asyncCb)-> countFolder folder, asyncCb
async.series jobs, (err, results)->
total = _.reduce results, (a, b)-> return a+b
total += folder?.docs?.length
total += folder?.fileRefs?.length
cb(null, subTotal)
countFolder project.rootFolder[0], callback
getIndexOf = (searchEntity, id)->
length = searchEntity.length
count = 0
while(count < length)
if searchEntity[count]._id+"" == id+""
return count
count++

View file

@ -1063,5 +1063,66 @@ describe 'ProjectEntityHandler', ->
describe "_countElements", ->
beforeEach ->
@project.rootFolder[0].docs = [{_id:123}, {_id:345}]
@project.rootFolder[0].fileRefs = [{_id:123}, {_id:345}, {_id:456}]
@project.rootFolder[0].folders = [
{
docs:
[{_id:123}, {_id:345}, {_id:456}]
fileRefs:{}
folders: [
{
docs:[_id:1234],
fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
folders:[
{
docs:[{_id:321321}, {_id:123213}]
fileRefs:[{_id:312321}]
folders:[]
}
]
}
]
},{
docs:[{_id:123}, {_id:32131}]
fileRefs:[]
folders:[
{
docs:[{_id:3123}]
fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
folders:0
}
]
}
]
it "should return the correct number", (done)->
@ProjectEntityHandler._countElements @project, (err, count)->
count.should.equal 21
done()
it "should deal with null folders", (done)->
@project.rootFolder[0].folders[0].folders = undefined
@ProjectEntityHandler._countElements @project, (err, count)->
count.should.equal 14
done()
it "should deal with null docs", (done)->
@project.rootFolder[0].folders[0].docs = undefined
@ProjectEntityHandler._countElements @project, (err, count)->
count.should.equal 18
done()
it "should deal with null fileRefs", (done)->
@project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
@ProjectEntityHandler._countElements @project, (err, count)->
count.should.equal 18
done()