Mercurial > browser-couch
annotate tests.js @ 53:08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 14 Apr 2009 17:35:22 -0700 |
parents | 6da7638d056f |
children | 3607a6844691 |
rev | line source |
---|---|
5 | 1 var Tests = { |
6
25d079125b95
Changed display of test output to be more colorful and not require firebug.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
2 run: function(listener, container, setTimeout) { |
5 | 3 if (!container) |
4 container = this; | |
5 if (!setTimeout) | |
6 setTimeout = window.setTimeout; | |
7 | |
8 var tests = []; | |
9 | |
10 for (name in container) | |
11 if (name.indexOf("test") == "0") { | |
12 var test = { | |
13 name: name, | |
14 func: container[name], | |
15 isAsync: name.indexOf("_async") != -1, | |
6
25d079125b95
Changed display of test output to be more colorful and not require firebug.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
16 id: tests.length, |
5 | 17 assertEqual: function assertEqual(a, b) { |
18 if (a != b) | |
19 throw new Error(a + " != " + b); | |
20 } | |
21 }; | |
22 tests.push(test); | |
23 } | |
24 | |
6
25d079125b95
Changed display of test output to be more colorful and not require firebug.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
25 listener.onReady(tests); |
5 | 26 var nextTest = 0; |
27 | |
28 function runNextTest() { | |
29 if (nextTest < tests.length) { | |
30 var test = tests[nextTest]; | |
6
25d079125b95
Changed display of test output to be more colorful and not require firebug.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
31 listener.onRun(test); |
53
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
32 test.skip = function() { |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
33 listener.onSkip(this); |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
34 setTimeout(runNextTest, 0); |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
35 }; |
5 | 36 test.done = function() { |
6
25d079125b95
Changed display of test output to be more colorful and not require firebug.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
37 listener.onFinish(this); |
5 | 38 setTimeout(runNextTest, 0); |
39 }; | |
33 | 40 test.func.call(container, test); |
5 | 41 if (!test.isAsync) |
42 test.done(); | |
43 nextTest++; | |
6
25d079125b95
Changed display of test output to be more colorful and not require firebug.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
44 } |
5 | 45 } |
46 | |
47 runNextTest(); | |
48 }, | |
13
07e8dc256570
Added unit tests for Dictionary and fixed a bug or two in the Dictionary's implementation.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
49 testDictionary: function(self) { |
07e8dc256570
Added unit tests for Dictionary and fixed a bug or two in the Dictionary's implementation.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
50 var dict = new BrowserCouch._Dictionary(); |
07e8dc256570
Added unit tests for Dictionary and fixed a bug or two in the Dictionary's implementation.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
51 dict.set('foo', {a: 'hello'}); |
07e8dc256570
Added unit tests for Dictionary and fixed a bug or two in the Dictionary's implementation.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
52 dict.set('bar', {b: 'goodbye'}); |
07e8dc256570
Added unit tests for Dictionary and fixed a bug or two in the Dictionary's implementation.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
53 self.assertEqual(dict.get('foo').a, 'hello'); |
07e8dc256570
Added unit tests for Dictionary and fixed a bug or two in the Dictionary's implementation.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
54 self.assertEqual(dict.get('bar').b, 'goodbye'); |
18
75b17e0a7897
Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
55 self.assertEqual(dict.getKeys().length, 2); |
13
07e8dc256570
Added unit tests for Dictionary and fixed a bug or two in the Dictionary's implementation.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
56 self.assertEqual(dict.has('foo'), true); |
07e8dc256570
Added unit tests for Dictionary and fixed a bug or two in the Dictionary's implementation.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
57 self.assertEqual(dict.has('bar'), true); |
07e8dc256570
Added unit tests for Dictionary and fixed a bug or two in the Dictionary's implementation.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
58 self.assertEqual(dict.has('spatula'), false); |
41
3fd3aacf33fb
Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
59 dict.remove('bar'); |
18
75b17e0a7897
Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
60 self.assertEqual(dict.getKeys().length, 1); |
14
fbee4473dbdd
Added more unit tests and fixed bugs unveiled by them.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
61 self.assertEqual(dict.has('foo'), true); |
13
07e8dc256570
Added unit tests for Dictionary and fixed a bug or two in the Dictionary's implementation.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
62 }, |
33 | 63 _setupTestDb: function(cb) { |
5 | 64 BrowserCouch.get( |
65 "blarg", | |
66 function(db) { | |
33 | 67 db.wipe( |
5 | 68 function() { |
33 | 69 db.put( |
70 [{id: "monkey", | |
71 content: "hello there dude"}, | |
72 {id: "chunky", | |
73 content: "hello there dogen"}], | |
74 function() { | |
75 ModuleLoader.require( | |
76 "JSON", | |
77 function() { cb(db); } | |
78 ); | |
79 } | |
80 ); | |
81 }); | |
40
f54c719b118d
Tests now use fake storage.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
82 }, |
f54c719b118d
Tests now use fake storage.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
83 new FakeStorage() |
f54c719b118d
Tests now use fake storage.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
84 ); |
33 | 85 }, |
86 _mapWordFrequencies: function(doc, emit) { | |
87 var words = doc.content.split(" "); | |
88 for (var i = 0; i < words.length; i++) | |
89 emit(words[i], 1); | |
90 }, | |
91 _reduceWordFrequencies: function(keys, values) { | |
92 var sum = 0; | |
93 for (var i = 0; i < values.length; i++) | |
94 sum += values[i]; | |
95 return sum; | |
96 }, | |
34 | 97 testViewMap_async: function(self) { |
98 var map = this._mapWordFrequencies; | |
99 this._setupTestDb( | |
100 function(db) { | |
101 db.view( | |
102 {map: map, | |
103 finished: function(result) { | |
104 var expected = { | |
105 rows:[{"id":"chunky","key":"dogen","value":1}, | |
106 {"id":"monkey","key":"dude","value":1}, | |
107 {"id":"monkey","key":"hello","value":1}, | |
108 {"id":"chunky","key":"hello","value":1}, | |
109 {"id":"monkey","key":"there","value":1}, | |
110 {"id":"chunky","key":"there","value":1}] | |
111 }; | |
112 self.assertEqual(JSON.stringify(expected), | |
113 JSON.stringify(result)); | |
114 self.done(); | |
115 }}); | |
116 }); | |
117 }, | |
45
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
118 testViewMapFindRow_async: function(self) { |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
119 var map = this._mapWordFrequencies; |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
120 this._setupTestDb( |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
121 function(db) { |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
122 db.view( |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
123 {map: map, |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
124 finished: function(view) { |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
125 self.assertEqual(view.findRow("dogen"), 0); |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
126 self.assertEqual(view.findRow("dude"), 1); |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
127 self.assertEqual(view.findRow("hello"), 2); |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
128 self.assertEqual(view.findRow("there"), 4); |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
129 self.done(); |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
130 }}); |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
131 }); |
3a34b9ed3a36
Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
132 }, |
34 | 133 testViewProgress_async: function(self) { |
33 | 134 var map = this._mapWordFrequencies; |
135 var reduce = this._reduceWordFrequencies; | |
136 this._setupTestDb( | |
137 function(db) { | |
138 var progressCalled = false; | |
139 var timesProgressCalled = 0; | |
140 db.view( | |
141 {map: map, | |
142 reduce: reduce, | |
143 chunkSize: 1, | |
144 progress: function(phase, percentDone, resume) { | |
145 if (phase == "map") { | |
146 self.assertEqual(percentDone, 0.5); | |
147 progressCalled = true; | |
148 } | |
149 resume(); | |
150 }, | |
151 finished: function(result) { | |
152 self.assertEqual(progressCalled, true); | |
34 | 153 self.done(); |
154 }}); | |
155 }); | |
156 }, | |
46
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
157 testViewMapReduceFindRow_async: function(self) { |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
158 var map = this._mapWordFrequencies; |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
159 var reduce = this._reduceWordFrequencies; |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
160 this._setupTestDb( |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
161 function(db) { |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
162 db.view( |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
163 {map: map, |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
164 reduce: reduce, |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
165 finished: function(view) { |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
166 self.assertEqual(view.findRow("dogen"), 0); |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
167 self.assertEqual(view.findRow("dude"), 1); |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
168 self.assertEqual(view.findRow("hello"), 2); |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
169 self.assertEqual(view.findRow("there"), 3); |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
170 self.done(); |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
171 }}); |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
172 }); |
6da7638d056f
Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
173 }, |
53
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
174 testViewMapReduceWebWorker_async: function(self) { |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
175 if (window.Worker) { |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
176 var map = this._mapWordFrequencies; |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
177 var reduce = this._reduceWordFrequencies; |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
178 this._setupTestDb( |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
179 function(db) { |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
180 db.view( |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
181 {map: map, |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
182 reduce: reduce, |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
183 mapReducer: new WebWorkerMapReducer(2), |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
184 chunkSize: 1, |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
185 finished: function(result) { |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
186 var expected = {rows: [{key: "dogen", value: 1}, |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
187 {key: "dude", value: 1}, |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
188 {key: "hello", value: 2}, |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
189 {key: "there", value: 2}]}; |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
190 self.assertEqual(JSON.stringify(expected), |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
191 JSON.stringify(result)); |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
192 self.done(); |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
193 }}); |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
194 }); |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
195 } else |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
196 self.skip(); |
08f868fc6e2b
Added a unit test for the web worker map reducer, and added the ability to skip tests into the test framework.
Atul Varma <varmaa@toolness.com>
parents:
46
diff
changeset
|
197 }, |
34 | 198 testViewMapReduce_async: function(self) { |
199 var map = this._mapWordFrequencies; | |
200 var reduce = this._reduceWordFrequencies; | |
201 this._setupTestDb( | |
202 function(db) { | |
203 db.view( | |
204 {map: map, | |
205 reduce: reduce, | |
206 finished: function(result) { | |
33 | 207 var expected = {rows: [{key: "dogen", value: 1}, |
208 {key: "dude", value: 1}, | |
209 {key: "hello", value: 2}, | |
210 {key: "there", value: 2}]}; | |
211 self.assertEqual(JSON.stringify(expected), | |
212 JSON.stringify(result)); | |
213 self.done(); | |
214 }}); | |
5 | 215 }); |
216 } | |
217 }; |