-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbwrapper_gears_test.html
404 lines (345 loc) · 12 KB
/
dbwrapper_gears_test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<!DOCTYPE html>
<!--
Copyright 2009 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<title>Gears database wrapper tests</title>
<script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script>
<script type="text/javascript" src="jsmock.js"></script>
<script type="text/javascript" src="global_functions.js"></script>
<script type="text/javascript" src="gearsutils.js"></script>
<script type="text/javascript" src="dbwrapperapi.js"></script>
<script type="text/javascript" src="gears_resultset.js"></script>
<script type="text/javascript" src="gears_transaction.js"></script>
<script type="text/javascript" src="dbworker.js"></script>
<script type="text/javascript" src="dbwrapper_gears.js"></script>
</head>
<body>
<script type='text/javascript'>
var mockControl;
var callbackMock;
var dbMock;
var wp;
var workerId = 123;
var transMock;
function setUp() {
mockControl = new MockControl();
callbackMock = mockControl.createMock({
onSuccess: function(){},
onFailure: function(){}
});
transMock = mockControl.createMock({
success: function(){},
failure: function(){}
});
wp = mockControl.createMock({
allowCrossOrigin: function(){},
createWorkerFromUrl: function(){},
sendMessage: function(){}
});
dbMock = mockControl.createMock({
execute: function(){},
open: function(){},
close: function(){},
remove: function(){}
});
google.wspl.GearsUtils.resultSetToObjectArray = function(rs) {
return rs;
};
}
function buildSyncDatabase() {
var database = new google.wspl.gears.Database(true);
database.userId_ = 'userId';
database.name_ = 'name';
database.db_ = dbMock;
return database;
}
function buildDatabase() {
var database = buildSyncDatabase();
var type = google.wspl.gears.DbWorker.CommandTypes.OPEN;
wp.expects().createWorkerFromUrl('workerUrl').andReturn(workerId);
wp.expects().sendMessage(TypeOf.isA(Object), workerId).andStub(function() {
assertEquals('Wrong message type.', type, arguments[0].type);
assertEquals('Incorrect name.', 'name', arguments[0].name);
assertEquals('Incorrect user id.', 'userId', arguments[0].userId);
});
database.startWorker(wp, 'workerUrl', 0);
database.handleStarted_();
database.handleOpenSuccessful_();
database.useWorker_ = true;
return database;
}
function testConstructor() {
var db = buildSyncDatabase();
mockControl.verify();
}
function testStartWorker() {
var db = buildDatabase();
assertTrue('Expected worker to be ready.', db.workerReady_);
mockControl.verify();
}
function testCreateTransaction() {
var db = buildDatabase();
var tx;
var populateMock = function(txa) {
tx = txa;
var transactions = db.transactions_;
assertEquals('missing transaction', tx, transactions[tx.id_].transaction);
assertEquals('missing callback', callbackMock,
transactions[tx.id_].callback);
assertEquals('database not saved', db, tx.db_);
assertTrue('database should be locked', db.locked_);
};
callbackMock.expects().onSuccess();
var transactions = db.transactions_;
db.createTransaction(populateMock, callbackMock);
assertEquals('failed to clean up transaction', undefined,
transactions[tx.id_]);
assertFalse('database should not be locked', db.locked_);
mockControl.verify();
}
function testMultipleTransactions() {
var db = buildDatabase();
var handler = mockControl.createMock();
handler.addMockMethod('populate');
handler.addMockMethod('onSuccess');
handler.addMockMethod('onFailure');
var trans;
handler.expects().populate(TypeOf.isA(google.wspl.gears.Transaction)).andStub(
function(tx) {
trans = tx;
tx.numActiveExecutes_ = 1;
});
db.createTransaction(handler.populate, handler);
db.createTransaction(handler.populate, handler);
mockControl.verify();
handler.expects().onSuccess();
handler.expects().populate(TypeOf.isA(google.wspl.gears.Transaction)).andStub(
function(tx) {
trans = tx;
tx.numActiveExecutes_ = 1;
});
db.handleCommit_(trans.id_);
mockControl.verify();
handler.expects().onFailure(undefined).andStub(function() {
db.createTransaction(handler.populate, handler);
});
handler.expects().populate(TypeOf.isA(google.wspl.gears.Transaction)).andStub(
function(tx) {
trans = tx;
tx.numActiveExecutes_ = 1;
});
db.handleRollback_(trans.id_);
mockControl.verify();
handler.expects().onSuccess();
db.handleCommit_(trans.id_);
mockControl.verify();
}
function testDoBegin() {
var db = buildDatabase();
var transactionId = 10;
var type = google.wspl.gears.DbWorker.CommandTypes.BEGIN;
wp.expects().sendMessage(TypeOf.isA(Object), workerId).andStub(function() {
assertEquals('wrong message type', type, arguments[0].type);
assertNotUndefined('Missing transaction id.', arguments[0].transactionId);
});
db.doBegin(transactionId);
mockControl.verify();
}
function testSynchronousBegin() {
var db = buildSyncDatabase();
dbMock.expects().execute('BEGIN IMMEDIATE');
db.doBegin(1);
mockControl.verify();
}
function testDoExecute() {
var statements = [{sql: 's1', params: 'p1'},
{sql: 's2', params: 'p2'},
{sql: 's3', params: 'p3'}];
var callbackId = 5;
var transactionId = 10;
var db = buildDatabase();
var type = google.wspl.gears.DbWorker.CommandTypes.EXECUTE;
wp.expects().sendMessage(TypeOf.isA(Object), workerId).andStub(function() {
assertEquals('wrong message type', type, arguments[0].type);
assertEquals('statements do not match', statements.length,
arguments[0].statements.length);
for (var i = 0; i < statements.length; i++) {
assertEquals('a statement sql does not match', statements[i].sql,
arguments[0].statements[i].sql);
assertEquals('a statement params does not match', statements[i].params,
arguments[0].statements[i].params);
}
assertEquals('missing callback id', callbackId, arguments[0].callbackId);
assertEquals('missing trans id', transactionId, arguments[0].transactionId);
});
db.doExecute(statements, callbackId, transactionId);
mockControl.verify();
}
function testSynchronousExecute() {
var statements = [{sql: 's1', params: 'p1'},
{sql: 's2', params: 'p2'},
{sql: 's3', params: 'p3'}];
var callbackId = 5;
var transactionId = 10;
var db = buildSyncDatabase();
for (var i = 0; i < 3; i++) {
var stat = statements[i];
dbMock.expects().execute(stat.sql, stat.params).andReturn('result' + i);
}
var handler = mockControl.createMock();
handler.addMockMethod('handleResult_');
db.handleResult_ = handler.handleResult_;
handler.expects().handleResult_(
TypeOf.isA(Array), callbackId, transactionId).andStub(function(results) {
var expected = ['result0', 'result1', 'result2'];
assertArrayEquals('Wrong results.', expected, results);
});
db.doExecute(statements, callbackId, transactionId);
mockControl.verify();
}
function testSynchronousExecute_failure() {
var statements = [{sql: 's1', params: 'p1'},
{sql: 's2', params: 'p2'},
{sql: 's3', params: 'p3'}];
var callbackId = 5;
var transactionId = 10;
var db = buildSyncDatabase();
dbMock.expects().execute('s1', 'p1').andReturn('result0');
dbMock.expects().execute('s2', 'p2').andThrow(Error('db error'));
var handler = mockControl.createMock();
handler.addMockMethod('handleFailure_');
db.handleFailure_ = handler.handleFailure_;
handler.expects().handleFailure_(
TypeOf.isA(Error), callbackId, transactionId);
db.doExecute(statements, callbackId, transactionId);
mockControl.verify();
}
function testDoCommit() {
var transactionId = 10;
var db = buildDatabase();
var type = google.wspl.gears.DbWorker.CommandTypes.COMMIT;
wp.expects().sendMessage(TypeOf.isA(Object), workerId).andStub(function() {
assertEquals('wrong message type', type, arguments[0].type);
assertEquals('missing trans id', transactionId, arguments[0].transactionId);
});
db.doCommit(transactionId);
mockControl.verify();
}
function testSynchronousCommit() {
var db = buildSyncDatabase();
dbMock.expects().execute('COMMIT');
db.doCommit(1);
mockControl.verify();
}
function testDoRollback() {
var transactionId = 10;
var db = buildDatabase();
var type = google.wspl.gears.DbWorker.CommandTypes.ROLLBACK;
wp.expects().sendMessage(TypeOf.isA(Object), workerId).andStub(function() {
assertEquals('wrong message type', type, arguments[0].type);
assertEquals('missing trans id', transactionId, arguments[0].transactionId);
});
db.doRollback(transactionId);
mockControl.verify();
}
function testSynchronousRollback() {
var db = buildSyncDatabase();
dbMock.expects().execute('ROLLBACK');
db.doRollback(1);
mockControl.verify();
}
function testHandleCommit() {
var db = buildDatabase();
db.transactions_[5] = {transaction: 'tx', callback: callbackMock};
callbackMock.expects().onSuccess();
db.handleCommit_(5);
assertUndefined('failed to remove transaction', db.transactions_[5]);
mockControl.verify();
}
function testHandleRollback() {
var db = buildDatabase();
db.transactions_[5] = {
transaction: 'tx',
callback: callbackMock,
error: Error('error')
};
callbackMock.expects().onFailure(TypeOf.isA(Error)).andStub(function() {
assertEquals('did not pass error', 'error', arguments[0].message);
});
db.handleRollback_(5);
assertUndefined('failed to remove transaction', db.transactions_[5]);
mockControl.verify();
}
function testHandleResult() {
var db = buildDatabase();
transMock.expects().success(TypeOf.isA(google.wspl.gears.ResultSet),
22).andStub(function() {
assertEquals('result not set', 'result1', arguments[0].resultArray_);
});
transMock.expects().success(TypeOf.isA(google.wspl.gears.ResultSet),
22).andStub(function() {
assertEquals('result not set', 'result2', arguments[0].resultArray_);
});
db.transactions_[5] = {transaction: transMock, callback: 'cb'};
db.handleResult_(['result1', 'result2'], 22, 5);
mockControl.verify();
}
function testHandleFailure() {
var db = buildDatabase();
transMock.expects().failure(TypeOf.isA(Error), 22).andStub(function() {
assertEquals('error not set', 'error', arguments[0].message);
});
db.transactions_[5] = {transaction: transMock, callback: 'cb'};
db.handleFailure_(Error('error'), 22, 5);
assertEquals('failed to save error', 'error',
db.transactions_[5].error.message);
mockControl.verify();
}
function testHandleStarted() {
var db = buildDatabase();
var type = google.wspl.gears.DbWorker.CommandTypes.OPEN;
wp.expects().sendMessage(TypeOf.isA(Object), workerId).andStub(function() {
assertEquals('wrong message type', type, arguments[0].type);
assertEquals('wrong db userId', 'userId', arguments[0].userId);
assertEquals('wrong db name', 'name', arguments[0].name);
});
db.handleStarted_();
mockControl.verify();
}
function testHandleStarted_afterTimeout() {
var db = buildDatabase();
db.handleTimeout_();
// This should do nothing.
db.handleStarted_();
mockControl.verify();
}
function testHandleTimeout() {
var db = buildDatabase();
db.handleTimeout_();
mockControl.verify();
}
function testHandleOpenSuccessful() {
var db = buildDatabase();
db.handleOpenSuccessful_();
assertTrue('Worker should be ready.', db.workerReady_);
mockControl.verify();
}
function testHandleOpenFailed() {
var db = buildDatabase();
db.handleOpenFailed_('error');
mockControl.verify();
}
</script>
</body>
</html>