Skip to content

Commit

Permalink
Fixed up some unit-tests and the mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
supernomad committed Jul 3, 2015
1 parent 3d25394 commit 4c60a46
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 59 deletions.
6 changes: 2 additions & 4 deletions libs/managers/uploadManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ function updateUpload(uploadId, index, file, ttl, done) {
function(upload, callback) {
upload.chunks[index] = true;
dataCache.update(upload.Id, upload, ttl, function(error, success) {
if(typeHelper.doesExist(error)) {
callback(error);
} else if(!success) {
if(!success) {
callback(errorModels.ServerError());
} else {
callback(null, upload);
callback(error, upload);
}
});
},
Expand Down
24 changes: 7 additions & 17 deletions mocks/libs/caching/localCache.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
var cache = {},
returnValue = true,
returnErrorOnDelete = false,
returnErrorOnCreate = false,
returnErrorOnRestore = false;
errorValue = null;

function setReturnValue(retValue) {
returnValue = retValue;
}

function setReturnErrorOnRestore(value) {
returnErrorOnRestore = value;
}
function setReturnErrorOnCreate(value) {
returnErrorOnCreate = value;
}
function setReturnErrorOnDelete(value) {
returnErrorOnDelete = value;
function setErrorValue(shouldError) {
errorValue = shouldError ? new Error('Random failure') : null;
}

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

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

function update(key, val, ttl, callback) {
Expand All @@ -33,14 +25,12 @@ function update(key, val, ttl, callback) {

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

module.exports = {
setReturnValue: setReturnValue,
setReturnErrorOnRestore: setReturnErrorOnRestore,
setReturnErrorOnCreate: setReturnErrorOnCreate,
setReturnErrorOnDelete: setReturnErrorOnDelete,
setErrorValue: setErrorValue,
'create': create,
'restore': restore,
'update': update,
Expand Down
38 changes: 18 additions & 20 deletions mocks/libs/io.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
/* global Buffer */
var returnErrorOnGetFileStats = false;
var fileSize = 1024,
errorValue = null;

function setReturnErrorOnGetFileStats(value) {
returnErrorOnGetFileStats = value;
function setFileSize(size) {
fileSize = size;
}

function setErrorValue(shouldError) {
errorValue = shouldError ? new Error('Random Error') : null;
}

function getFileStats(path, callback) {
if(path.indexOf('chunksize') !== -1) {
callback(returnErrorOnGetFileStats ? new Error('RandomError') : null, {
size: 1025
});
} else {
callback(returnErrorOnGetFileStats ? new Error('RandomError') : null, {
size: 1024
});
}
callback(errorValue, { size: fileSize });
}

function createFile(path, buffer, offset, length, callback) {
callback(null);
callback(errorValue);
}

function writeFileChunk(path, buffer, offset, length, position, callback) {
callback(null);
callback(errorValue);
}

function deleteFile(path, callback) {
callback(null);
callback(errorValue);
}

function readFile(path, callback) {
callback(null, new Buffer(0));
callback(errorValue, new Buffer(0));
}

function renameFile(path, newPath, callback) {
callback(null);
callback(errorValue);
}

function readFileChunk(path, buffer, offset, length, position, callback) {
callback(null, buffer.length, buffer);
function readFileChunk(path, buffer, offset, length, position, callback) {
callback(errorValue, buffer.length, buffer);
}

module.exports = {
setReturnErrorOnGetFileStats: setReturnErrorOnGetFileStats,
setFileSize: setFileSize,
setErrorValue: setErrorValue,
GetFileStats: getFileStats,
CreateFile: createFile,
WriteFileChunk: writeFileChunk,
Expand Down
5 changes: 1 addition & 4 deletions routes/chunked-upload-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ var routes = {
} else {
var file = {};
for (var key in req.files) {
if (req.files.hasOwnProperty(key)) {
file = req.files[key];
break;
}
file = req.files[key];
}
uploadManager.updateUpload(req.params.uploadId, index, file, defaultTtl, function(error, upload, complete) {
if(typeHelper.doesExist(error)) {
Expand Down
27 changes: 21 additions & 6 deletions test/routes/chunked-download-routes-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,34 @@ var async = require('async'),
describe('chunked-download-routes.js', function() {
var io_mock = require.main.require('mocks/libs/io'),
cache_mock = require.main.require('mocks/libs/caching/localCache'),
routes = require.main.require('routes/chunked-download-routes')(cache_mock, io_mock, {debug:true, routePrefix:'/chunked/download', chunkSize: 1024}),
routes = require.main.require('routes/chunked-download-routes')(cache_mock, io_mock, {debug:true, routePrefix:'/chunked/download', chunkSize: 1024, defaultTtl: 3600}),
downloadId = null;

afterEach('reset the cache_mock', function() {
cache_mock.setReturnValue(true);
cache_mock.setReturnErrorOnRestore(false);
io_mock.setReturnErrorOnGetFileStats(false);
cache_mock.setErrorValue(false);
io_mock.setErrorValue(false);
io_mock.setFileSize(1024);
});

it('should return a route object', function() {
should.exist(routes);
routes.should.be.a.Object();
});

it('should handle missing options', function() {
var testRoutes = require.main.require('routes/chunked-download-routes')(cache_mock, io_mock, {routePrefix:'/chunked/download', chunkSize: 1024, defaultTtl: 3600});
should.exist(testRoutes);
testRoutes = require.main.require('routes/chunked-download-routes')(cache_mock, io_mock, {debug:true, chunkSize: 1024, defaultTtl: 3600});
should.exist(testRoutes);
testRoutes = require.main.require('routes/chunked-download-routes')(cache_mock, io_mock, {debug:true, routePrefix:'/chunked/download', defaultTtl: 3600});
should.exist(testRoutes);
testRoutes = require.main.require('routes/chunked-download-routes')(cache_mock, io_mock, {debug:true, routePrefix:'/chunked/download'});
should.exist(testRoutes);
testRoutes = require.main.require('routes/chunked-download-routes')(cache_mock, io_mock);
should.exist(testRoutes);
});

describe('#POST', function() {
it('should have a uri and handler', function() {
should.exist(routes.post);
Expand Down Expand Up @@ -54,6 +68,7 @@ describe('chunked-download-routes.js', function() {
});

it('should create a new download if the request object is considered valid', function(done) {
io_mock.setFileSize(1025);
routes.post.handler({
body: {
path: '/i/am/a/path/to/a/chunksize'
Expand Down Expand Up @@ -92,7 +107,7 @@ describe('chunked-download-routes.js', function() {
});

it('should throw a ServerError if the file stats cannot be found', function(done) {
io_mock.setReturnErrorOnGetFileStats(true);
io_mock.setErrorValue(true);
routes.post.handler({
body: {
path: '/i/am/a/path/to/a/destination'
Expand Down Expand Up @@ -171,7 +186,7 @@ describe('chunked-download-routes.js', function() {
});

it('should throw a ServerError if the cache fails to retrieve the download data', function(done) {
cache_mock.setReturnErrorOnRestore(true);
cache_mock.setErrorValue(true);
routes.get.handler({
params: {
downloadId: downloadId,
Expand Down Expand Up @@ -253,7 +268,7 @@ describe('chunked-download-routes.js', function() {
});

it('should throw a ServerError if the cache fails to restore the download data', function(done) {
cache_mock.setReturnErrorOnRestore(true);
cache_mock.setErrorValue(true);
routes.delete.handler({
params: {
downloadId: downloadId
Expand Down
47 changes: 39 additions & 8 deletions test/routes/chunked-upload-routes-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,32 @@ var async = require('async'),
describe('chunked-upload-routes.js', function() {
var io_mock = require.main.require('mocks/libs/io'),
cache_mock = require.main.require('mocks/libs/caching/localCache'),
routes = require.main.require('routes/chunked-upload-routes')(cache_mock, io_mock, {debug:true, routePrefix:'/chunked/upload'}),
routes = require.main.require('routes/chunked-upload-routes')(cache_mock, io_mock, {debug:true, routePrefix:'/chunked/upload', maxSize: 2147483648, defaultTtl: 3600}),
uploadId = null;

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

it('should return a route object', function() {
should.exist(routes);
routes.should.be.a.Object();
});

it('should handle missing options', function() {
var testRoutes = require.main.require('routes/chunked-upload-routes')(cache_mock, io_mock, {routePrefix:'/chunked/upload', maxSize: 2147483648, defaultTtl: 3600});
should.exist(testRoutes);
testRoutes = require.main.require('routes/chunked-upload-routes')(cache_mock, io_mock, {debug:true, maxSize: 2147483648, defaultTtl: 3600});
should.exist(testRoutes);
testRoutes = require.main.require('routes/chunked-upload-routes')(cache_mock, io_mock, {debug:true, routePrefix:'/chunked/upload', defaultTtl: 3600});
should.exist(testRoutes);
testRoutes = require.main.require('routes/chunked-upload-routes')(cache_mock, io_mock, {debug:true, routePrefix:'/chunked/upload', maxSize: 2147483648});
should.exist(testRoutes);
testRoutes = require.main.require('routes/chunked-upload-routes')(cache_mock, io_mock);
should.exist(testRoutes);
});

describe('#POST', function() {
it('should have a uri and handler', function() {
should.exist(routes.post);
Expand Down Expand Up @@ -148,7 +159,7 @@ describe('chunked-upload-routes.js', function() {
});

it('should handle an error thrown by the cache', function() {
cache_mock.setReturnErrorOnRestore(true);
cache_mock.setErrorValue(true);
routes.get.handler({
params: {
uploadId: guidHelper.newGuid()
Expand All @@ -175,7 +186,27 @@ describe('chunked-upload-routes.js', function() {
});

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

it('should handle unexpected function/property names in the files object', function() {
routes.put.handler({
params: {
uploadId: uploadId,
Expand Down Expand Up @@ -355,7 +386,7 @@ describe('chunked-upload-routes.js', function() {
});

it('should handle an error thrown by the cache restoring an upload', function() {
cache_mock.setReturnErrorOnRestore(true);
cache_mock.setErrorValue(true);
routes.put.handler({
params: {
uploadId: guidHelper.newGuid(),
Expand Down Expand Up @@ -440,7 +471,7 @@ describe('chunked-upload-routes.js', function() {
});

it('should throw a ServerError if the cache fails to restore an existing upload', function(done) {
cache_mock.setReturnErrorOnDelete(true);
cache_mock.setErrorValue(true);
routes.delete.handler({
params: {
uploadId: uploadId
Expand Down

0 comments on commit 4c60a46

Please sign in to comment.