Parcourir la source

Improve testing of error conditions in the FSPersistorManager.

Shane Kilkelly il y a 11 ans
Parent
commit
82af1be756

+ 1 - 1
services/filestore/app/coffee/FSPersistorManager.coffee

@@ -40,7 +40,7 @@ module.exports =
       if err.code == 'ENOENT'
         callback new Errors.NotFoundError(err.message), null
       else
-        callback err
+        callback err, null
     sourceStream.on 'readable', () ->
       # This can be called multiple times, but the callback wrapper
       # ensures the callback is only called once

+ 38 - 2
services/filestore/test/unit/coffee/FSPersistorManagerTests.coffee

@@ -29,6 +29,8 @@ describe "FSPersistorManagerTests", ->
         err:->
       "response":response
       "rimraf":@Rimraf
+      "./Errors": @Errors =
+        NotFoundError: sinon.stub()
     @location = "/tmp"
     @name1 = "530f2407e7ef165704000007/530f838b46d9a9e859000008"
     @name1Filtered ="530f2407e7ef165704000007_530f838b46d9a9e859000008"
@@ -89,6 +91,42 @@ describe "FSPersistorManagerTests", ->
         @Fs.createReadStream.calledWith("#{@location}/#{@name1Filtered}", @opts).should.equal true
         done()
 
+    describe "error conditions", ->
+
+      beforeEach ->
+        @fakeCode = 'ENOENT'
+        @Fs.createReadStream.returns(
+          on: (key, callback) =>
+            err = new Error()
+            err.code = @fakeCode
+            callback(err, null)
+        )
+
+      describe "when the file does not exist", ->
+
+        beforeEach ->
+          @fakeCode = 'ENOENT'
+
+        it "should give a NotFoundError", (done) ->
+          @FSPersistorManager.getFileStream @location, @name1, @opts, (err,res)=>
+            expect(res).to.equal null
+            expect(err).to.not.equal null
+            expect(err instanceof @Errors.NotFoundError).to.equal true
+            done()
+
+      describe "when some other error happens", ->
+
+        beforeEach ->
+          @fakeCode = 'SOMETHINGHORRIBLE'
+
+        it "should give an Error", (done) ->
+          @FSPersistorManager.getFileStream @location, @name1, @opts, (err,res)=>
+            expect(res).to.equal null
+            expect(err).to.not.equal null
+            expect(err instanceof Error).to.equal true
+            done()
+
+
 
   describe "copyFile", ->
     beforeEach ->
@@ -170,5 +208,3 @@ describe "FSPersistorManagerTests", ->
       @FSPersistorManager.checkIfFileExists @location, @name1, (err,exists) =>
         exists.should.be.false
         done()
-
-