From 4c68bae3ba8040a4e643b10b3f5fd754600a3d51 Mon Sep 17 00:00:00 2001 From: MacLeod Broad <> Date: Sat, 21 Oct 2017 14:19:46 -0400 Subject: [PATCH] Adds additional tests to DataSet --- test/DataSet.test.js | 116 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 2 deletions(-) diff --git a/test/DataSet.test.js b/test/DataSet.test.js index 2d88c054..295f00a4 100644 --- a/test/DataSet.test.js +++ b/test/DataSet.test.js @@ -181,10 +181,15 @@ describe('DataSet', function () { {_id: 2, content: 'Item 2', start: now.toISOString()} ], {fieldId: '_id'}); assert.deepEqual(data.getIds(), [1, 2]); + }); - // TODO: extensively test DataSet - // TODO: test subscribing to events + it('constructor should prevent duplicate ids', function () { + assert.throws(function () { new DataSet([{id: 'duplicate'}, {id: 'duplicate'}]) }, Error, "duplicate id throws error"); + }); + it('add should prevent duplicate ids', function () { + var dataset = new DataSet([{id: 'duplicate'}]); + assert.throws(function () { dataset.add({id: 'duplicate'}) }, Error, "duplicate id throws error"); }); it('should queue and flush changes', function () { @@ -398,4 +403,111 @@ describe('DataSet', function () { assert.equal(count, 1); }); }); + + describe('min', function () { + + it('finds the minimum value', function () { + var dataset = new DataSet([{id: 1}, {id: 2}, {id: 3}]); + var minValue = dataset.min('id'); + assert.deepEqual(minValue, {id: 1}); + }); + + it('returns null for empty dataset', function () { + var dataset = new DataSet([]); + var minValue = dataset.min('id'); + assert.equal(minValue, null); + }); + + it('ignores undefined values', function () { + var dataset = new DataSet([{id: undefined}, {id: 1}, {id: 2}, {id: 3}]); + var minValue = dataset.min('id'); + assert.deepEqual(minValue, {id: 1}); + }); + + it('handles big values', function () { + var dataset = new DataSet([{id: 10000000000000001}, {id: 10000000000000002}, {id: 10000000000000003}]); + var minValue = dataset.min('id'); + assert.deepEqual(minValue, {id: 10000000000000001}); + }); + + xit('handles big values - but not really, because of javascript number handling', function () { + var dataset = new DataSet([{id: -10000000000000001}, {id: -10000000000000002}, {id: -10000000000000003}]); + var minValue = dataset.min('id'); + assert.deepEqual(minValue, {id: -10000000000000003}); + assert.equal('' + minValue.id, '-10000000000000003') + }); + }); + describe('max', function () { + + it('finds the maximum value', function () { + var dataset = new DataSet([{id: 1}, {id: 2}, {id: 3}]); + var maxValue = dataset.max('id'); + assert.deepEqual(maxValue, {id: 3}); + }); + + it('returns null for empty dataset', function () { + var dataset = new DataSet([]); + var maxValue = dataset.max('id'); + assert.equal(maxValue, null); + }); + + it('ignores undefined values', function () { + var dataset = new DataSet([{id: undefined}, {id: 1}, {id: 2}, {id: 3}]); + var maxValue = dataset.max('id'); + assert.deepEqual(maxValue, {id: 3}); + }); + + xit('handles big values - but not really, because of javascript number handling', function () { + var dataset = new DataSet([{id: 10000000000000001}, {id: 10000000000000002}, {id: 10000000000000003}]); + var maxValue = dataset.max('id'); + assert.deepEqual(maxValue, {id: 10000000000000003}); + assert.equal('' + maxValue.id, '10000000000000003') + }); + + xit('handles negative values - This does not work because of javascript', function () { + var dataset = new DataSet([{id: -10000000000000001}, {id: -10000000000000002}, {id: -10000000000000003}]); + var maxValue = dataset.max('id'); + assert.deepEqual(maxValue, {id: -10000000000000001}); + }); + }); + + describe('distinct', function () { + + it('finds distinct values', function () { + var dataset = new DataSet([{val: 1}, {val: 2}, {val: 3}]); + var distinctValues = dataset.distinct('val'); + assert.deepEqual(distinctValues, [1, 2, 3]); + }); + + it('returns empty list for empty dataset', function () { + var dataset = new DataSet([]); + var distinctValues = dataset.distinct('val'); + assert.deepEqual(distinctValues, []); + }); + + it('ignores undefined values', function () { + var dataset = new DataSet([{val: undefined}, {val: 1}, {val: 2}, {val: 3}]); + var distinctValues = dataset.distinct('val'); + assert.deepEqual(distinctValues, [1, 2, 3]); + }); + + xit('handles big values - but not really, because of javascript number handling', function () { + var dataset = new DataSet([{val: 10000000000000001}, {val: 10000000000000002}, {val: 10000000000000003}, {val: 10000000000000004}]); + var distinctValues = dataset.distinct('val'); + assert.deepEqual(distinctValues, [ 10000000000000000, 10000000000000002, 10000000000000003, 10000000000000004]); + }); + + xit('handles negative values - This does not work because of javascript', function () { + var dataset = new DataSet([{val: -10000000000000001}, {val: -10000000000000002}, {val: -10000000000000003}, {val: -10000000000000004}]); + var distinctValues = dataset.distinct('val'); + assert.deepEqual(distinctValues, [ -10000000000000000, -10000000000000002, -10000000000000003, -10000000000000004]); + }); + }); + + describe('getDataSet', function () { + it('returns this', function () { + var dataset = new DataSet([{val: 1}, {val: 2}, {val: 3}]); + assert.equal(dataset, dataset.getDataSet()); + }); + }); });