add unit tests

This commit is contained in:
Henrique Santos 2016-03-13 20:45:48 -03:00
parent 0bb12f32fa
commit ffaac4c81b
5 changed files with 67 additions and 13 deletions

View file

@ -251,7 +251,22 @@ describe "AWSSDKPersistorManager", ->
describe "directorySize", -> describe "directorySize", ->
it "should list the directory content using s3.listObjects", (done) -> it "should list the directory content using s3.listObjects", (done) ->
@s3.listObjects.callsArgWith 1, null, Contents: []
@AWSSDKPersistorManager.directorySize @bucketName, @key, (err) =>
expect(err).to.not.be.ok
expect(@s3.listObjects.calledOnce, "called only once").to.be.true
expect((@s3.listObjects.calledWith Bucket: @bucketName, Prefix: @key),
"called with correct arguments").to.be.true
done() done()
it "should sum directory files size", (done) -> it "should dispatch the error from s3.listObjects", (done) ->
@s3.listObjects.callsArgWith 1, @error
@AWSSDKPersistorManager.directorySize @bucketName, @key, (err) =>
expect(err).to.equal @error
done()
it "should sum directory files sizes", (done) ->
@s3.listObjects.callsArgWith 1, null, Contents: [ { Size: 1024 }, { Size: 2048 }]
@AWSSDKPersistorManager.directorySize @bucketName, @key, (err, size) =>
expect(size).to.equal 3072
done() done()

View file

@ -18,6 +18,10 @@ describe "FSPersistorManagerTests", ->
unlink:sinon.stub() unlink:sinon.stub()
rmdir:sinon.stub() rmdir:sinon.stub()
exists:sinon.stub() exists:sinon.stub()
readdir:sinon.stub()
openSync:sinon.stub()
fstatSync:sinon.stub()
closeSync:sinon.stub()
@Rimraf = sinon.stub() @Rimraf = sinon.stub()
@LocalFileWriter = @LocalFileWriter =
writeStream: sinon.stub() writeStream: sinon.stub()
@ -211,5 +215,15 @@ describe "FSPersistorManagerTests", ->
describe "directorySize", -> describe "directorySize", ->
it "should sum directory files size", (done) -> it "should propogate the error", (done) ->
@Fs.readdir.callsArgWith(1, @error)
@FSPersistorManager.directorySize @location, @name1, (err, totalsize) =>
err.should.equal @error
done()
it "should sum directory files size", (done) ->
@Fs.readdir.callsArgWith(1, null, [ {'file1'}, {'file2'} ])
@Fs.fstatSync.returns({size : 1024})
@FSPersistorManager.directorySize @location, @name1, (err, totalsize) =>
expect(totalsize).to.equal 2048
done() done()

View file

@ -22,6 +22,7 @@ describe "FileController", ->
getFile: sinon.stub() getFile: sinon.stub()
deleteFile: sinon.stub() deleteFile: sinon.stub()
insertFile: sinon.stub() insertFile: sinon.stub()
getDirectorySize: sinon.stub()
@LocalFileWriter = {} @LocalFileWriter = {}
@controller = SandboxedModule.require modulePath, requires: @controller = SandboxedModule.require modulePath, requires:
"./LocalFileWriter":@LocalFileWriter "./LocalFileWriter":@LocalFileWriter
@ -155,5 +156,15 @@ describe "FileController", ->
describe "directorySize", -> describe "directorySize", ->
it "should call the file handler the directory size", (done) -> it "should return total directory size bytes", (done) ->
@FileHandler.getDirectorySize.callsArgWith(2, null, 1024)
@controller.directorySize @req, json:(result)=>
expect(result['total bytes']).to.equal 1024
done() done()
it "should send a 500 if there was an error", (done)->
@FileHandler.getDirectorySize.callsArgWith(2, "error")
@res.send = (code)->
code.should.equal 500
done()
@controller.directorySize @req, @res

View file

@ -20,6 +20,7 @@ describe "FileHandler", ->
deleteDirectory: sinon.stub() deleteDirectory: sinon.stub()
sendStream: sinon.stub() sendStream: sinon.stub()
insertFile: sinon.stub() insertFile: sinon.stub()
directorySize: sinon.stub()
@LocalFileWriter = @LocalFileWriter =
writeStream: sinon.stub() writeStream: sinon.stub()
deleteFile: sinon.stub() deleteFile: sinon.stub()
@ -193,7 +194,12 @@ describe "FileHandler", ->
@LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true @LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true
done() done()
describe "directorySize", -> describe "getDirectorySize", ->
it "should call the filestore manager to get directory size", (done) -> beforeEach ->
@PersistorManager.directorySize.callsArgWith(2)
it "should call the filestore manager to get directory size", (done)->
@handler.getDirectorySize @bucket, @key, =>
@PersistorManager.directorySize.calledWith(@bucket, @key).should.equal true
done() done()

View file

@ -259,5 +259,13 @@ describe "S3PersistorManagerTests", ->
describe "directorySize", -> describe "directorySize", ->
beforeEach ->
@S3PersistorManager = SandboxedModule.require modulePath, requires: @requires
it "should sum directory files size", (done) -> it "should sum directory files size", (done) ->
data =
Contents: [ {Size: 1024}, {Size: 2048} ]
@stubbedKnoxClient.list.callsArgWith(1, null, data)
@S3PersistorManager.directorySize @bucketName, @key, (err, totalSize)=>
totalSize.should.equal 3072
done() done()