aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/gears/database_test.html
blob: 120c0312ede894feae23ef636e0bca17e62fd11e (plain)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
<!DOCTYPE html>
<html>
<!--
Copyright 2007 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.gears.Database</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.gears.Database');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

function isGearsAllowed() {
  return goog.gears.hasFactory() && goog.gears.getFactory().hasPermission;
}

var database;

function setUpPage() {
  if (isGearsAllowed()) {
    try {
      database = new goog.gears.Database('tester', 'database-test');
    } catch (e) {
      debug('Could not create the database');
    }
  }
  setUpPageStatus = 'complete'; // jsunit magic
}

function setUp() {
  if (!database) {
    return;
  }
  database.execute('DROP TABLE IF EXISTS Test');
  database.execute('CREATE TABLE IF NOT EXISTS Test (x TEXT, y TEXT)');

  database.execute('INSERT INTO Test VALUES (?, ?)', 'a', 'alpha');
  database.execute('INSERT INTO Test VALUES (?, ?)', 'b', 'beta');
  database.execute('INSERT INTO Test VALUES (?, ?)', 'g', 'gamma');
}

function tearDown() {
  if (database) {
    goog.events.removeAll(database);

    // Rollback to clean up transaction state.
    while (database.isInTransaction()) {
      database.rollback();
    }
  }
}

function testCreateDatabase() {
  if (!database) {
    return;
  }
  assertNotNull('Could not create the database', database);
}

function testCreateTable() {
  if (!database) {
    return;
  }

  var created = database.queryValue('SELECT 1 FROM SQLITE_MASTER ' +
      'WHERE TYPE=? AND NAME=?',
      'table',
      'Test') != null;
  assertTrue('Test table could not be created', created);
  assertEquals('Wrong number of rows after more insertions', 3,
               database.queryValue('SELECT COUNT(*) FROM Test'));
}

function testQueryValue() {
  if (!database) {
    return;
  }
  // testing retrieving a single value
  var val = database.queryValue('SELECT y FROM Test WHERE x=?', 'b');
  assertEquals('Wrong value returned by query', 'beta', val);

  // testing a query that returns nothing
  val = database.queryValue('SELECT y FROM Test WHERE x=?', 'd');
  assertNull('Should not have returned a value for the query', val);

  // testing that the query returns the first row of the query
  val = database.queryValue('SELECT y FROM Test');
  assertEquals('Wrong value returned by query', 'alpha', val);

  // testing that the query returns the first element of the row of the query
  val = database.queryValue('SELECT * FROM Test');
  assertEquals('Wrong value returned by query', 'a', val);

  // And the same with an array.

  // testing retrieving a single value
  val = database.queryValue('SELECT y FROM Test WHERE x=?', ['b']);
  assertEquals('Wrong value returned by query', 'beta', val);

  // testing a query that returns nothing
  val = database.queryValue('SELECT y FROM Test WHERE x=?', ['d']);
  assertNull('Should not have returned a value for the query', val);
}

function testQueryObject() {
  if (!database) {
    return;
  }
  // testing that the query returns the correct object
  var obj = database.queryObject('SELECT * FROM Test WHERE x=?', 'b');
  assertNotNull('Should have returned a value for the query', obj);
  assertEquals('Wrong value returned by query', 'b', obj['x']);
  assertEquals('Wrong value returned by query', 'beta', obj['y']);

  // testing that the query returns null when there is no match
  obj = database.queryValue('SELECT * FROM Test WHERE x=?', 'd');
  assertNull('Should not have returned a value for the query', obj);

  // testing that the query returns the first row when there are multiple
  // rows returned.
  obj = database.queryObject('SELECT * FROM Test');
  assertNotNull('Should have returned a value for the query', obj);
  assertEquals('Wrong value returned by query', 'a', obj['x']);
  assertEquals('Wrong value returned by query', 'alpha', obj['y']);

  // And the same with an array.

  // testing that the query returns the correct object
  obj = database.queryObject('SELECT * FROM Test WHERE x=?', ['b']);
  assertNotNull('Should have returned a value for the query', obj);
  assertEquals('Wrong value returned by query', 'b', obj['x']);
  assertEquals('Wrong value returned by query', 'beta', obj['y']);

  // testing that the query returns null when there is no match
  obj = database.queryValue('SELECT * FROM Test WHERE x=?', ['d']);
  assertNull('Should not have returned a value for the query', obj);
}

function testQueryValueArray() {
  if (!database) {
    return;
  }
  // testing selecting returning of multiple single values
  var arr = database.queryValueArray('SELECT y FROM Test');
  assertNotNull('Should have returned a value for the query', arr);
  assertEquals('Wrong value returned by query', 3, arr.length);
  assertEquals('Wrong value returned by query', 'alpha', arr[0]);
  assertEquals('Wrong value returned by query', 'beta', arr[1]);
  assertEquals('Wrong value returned by query', 'gamma', arr[2]);

  // testing selecting returning of multiple single values when there
  // are no matches.
  arr = database.queryValueArray('SELECT y FROM Test WHERE x=?', 'd');
  assertNotNull('Should have returned a value for the query', arr);
  assertEquals('Wrong value returned by query', 0, arr.length);

  // testing selecting returning of multiple single values even when
  // selecting multiple columns - should return the first column
  arr = database.queryValueArray('SELECT * FROM Test');
  assertNotNull('Should have returned a value for the query', arr);
  assertEquals('Wrong value returned by query', 3, arr.length);
  assertEquals('Wrong value returned by query', 'a', arr[0]);
  assertEquals('Wrong value returned by query', 'b', arr[1]);
  assertEquals('Wrong value returned by query', 'g', arr[2]);

  // And the same with an array.

  // testing selecting returning of multiple single values when there
  // are no matches.
  arr = database.queryValueArray('SELECT y FROM Test WHERE x=?', ['d']);
  assertNotNull('Should have returned a value for the query', arr);
  assertEquals('Wrong value returned by query', 0, arr.length);

}

function testQueryObjectArray() {
  if (!database) {
    return;
  }
  // testing selecting returning of array of objects
  var arr = database.queryObjectArray('SELECT * FROM Test');
  assertNotNull('Should have returned a value for the query', arr);
  assertEquals('Wrong value returned by query', 3, arr.length);
  assertEquals('Wrong value returned by query', 'a', arr[0]['x']);
  assertEquals('Wrong value returned by query', 'alpha', arr[0]['y']);
  assertEquals('Wrong value returned by query', 'b', arr[1]['x']);
  assertEquals('Wrong value returned by query', 'beta', arr[1]['y']);
  assertEquals('Wrong value returned by query', 'g', arr[2]['x']);
  assertEquals('Wrong value returned by query', 'gamma', arr[2]['y']);

  // testing selecting returning of multiple objects when there
  // are no matches.
  arr = database.queryObjectArray('SELECT y FROM Test WHERE x=?', 'd');
  assertNotNull('Should have returned a value for the query', arr);
  assertEquals('Wrong value returned by query', 0, arr.length);

  // And the same with an array.

  // testing selecting returning of multiple objects when there
  // are no matches.
  arr = database.queryObjectArray('SELECT y FROM Test WHERE x=?', ['d']);
  assertNotNull('Should have returned a value for the query', arr);
  assertEquals('Wrong value returned by query', 0, arr.length);

}

function testQueryToArrays() {
  if (!database) {
    return;
  }
  // testing selecting returning of array of arrays
  var arr = database.queryArrays('SELECT * FROM Test');
  assertNotNull('Should have returned a value for the query', arr);
  assertEquals('Wrong value returned by query', 3, arr.length);
  assertEquals('Wrong value returned by query', 'a', arr[0][0]);
  assertEquals('Wrong value returned by query', 'alpha', arr[0][1]);
  assertEquals('Wrong value returned by query', 'b', arr[1][0]);
  assertEquals('Wrong value returned by query', 'beta', arr[1][1]);
  assertEquals('Wrong value returned by query', 'g', arr[2][0]);
  assertEquals('Wrong value returned by query', 'gamma', arr[2][1]);

  arr = database.queryArrays('SELECT * FROM Test WHERE x=?', 'd');
  assertNotNull('Should have returned a value for the query', arr);
  assertEquals('Wrong value returned by query', 0, arr.length);

  // And the same with an array.

  arr = database.queryArrays('SELECT * FROM Test WHERE x=?', ['d']);
  assertNotNull('Should have returned a value for the query', arr);
  assertEquals('Wrong value returned by query', 0, arr.length);
}

function testForEachValue() {
  if (!database) {
    return;
  }
  // testing the foreach value when there are matching values
  var vals = [];
  database.forEachValue('SELECT * FROM Test',
                        function(val, row, col, name) {
                          vals.push({val: val, row: row, col: col, name: name});
                          return true;
                        },
                        null);
  assertEquals('Wrong value returned by query', 6, vals.length);
  assertEquals('Wrong value returned by query', 'a', vals[0].val);
  assertEquals('Wrong value returned by query', 0, vals[0].row);
  assertEquals('Wrong value returned by query', 0, vals[0].col);
  assertEquals('Wrong value returned by query', 'x', vals[0].name);
  assertEquals('Wrong value returned by query', 'alpha', vals[1].val);
  assertEquals('Wrong value returned by query', 0, vals[1].row);
  assertEquals('Wrong value returned by query', 1, vals[1].col);
  assertEquals('Wrong value returned by query', 'y', vals[1].name);
  assertEquals('Wrong value returned by query', 'b', vals[2].val);
  assertEquals('Wrong value returned by query', 1, vals[2].row);
  assertEquals('Wrong value returned by query', 0, vals[2].col);
  assertEquals('Wrong value returned by query', 'x', vals[2].name);
  assertEquals('Wrong value returned by query', 'beta', vals[3].val);
  assertEquals('Wrong value returned by query', 1, vals[3].row);
  assertEquals('Wrong value returned by query', 1, vals[3].col);
  assertEquals('Wrong value returned by query', 'y', vals[3].name);
  assertEquals('Wrong value returned by query', 'g', vals[4].val);
  assertEquals('Wrong value returned by query', 2, vals[4].row);
  assertEquals('Wrong value returned by query', 0, vals[4].col);
  assertEquals('Wrong value returned by query', 'x', vals[4].name);
  assertEquals('Wrong value returned by query', 'gamma', vals[5].val);
  assertEquals('Wrong value returned by query', 2, vals[5].row);
  assertEquals('Wrong value returned by query', 1, vals[5].col);
  assertEquals('Wrong value returned by query', 'y', vals[5].name);

  vals = [];
  database.forEachValue('SELECT * FROM Test WHERE x=?',
                        function(val, row, col, name) {
                          vals.push({val: val, row: row, col: col, name: name});
                        },
                        null,
                        'd');
  assertEquals('Wrong value returned by query', 0, vals.length);

  // And the same with an array.

  vals = [];
  database.forEachValue('SELECT * FROM Test WHERE x=?',
                        function(val, row, col, name) {
                          vals.push({val: val, row: row, col: col, name: name});
                        },
                        null,
                        ['d']);
  assertEquals('Wrong value returned by query', 0, vals.length);

}

function testForEachRow() {
  if (!database) {
    return;
  }
  var vals = [];
  database.forEachRow('SELECT * FROM Test',
                      function(arr, row) {
                        vals.push({arr: arr, row: row});
                        return true;
                      },
                      null);
  assertEquals('Wrong value returned by query', 3, vals.length);
  assertEquals('Wrong value returned by query', 'a', vals[0].arr[0]);
  assertEquals('Wrong value returned by query', 'alpha', vals[0].arr[1]);
  assertEquals('Wrong value returned by query', 0, vals[0].row);
  assertEquals('Wrong value returned by query', 'b', vals[1].arr[0]);
  assertEquals('Wrong value returned by query', 'beta', vals[1].arr[1]);
  assertEquals('Wrong value returned by query', 1, vals[1].row);
  assertEquals('Wrong value returned by query', 'g', vals[2].arr[0]);
  assertEquals('Wrong value returned by query', 'gamma', vals[2].arr[1]);
  assertEquals('Wrong value returned by query', 2, vals[2].row);

  vals = [];
  database.forEachRow('SELECT * FROM Test WHERE x=?',
                      function(arr, row) {
                        vals.push({arr: arr, row: row});
                        return true;
                      },
                      null,
                      'd');
  assertEquals('Wrong value returned by query', 0, vals.length);

  // And the same with an array.

  vals = [];
  database.forEachRow('SELECT * FROM Test WHERE x=?',
                      function(arr, row) {
                        vals.push({arr: arr, row: row});
                        return true;
                      },
                      null,
                      ['d']);
  assertEquals('Wrong value returned by query', 0, vals.length);

}

function testMultipleBegins() {
  if (!database) {
    return;
  }
  var db1;
  var db2;
  try {
    db1 = new goog.gears.Database('tester', 'database-test');
    db2 = new goog.gears.Database('tester', 'database-test');
  } catch (e) {
    fail('Could not create the databases');
  }

  db1.begin();
  try {
    // This should time out as being locked.
    db2.begin();
    fail('Should not have been able to get past second begin transaction.');
  } catch (e) {
    assertFalse(
        'Second begin should have failed, but claims to be in a transaction.',
        db2.isInTransaction());
    assertTrue(
        'Second begin not reported as being locked',
        goog.gears.Database.isLockedException(e));
  }
  db1.commit();
  db2.begin();
  assertTrue('New second begin failed', db2.isInTransaction());
  db2.commit();
};


function testIsLockedExceptionMessage() {
  if (!database) {
    return;
  }

  var lockedExStr = 'Database operation failed. ERROR: database is locked ' +
      'DETAILS: database is locked';

  assertTrue(goog.gears.Database.isLockedException(lockedExStr));
  assertFalse(goog.gears.Database.isLockedException(
      'Database operation failed. ERROR: invalid SQL operation'));

  assertTrue(goog.gears.Database.isLockedException(Error(lockedExStr)));
}

function testNestedBegins() {
  if (!database) {
    return;
  }

  // Try normal nested begins.
  database.begin();
  try {
    database.begin();
    database.commit();
    database.commit();
  } catch (e) {
    fail('Could not run nested transactions');
  }

  // Now try nested begins with lower begin levels
  database.begin();
  try {
    database.beginDeferred();
    database.commit();
    database.commit();
  } catch (e) {
    fail('Could not run nested transactions');
  }

  // Now try begins with increasing begin levels. This should throw
  // an error.
  database.beginDeferred();
  try {
    database.beginImmediate();
    fail('Could not run nested transactions');
    database.commit();
    database.commit();
  } catch (e) {
    database.rollback(e);
  }
}


function testCommit() {
  if (!database) {
    return;
  }
  var db;
  try {
    db = new goog.gears.Database('tester', 'database-test');
  } catch (e) {
    debug('Could not create the database');
  }

  var committed = false;
  var rollbacked = false;

  database.begin();
  goog.events.listen(database, 'commit', function(e) {committed = true;});
  goog.events.listen(database, 'rollback', function(e) {rollbacked = true;});
  assertTrue('Incorrect reporting of being in a transaction',
             database.isInTransaction());
  database.execute('INSERT INTO Test VALUES(?, ?)', 'o', 'omega');
  var cnt = database.queryValue('SELECT COUNT(*) FROM Test');
  assertEquals('Found wrong number of rows', 4, cnt);
  cnt = db.queryValue('SELECT COUNT(*) FROM Test');
  assertEquals('Found wrong number of rows', 3, cnt);
  database.commit();
  cnt = db.queryValue('SELECT COUNT(*) FROM Test');
  assertEquals('Found wrong number of rows', 4, cnt);

  assertFalse('Incorrect reporting of being in a transaction',
              database.isInTransaction());

  assertTrue('Commit event not dispatched', committed);
  assertFalse('Rollback event wrongly dispatched', rollbacked);
}

function testCommit2() {
  if (!database) {
    return;
  }
  var db;
  try {
    db = new goog.gears.Database('tester', 'database-test');
  } catch (e) {
    debug('Could not create the database');
  }

  var committed = false;
  var rollbacked = false;

  database.begin();
  goog.events.listen(database, 'commit', function(e) {committed = true;});
  goog.events.listen(database, 'rollback', function(e) {rollbacked = true;});
  database.execute('INSERT INTO Test VALUES(?, ?)', 's', 'sigma');
  assertTrue('Incorrect reporting of being in a transaction',
             database.isInTransaction());
  database.begin();
  assertTrue('Incorrect reporting of being in a transaction',
             database.isInTransaction());
  database.execute('INSERT INTO Test VALUES(?, ?)', 's', 'sigma');
  database.commit();

  assertFalse('Commit event wrongly dispatched', committed);
  assertFalse('Rollback event wrongly dispatched', rollbacked);

  assertTrue('Incorrect reporting of being in a transaction',
             database.isInTransaction());
  // the values should not be committed at this point.
  cnt = database.queryValue('SELECT COUNT(*) FROM Test');
  assertEquals('Found wrong number of rows', 5, cnt);
  cnt = db.queryValue('SELECT COUNT(*) FROM Test');
  assertEquals('Found wrong number of rows', 3, cnt);
  database.commit();
  cnt = db.queryValue('SELECT COUNT(*) FROM Test');
  assertEquals('Found wrong number of rows', 5, cnt);
  assertFalse('Incorrect reporting of being in a transaction',
              database.isInTransaction());
  assertTrue('Commit function not called', committed);
  assertFalse('Rollback function wrongly called', rollbacked);
}

function testRollback() {
  if (!database) {
    return;
  }

  var begun = false;
  var committed = false;
  var rollbacked = false;

  goog.events.listen(database, 'begin', function(e) {begun = true;});
  database.begin();
  assertTrue('Begin event not dispatched', begun);
  goog.events.listen(database, 'commit', function(e) {committed = true;});
  goog.events.listen(database, 'rollback', function(e) {rollbacked = true;});
  database.execute('INSERT INTO Test VALUES(?, ?)', 'o', 'omega');
  var cnt = database.queryValue('SELECT COUNT(*) FROM Test');
  assertEquals('Found wrong number of rows', 4, cnt);
  database.rollback();
  cnt = database.queryValue('SELECT COUNT(*) FROM Test');
  assertEquals('Found wrong number of rows', 3, cnt);
  assertFalse('Incorrect reporting of being in a transaction',
              database.isInTransaction());
  assertFalse('Commit event wrongly dispatched', committed);
  assertTrue('Rollback event not dispatched', rollbacked);
}

function testRollback2() {
  if (!database) {
    return;
  }

  var begun = false;
  var committed = false;
  var rollbacked = false;

  database.begin();
  goog.events.listen(database, 'commit', function(e) {committed = true;});
  goog.events.listen(database, 'rollback', function(e) {rollbacked = true;});
  database.execute('INSERT INTO Test VALUES(?, ?)', 's', 'sigma');
  database.begin();
  database.execute('INSERT INTO Test VALUES(?, ?)', 's', 'sigma');
  database.rollback();
  // the values should not be rolledback at this point.
  cnt = database.queryValue('SELECT COUNT(*) FROM Test');
  assertEquals('Found wrong number of rows', 5, cnt);
  assertFalse('Commit event wrongly dispatched', committed);
  assertFalse('Rollback event wrongly dispatched', rollbacked);

  database.rollback();
  cnt = database.queryValue('SELECT COUNT(*) FROM Test');
  assertEquals('Found wrong number of rows', 3, cnt);
  assertFalse('Incorrect reporting of being in a transaction',
              database.isInTransaction());
  assertFalse('Commit event wrongly dispatched', committed);
  assertTrue('Rollback event not dispatched', rollbacked);
}

function testPreventDefault() {
  if (!database) {
    return;
  }

  var committed = false;
  database.begin();
  goog.events.listen(database, 'beforecommit', function(e) {
    e.preventDefault();
  });
  goog.events.listen(database, 'commit', function(e) {committed = true;});
  database.execute('INSERT INTO Test VALUES(?, ?)', 'd', 'delta');
  database.commit();
  assertFalse('Commit ignored beforecommit preventDefault()', committed);
  database.rollback();
}

function testPreventDefault2() {
  if (!database) {
    return;
  }

  var rollbacked = false;
  database.begin();
  goog.events.listen(database, 'beforerollback', function(e) {
    e.preventDefault();
  });
  goog.events.listen(database, 'rollback', function(e) {
    rollbacked = true;
  });
  database.execute('INSERT INTO Test VALUES(?, ?)', 'd', 'delta');
  database.rollback();
  assertFalse('Rollback ignored beforerollback preventDefault()', rollbacked);
  goog.events.removeAll(database);
  database.rollback();
}

function testLastInsertId() {
  if (!database) {
    return;
  }
  database.execute('INSERT INTO Test VALUES (?, ?)', 'a', 'alpha');
  var id = database.getLastInsertRowId();
  database.execute('INSERT INTO Test VALUES (?, ?)', 'b', 'beta');
  assertEquals('Incorrect last insert id',
               id + 1, database.getLastInsertRowId());
}

function testError() {
  if (!database) {
    return;
  }

  var sql = 'INSERT INTO Unknown VALUES(?, ?)';

  try {
    database.execute('INSERT INTO Unknown VALUES(?, ?)', 'alpha', 1);
    fail('Should not have gotten here');
  } catch (e) {
    assertEquals('Wrong sql information', sql + ': ["alpha",1]', e.message0);
  }
}

function testThrowInBeforeSubmit() {
  if (!database) {
    return;
  }

  var errorSentinel = Error();

  var committed = false;
  database.begin();
  goog.events.listen(database, 'beforecommit', function(e) {
    throw errorSentinel;
  });
  goog.events.listen(database, 'commit', function(e) {
    committed = true;
  });
  database.execute('INSERT INTO Test VALUES(?, ?)', 'd', 'delta');
  try {
    database.commit();
    fail('Should have thrown an error');
  } catch (ex) {
    assertEquals(errorSentinel, ex);
    assertTrue('Should still be in the transaction',
               database.isInTransaction());
  }

  assertFalse('Should not have been commited since we threw an error ' +
              'in beforecommit', committed);
  database.rollback();
}

function testThrowInBeforeRollback() {
  if (!database) {
    return;
  }

  var errorSentinel = Error();

  var rollbacked = false;
  database.begin();
  goog.events.listen(database, 'beforerollback', function(e) {
    throw errorSentinel;
  });
  goog.events.listen(database, 'rollback', function(e) {
    rollbacked = true;
  });
  database.execute('INSERT INTO Test VALUES(?, ?)', 'd', 'delta');

  try {
    database.rollback();
    fail('Should have thrown an error');
  } catch (ex) {
    assertEquals(errorSentinel, ex);
    assertTrue('Should still be in the transaction',
               database.isInTransaction());
  }
  assertFalse('Should not have been rolled back since we threw an error ' +
              'in beforerollback', rollbacked);
  goog.events.removeAll(database);
  database.rollback();
}

</script>
</body>
</html>