1 var dbName = "SQLExceptionCodesTest" + (new Date()).getTime();
3 function testTransaction(db, executeStatementsCallback, expectedError)
5 db.transaction(function(tx) {
7 executeStatementsCallback(tx);
8 postMessage("FAIL: an exception (" + expectedError + ") should've been thrown.");
10 postMessage("LOG: " + err);
11 if (typeof err == "string" && err == expectedError)
12 postMessage("PASS: expected and got error with message " + expectedError);
13 else if (err.name == expectedError)
14 postMessage("PASS: expected and got error name " + expectedError);
16 postMessage("FAIL: expected error name " + expectedError + ", got " + err);
21 function testTransactionThrowsException(db)
23 testTransaction(db, function(tx) { throw "Exception thrown in transaction callback."; }, "Exception thrown in transaction callback.");
26 function testInvalidStatement(db)
28 testTransaction(db, function(tx) { tx.executeSql("BAD STATEMENT"); }, "SyntaxError");
31 function testIncorrectNumberOfBindParameters(db)
35 tx.executeSql("CREATE TABLE IF NOT EXISTS BadBindNumberTest (Foo INT, Bar INT)");
36 tx.executeSql("INSERT INTO BadBindNumberTest VALUES (?, ?)", [1]);
40 function testBindParameterOfWrongType(db)
43 badString.toString = function() { throw "Cannot call toString() on this object." };
45 testTransaction(db, function(tx) {
46 tx.executeSql("CREATE TABLE IF NOT EXISTS BadBindTypeTest (Foo TEXT)");
47 tx.executeSql("INSERT INTO BadBindTypeTest VALUES (?)", [badString]);
48 }, "Cannot call toString() on this object.");
51 function testQuotaExceeded(db)
53 // Sometimes, SQLite automatically rolls back a transaction if executing a statement fails.
54 // This seems to be one of those cases.
58 tx.executeSql("CREATE TABLE IF NOT EXISTS QuotaTest (Foo BLOB)");
59 tx.executeSql("INSERT INTO QuotaTest VALUES (ZEROBLOB(10 * 1024 * 1024))");
60 }, "QuotaExceededError");
61 postMessage("FAIL: Transaction should've been rolled back by SQLite.");
63 if (err.name == "DatabaseError")
64 postMessage("PASS: Transaction was rolled back by SQLite as expected.");
66 postMessage("FAIL: An unexpected exception was thrown: " + err);
70 function testVersionMismatch(db)
72 var db2 = openDatabaseSync(dbName, "1.0", "Tests the error codes.", 1);
73 db2.changeVersion("1.0", "2.0", function(tx) { });
76 tx.executeSql("THIS STATEMENT SHOULD NEVER GET EXECUTED");
80 var db = openDatabaseSync(dbName, "1.0", "Tests the exception codes.", 1);
81 testTransactionThrowsException(db);
82 testInvalidStatement(db);
83 testIncorrectNumberOfBindParameters(db);
84 testBindParameterOfWrongType(db);
85 testQuotaExceeded(db);
86 testVersionMismatch(db);