forked from crossfilter/reductio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstd.spec.js
91 lines (70 loc) · 2.71 KB
/
std.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
// Counting tests
describe('Reductio std', function () {
var std, noSumOfSq, accStd;
beforeEach(function () {
var data = crossfilter([
{ foo: 'one', bar: 1 },
{ foo: 'two', bar: 2 },
{ foo: 'three', bar: 3 },
{ foo: 'one', bar: 4 },
{ foo: 'one', bar: 5 },
{ foo: 'two', bar: 6 },
]);
var dim = data.dimension(function(d) { return d.foo; });
var group = dim.group();
var groupNoSumOfSq = dim.group();
var groupAccStd = dim.group();
var reducer = reductio()
.std(true)
.count(true);
// This doesn't work because no .sumOfSq(accessor) has been defined.
// The resulting group only tracks counts.
reducer(groupNoSumOfSq);
reducer.count(false).sumOfSq(false);
reducer.std(function(d) { return d.bar; });
reducer(groupAccStd);
reducer.sumOfSq(false);
reducer.std(false);
reducer.sumOfSq(function(d) { return d.bar; })
.std(true);
// Now it should track count, sumOfSq, and std.
reducer(group);
std = group;
noSumOfSq = groupNoSumOfSq;
accStd = groupAccStd;
});
it('has three groups', function (topic) {
expect(std.top(Infinity).length).toEqual(3);
});
it('grouping have the right averages', function (topic) {
var values = {};
std.top(Infinity).forEach(function (d) {
values[d.key] = d.value;
});
expect(Math.round(values['one'].std)).toEqual(Math.round(2.08167));
expect(Math.round(values['two'].std)).toEqual(Math.round(2.82843));
expect(Math.round(values['three'].std)).toEqual(Math.round(0));
});
it('grouping with .std() but no .sumOfSq() doesn\'t work', function (topic) {
var values = {};
noSumOfSq.top(Infinity).forEach(function (d) {
values[d.key] = d.value;
});
// It has a count, as defined.
expect(values['one'].count).toEqual(3);
// But no sumOfSq.
expect(values['one'].sumOfSq).toBeUndefined();
// And no std.
expect(values['one'].std).toBeUndefined();
// Also throws an error on the console, but that's more difficult to test.
});
it('grouping with .std(accessor) works', function (topic) {
var values = {};
accStd.top(Infinity).forEach(function (d) {
values[d.key] = d.value;
});
expect(Math.round(values['one'].std)).toEqual(Math.round(2.08167));
expect(Math.round(values['two'].std)).toEqual(Math.round(2.82843));
expect(Math.round(values['three'].std)).toEqual(Math.round(0));
});
});