forked from crossfilter/reductio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalues.spec.js
93 lines (71 loc) · 2.8 KB
/
values.spec.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
// Counting tests
describe('Multiple values', function () {
var values = {};
beforeEach(function () {
var data = crossfilter([
{ foo: 'one', x: 1, other: 2 },
{ foo: 'two', x: 2, other: 1 },
{ foo: 'three', x: 3, other: 4 },
{ foo: 'one', x: 4, other: 1 },
{ foo: 'one', x: 5, other: 2 },
{ foo: 'two', x: 6, other: 3 },
]);
var dim = data.dimension(function(d) { return d.foo; });
var group = dim.group();
var reducer = reductio()
.avg(function(d) { return d.x; })
.count(true);
reducer.value("x").count(true).sum(function (d) { return d.x; });
reducer.value("y", function(d) { return d.other; }).count(true).sum(function(d) { return d.other; }).avg(true);
reducer(group);
values = group;
});
it('has three groups with proper counts', function (topic) {
expect(values.top(Infinity).length).toEqual(3);
var vals = {};
values.top(Infinity).forEach(function (d) {
vals[d.key] = d.value['x'];
});
expect(vals['one'].count).toEqual(3);
expect(vals['two'].count).toEqual(2);
expect(vals['three'].count).toEqual(1);
values.top(Infinity).forEach(function (d) {
vals[d.key] = d.value['y'];
});
expect(vals['one'].count).toEqual(3);
expect(vals['two'].count).toEqual(2);
expect(vals['three'].count).toEqual(1);
});
it('has sums as expected', function (topic) {
expect(values.top(Infinity).length).toEqual(3);
var vals = {};
values.top(Infinity).forEach(function (d) {
vals[d.key] = d.value['x'];
});
expect(vals['one'].sum).toEqual(10);
expect(vals['two'].sum).toEqual(8);
expect(vals['three'].sum).toEqual(3);
values.top(Infinity).forEach(function (d) {
vals[d.key] = d.value['y'];
});
expect(vals['one'].sum).toEqual(5);
expect(vals['two'].sum).toEqual(4);
expect(vals['three'].sum).toEqual(4);
});
it('has averages as expected', function (topic) {
expect(values.top(Infinity).length).toEqual(3);
var vals = {};
values.top(Infinity).forEach(function (d) {
vals[d.key] = d.value['x'];
});
expect(vals['one'].avg).toBeUndefined();
expect(vals['two'].avg).toBeUndefined();
expect(vals['three'].avg).toBeUndefined();
values.top(Infinity).forEach(function (d) {
vals[d.key] = d.value['y'];
});
expect(Math.round(vals['one'].avg)).toEqual(Math.round(5/3));
expect(Math.round(vals['two'].avg)).toEqual(Math.round(4/2));
expect(Math.round(vals['three'].avg)).toEqual(Math.round(4/1));
});
});