forked from vega/vega
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.test.js
162 lines (145 loc) · 5.93 KB
/
stack.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
describe('Stack', function() {
var EPSILON = 1e-8;
var values = [
{a: 1, b: 1, c: 'a'},
{a: 1, b: 2, c: 'b'},
{a: 1, b: 3, c: 'c'},
{a: 2, b: 4, c: 'a'},
{a: 2, b: 5, c: 'b'},
{a: 2, b: 6, c: 'c'},
{a: 3, b: 7, c: 'a'},
{a: 3, b: 8, c: 'b'},
{a: 3, b: 9, c: 'c'},
{a: 3, b: 2, c: 'a'}
];
function spec(opt) {
var stack = {
type: "stack",
groupby: opt.groupby,
sortby: opt.sortby,
field: opt.field,
offset: opt.offset,
output: {start: "y2", end: "y", mid: "cy"}
};
return {
data: [{
name: "table",
values: values,
transform: [stack]
}]
};
}
it('should perform flat stack', function(done) {
parseSpec(spec({groupby:null, sortby:null, field:"b", offset:"zero"}),
modelFactory,
function(error, model) {
var ds = model.data('table'),
data = ds.values(),
y = [0,1,3,6,10,15,21,28,36,45,47];
for (var i=0; i<data.length; ++i) {
expect(data[i]).to.have.property('y2', y[i]);
expect(data[i]).to.have.property('y', y[i+1]);
expect(data[i]).to.have.property('cy', 0.5 * (y[i] + y[i+1]));
}
done();
});
});
it('should perform grouped stack', function(done) {
parseSpec(spec({groupby:["a"], sortby:null, field:"b", offset:"zero"}),
modelFactory,
function(error, model) {
var ds = model.data('table'),
data = ds.values(),
y0 = [0,1,3, 0,4, 9, 0, 7,15,24],
y1 = [1,3,6, 4,9,15, 7,15,24,26];
for (var i=0; i<data.length; ++i) {
expect(data[i]).to.have.property('y2', y0[i]);
expect(data[i]).to.have.property('y', y1[i]);
expect(data[i]).to.have.property('cy', 0.5 * (y0[i] + y1[i]));
}
done();
});
});
it('should perform grouped stack with offset center', function(done) {
parseSpec(spec({groupby:["a"], sortby:null, field:"b", offset:"center"}),
modelFactory,
function(error, model) {
var ds = model.data('table'),
data = ds.values(),
y0 = [10,11,13, 5.5, 9.5,14.5, 0, 7,15,24],
y1 = [11,13,16, 9.5,14.5,20.5, 7,15,24,26];
for (var i=0; i<data.length; ++i) {
expect(data[i]).to.have.property('y2', y0[i]);
expect(data[i]).to.have.property('y', y1[i]);
expect(data[i]).to.have.property('cy', 0.5 * (y0[i] + y1[i]));
}
done();
});
});
it('should perform grouped stack with offset normalize', function(done) {
parseSpec(spec({groupby:["a"], sortby:null, field:"b", offset:"normalize"}),
modelFactory,
function(error, model) {
var ds = model.data('table'),
data = ds.values(),
y0 = [0/6,1/6,3/6, 0/15,4/15, 9/15, 0/26, 7/26,15/26,24/26],
y1 = [1/6,3/6,6/6, 4/15,9/15,15/15, 7/26,15/26,24/26,26/26];
for (var i=0; i<data.length; ++i) {
expect(data[i]).to.have.property('y2').closeTo(y0[i], EPSILON);
expect(data[i]).to.have.property('y').closeTo(y1[i], EPSILON);
expect(data[i]).to.have.property('cy').closeTo(0.5 * (y0[i] + y1[i]), EPSILON);
}
done();
});
});
it('should perform grouped sorted stack', function(done) {
parseSpec(spec({groupby:["a"], sortby:"c", field:"b", offset:"zero"}),
modelFactory,
function(error, model) {
var ds = model.data('table'),
data = ds.values(),
y0 = [0,1,3, 0,4, 9, 0, 9,17,7],
y1 = [1,3,6, 4,9,15, 7,17,26,9];
for (var i=0; i<data.length; ++i) {
expect(data[i]).to.have.property('y2', y0[i]);
expect(data[i]).to.have.property('y', y1[i]);
expect(data[i]).to.have.property('cy', 0.5 * (y0[i] + y1[i]));
}
done();
});
});
it('should validate against the schema', function() {
var schema = schemaPath(transforms.stack.schema),
validate = validator(schema);
expect(validate({ "type": "stack", "groupby": ["country"], "field": "medals" })).to.be.true;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "sortby": ["gdp"] })).to.be.true;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "offset": "zero" })).to.be.true;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "offset": "center" })).to.be.true;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "offset": "normalize" })).to.be.true;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "output": {"start": "start", "mid": "mid", "end": "end"} })).to.be.true;
expect(validate({ "type": "foo" })).to.be.false;
expect(validate({ "type": "stack" })).to.be.false;
expect(validate({ "type": "stack", "groupby": ["country"] })).to.be.false;
expect(validate({ "type": "stack", "groupby": "country", "field": "medals" })).to.be.false;
expect(validate({ "type": "stack", "groupby": ["country"], "field": ["medals"] })).to.be.false;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "sortby": "gdp" })).to.be.false;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "offset": "foo" })).to.be.false;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "output": {"foo": "bar"} })).to.be.false;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "bar": "foo" })).to.be.false;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "offset": "silhouette" })).to.be.false;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "offset": "wiggle" })).to.be.false;
expect(validate({ "type": "stack", "groupby": ["country"],
"field": "medals", "offset": "expand" })).to.be.false;
});
});