diff --git a/lib/index.js b/lib/index.js index 256252e..3b5d22c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -29,7 +29,7 @@ engine.generateReport = function (dataDriver, startDate, endDate, callback) { } if (data === null) { - for (var i = 0; i < daysToLoop; i++) { + for (var x = 0; x < daysToLoop; x++) { result.push(0); } @@ -37,14 +37,23 @@ engine.generateReport = function (dataDriver, startDate, endDate, callback) { return; } - for (var i = 0; i < daysToLoop; i++) { + var loopDate = moment(startDate).add(i, 'day').toDate(); + var dayData = data.filter(function (value, i) { - return value.date === moment(startDate); + return value.date.getTime() === loopDate.getTime(); }); - if (dayData === null || dayData.length === 0) { - result.push(0); + if (dayData === null || dayData === undefined || dayData.length === 0) { + result.push({ + date: loopDate, + value: 0 + }); + } else { + result.push({ + date: loopDate, + value: dayData[0].value + }); } } diff --git a/test/basicEngineTests.js b/test/basicEngineTests.js index 66452a0..6f62534 100644 --- a/test/basicEngineTests.js +++ b/test/basicEngineTests.js @@ -45,4 +45,28 @@ exports.checkThatDataDriverErrorGetsSent = function (test) { test.equal(null, data); test.done(); }); +}; + + +exports.checkThatOneDayOfDataIsReturned = function (test) { + var startDate = new Date(2014, 9, 1); + var endDate = new Date(2014, 9, 2); + var testEngine = { + getData: function (startDate, endDate, callback) { + callback( + null, + [ + {date: new Date(2014, 9, 1), value: 10} + ] + ); + } + }; + + engine.generateReport(testEngine, startDate, endDate, function (err, data) { + test.ok(data); + test.equal(1, data.length); + test.equal(new Date(2014, 9, 1).getTime(), data[0].date.getTime()); + test.equal(10, data[0].value); + test.done(); + }); }; \ No newline at end of file