Skip to content

Commit

Permalink
added some more unit-tests around cache failure in the chunked-upload…
Browse files Browse the repository at this point in the history
…-routes
  • Loading branch information
supernomad committed Jun 25, 2015
1 parent 5fb8c04 commit 320c155
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
27 changes: 22 additions & 5 deletions mocks/libs/caching/localCache.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
var typeHelper = require.main.require('libs/helpers/typeHelper'),
cache = {},
returnValue = true;
returnValue = true,
returnErrorOnDelete = false,
returnErrorOnCreate = false,
returnErrorOnRestore = false;

function setReturnValue(retValue) {
returnValue = retValue;
}
}

function setReturnErrorOnRestore(value) {
returnErrorOnRestore = value;
}
function setReturnErrorOnCreate(value) {
returnErrorOnCreate = value;
}
function setReturnErrorOnDelete(value) {
returnErrorOnDelete = value;
}

function create(key, val, ttl, callback) {
cache[key] = val;
callback(null, returnValue);
callback(returnErrorOnCreate ? new Error("Random failure") : null, returnValue);
}

function restore(key, callback) {
callback(null, {key: key, value: cache[key]});
callback(returnErrorOnRestore ? new Error("Random failure") : null, {key: key, value: cache[key]});
}

function update(key, val, ttl, callback) {
Expand All @@ -20,11 +34,14 @@ function update(key, val, ttl, callback) {

function del(key, callback) {
delete cache[key];
callback(null, 1);
callback(returnErrorOnDelete ? new Error("Random failure") : null, 1);
}

module.exports = {
setReturnValue: setReturnValue,
setReturnErrorOnRestore: setReturnErrorOnRestore,
setReturnErrorOnCreate: setReturnErrorOnCreate,
setReturnErrorOnDelete: setReturnErrorOnDelete,
"create": create,
"restore": restore,
"update": update,
Expand Down
60 changes: 60 additions & 0 deletions test/routes/chunked-upload-routes-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ describe("chunked-upload-routes.js", function() {

afterEach('reset the cache_mock', function() {
cache_mock.setReturnValue(true);
cache_mock.setReturnErrorOnDelete(false);
cache_mock.setReturnErrorOnRestore(false);
cache_mock.setReturnErrorOnCreate(false);
});

it('should return a route object', function() {
Expand Down Expand Up @@ -143,6 +146,21 @@ describe("chunked-upload-routes.js", function() {
error.should.be.an.instanceOf(errorModels.GenericError);
});
});

it('should handle an error thrown by the cache', function() {
cache_mock.setReturnErrorOnRestore(true);
routes.get.handler({
params: {
uploadId: guidHelper.newGuid()
}
}, {
json: function(data) {
}
}, function(error) {
should.exist(error);
error.should.be.an.instanceOf(Error);
});
});
});

describe("#PUT", function() {
Expand Down Expand Up @@ -314,6 +332,48 @@ describe("chunked-upload-routes.js", function() {
error.should.be.an.instanceOf(errorModels.GenericError);
});
});

it('should handle an error thrown by the cache restoring an upload', function() {
cache_mock.setReturnErrorOnRestore(true);
routes.put.handler({
params: {
uploadId: guidHelper.newGuid(),
index: 0
},
files: {
testFile: {
path: "random/path/to/nothing"
}
}
}, {
json: function(data) {
}
}, function(error) {
should.exist(error);
error.should.be.an.instanceOf(Error);
});
});

it('should handle an error thrown by the cache updating an upload', function() {
cache_mock.setReturnErrorOnCreate(true);
routes.put.handler({
params: {
uploadId: guidHelper.newGuid(),
index: 0
},
files: {
testFile: {
path: "random/path/to/nothing"
}
}
}, {
json: function(data) {
}
}, function(error) {
should.exist(error);
error.should.be.an.instanceOf(Error);
});
});
});

describe("#DELETE", function() {
Expand Down

0 comments on commit 320c155

Please sign in to comment.