Skip to content

Commit

Permalink
Added unit converter tests and extra checks for malformed input data
Browse files Browse the repository at this point in the history
  • Loading branch information
atruskie committed Jul 4, 2014
1 parent 67bc7e4 commit 8ed63e5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
15 changes: 10 additions & 5 deletions src/components/services/unitConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ uc.factory("bawApp.unitConverter", ['conf.constants', function (constants) {
return Math.abs(fraction - 1);
}

var keysToCheck = ["sampleRate", "spectrogramWindowSize", "startOffset", "endOffset"];

function calculateUnitConversions(data) {
var result = {
pixelsPerSecond: NaN,
Expand All @@ -15,15 +17,18 @@ uc.factory("bawApp.unitConverter", ['conf.constants', function (constants) {
enforcedImageWidth: NaN,
enforcedImageHeight: NaN
};

if (data.sampleRate === undefined ||
data.spectrogramWindowSize === undefined ||
data.startOffset === undefined ||
data.endOffset === undefined) {

if (keysToCheck.some(function (key) { return data[key] === undefined; })) {
console.warn("unitConverter:calculateUnitConversions: not enough information available to calculate unit conversions");
return result;
}

keysToCheck.forEach(function(key) {
if (!angular.isNumber(data[key])) {
throw "Input data field `" + key + "` should be a number!";
}
});

// based on meta data only
var duration = data.endOffset - data.startOffset,
nyquistFrequency = (data.sampleRate / 2.0),
Expand Down
33 changes: 20 additions & 13 deletions src/components/services/unitConverters.spec.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
describe("The unitConverter service", function () {

var unitConverter;
var inputArgs = {
sampleRate: null,
spectrogramWindowSize: null,
endOffset: null,
startOffset: null,
imageElement: null
};
var inputArgs = {};

beforeEach(module('bawApp.services.unitConverter'));

beforeEach(inject(["bawApp.unitConverter", function (providedUnitConverted) {
unitConverter = providedUnitConverted;

inputArgs = {
sampleRate: null,
spectrogramWindowSize: null,
endOffset: null,
startOffset: null,
imageElement: null,
audioRecordingAbsoluteStartDate: null
sampleRate: undefined,
spectrogramWindowSize: undefined,
endOffset: undefined,
startOffset: undefined,
imageElement: undefined,
audioRecordingAbsoluteStartDate: undefined
};
}]));

Expand Down Expand Up @@ -75,6 +69,19 @@ describe("The unitConverter service", function () {
expect(converters.input).toBe(inputArgs);
});

["sampleRate", "spectrogramWindowSize", "startOffset", "endOffset"].forEach(function (key) {
it("requires " + key + " be provided as a number", function () {

var f = function () {
var local = angular.extend({}, inputArgs);
local[key] = inputArgs[key].toString();
unitConverter.getConversions(local);
};

expect(f).toThrowError("Input data field `" + key + "` should be a number!");
});
});

it("ensure the absolute start date of the input object is output and is a date", function(){
var isDate = converters.input.audioRecordingAbsoluteStartDate instanceof Date;

Expand Down

0 comments on commit 8ed63e5

Please sign in to comment.