~smagoun/whoopsie/whoopsie-lp1017637

« back to all changes in this revision

Viewing changes to backend/stats/static/js/d3/test/core/nest-test.js

  • Committer: Evan Dandrea
  • Date: 2012-05-09 05:53:45 UTC
  • Revision ID: evan.dandrea@canonical.com-20120509055345-z2j41tmcbf4as5uf
The backend now lives in lp:daisy and the website (errors.ubuntu.com) now lives in lp:errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require("../env");
2
 
 
3
 
var vows = require("vows"),
4
 
    assert = require("assert");
5
 
 
6
 
var suite = vows.describe("d3.nest");
7
 
 
8
 
suite.addBatch({
9
 
  "entries": {
10
 
    topic: function() {
11
 
      return d3.nest;
12
 
    },
13
 
    "returns an array of each distinct key in arbitrary order": function(nest) {
14
 
      var keys = nest()
15
 
          .key(function(d) { return d.foo; })
16
 
          .entries([{foo: 1}, {foo: 1}, {foo: 2}])
17
 
          .map(function(d) { return d.key; })
18
 
          .sort(d3.ascending);
19
 
      assert.deepEqual(keys, ["1", "2"]);
20
 
    },
21
 
    "each entry is a key-values object, with values in input order": function(nest) {
22
 
      var entries = nest()
23
 
          .key(function(d) { return d.foo; })
24
 
          .entries([{foo: 1, bar: 0}, {foo: 2}, {foo: 1, bar: 1}]);
25
 
      assert.deepEqual(entries, [
26
 
        {key: "1", values: [{foo: 1, bar: 0}, {foo: 1, bar: 1}]},
27
 
        {key: "2", values: [{foo: 2}]}
28
 
      ]);
29
 
    },
30
 
    "keys can be sorted using an optional comparator": function(nest) {
31
 
      var keys = nest()
32
 
          .key(function(d) { return d.foo; }).sortKeys(d3.descending)
33
 
          .entries([{foo: 1}, {foo: 1}, {foo: 2}])
34
 
          .map(function(d) { return d.key; });
35
 
      assert.deepEqual(keys, ["2", "1"]);
36
 
    },
37
 
    "values can be sorted using an optional comparator": function(nest) {
38
 
      var entries = nest()
39
 
          .key(function(d) { return d.foo; })
40
 
          .sortValues(function(a, b) { return a.bar - b.bar; })
41
 
          .entries([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]);
42
 
      assert.deepEqual(entries, [
43
 
        {key: "1", values: [{foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 1, bar: 2}]},
44
 
        {key: "2", values: [{foo: 2}]}
45
 
      ]);
46
 
    },
47
 
    "values can be aggregated using an optional rollup": function(nest) {
48
 
      var entries = nest()
49
 
          .key(function(d) { return d.foo; })
50
 
          .rollup(function(values) { return d3.sum(values, function(d) { return d.bar; }); })
51
 
          .entries([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]);
52
 
      assert.deepEqual(entries, [
53
 
        {key: "1", values: 3},
54
 
        {key: "2", values: 0}
55
 
      ]);
56
 
    },
57
 
    "multiple key functions can be specified": function(nest) {
58
 
      var entries = nest()
59
 
          .key(function(d) { return d[0]; }).sortKeys(d3.ascending)
60
 
          .key(function(d) { return d[1]; }).sortKeys(d3.ascending)
61
 
          .entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
62
 
      assert.deepEqual(entries, [
63
 
        {key: "0", values: [
64
 
          {key: "1", values: [[0, 1]]},
65
 
          {key: "2", values: [[0, 2], [0, 2]]}
66
 
        ]},
67
 
        {key: "1", values: [
68
 
          {key: "1", values: [[1, 1]]},
69
 
          {key: "2", values: [[1, 2]]}
70
 
        ]}
71
 
      ]);
72
 
    },
73
 
    "the rollup function only applies to leaf values": function(nest) {
74
 
      var entries = nest()
75
 
          .key(function(d) { return d[0]; }).sortKeys(d3.ascending)
76
 
          .key(function(d) { return d[1]; }).sortKeys(d3.ascending)
77
 
          .rollup(function(values) { return values.length; })
78
 
          .entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
79
 
      assert.deepEqual(entries, [
80
 
        {key: "0", values: [
81
 
          {key: "1", values: 1},
82
 
          {key: "2", values: 2}
83
 
        ]},
84
 
        {key: "1", values: [
85
 
          {key: "1", values: 1},
86
 
          {key: "2", values: 1}
87
 
        ]}
88
 
      ]);
89
 
    },
90
 
    "the value comparator only applies to leaf values": function(nest) {
91
 
      var entries = nest()
92
 
          .key(function(d) { return d[0]; }).sortKeys(d3.ascending)
93
 
          .key(function(d) { return d[1]; }).sortKeys(d3.ascending)
94
 
          .sortValues(function(a, b) { return a[2] - b[2]; })
95
 
          .entries([[0, 1], [0, 2, 1], [1, 1], [1, 2], [0, 2, 0]]);
96
 
      assert.deepEqual(entries, [
97
 
        {key: "0", values: [
98
 
          {key: "1", values: [[0, 1]]},
99
 
          {key: "2", values: [[0, 2, 0], [0, 2, 1]]}
100
 
        ]},
101
 
        {key: "1", values: [
102
 
          {key: "1", values: [[1, 1]]},
103
 
          {key: "2", values: [[1, 2]]}
104
 
        ]}
105
 
      ]);
106
 
    },
107
 
    "the key comparator only applies to the last-specified key": function(nest) {
108
 
      var entries = nest()
109
 
          .key(function(d) { return d[0]; }).sortKeys(d3.ascending)
110
 
          .key(function(d) { return d[1]; }).sortKeys(d3.descending)
111
 
          .entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
112
 
      assert.deepEqual(entries, [
113
 
        {key: "0", values: [
114
 
          {key: "2", values: [[0, 2], [0, 2]]},
115
 
          {key: "1", values: [[0, 1]]}
116
 
        ]},
117
 
        {key: "1", values: [
118
 
          {key: "2", values: [[1, 2]]},
119
 
          {key: "1", values: [[1, 1]]}
120
 
        ]}
121
 
      ]);
122
 
      var entries = nest()
123
 
          .key(function(d) { return d[0]; }).sortKeys(d3.descending)
124
 
          .key(function(d) { return d[1]; }).sortKeys(d3.ascending)
125
 
          .entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
126
 
      assert.deepEqual(entries, [
127
 
        {key: "1", values: [
128
 
          {key: "1", values: [[1, 1]]},
129
 
          {key: "2", values: [[1, 2]]}
130
 
        ]},
131
 
        {key: "0", values: [
132
 
          {key: "1", values: [[0, 1]]},
133
 
          {key: "2", values: [[0, 2], [0, 2]]}
134
 
        ]}
135
 
      ]);
136
 
    },
137
 
    "if no keys are specified, the input array is returned": function(nest) {
138
 
      var array = [new Object()];
139
 
      assert.strictEqual(nest().entries(array), array);
140
 
    }
141
 
  }
142
 
});
143
 
 
144
 
suite.addBatch({
145
 
  "map": {
146
 
    topic: function() {
147
 
      return d3.nest;
148
 
    },
149
 
    "returns a map of each distinct key": function(nest) {
150
 
      var map = nest()
151
 
          .key(function(d) { return d.foo; })
152
 
          .map([{foo: 1, bar: 0}, {foo: 2}, {foo: 1, bar: 1}]);
153
 
      assert.deepEqual(map, {
154
 
        "1": [{foo: 1, bar: 0}, {foo: 1, bar: 1}],
155
 
        "2": [{foo: 2}]
156
 
      });
157
 
    },
158
 
    "values can be sorted using an optional comparator": function(nest) {
159
 
      var map = nest()
160
 
          .key(function(d) { return d.foo; })
161
 
          .sortValues(function(a, b) { return a.bar - b.bar; })
162
 
          .map([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]);
163
 
      assert.deepEqual(map, {
164
 
        "1": [{foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 1, bar: 2}],
165
 
        "2": [{foo: 2}]
166
 
      });
167
 
    },
168
 
    "values can be aggregated using an optional rollup": function(nest) {
169
 
      var map = nest()
170
 
          .key(function(d) { return d.foo; })
171
 
          .rollup(function(values) { return d3.sum(values, function(d) { return d.bar; }); })
172
 
          .map([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]);
173
 
      assert.deepEqual(map, {
174
 
        "1": 3,
175
 
        "2": 0
176
 
      });
177
 
    },
178
 
    "multiple key functions can be specified": function(nest) {
179
 
      var map = nest()
180
 
          .key(function(d) { return d[0]; }).sortKeys(d3.ascending)
181
 
          .key(function(d) { return d[1]; }).sortKeys(d3.ascending)
182
 
          .map([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
183
 
      assert.deepEqual(map, {
184
 
        "0": {
185
 
          "1": [[0, 1]],
186
 
          "2": [[0, 2], [0, 2]]
187
 
        },
188
 
        "1": {
189
 
          "1": [[1, 1]],
190
 
          "2": [[1, 2]]
191
 
        }
192
 
      });
193
 
    },
194
 
    "the rollup function only applies to leaf values": function(nest) {
195
 
      var map = nest()
196
 
          .key(function(d) { return d[0]; }).sortKeys(d3.ascending)
197
 
          .key(function(d) { return d[1]; }).sortKeys(d3.ascending)
198
 
          .rollup(function(values) { return values.length; })
199
 
          .map([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
200
 
      assert.deepEqual(map, {
201
 
        "0": {
202
 
          "1": 1,
203
 
          "2": 2
204
 
        },
205
 
        "1": {
206
 
          "1": 1,
207
 
          "2": 1
208
 
        }
209
 
      });
210
 
    },
211
 
    "the value comparator only applies to leaf values": function(nest) {
212
 
      var map = nest()
213
 
          .key(function(d) { return d[0]; }).sortKeys(d3.ascending)
214
 
          .key(function(d) { return d[1]; }).sortKeys(d3.ascending)
215
 
          .sortValues(function(a, b) { return a[2] - b[2]; })
216
 
          .map([[0, 1], [0, 2, 1], [1, 1], [1, 2], [0, 2, 0]]);
217
 
      assert.deepEqual(map, {
218
 
        "0": {
219
 
          "1": [[0, 1]],
220
 
          "2": [[0, 2, 0], [0, 2, 1]]
221
 
        },
222
 
        "1": {
223
 
          "1": [[1, 1]],
224
 
          "2": [[1, 2]]
225
 
        }
226
 
      });
227
 
    },
228
 
    "if no keys are specified, the input array is returned": function(nest) {
229
 
      var array = [new Object()];
230
 
      assert.strictEqual(nest().map(array), array);
231
 
    },
232
 
    "handles keys that are built-in prototype properties": function(nest) {
233
 
      var keys = nest()
234
 
          .key(String)
235
 
          .map(["hasOwnProperty"]);
236
 
      assert.deepEqual(keys, {hasOwnProperty: ["hasOwnProperty"]});
237
 
    }
238
 
  }
239
 
});
240
 
 
241
 
suite.export(module);