From 7757cc98ab01a1993aa143cffbef35dbc0e84a6b Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Mon, 24 Feb 2014 15:26:11 -0700 Subject: [PATCH] Adding checks for arguments --- src/courier/calculateIndices.js | 6 +++++- test/unit/specs/calculateIndices.js | 19 +++---------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/courier/calculateIndices.js b/src/courier/calculateIndices.js index 1f0cebe553f57..6c336d3ff79d5 100644 --- a/src/courier/calculateIndices.js +++ b/src/courier/calculateIndices.js @@ -2,13 +2,17 @@ define(function (require) { return function (start, end, interval, pattern) { if (end.isBefore(start)) { - throw new Error('Start must begin before end'); + throw new Error('Start must begin before end.'); } if (!~['hour','day','week','year'].indexOf(interval)) { throw new Error('Interval must be hour, day, week, or year.'); } + if (!pattern) { + throw new Error('Pattern can not be empty.'); + } + var data = []; while(start.isBefore(end)) { start.add(interval, '1'); diff --git a/test/unit/specs/calculateIndices.js b/test/unit/specs/calculateIndices.js index b6f1da0899e84..474dafb3e6841 100644 --- a/test/unit/specs/calculateIndices.js +++ b/test/unit/specs/calculateIndices.js @@ -5,19 +5,18 @@ define(function (require) { describe('calculateIndices()', function () { describe('error checking', function() { - it('should throw an error if start is > end', function () { expect(function () { calculateIndices(moment().add('day', 1), moment()); }).to.throwError(); }); - it('should throw an error if interval is not [ hour, day, week, year ]', function () { expect(function () { calculateIndices(moment().subtract('day', 1), moment(), 'century' ); }).to.throwError(); }); - + it('should throw an error if pattern is not set', function () { + expect(function () { calculateIndices(moment().subtract('day', 1), moment(), 'hour' ); }).to.throwError(); + }); }); describe('hourly interval', function() { - beforeEach(function () { var date = '2014-01-15 04:30:10'; this.start = moment(date).subtract('hours', 4); @@ -31,16 +30,13 @@ define(function (require) { 'logstash-2014.01.15.04' ] }); - it('should return a set of hourly indices', function () { expect(calculateIndices(this.start, this.end, this.interval, this.pattern)) .to.eql(this.fixture); }); - }); describe('daily interval', function() { - beforeEach(function () { var date = '2014-01-15 04:30:10'; this.start = moment(date).subtract('days', 4); @@ -54,16 +50,13 @@ define(function (require) { 'logstash-2014.01.15' ] }); - it('should return a set of daily indices', function () { expect(calculateIndices(this.start, this.end, this.interval, this.pattern)) .to.eql(this.fixture); }); - }); describe('weekly interval', function() { - beforeEach(function () { var date = '2014-01-15 04:30:10'; this.start = moment(date).subtract('week', 4); @@ -77,16 +70,13 @@ define(function (require) { 'logstash-2014.01.15' ] }); - it('should return a set of daily indices', function () { expect(calculateIndices(this.start, this.end, this.interval, this.pattern)) .to.eql(this.fixture); }); - }); describe('yearly interval', function() { - beforeEach(function () { var date = '2014-01-15 04:30:10'; this.start = moment(date).subtract('years', 4); @@ -100,15 +90,12 @@ define(function (require) { 'logstash-2014.01.15' ] }); - it('should return a set of yearly indices', function () { expect(calculateIndices(this.start, this.end, this.interval, this.pattern)) .to.eql(this.fixture); }); - }); - }); });