OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 Google Inc. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style |
| 5 * license that can be found in the LICENSE file or at |
| 6 * https://developers.google.com/open-source/licenses/bsd |
| 7 */ |
| 8 library charted.test.aggregationtransformer; |
| 9 |
| 10 import 'package:charted/charts/charts.dart'; |
| 11 import 'package:charted/core/utils.dart'; |
| 12 import 'package:unittest/unittest.dart'; |
| 13 import 'package:observe/observe.dart'; |
| 14 |
| 15 main() { |
| 16 List COLUMNS = [ |
| 17 new ChartColumnSpec(label:'Continent', type:ChartColumnSpec.TYPE_STRING), |
| 18 new ChartColumnSpec(label:'Country', type:ChartColumnSpec.TYPE_STRING), |
| 19 new ChartColumnSpec(label:'City', type:ChartColumnSpec.TYPE_STRING), |
| 20 new ChartColumnSpec(label:'Stats1'), |
| 21 new ChartColumnSpec(label:'Stats2'), |
| 22 new ChartColumnSpec(label:'Stats3') |
| 23 ]; |
| 24 |
| 25 const List DATA = const [ |
| 26 const['America', 'USA', 'LA', 4.51, 7, 1000], |
| 27 const['America', 'USA', 'SF', 9.50, 50, 2000], |
| 28 const['Asia', 'Japan', 'Tokyo', 1.50, 99, 2000], |
| 29 const['Asia', 'Japan', 'Kyoto', 5.10, 66, 4000], |
| 30 const['Asia', 'Taiwan', 'Taipei', 3.50, 127, 1337], |
| 31 const['Asia', 'Japan', 'Osaka', 4.50, 19, 2000], |
| 32 const['Asia', 'Taiwan', 'Tainan', 1.50, 10, 100], |
| 33 const['Europe', 'France', 'Nice', 2.50, 29, 6000], |
| 34 const['Europe', 'France', 'Paris', 6.50, 129, 3000], |
| 35 const['Europe', 'Germany', 'Berlin', 10.99, 999, 10000], |
| 36 const['Europe', 'England', 'London', 2.50, 10, 3000], |
| 37 const['America', 'USA', 'NY', 3.50, 17, 4000], |
| 38 const['America', 'Brazil', 'Brasilia', 1.50, 27, 6000], |
| 39 const['America', 'Argentina', 'Buenos Aires', 5.50, 37, 2000], |
| 40 const['America', 'Brazil', 'Rio de Janeiro', 2.50, 52, 3000], |
| 41 ]; |
| 42 |
| 43 ChartData inputData = new ChartData(COLUMNS, DATA); |
| 44 |
| 45 test('Sum aggregation all dimension collapsed', () { |
| 46 AggregationTransformer transformer = new AggregationTransformer([0, 1, 2], |
| 47 [3, 4, 5]); |
| 48 ChartData result = transformer.transform(inputData); |
| 49 // Expected data: |
| 50 // [America, , , 27.0, 190.0, 18000.0] |
| 51 // [Asia, , , 16.1, 321.0, 9437.0] |
| 52 // [Europe, , , 22.49, 1167.0, 22000.0] |
| 53 |
| 54 // America |
| 55 expect(result.rows.elementAt(0).elementAt(3), closeTo(27.01, EPSILON)); |
| 56 expect(result.rows.elementAt(0).elementAt(4), equals(190)); |
| 57 expect(result.rows.elementAt(0).elementAt(5), equals(18000)); |
| 58 |
| 59 // Asia |
| 60 expect(result.rows.elementAt(1).elementAt(3), closeTo(16.1, EPSILON)); |
| 61 expect(result.rows.elementAt(1).elementAt(4), equals(321)); |
| 62 expect(result.rows.elementAt(1).elementAt(5), equals(9437)); |
| 63 |
| 64 // Europe |
| 65 expect(result.rows.elementAt(2).elementAt(3), closeTo(22.49, EPSILON)); |
| 66 expect(result.rows.elementAt(2).elementAt(4), equals(1167)); |
| 67 expect(result.rows.elementAt(2).elementAt(5), equals(22000)); |
| 68 }); |
| 69 |
| 70 test('Sum aggregation expanding America in continent dimension', () { |
| 71 AggregationTransformer transformer = new AggregationTransformer([0, 1, 2], |
| 72 [3, 4, 5]); |
| 73 transformer.expand(['America']); |
| 74 ChartData result = transformer.transform(inputData); |
| 75 |
| 76 // Expected data: |
| 77 // [America, Argentina, , 5.5, 37.0, 2000.0] |
| 78 // [America, Brazil, , 4.0, 79.0, 9000.0] |
| 79 // [America, USA, , 17.51, 74.0, 7000.0] |
| 80 // [Asia, , , 16.1, 321.0, 9437.0] |
| 81 // [Europe, , , 22.49, 1167.0, 22000.0] |
| 82 |
| 83 // America |
| 84 expect(result.rows.elementAt(0).elementAt(3), closeTo(5.5, EPSILON)); |
| 85 expect(result.rows.elementAt(0).elementAt(4), equals(37)); |
| 86 expect(result.rows.elementAt(0).elementAt(5), equals(2000)); |
| 87 expect(result.rows.elementAt(1).elementAt(3), closeTo(4, EPSILON)); |
| 88 expect(result.rows.elementAt(1).elementAt(4), equals(79)); |
| 89 expect(result.rows.elementAt(1).elementAt(5), equals(9000)); |
| 90 expect(result.rows.elementAt(2).elementAt(3), closeTo(17.51, EPSILON)); |
| 91 expect(result.rows.elementAt(2).elementAt(4), equals(74)); |
| 92 expect(result.rows.elementAt(2).elementAt(5), equals(7000)); |
| 93 |
| 94 // Asia |
| 95 expect(result.rows.elementAt(3).elementAt(3), closeTo(16.1, EPSILON)); |
| 96 expect(result.rows.elementAt(3).elementAt(4), equals(321)); |
| 97 expect(result.rows.elementAt(3).elementAt(5), equals(9437)); |
| 98 |
| 99 // Europe |
| 100 expect(result.rows.elementAt(4).elementAt(3), closeTo(22.49, EPSILON)); |
| 101 expect(result.rows.elementAt(4).elementAt(4), equals(1167)); |
| 102 expect(result.rows.elementAt(4).elementAt(5), equals(22000)); |
| 103 }); |
| 104 |
| 105 test('Sum aggregation expanding [America, Brazil] in continent/country '+ |
| 106 'dimension', () { |
| 107 AggregationTransformer transformer = new AggregationTransformer([0, 1, 2], |
| 108 [3, 4, 5]); |
| 109 transformer.expand(['America']); |
| 110 transformer.expand(['America', 'Brazil']); |
| 111 ChartData result = transformer.transform(inputData); |
| 112 |
| 113 // Expected data: |
| 114 // [America, Argentina, , 5.5, 37.0, 2000.0] |
| 115 // [America, Brazil, 'Brasilia, 1.50, 27, 6000] |
| 116 // [America, Brazil, 'Rio de Janeiro, 2.50, 52, 3000] |
| 117 // [America, USA, , 17.51, 74.0, 7000.0] |
| 118 // [Asia, , , 16.1, 321.0, 9437.0] |
| 119 // [Europe, , , 22.49, 1167.0, 22000.0] |
| 120 |
| 121 // America Argentina |
| 122 expect(result.rows.elementAt(0).elementAt(3), closeTo(5.5, EPSILON)); |
| 123 expect(result.rows.elementAt(0).elementAt(4), equals(37)); |
| 124 expect(result.rows.elementAt(0).elementAt(5), equals(2000)); |
| 125 |
| 126 // America Brazil Brasilia |
| 127 expect(result.rows.elementAt(1).elementAt(3), closeTo(1.5, EPSILON)); |
| 128 expect(result.rows.elementAt(1).elementAt(4), equals(27)); |
| 129 expect(result.rows.elementAt(1).elementAt(5), equals(6000)); |
| 130 |
| 131 // America Brazil Rio de Janeiro |
| 132 expect(result.rows.elementAt(2).elementAt(3), closeTo(2.5, EPSILON)); |
| 133 expect(result.rows.elementAt(2).elementAt(4), equals(52)); |
| 134 expect(result.rows.elementAt(2).elementAt(5), equals(3000)); |
| 135 |
| 136 // America USA |
| 137 expect(result.rows.elementAt(3).elementAt(3), closeTo(17.51, EPSILON)); |
| 138 expect(result.rows.elementAt(3).elementAt(4), equals(74)); |
| 139 expect(result.rows.elementAt(3).elementAt(5), equals(7000)); |
| 140 |
| 141 // Asia |
| 142 expect(result.rows.elementAt(4).elementAt(3), closeTo(16.1, EPSILON)); |
| 143 expect(result.rows.elementAt(4).elementAt(4), equals(321)); |
| 144 expect(result.rows.elementAt(4).elementAt(5), equals(9437)); |
| 145 |
| 146 // Europe |
| 147 expect(result.rows.elementAt(5).elementAt(3), closeTo(22.49, EPSILON)); |
| 148 expect(result.rows.elementAt(5).elementAt(4), equals(1167)); |
| 149 expect(result.rows.elementAt(5).elementAt(5), equals(22000)); |
| 150 }); |
| 151 |
| 152 test('Collapsing parent of expanded dimension should collapse dimension', () { |
| 153 AggregationTransformer transformer = new AggregationTransformer([0, 1, 2], |
| 154 [3, 4, 5]); |
| 155 transformer.expand(['America']); |
| 156 transformer.expand(['America', 'Brazil']); |
| 157 transformer.collapse(['America']); |
| 158 ChartData result = transformer.transform(inputData); |
| 159 |
| 160 // Expected data: |
| 161 // [America, , , 27.0, 190.0, 18000.0] |
| 162 // [Asia, , , 16.1, 321.0, 9437.0] -- not tested in this case |
| 163 // [Europe, , , 22.49, 1167.0, 22000.0] -- not tested in this case |
| 164 |
| 165 // America |
| 166 expect(result.rows.elementAt(0).elementAt(3), closeTo(27.01, EPSILON)); |
| 167 expect(result.rows.elementAt(0).elementAt(4), equals(190)); |
| 168 expect(result.rows.elementAt(0).elementAt(5), equals(18000)); |
| 169 }); |
| 170 |
| 171 test('Expanding the lowest dimension should be ignored', () { |
| 172 AggregationTransformer transformer = new AggregationTransformer([0, 1, 2], |
| 173 [3, 4, 5]); |
| 174 transformer.expand(['America']); |
| 175 transformer.expand(['America', 'Brazil']); |
| 176 transformer.expand(['America', 'Brazil', 'Brasilia']); |
| 177 ChartData result = transformer.transform(inputData); |
| 178 |
| 179 // Expected data: |
| 180 // [America, Argentina, , 5.5, 37.0, 2000.0] |
| 181 // [America, Brazil, 'Brasilia, 1.50, 27, 6000] |
| 182 // [America, Brazil, 'Rio de Janeiro, 2.50, 52, 3000] |
| 183 // [America, USA, , 17.51, 74.0, 7000.0] |
| 184 // [Asia, , , 16.1, 321.0, 9437.0] -- not tested in this case |
| 185 // [Europe, , , 22.49, 1167.0, 22000.0] -- not tested in this case |
| 186 |
| 187 // America Argentina |
| 188 expect(result.rows.elementAt(0).elementAt(3), closeTo(5.5, EPSILON)); |
| 189 expect(result.rows.elementAt(0).elementAt(4), equals(37)); |
| 190 expect(result.rows.elementAt(0).elementAt(5), equals(2000)); |
| 191 |
| 192 // America Brazil Brasilia |
| 193 expect(result.rows.elementAt(1).elementAt(3), closeTo(1.5, EPSILON)); |
| 194 expect(result.rows.elementAt(1).elementAt(4), equals(27)); |
| 195 expect(result.rows.elementAt(1).elementAt(5), equals(6000)); |
| 196 |
| 197 // America Brazil Rio de Janeiro |
| 198 expect(result.rows.elementAt(2).elementAt(3), closeTo(2.5, EPSILON)); |
| 199 expect(result.rows.elementAt(2).elementAt(4), equals(52)); |
| 200 expect(result.rows.elementAt(2).elementAt(5), equals(3000)); |
| 201 |
| 202 // America USA |
| 203 expect(result.rows.elementAt(3).elementAt(3), closeTo(17.51, EPSILON)); |
| 204 expect(result.rows.elementAt(3).elementAt(4), equals(74)); |
| 205 expect(result.rows.elementAt(3).elementAt(5), equals(7000)); |
| 206 }); |
| 207 |
| 208 test('Expanding the multiple dimension at different level', () { |
| 209 AggregationTransformer transformer = new AggregationTransformer([0, 1, 2], |
| 210 [3, 4, 5]); |
| 211 transformer.expand(['Asia']); |
| 212 transformer.expand(['Asia', 'Taiwan']); |
| 213 transformer.expand(['Asia', 'Japan', 'Osaka']); |
| 214 transformer.expand(['Europe']); |
| 215 transformer.collapse(['Asia', 'Japan']); |
| 216 ChartData result = transformer.transform(inputData); |
| 217 |
| 218 |
| 219 // Expected data: |
| 220 // [America, , , 27.0, 190.0, 18000.0] |
| 221 // [Asia, Japan, , 10.10, 184.0, 8000.0] |
| 222 // [Asia, Taiwan, Tainan, 1.50, 10.0, 100.0] |
| 223 // [Asia, Taiwan, Taipei, 3.50, 127.0, 1337.0] |
| 224 // [Europe, England, , 2.50, 10.0, 3000.0] |
| 225 // [Europe, France, , 9.00, 158.0, 9000.0] |
| 226 // [Europe, Germany, , 10.99, 999.0, 10000.0] |
| 227 |
| 228 // America |
| 229 expect(result.rows.elementAt(0).elementAt(3), closeTo(27.01, EPSILON)); |
| 230 expect(result.rows.elementAt(0).elementAt(4), equals(190)); |
| 231 expect(result.rows.elementAt(0).elementAt(5), equals(18000)); |
| 232 |
| 233 // Asia Japan |
| 234 expect(result.rows.elementAt(1).elementAt(3), closeTo(11.1, EPSILON)); |
| 235 expect(result.rows.elementAt(1).elementAt(4), equals(184)); |
| 236 expect(result.rows.elementAt(1).elementAt(5), equals(8000)); |
| 237 |
| 238 // Asia Taiwan Tainan |
| 239 expect(result.rows.elementAt(2).elementAt(3), closeTo(1.5, EPSILON)); |
| 240 expect(result.rows.elementAt(2).elementAt(4), equals(10)); |
| 241 expect(result.rows.elementAt(2).elementAt(5), equals(100)); |
| 242 |
| 243 // Asia Taiwan Taipei |
| 244 expect(result.rows.elementAt(3).elementAt(3), closeTo(3.5, EPSILON)); |
| 245 expect(result.rows.elementAt(3).elementAt(4), equals(127)); |
| 246 expect(result.rows.elementAt(3).elementAt(5), equals(1337)); |
| 247 |
| 248 // Europe England |
| 249 expect(result.rows.elementAt(4).elementAt(3), closeTo(2.5, EPSILON)); |
| 250 expect(result.rows.elementAt(4).elementAt(4), equals(10)); |
| 251 expect(result.rows.elementAt(4).elementAt(5), equals(3000)); |
| 252 |
| 253 // Europe France |
| 254 expect(result.rows.elementAt(5).elementAt(3), closeTo(9, EPSILON)); |
| 255 expect(result.rows.elementAt(5).elementAt(4), equals(158)); |
| 256 expect(result.rows.elementAt(5).elementAt(5), equals(9000)); |
| 257 |
| 258 // Europe Germany |
| 259 expect(result.rows.elementAt(6).elementAt(3), closeTo(10.99, EPSILON)); |
| 260 expect(result.rows.elementAt(6).elementAt(4), equals(999)); |
| 261 expect(result.rows.elementAt(6).elementAt(5), equals(10000)); |
| 262 }); |
| 263 |
| 264 test('Expanding the multiple dimension at different level', () { |
| 265 AggregationTransformer transformer = new AggregationTransformer([0, 1, 2], |
| 266 [3, 4, 5]); |
| 267 transformer.expand(['Asia']); |
| 268 transformer.expand(['Asia', 'Taiwan']); |
| 269 transformer.expand(['Asia', 'Japan', 'Osaka']); |
| 270 transformer.expand(['Europe']); |
| 271 transformer.collapse(['Asia', 'Japan']); |
| 272 |
| 273 ChartData result = transformer.transform(inputData); |
| 274 |
| 275 |
| 276 // Expected data: |
| 277 // [America, , , 27.0, 190.0, 18000.0] |
| 278 // [Asia, Japan, , 10.10, 184.0, 8000.0] |
| 279 // [Asia, Taiwan, Tainan, 1.50, 10.0, 100.0] |
| 280 // [Asia, Taiwan, Taipei, 3.50, 127.0, 1337.0] |
| 281 // [Europe, England, , 2.50, 10.0, 3000.0] |
| 282 // [Europe, France, , 9.00, 158.0, 9000.0] |
| 283 // [Europe, Germany, , 10.99, 999.0, 10000.0] |
| 284 |
| 285 transformer.expandAll(); |
| 286 |
| 287 // America |
| 288 expect(result.rows.elementAt(0).elementAt(3), closeTo(27.01, EPSILON)); |
| 289 expect(result.rows.elementAt(0).elementAt(4), equals(190)); |
| 290 expect(result.rows.elementAt(0).elementAt(5), equals(18000)); |
| 291 |
| 292 // Asia Japan |
| 293 expect(result.rows.elementAt(1).elementAt(3), closeTo(11.1, EPSILON)); |
| 294 expect(result.rows.elementAt(1).elementAt(4), equals(184)); |
| 295 expect(result.rows.elementAt(1).elementAt(5), equals(8000)); |
| 296 |
| 297 // Asia Taiwan Tainan |
| 298 expect(result.rows.elementAt(2).elementAt(3), closeTo(1.5, EPSILON)); |
| 299 expect(result.rows.elementAt(2).elementAt(4), equals(10)); |
| 300 expect(result.rows.elementAt(2).elementAt(5), equals(100)); |
| 301 |
| 302 // Asia Taiwan Taipei |
| 303 expect(result.rows.elementAt(3).elementAt(3), closeTo(3.5, EPSILON)); |
| 304 expect(result.rows.elementAt(3).elementAt(4), equals(127)); |
| 305 expect(result.rows.elementAt(3).elementAt(5), equals(1337)); |
| 306 |
| 307 // Europe England |
| 308 expect(result.rows.elementAt(4).elementAt(3), closeTo(2.5, EPSILON)); |
| 309 expect(result.rows.elementAt(4).elementAt(4), equals(10)); |
| 310 expect(result.rows.elementAt(4).elementAt(5), equals(3000)); |
| 311 |
| 312 // Europe France |
| 313 expect(result.rows.elementAt(5).elementAt(3), closeTo(9, EPSILON)); |
| 314 expect(result.rows.elementAt(5).elementAt(4), equals(158)); |
| 315 expect(result.rows.elementAt(5).elementAt(5), equals(9000)); |
| 316 |
| 317 // Europe Germany |
| 318 expect(result.rows.elementAt(6).elementAt(3), closeTo(10.99, EPSILON)); |
| 319 expect(result.rows.elementAt(6).elementAt(4), equals(999)); |
| 320 expect(result.rows.elementAt(6).elementAt(5), equals(10000)); |
| 321 }); |
| 322 |
| 323 test('Grouping by country-continent-city with less fact columns', () { |
| 324 AggregationTransformer transformer = new AggregationTransformer([1, 2, 0], |
| 325 [5, 3]); |
| 326 |
| 327 ChartData result = transformer.transform(inputData); |
| 328 |
| 329 // Expected data: |
| 330 // [Argentina, , , 2000.0, 5.50] |
| 331 // [Brazil, , , 9000.0, 4.00] |
| 332 // [England, , , 3000.0, 2.50] |
| 333 // [France, , , 9000.0, 9.00] |
| 334 // more entries not listed/tested.. |
| 335 // Argentian |
| 336 expect(result.rows.elementAt(0).elementAt(3), equals(2000)); |
| 337 expect(result.rows.elementAt(0).elementAt(4), closeTo(5.5, EPSILON)); |
| 338 |
| 339 // Brazil |
| 340 expect(result.rows.elementAt(1).elementAt(3), equals(9000)); |
| 341 expect(result.rows.elementAt(1).elementAt(4), closeTo(4, EPSILON)); |
| 342 |
| 343 // England |
| 344 expect(result.rows.elementAt(2).elementAt(3), equals(3000)); |
| 345 expect(result.rows.elementAt(2).elementAt(4), closeTo(2.5, EPSILON)); |
| 346 |
| 347 // France |
| 348 expect(result.rows.elementAt(3).elementAt(3), equals(9000)); |
| 349 expect(result.rows.elementAt(3).elementAt(4), closeTo(9, EPSILON)); |
| 350 }); |
| 351 |
| 352 |
| 353 test('Modifying data row when it is an ObeservableList should cause ' + |
| 354 'transform to be called', () { |
| 355 ObservableList observableRows = new ObservableList.from(DATA); |
| 356 ChartData observableData = new ChartData(COLUMNS, observableRows); |
| 357 AggregationTransformer aggrTransformer = new AggregationTransformer( |
| 358 [1, 2, 0], [5, 3]); |
| 359 |
| 360 ChartData result = aggrTransformer.transform(observableData); |
| 361 // Result at this point: |
| 362 // [Argentina, , , 2000.0, 5.50] |
| 363 // [Brazil, , , 9000.0, 4.00] |
| 364 // [England, , , 3000.0, 2.50] |
| 365 // [France, , , 9000.0, 9.00] |
| 366 // ... |
| 367 |
| 368 // Remove Brazil, Rio de Janeiro from the original data, causing aggregation |
| 369 // of stats1 for Brazil to drop from 4 to 1.5 and stats3 from 9000 to 6000. |
| 370 observableRows.remove(observableRows.last); |
| 371 observableRows.deliverListChanges(); |
| 372 (observableData as Observable).deliverChanges(); |
| 373 |
| 374 // [Argentina, , , 2000.0, 5.50] |
| 375 // [Brazil, , , 6000.0, 2.50] |
| 376 expect(result.rows.elementAt(0).elementAt(3), equals(2000)); |
| 377 expect(result.rows.elementAt(0).elementAt(4), closeTo(5.5, EPSILON)); |
| 378 expect(result.rows.elementAt(1).elementAt(3), equals(6000)); |
| 379 expect(result.rows.elementAt(1).elementAt(4), closeTo(1.5, EPSILON)); |
| 380 }); |
| 381 } |
OLD | NEW |