-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdata-transformation-test.js
185 lines (169 loc) · 6.01 KB
/
data-transformation-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
//
//
var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var should = chai.should();
var expect = require('chai').expect;
var sampleData = require('./sample-data');
var dataParser = require('../src/js/data-parser');
chai.use(sinonChai);
//
describe('Data transformation (passing rows through columns)', function() {
beforeEach(function() {
this.rows = sampleData.countries;
});
//
//
//
describe('Discrete columns', function() {
beforeEach(function() {
this.columns = [ {
label: 'Continent',
accessor: 'continent',
type: 'discrete' } ];
});
it('builds a list of unique values', function() {
var parsed = dataParser(this.rows, this.columns),
parsedCol = parsed.columns[0];
parsedCol.uniqValues.should.have.length(7);
parsedCol.uniqValues.should.include('EU');
});
it('sets row value for column', function() {
var parsed = dataParser(this.rows, this.columns),
parsedRow = parsed.rows[0];
parsedRow._values[0].should.equal('EU');
});
it('accepts custom sort orders', function() {
this.columns = [ {
label: 'Day of week',
accessor: 'day',
sortOrder: 'Mon Tue Wed Thu Fri Sat Sun'.split(' '),
type: 'discrete' } ];
this.rows = [
{ first: 'c', day: 'Thu' },
{ first: 'z', day: 'Red' }, // values not in sort order are -1
{ first: 'a', day: 'Wed' },
{ first: 'b', day: 'Sat' },
{ first: 'a', day: 'Mon' },
{ first: 'z', day: 'Sun' },
{ first: 'z', day: 'Tue' } ];
var parsed = dataParser(this.rows, this.columns);
parsed.columns[0].should.have.property('sortOrder');
parsed.rows[0].should.have.property('_sortValues');
expect(parsed.rows[0]._sortValues[0]).to.equal(3);
expect(parsed.rows[1]._sortValues[0]).to.equal(-1);
expect(parsed.rows[2]._sortValues[0]).to.equal(2);
});
});
//
//
//
describe('Continuous columns', function() {
beforeEach(function() {
this.columns = [ {
label: 'Population',
accessor: 'population' } ];
this.parsed = dataParser(this.rows, this.columns);
this.parsedCol = this.parsed.columns[0];
});
it('calculates the bounds of values for given column', function() {
this.parsedCol.should.have.property('min');
this.parsedCol.should.have.property('max');
this.parsedCol.min.should.be.below(this.parsedCol.max);
});
it('generates a width fn based on min/max bounds', function() {
var widthFn = this.parsedCol.widthFn;
widthFn.should.be.a('function');
expect(widthFn(this.parsedCol.min)).to.equal(0);
expect(widthFn(this.parsedCol.max)).to.equal(100);
});
it('calculates summary stats: mean, median, sd, etc..', function() {
this.parsedCol.should.have.property('mean');
this.parsedCol.should.have.property('median');
this.parsedCol.should.have.property('sd');
});
it('sets row value for column', function() {
var parsed = dataParser(this.rows, this.columns),
parsedRow = parsed.rows[0];
parsedRow._values[0].should.equal(84000);
});
it('sets row width for column', function() {
var parsed = dataParser(this.rows, this.columns),
parsedRow = parsed.rows[0];
parsedRow._widths[0].should.be.a('Number');
});
//
describe('Histogram', function() {
it('calculates histogram from row values', function() {
this.parsedCol.should.have.property('histogram');
});
it('calculates histogram bin with the largest count', function() {
this.parsed.should.have.property('histogramMax');
this.parsed.histogramMax.should.be.a('Number');
});
});
});
//
//
//
describe('Function column definitions', function() {
beforeEach(function() {
this.columns = [ {
label: 'Density',
accessor: function(d) { return d.population / d.areaInSqKm; } },
{ label: 'DoubleNdx',
accessor: function(d, ndx) { return ndx * 2; } } ];
this.densityColumn = dataParser(this.rows, this.columns).columns[0];
this.doubleNdxColumn = dataParser(this.rows, this.columns).columns[1];
});
it('calculates values based on fn', function() {
expect(this.doubleNdxColumn.min).to.equal(0);
expect(this.doubleNdxColumn.max).to.equal((this.rows.length-1) * 2);
});
it('sets row value for column', function() {
var parsed = dataParser(this.rows, this.columns);
parsed.rows[0]._values[1].should.equal(0);
parsed.rows[1]._values[1].should.equal(2);
});
});
//
describe('Columns with format functions', function() {
beforeEach(function() {
this.columns = [ {
label: 'with format',
format: function(v) { return 'fizz' + v; },
accessor: function(d) { return 'foo' } },
{ label: 'without format',
accessor: function(d) { return 'foo' } } ];
});
it('uses the format function for labels', function() {
var parsed = dataParser(this.rows, this.columns),
parsedRow = parsed.rows[0];
parsedRow._labels[0].should.equal('fizzfoo');
parsedRow._labels[1].should.equal('foo');
});
});
//
describe('Row label definition', function() {
beforeEach(function() {
this.columns = [ {
label: 'Pop',
accessor: 'population' } ];
});
it('uses row index when `label` is not passed in', function() {
var parsed = dataParser(this.rows, this.columns);
parsed.rows[0]._rowLabel.should.equal('0');
});
it('can be passed in as a string(property name)', function() {
var parsed = dataParser(this.rows, this.columns, 'countryName');
parsed.rows[0]._rowLabel.should.equal('Andorra');
});
it('can be passed in as an accessor function with `row`, `ndx` params', function() {
var parsed = dataParser(this.rows, this.columns,
function(d,ndx) { return d.continent + ndx });
parsed.rows[0]._rowLabel.should.equal('EU0');
});
});
});