Skip to content

Commit 97596ae

Browse files
committed
handle async in multiple transform_aggregate tests
1 parent 5a127f0 commit 97596ae

File tree

1 file changed

+148
-116
lines changed

1 file changed

+148
-116
lines changed

test/jasmine/tests/transform_aggregate_test.js

Lines changed: 148 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('aggregate', function() {
1111

1212
afterEach(destroyGraphDiv);
1313

14-
it('handles all funcs for numeric data', function() {
14+
it('handles all funcs for numeric data', function(done) {
1515
// throw in some non-numbers, they should get discarded except first/last
1616
Plotly.newPlot(gd, [{
1717
x: [1, 2, 3, 4, 'fail'],
@@ -51,25 +51,28 @@ describe('aggregate', function() {
5151
}], {
5252
// log axis doesn't change how sum (or avg but not tested) works
5353
xaxis: {type: 'log'}
54-
});
55-
56-
var traceOut = gd._fullData[0];
57-
58-
expect(traceOut.x).toEqual([8, 2]);
59-
expect(traceOut.y).toBeCloseToArray([3.3, 2.2], 5);
60-
expect(traceOut.customdata).toEqual([-3, undefined]);
61-
expect(traceOut.marker.size).toEqual([0.1, 0.2]);
62-
expect(traceOut.marker.color).toEqual([10, 4]);
63-
expect(traceOut.marker.opacity).toEqual([0.6, 'boo']);
64-
expect(traceOut.marker.line.color).toEqual(['the end', 3.3]);
65-
expect(traceOut.marker.line.width).toEqual([4, 1]);
66-
67-
var transform = traceOut.transforms[0];
68-
var inverseMapping = transform._indexToPoints;
69-
expect(inverseMapping).toEqual({0: [0, 2, 3, 4], 1: [1]});
54+
})
55+
.then(function() {
56+
var traceOut = gd._fullData[0];
57+
58+
expect(traceOut.x).toEqual([8, 2]);
59+
expect(traceOut.y).toBeCloseToArray([3.3, 2.2], 5);
60+
expect(traceOut.customdata).toEqual([-3, undefined]);
61+
expect(traceOut.marker.size).toEqual([0.1, 0.2]);
62+
expect(traceOut.marker.color).toEqual([10, 4]);
63+
expect(traceOut.marker.opacity).toEqual([0.6, 'boo']);
64+
expect(traceOut.marker.line.color).toEqual(['the end', 3.3]);
65+
expect(traceOut.marker.line.width).toEqual([4, 1]);
66+
67+
var transform = traceOut.transforms[0];
68+
var inverseMapping = transform._indexToPoints;
69+
expect(inverseMapping).toEqual({0: [0, 2, 3, 4], 1: [1]});
70+
})
71+
.catch(failTest)
72+
.then(done);
7073
});
7174

72-
it('handles all funcs except sum for date data', function() {
75+
it('handles all funcs except sum for date data', function(done) {
7376
// weird cases handled in another test
7477
Plotly.newPlot(gd, [{
7578
x: ['2001-01-01', '', '2001-01-03', '2001-01-05', '2001-01-07'],
@@ -95,19 +98,22 @@ describe('aggregate', function() {
9598
{target: 'x', func: 'min'}
9699
]
97100
}]
98-
}]);
99-
100-
var traceOut = gd._fullData[0];
101+
}])
102+
.then(function() {
103+
var traceOut = gd._fullData[0];
101104

102-
expect(traceOut.x).toEqual(['2001-01-04', undefined]);
103-
expect(traceOut.y).toEqual(['1990-12-23', '2005-03-15']);
104-
expect(traceOut.text).toEqual(['2001-01-01 12:37', '2001-01-01 12:35']);
105-
expect(traceOut.hovertext).toEqual(['a', '2001-01-02']);
106-
expect(traceOut.customdata).toEqual(['2001-05', 'b']);
107-
expect(traceOut.marker.line.width).toEqual([4, 1]);
105+
expect(traceOut.x).toEqual(['2001-01-04', undefined]);
106+
expect(traceOut.y).toEqual(['1990-12-23', '2005-03-15']);
107+
expect(traceOut.text).toEqual(['2001-01-01 12:37', '2001-01-01 12:35']);
108+
expect(traceOut.hovertext).toEqual(['a', '2001-01-02']);
109+
expect(traceOut.customdata).toEqual(['2001-05', 'b']);
110+
expect(traceOut.marker.line.width).toEqual([4, 1]);
111+
})
112+
.catch(failTest)
113+
.then(done);
108114
});
109115

110-
it('handles all funcs except sum and avg for category data', function() {
116+
it('handles all funcs except sum and avg for category data', function(done) {
111117
// weird cases handled in another test
112118
Plotly.newPlot(gd, [{
113119
x: ['a', 'b', 'c', 'aa', 'd'],
@@ -130,20 +136,23 @@ describe('aggregate', function() {
130136
}]
131137
}], {
132138
xaxis: {categoryarray: ['aaa', 'aa', 'a', 'b', 'c']}
133-
});
134-
135-
var traceOut = gd._fullData[0];
139+
})
140+
.then(function() {
141+
var traceOut = gd._fullData[0];
136142

137-
// explicit order (only possible for axis data)
138-
expect(traceOut.x).toEqual(['aa', 'b']);
139-
// implied order from data
140-
expect(traceOut.y).toEqual(['t', 'w']);
141-
expect(traceOut.text).toEqual(['a', 'b']);
142-
expect(traceOut.hovertext).toEqual(['c', 'b']);
143-
expect(traceOut.marker.line.width).toEqual([4, 1]);
143+
// explicit order (only possible for axis data)
144+
expect(traceOut.x).toEqual(['aa', 'b']);
145+
// implied order from data
146+
expect(traceOut.y).toEqual(['t', 'w']);
147+
expect(traceOut.text).toEqual(['a', 'b']);
148+
expect(traceOut.hovertext).toEqual(['c', 'b']);
149+
expect(traceOut.marker.line.width).toEqual([4, 1]);
150+
})
151+
.catch(failTest)
152+
.then(done);
144153
});
145154

146-
it('allows date and category sums, and category avg, with weird output', function() {
155+
it('allows date and category sums, and category avg, with weird output', function(done) {
147156
// this test is more of an FYI than anything else - it doesn't break but
148157
// these results are usually meaningless.
149158

@@ -160,24 +169,27 @@ describe('aggregate', function() {
160169
{target: 'text', func: 'avg'}
161170
]
162171
}]
163-
}]);
164-
165-
var traceOut = gd._fullData[0];
166-
167-
// date sums: 1970-01-01 is "zero", there are shifts due to # of leap years
168-
// without that shift these would be 2032-01-02 and 2032-01-06
169-
expect(traceOut.x).toEqual(['2032-01-03', '2032-01-07']);
170-
// category sums: can go off the end of the category array -> gives undefined
171-
expect(traceOut.y).toEqual(['b', undefined]);
172-
// category average: can result in fractional categories -> rounds (0.5 rounds to 1)
173-
expect(traceOut.text).toEqual(['b', 'b']);
172+
}])
173+
.then(function() {
174+
var traceOut = gd._fullData[0];
174175

175-
var transform = traceOut.transforms[0];
176-
var inverseMapping = transform._indexToPoints;
177-
expect(inverseMapping).toEqual({0: [0, 1], 1: [2, 3]});
176+
// date sums: 1970-01-01 is "zero", there are shifts due to # of leap years
177+
// without that shift these would be 2032-01-02 and 2032-01-06
178+
expect(traceOut.x).toEqual(['2032-01-03', '2032-01-07']);
179+
// category sums: can go off the end of the category array -> gives undefined
180+
expect(traceOut.y).toEqual(['b', undefined]);
181+
// category average: can result in fractional categories -> rounds (0.5 rounds to 1)
182+
expect(traceOut.text).toEqual(['b', 'b']);
183+
184+
var transform = traceOut.transforms[0];
185+
var inverseMapping = transform._indexToPoints;
186+
expect(inverseMapping).toEqual({0: [0, 1], 1: [2, 3]});
187+
})
188+
.catch(failTest)
189+
.then(done);
178190
});
179191

180-
it('can aggregate on an existing data array', function() {
192+
it('can aggregate on an existing data array', function(done) {
181193
Plotly.newPlot(gd, [{
182194
x: [1, 2, 3, 4, 5],
183195
y: [2, 4, 6, 8, 10],
@@ -190,20 +202,23 @@ describe('aggregate', function() {
190202
{target: 'y', func: 'avg'}
191203
]
192204
}]
193-
}]);
194-
195-
var traceOut = gd._fullData[0];
205+
}])
206+
.then(function() {
207+
var traceOut = gd._fullData[0];
196208

197-
expect(traceOut.x).toEqual([8, 7]);
198-
expect(traceOut.y).toBeCloseToArray([16 / 3, 7], 5);
199-
expect(traceOut.marker.size).toEqual([10, 20]);
209+
expect(traceOut.x).toEqual([8, 7]);
210+
expect(traceOut.y).toBeCloseToArray([16 / 3, 7], 5);
211+
expect(traceOut.marker.size).toEqual([10, 20]);
200212

201-
var transform = traceOut.transforms[0];
202-
var inverseMapping = transform._indexToPoints;
203-
expect(inverseMapping).toEqual({0: [0, 1, 4], 1: [2, 3]});
213+
var transform = traceOut.transforms[0];
214+
var inverseMapping = transform._indexToPoints;
215+
expect(inverseMapping).toEqual({0: [0, 1, 4], 1: [2, 3]});
216+
})
217+
.catch(failTest)
218+
.then(done);
204219
});
205220

206-
it('can handle case where aggregation array is missing', function() {
221+
it('can handle case where aggregation array is missing', function(done) {
207222
Plotly.newPlot(gd, [{
208223
x: [1, 2, 3, 4, 5],
209224
y: [2, 4, 6, 8, 10],
@@ -212,20 +227,23 @@ describe('aggregate', function() {
212227
type: 'aggregate',
213228
groups: 'marker.size'
214229
}]
215-
}]);
216-
217-
var traceOut = gd._fullData[0];
230+
}])
231+
.then(function() {
232+
var traceOut = gd._fullData[0];
218233

219-
expect(traceOut.x).toEqual([1, 3]);
220-
expect(traceOut.y).toEqual([2, 6]);
221-
expect(traceOut.marker.size).toEqual([10, 20]);
234+
expect(traceOut.x).toEqual([1, 3]);
235+
expect(traceOut.y).toEqual([2, 6]);
236+
expect(traceOut.marker.size).toEqual([10, 20]);
222237

223-
var transform = traceOut.transforms[0];
224-
var inverseMapping = transform._indexToPoints;
225-
expect(inverseMapping).toEqual({0: [0, 1, 4], 1: [2, 3]});
238+
var transform = traceOut.transforms[0];
239+
var inverseMapping = transform._indexToPoints;
240+
expect(inverseMapping).toEqual({0: [0, 1, 4], 1: [2, 3]});
241+
})
242+
.catch(failTest)
243+
.then(done);
226244
});
227245

228-
it('handles median, mode, rms, stddev, change & range for numeric data', function() {
246+
it('handles median, mode, rms, stddev, change & range for numeric data', function(done) {
229247
// again, nothing is going to barf with non-numeric data, but sometimes it
230248
// won't make much sense.
231249

@@ -252,22 +270,25 @@ describe('aggregate', function() {
252270
{target: 'marker.color', func: 'stddev'}
253271
]
254272
}]
255-
}]);
256-
257-
var traceOut = gd._fullData[0];
258-
259-
// 1 and 2 both have count of 2 in the first group,
260-
// but 2 gets to that count first
261-
expect(traceOut.x).toEqual([2, 1]);
262-
expect(traceOut.y).toBeCloseToArray([3.5, 2], 5);
263-
expect(traceOut.customdata).toEqual([-4, 0]);
264-
expect(traceOut.marker.size).toBeCloseToArray([Math.sqrt(51 / 4), 2], 5);
265-
expect(traceOut.marker.opacity).toEqual([0.8, 0]);
266-
expect(traceOut.marker.line.width).toBeCloseToArray([0.5, 0], 5);
267-
expect(traceOut.marker.color).toBeCloseToArray([Math.sqrt(1 / 3), 0], 5);
273+
}])
274+
.then(function() {
275+
var traceOut = gd._fullData[0];
276+
277+
// 1 and 2 both have count of 2 in the first group,
278+
// but 2 gets to that count first
279+
expect(traceOut.x).toEqual([2, 1]);
280+
expect(traceOut.y).toBeCloseToArray([3.5, 2], 5);
281+
expect(traceOut.customdata).toEqual([-4, 0]);
282+
expect(traceOut.marker.size).toBeCloseToArray([Math.sqrt(51 / 4), 2], 5);
283+
expect(traceOut.marker.opacity).toEqual([0.8, 0]);
284+
expect(traceOut.marker.line.width).toBeCloseToArray([0.5, 0], 5);
285+
expect(traceOut.marker.color).toBeCloseToArray([Math.sqrt(1 / 3), 0], 5);
286+
})
287+
.catch(failTest)
288+
.then(done);
268289
});
269290

270-
it('handles ragged data - extra groups are ignored', function() {
291+
it('handles ragged data - extra groups are ignored', function(done) {
271292
Plotly.newPlot(gd, [{
272293
x: [1, 1, 2, 2, 1, 3, 4],
273294
y: [1, 2, 3, 4, 5], // shortest array controls all
@@ -279,15 +300,18 @@ describe('aggregate', function() {
279300
{target: 'y', func: 'median'},
280301
]
281302
}]
282-
}]);
283-
284-
var traceOut = gd._fullData[0];
303+
}])
304+
.then(function() {
305+
var traceOut = gd._fullData[0];
285306

286-
expect(traceOut.x).toEqual([2, 1]);
287-
expect(traceOut.y).toBeCloseToArray([3.5, 2], 5);
307+
expect(traceOut.x).toEqual([2, 1]);
308+
expect(traceOut.y).toBeCloseToArray([3.5, 2], 5);
309+
})
310+
.catch(failTest)
311+
.then(done);
288312
});
289313

290-
it('handles ragged data - groups is the shortest, others are ignored', function() {
314+
it('handles ragged data - groups is the shortest, others are ignored', function(done) {
291315
Plotly.newPlot(gd, [{
292316
x: [1, 1, 2, 2, 1, 3, 4],
293317
y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
@@ -299,15 +323,18 @@ describe('aggregate', function() {
299323
{target: 'y', func: 'median'},
300324
]
301325
}]
302-
}]);
303-
304-
var traceOut = gd._fullData[0];
326+
}])
327+
.then(function() {
328+
var traceOut = gd._fullData[0];
305329

306-
expect(traceOut.x).toEqual([2, 1]);
307-
expect(traceOut.y).toBeCloseToArray([3.5, 2], 5);
330+
expect(traceOut.x).toEqual([2, 1]);
331+
expect(traceOut.y).toBeCloseToArray([3.5, 2], 5);
332+
})
333+
.catch(failTest)
334+
.then(done);
308335
});
309336

310-
it('links fullData aggregations to userData via _index', function() {
337+
it('links fullData aggregations to userData via _index', function(done) {
311338
Plotly.newPlot(gd, [{
312339
x: [1, 2, 3, 4, 5],
313340
y: [2, 4, 6, 8, 10],
@@ -324,23 +351,26 @@ describe('aggregate', function() {
324351
{target: 'y', func: 'avg'}
325352
]
326353
}]
327-
}]);
328-
329-
var traceOut = gd._fullData[0];
330-
var fullAggregation = traceOut.transforms[0];
331-
var fullAggregations = fullAggregation.aggregations;
332-
var enabledAggregations = fullAggregations.filter(function(agg) {
333-
return agg.enabled;
334-
});
354+
}])
355+
.then(function() {
356+
var traceOut = gd._fullData[0];
357+
var fullAggregation = traceOut.transforms[0];
358+
var fullAggregations = fullAggregation.aggregations;
359+
var enabledAggregations = fullAggregations.filter(function(agg) {
360+
return agg.enabled;
361+
});
335362

336-
expect(enabledAggregations[0].target).toEqual('x');
337-
expect(enabledAggregations[0]._index).toEqual(0);
363+
expect(enabledAggregations[0].target).toEqual('x');
364+
expect(enabledAggregations[0]._index).toEqual(0);
338365

339-
expect(enabledAggregations[1].target).toEqual('y');
340-
expect(enabledAggregations[1]._index).toEqual(2);
366+
expect(enabledAggregations[1].target).toEqual('y');
367+
expect(enabledAggregations[1]._index).toEqual(2);
341368

342-
expect(enabledAggregations[2].target).toEqual('marker.color');
343-
expect(enabledAggregations[2]._index).toEqual(-1);
369+
expect(enabledAggregations[2].target).toEqual('marker.color');
370+
expect(enabledAggregations[2]._index).toEqual(-1);
371+
})
372+
.catch(failTest)
373+
.then(done);
344374
});
345375

346376
it('does not error out on bad *group* value', function(done) {
@@ -361,7 +391,7 @@ describe('aggregate', function() {
361391
.then(done);
362392
});
363393

364-
it('use numeric sort function for median', function() {
394+
it('use numeric sort function for median', function(done) {
365395
var subject = ['M', 'M', 'M'];
366396
var score = [2, 10, 11];
367397

@@ -383,6 +413,8 @@ describe('aggregate', function() {
383413
.then(function() {
384414
var traceOut = gd._fullData[0];
385415
expect(traceOut.y[0]).toBe(10);
386-
});
416+
})
417+
.catch(failTest)
418+
.then(done);
387419
});
388420
});

0 commit comments

Comments
 (0)