Created
November 30, 2012 19:00
-
-
Save biovisualize/4177763 to your computer and use it in GitHub Desktop.
Pivot/aggregate function jasmine specs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('New Pivot Function', function() { | |
var pivot, data, columnNames; | |
beforeEach(function () { | |
data = [ | |
['P1', 'R1', 'T1', 10, 3], | |
['P1', 'R1', 'T2', 20, 12], | |
['P1', 'R2', 'T1', 3, 1], | |
['P2', 'R2', 'T2', 4, 40]]; | |
columnNames = ['Product', 'Region', 'Time', 'Cost', 'Volume']; | |
pivot = d3.dataTransformer.pivot(); | |
}); | |
it('should work with minimal settings, returning a flat table', function() { | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 'R1', 'T1', 10, 3], | |
['P1', 'R1', 'T2', 20, 12], | |
['P1', 'R2', 'T1', 3, 1], | |
['P2', 'R2', 'T2', 4, 40]]); | |
expect(table.colHeaders).toEqual(['Product', 'Region', 'Time', 'Cost', 'Volume']); | |
expect(table.rowHeaders).toBeNull(); | |
}); | |
it('filters a flat table by some measures', function() { | |
pivot.measures(['Product', 'Cost']); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 10], | |
['P1', 20], | |
['P1', 3], | |
['P2', 4]]); | |
expect(table.colHeaders).toEqual(['Product', 'Cost']); | |
expect(table.rowHeaders).toBeNull(); | |
}); | |
it('aggregates when nestKeysCol is provided', function() { | |
pivot.nestKeysCol(['Product']) | |
.measures(['Cost']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 33], | |
['P2', 4]]); | |
expect(table.colHeaders).toEqual(['Product', 'Cost']); | |
}); | |
it('aggregates when nestKeysRow is provided', function() { | |
pivot.nestKeysRow(['Product']) | |
.measures(['Cost']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 'P2'], | |
[33, 4]]); | |
expect(table.colHeaders).toBeNull(); | |
expect(table.rowHeaders).toEqual(['Product', 'Cost']); | |
}); | |
it('aggregates multiple nestKeys', function() { | |
pivot.nestKeysCol(['Product', 'Region']) | |
.measures(['Cost']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 'R1', 30], | |
['P1', 'R2', 3], | |
['P2', 'R2', 4]]); | |
expect(table.colHeaders).toEqual(['Product', 'Region', 'Cost']); | |
}); | |
it('aggregates on mixed strings and numbers measures', function() { | |
pivot.nestKeysCol(['Product', 'Region']) | |
.measures(['Cost', 'Time']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 'R1', 30, 2], | |
['P1', 'R2', 3, 1], | |
['P2', 'R2', 4, 1]]); | |
expect(table.colHeaders).toEqual(['Product', 'Region', 'Cost', 'Time']); | |
}); | |
it('aggregates on two measures', function() { | |
pivot.nestKeysCol(['Product', 'Region']) | |
.measures(['Cost', 'Volume']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 'R1', 30, 15], | |
['P1', 'R2', 3, 1], | |
['P2', 'R2', 4, 40]]); | |
expect(table.colHeaders).toEqual(['Product', 'Region', 'Cost', 'Volume']); | |
}); | |
it('aggregates on more than two measures', function() { | |
pivot.nestKeysCol(['Product', 'Region']) | |
.measures(['Cost', 'Volume', 'Region']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 'R1', 30, 15, 2], | |
['P1', 'R2', 3, 1, 1], | |
['P2', 'R2', 4, 40, 1]]); | |
expect(table.colHeaders).toEqual(['Product', 'Region', 'Cost', 'Volume', 'Region']); | |
}); | |
it('aggregates on strings, defaulting to sum', function() { | |
data = [ | |
['P1', 'R1', 10], | |
['P1', 'R1', 20], | |
['P1', 'R2', 3], | |
['P2', 'R2', 4]]; | |
columnNames = ['Product', 'Region', 'Cost']; | |
pivot.nestKeysCol(['Product']) | |
.measures(['Region', 'Cost']); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 3, 33], | |
['P2', 1, 4]]); | |
expect(table.colHeaders).toEqual(['Product', 'Region', 'Cost']); | |
}); | |
it('aggregates on strings, defaulting to count', function() { | |
data = [ | |
['P1', 'R1', 'A'], | |
['P1', 'R1', 'B'], | |
['P1', 'R2', 'C'], | |
['P2', 'R2', 'D']]; | |
columnNames = ['Product', 'Region', 'Cost']; | |
pivot.nestKeysCol(['Product', 'Region']) | |
.measures(['Cost']); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 'R1', 2], | |
['P1', 'R2', 1], | |
['P2', 'R2', 1]]); | |
expect(table.colHeaders).toEqual(['Product', 'Region', 'Cost']); | |
}); | |
it('aggregates dates, defaulting to count', function() { | |
data = [ | |
['P1', 'R1', new Date(80, 1, 2)], | |
['P1', 'R1', new Date(80, 2, 2)], | |
['P1', 'R2', new Date(80, 3, 2)], | |
['P2', 'R2', new Date(80, 4, 2)]]; | |
columnNames = ['Product', 'Region', 'Cost']; | |
pivot.nestKeysCol(['Product', 'Region']) | |
.measures(['Cost']); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', 'R1', 2], | |
['P1', 'R2', 1], | |
['P2', 'R2', 1]]); | |
expect(table.colHeaders).toEqual(['Product', 'Region', 'Cost']); | |
}); | |
it('handles built-in aggregate functions', function() { | |
pivot.nestKeysCol(['Product', 'Region']) | |
.measures(['Cost']); | |
var aggregateFunctions = ['sum', 'mean', 'median', 'max', 'min']; | |
var expectedResults = [30, 15, 15, 20, 10]; | |
aggregateFunctions.forEach(function(d, i){ | |
pivot.aggregateFunc(aggregateFunctions[i]); | |
var table = pivot(data, columnNames); | |
expect(table.values[0][2]).toEqual(expectedResults[i]); | |
}); | |
}); | |
it('handles custom aggregate functions', function() { | |
pivot.nestKeysCol(['Product']) | |
.measures(['Cost']) | |
.aggregateFunc(function(d){ return d.reduce(function(pD, d2, i, a){ return pD + ',' + d2; }); }); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
['P1', '10,20,3'], | |
['P2', 4]]); | |
expect(table.colHeaders).toEqual(['Product', 'Cost']); | |
}); | |
it('pivots when nestKeysRow is provided', function() { | |
pivot.nestKeysCol(['Product']) | |
.nestKeysRow(['Region']) | |
.measures(['Cost']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
[30, ''], | |
[3, 4]]); | |
expect(table.colHeaders).toEqual(['P1', 'P2']); | |
expect(table.rowHeaders).toEqual(['R1', 'R2']); | |
}); | |
it('only pivots on the first measure for now', function() { | |
pivot.nestKeysCol(['Product']) | |
.nestKeysRow(['Region']) | |
.measures(['Cost', 'Volume']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
[30, ''], | |
[3, 4]]); | |
expect(table.colHeaders).toEqual(['P1', 'P2']); | |
expect(table.rowHeaders).toEqual(['R1', 'R2']); | |
}); | |
it('only pivots on single nestKeys for now', function() { | |
pivot.nestKeysCol(['Product', 'Time']) | |
.nestKeysRow(['Region']) | |
.measures(['Cost']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
[30, ''], | |
[3, 4]]); | |
expect(table.colHeaders).toEqual(['P1', 'P2']); | |
expect(table.rowHeaders).toEqual(['R1', 'R2']); | |
}); | |
it('handles pivoting using the same dimension on rows and cols', function() { | |
pivot.nestKeysCol(['Region']) | |
.nestKeysRow(['Region']) | |
.measures(['Cost']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values).toEqual([ | |
[30, ''], | |
['', 7]]); | |
expect(table.colHeaders).toEqual(['R1', 'R2']); | |
expect(table.rowHeaders).toEqual(['R1', 'R2']); | |
}); | |
it('uses empty strings for empty cells', function() { | |
pivot.nestKeysCol(['Product']) | |
.nestKeysRow(['Region']) | |
.measures(['Cost']) | |
.aggregateFunc('sum'); | |
var table = pivot(data, columnNames); | |
expect(table.values[0][1]).toEqual(''); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment