~smagoun/whoopsie/whoopsie-lp1017637

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/tests/cache/tests/cache-tests.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
 
YUI.add('cache-tests', function(Y) {
2
 
        // Set up the page
3
 
        var ASSERT = Y.Assert,
4
 
            ARRAYASSERT = Y.ArrayAssert;
5
 
        
6
 
 
7
 
        var testClass = new Y.Test.Case({
8
 
            name: "Class Tests",
9
 
        
10
 
            testDefaults: function() {
11
 
                var cache = new Y.Cache();
12
 
                ASSERT.isInstanceOf(Y.Cache, cache, "Expected instance of Y.Cache.");
13
 
                ASSERT.areSame(0, cache.get("max"), "Expected default max of 0.");
14
 
                ARRAYASSERT.isEmpty(cache.get("entries"), "Expected empty array.");
15
 
            },
16
 
 
17
 
            testDestructor: function() {
18
 
                var cache = new Y.Cache();
19
 
                cache.destroy();
20
 
                ARRAYASSERT.isEmpty(cache.get("entries"), "Expected empty array.");
21
 
            }
22
 
        });
23
 
        
24
 
        var testBasic = new Y.Test.Case({
25
 
            name: "Basic Tests",
26
 
 
27
 
            testmax0: function() {
28
 
                var cache = new Y.Cache();
29
 
                ASSERT.areSame(0, cache.get("max"), "Expected max to be 0.");
30
 
                
31
 
                cache.add(1, "a");
32
 
                ASSERT.areSame(0, cache.get("size"), "Expected 0 entries.");
33
 
                ASSERT.isNull(cache.retrieve(1), "Expected null cached response.");
34
 
            },
35
 
 
36
 
            testmax2: function() {
37
 
                var cache = new Y.Cache({max:2});
38
 
                ASSERT.areSame(2, cache.get("max"), "Expected max to be 2.");
39
 
                
40
 
                cache.add(1, "a");
41
 
                ASSERT.areSame(1, cache.get("size"), "Expected 1 entry.");
42
 
                cache.add(2, "b");
43
 
                ASSERT.areSame(2, cache.get("size"), "Expected 2 entries.");
44
 
                cache.add(3, "c");
45
 
                ASSERT.areSame(2, cache.get("size"), "Expected 2 entries (still).");
46
 
            },
47
 
        
48
 
            testmax2to1: function() {
49
 
                var cache = new Y.Cache({max:2});
50
 
                cache.add(1, "a");
51
 
                cache.add(2, "b");
52
 
                cache.set("max", 1);
53
 
                ASSERT.areSame(1, cache.get("size"), "Expected 1 entry.");
54
 
 
55
 
                cache.add(3, "c");
56
 
                ASSERT.areSame(1, cache.get("size"), "Expected 1 entry (still).");
57
 
            },
58
 
 
59
 
            testmax2to0: function() {
60
 
                var cache = new Y.Cache({max:2});
61
 
                cache.add(1, "a");
62
 
                cache.add(2, "b");
63
 
                cache.set("max", 0);
64
 
                ARRAYASSERT.isEmpty(cache.get("entries"), "Expected empty array.");
65
 
                cache.add(3, "c");
66
 
                ARRAYASSERT.isEmpty(cache.get("entries"), "Expected empty array (still).");
67
 
            },
68
 
 
69
 
            testmax2toNegative: function() {
70
 
                var cache = new Y.Cache({max:2});
71
 
                cache.add(1, "a");
72
 
                cache.add(2, "b");
73
 
                cache.set("max", -5);
74
 
                ARRAYASSERT.isEmpty(cache.get("entries"), "Expected empty array.");
75
 
                cache.add(3, "c");
76
 
                ARRAYASSERT.isEmpty(cache.get("entries"), "Expected empty array (still).");
77
 
                ASSERT.areSame(0, cache.get("max"), "Expected negative value normalized to 0.");
78
 
            },
79
 
 
80
 
            testRetrieve: function() {
81
 
                var cache = new Y.Cache({max:2}),
82
 
                    cachedentry;
83
 
                cache.add(1, "a");
84
 
                cache.add("b", "c");
85
 
                cachedentry = cache.retrieve(1);
86
 
                ASSERT.areSame("a", cachedentry.response, "Expected first cached response.");
87
 
                ASSERT.isInstanceOf(Date, cachedentry.cached, "Expected first cached Date.");
88
 
                ASSERT.isNull(cachedentry.expires, "Expected null expires first.");
89
 
 
90
 
                cachedentry = cache.retrieve("b");
91
 
                ASSERT.areSame("c", cachedentry.response, "Expected second cached response.");
92
 
                ASSERT.isInstanceOf(Date, cachedentry.cached, "Expected second cached Date.");
93
 
                ASSERT.isNull(cachedentry.expires, "Expected null expires second.");
94
 
            },
95
 
 
96
 
            testNoExpires: function() {
97
 
                this.cache = new Y.Cache({max:5, expires:0}),
98
 
                this.cache.add(1, "a");
99
 
                this.cache.add("b", "c");
100
 
 
101
 
                var cachedentry = this.cache.retrieve(1);
102
 
                ASSERT.areSame("a", cachedentry.response, "Expected cached response.");
103
 
                ASSERT.isInstanceOf(Date, cachedentry.cached, "Expected cached Date.");
104
 
                ASSERT.isNull(cachedentry.expires, "Expected null expires.");
105
 
            },
106
 
 
107
 
            testExpiresNumber: function() {
108
 
                this.cache = new Y.Cache({max:5, expires:2000}),
109
 
                this.cache.add(1, "a");
110
 
 
111
 
                var cachedentry = this.cache.retrieve(1);
112
 
                ASSERT.areSame("a", cachedentry.response, "Expected cached response.");
113
 
                ASSERT.isInstanceOf(Date, cachedentry.expires, "Expected cached Date.");
114
 
 
115
 
                this.cache.set("expires", 1);
116
 
                this.cache.add(2, "b");
117
 
 
118
 
                this.wait(function(){
119
 
                    cachedentry = this.cache.retrieve(2);
120
 
                    ASSERT.isNull(cachedentry, "Expected expired data.");
121
 
                }, 50);
122
 
            },
123
 
 
124
 
            testExpiresDate: function() {
125
 
                this.cache = new Y.Cache({max:5, expires:new Date(new Date().getTime() + 86400000)}),
126
 
                this.cache.add(1, "a");
127
 
 
128
 
                var cachedentry = this.cache.retrieve(1);
129
 
                ASSERT.areSame("a", cachedentry.response, "Expected cached response.");
130
 
                ASSERT.isInstanceOf(Date, cachedentry.expires, "Expected cached Date.");
131
 
 
132
 
                this.cache.flush();
133
 
                this.cache.set("expires", new Date(new Date().getTime() - 86400000));
134
 
                this.cache.add(1, "a");
135
 
                cachedentry = this.cache.retrieve(1);
136
 
                ASSERT.isNull(cachedentry, "Expected expired data.");
137
 
            },
138
 
 
139
 
            testNoMatch: function() {
140
 
                var cache = new Y.Cache({max:2}),
141
 
                    cachedentry;
142
 
                cache.add("a", "b");
143
 
                cachedentry = cache.retrieve("c");
144
 
                ASSERT.areSame(null, cachedentry, "Expected no match.");
145
 
            },
146
 
 
147
 
            testFlush: function() {
148
 
                var cache = new Y.Cache({max:2});
149
 
                cache.add(1, "a");
150
 
                cache.add(2, "b");
151
 
                cache.flush();
152
 
                ASSERT.areSame(0, cache.get("size"), "Expected empty cache.");
153
 
            },
154
 
            
155
 
            testFlushItem: function() {
156
 
                var cache = new Y.Cache({max:2});
157
 
                cache.add(1, "a");
158
 
                cache.add(2, "b");
159
 
                cache.flush(1);
160
 
                ASSERT.areSame(1, cache.get("size"), "Expected single item");
161
 
                ASSERT.areSame("b", cache.get("entries")[0].response, "Expected 'b'");
162
 
            }
163
 
        });
164
 
    
165
 
        var testEvents = new Y.Test.Case({
166
 
            name: "Event Tests",
167
 
 
168
 
            testAdd: function() {
169
 
                var mock = new Y.Mock();
170
 
                Y.Mock.expect(mock, {
171
 
                    method: "handleAdd",
172
 
                    args: [Y.Mock.Value.Object]
173
 
                });
174
 
 
175
 
                var cache = new Y.Cache({max:2});
176
 
                cache.on("add", mock.handleAdd);
177
 
                cache.add(1, "a");
178
 
 
179
 
                Y.Mock.verify(mock);
180
 
            },
181
 
        
182
 
            testFlush: function() {
183
 
                var mock = new Y.Mock();
184
 
                Y.Mock.expect(mock, {
185
 
                    method: "handleFlush",
186
 
                    args: [Y.Mock.Value.Object]
187
 
                });
188
 
 
189
 
                var cache = new Y.Cache({max:2});
190
 
                cache.on("flush", mock.handleFlush);
191
 
                cache.add(1, "a");
192
 
                cache.flush();
193
 
 
194
 
                Y.Mock.verify(mock);
195
 
            },
196
 
 
197
 
            testRequest: function() {
198
 
                var mock = new Y.Mock();
199
 
                Y.Mock.expect(mock, {
200
 
                    method: "handleRequest",
201
 
                    args: [Y.Mock.Value(function(e){
202
 
                        ASSERT.areSame(2, e.request, "Expected request.");
203
 
                    })]
204
 
                });
205
 
 
206
 
                var cache = new Y.Cache({max:2});
207
 
                cache.on("request", mock.handleRequest);
208
 
                cache.add(1, "a");
209
 
                cache.retrieve(2);
210
 
 
211
 
                Y.Mock.verify(mock);
212
 
            },
213
 
 
214
 
            testRetrieveSuccess: function() {
215
 
                var mock = new Y.Mock();
216
 
                Y.Mock.expect(mock, {
217
 
                    method: "handleRetrieve",
218
 
                    args: [Y.Mock.Value(function(e){
219
 
                        ASSERT.areSame(1, e.entry.request);
220
 
                        ASSERT.areSame("a", e.entry.response);
221
 
                    })]
222
 
                });
223
 
 
224
 
                var cache = new Y.Cache({max:2});
225
 
                cache.on("retrieve", mock.handleRetrieve);
226
 
                cache.add(1, "a");
227
 
                cache.retrieve(1);
228
 
 
229
 
                Y.Mock.verify(mock);
230
 
            },
231
 
 
232
 
            testRetrieveFailure: function() {
233
 
                var mock = new Y.Mock();
234
 
                Y.Mock.expect(mock, {
235
 
                    method: "handleRetrieve",
236
 
                    args: [Y.Mock.Value.Any],
237
 
                    callCount: 0
238
 
                });
239
 
 
240
 
                var cache = new Y.Cache({max:2});
241
 
                cache.on("retrieve", mock.handleRetrieve);
242
 
                cache.add(1, "a");
243
 
                cache.retrieve(2);
244
 
 
245
 
                Y.Mock.verify(mock);
246
 
            },
247
 
 
248
 
            testCancelAdd: function() {
249
 
                var cache = new Y.Cache({max:2});
250
 
                cache.on("add", function(e) {
251
 
                    e.preventDefault();
252
 
                }, this, true);
253
 
                cache.add(1, "a");
254
 
                
255
 
                // Test the cancel
256
 
                ASSERT.areSame(0, cache.get("size"), "Expected 0 entries.");
257
 
            },
258
 
 
259
 
            testCancelFlush: function() {
260
 
                var cache = new Y.Cache({max:2});
261
 
                cache.on("flush", function(e) {
262
 
                    e.preventDefault();
263
 
                }, this, true);
264
 
                cache.add(1, "a");
265
 
                cache.flush();
266
 
                
267
 
                // Test the cancel
268
 
                ASSERT.areSame(1, cache.get("size"), "Expected 1 entry.");
269
 
            }
270
 
        });
271
 
 
272
 
        var testEntryManagement = new Y.Test.Case({
273
 
            name: "Entry Management Tests",
274
 
 
275
 
            testNonUniqueKeys: function() {
276
 
                var cache = new Y.Cache({max:3});
277
 
                cache.add(1, "a");
278
 
                cache.add(2, "b");
279
 
                cache.add(1, "c");
280
 
                ASSERT.areSame(3, cache.get("size"), "Expected 3 entries.");
281
 
            },
282
 
 
283
 
            testUniqueKeys: function() {
284
 
                var cache = new Y.Cache({max:3,uniqueKeys:true});
285
 
                cache.add(1, "a");
286
 
                cache.add(2, "b");
287
 
                cache.add(1, "c");
288
 
                ASSERT.areSame(2, cache.get("size"), "Expected 2 entries.");
289
 
            },
290
 
            
291
 
            testUniqueKeyValues: function() {
292
 
                var cache = new Y.Cache({max:3,uniqueKeys:true});
293
 
                cache.add(1, "a");
294
 
                cache.add(2, "b");
295
 
                cache.add(1, "c");
296
 
                ASSERT.areSame("c", cache.retrieve(1).response, "Expected 'c'");
297
 
            },
298
 
 
299
 
            testFreshness: function() {
300
 
                var cache = new Y.Cache({max:3});
301
 
                cache.add(1, "a");
302
 
                cache.add(2, "b");
303
 
                cache.add(3, "c");
304
 
                cache.retrieve(1);
305
 
                ASSERT.areSame(3, cache.get("size"), "Expected 3 entries.");
306
 
                ASSERT.areSame(1, cache.get("entries")[2].request, "Expected entry to be refreshed.");
307
 
            }
308
 
        });
309
 
 
310
 
        var testBoundaryValues = new Y.Test.Case({
311
 
            name: "Invalid Value Tests",
312
 
 
313
 
            testUndefinedRequest: function() {
314
 
                var cache = new Y.Cache({max:3});
315
 
                cache.add(undefined, "a");
316
 
                cache.add(undefined, "b");
317
 
                ASSERT.areSame("b", cache.retrieve().response, "Expected cached response.");
318
 
            },
319
 
 
320
 
            testNullRequest: function() {
321
 
                var cache = new Y.Cache({max:3});
322
 
                cache.add(null, "a");
323
 
                cache.add(null, "b");
324
 
                ASSERT.areSame("b", cache.retrieve(null).response, "Expected cached response.");
325
 
            },
326
 
 
327
 
            testNaNRequest: function() {
328
 
                var cache = new Y.Cache({max:3});
329
 
                cache.add(NaN, "a");
330
 
                cache.add(NaN, "b");
331
 
                ASSERT.areSame(0, cache.get("size"), "Expected 0 entries.");
332
 
            },
333
 
 
334
 
            testEmptyStringRequest: function() {
335
 
                var cache = new Y.Cache({max:3});
336
 
                cache.add("", "a");
337
 
                cache.add("", "b");
338
 
                ASSERT.areSame("b", cache.retrieve("").response, "Expected cached response.");
339
 
            }
340
 
        });
341
 
 
342
 
        var suite = new Y.Test.Suite({name:"Cache Test Suite"});
343
 
        suite.add(testClass);
344
 
        suite.add(testBasic);
345
 
        suite.add(testEvents);
346
 
        suite.add(testEntryManagement);
347
 
        suite.add(testBoundaryValues);
348
 
 
349
 
        Y.Test.Runner.setName("Cache Test Runner");
350
 
        Y.Test.Runner.add(suite);
351
 
});