Skip to content

Commit

Permalink
Add tests to reproduce bugs #109/#110/#111/#112 & verify fixes (missi…
Browse files Browse the repository at this point in the history
…ng openDatabase()/db.close()/deleteDatabase() callbacks; double db.executeSql() callbacks; deleteDatabase() not reliable); small changes to deleteAndConfirmDeleted() helper function for existing test of sqlitePlugin.deleteDatabase()
  • Loading branch information
Mark Oppenheim authored and Chris Brody committed Aug 20, 2014
1 parent 683179b commit 65caa9b
Showing 1 changed file with 358 additions and 9 deletions.
367 changes: 358 additions & 9 deletions test-www/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1046,16 +1046,16 @@
function deleteAndConfirmDeleted() {

window.sqlitePlugin.deleteDatabase("DB-Deletable", function () {

// check that the data's gone
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM test');
}, function (e) {
ok(true, 'got error like we expected');
testDeleteError();
},
function () {
ok(false, 'expected an error');
});
db.transaction(function (tx) {
tx.executeSql('SELECT name FROM test', []);
}, function (e) {
ok(true, 'got an expected transaction error');
testDeleteError();
}, function () {
ok(false, 'expected a transaction error');
});
}, function (e) {
ok(false, 'error: ' + e);
});
Expand All @@ -1074,6 +1074,355 @@
createAndInsertStuff();
});

test(suiteName + ' database.open calls its success callback', function () {
if (isWebSql) {
ok('skipped');
return;
}

// asynch test coming up
stop(1);

var dbName = "Database-Open-callback";
openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function (db) {
ok(true, 'expected close success callback to be called after database is closed');
start(1);
}, function (error) {
ok(false, 'expected close error callback not to be called after database is closed');
start(1);
});
});

test(suiteName + ' database.close calls its success callback', function () {
if (isWebSql) {
ok('skipped');
return;
}

// asynch test coming up
stop(1);

var dbName = "Database-Close-callback";
var db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE);

// close database - need to run tests directly in callbacks as nothing is guarenteed to be queued after a close
db.close(function () {
ok(true, 'expected close success callback to be called after database is closed');
start(1);
}, function (error) {
ok(false, 'expected close error callback not to be called after database is closed');
start(1);
});
});

test(suiteName + ' database.executeSql invokes call backs just once', function () {
if (isWebSql) {
ok('skipped - database.executeSql is not part of standard suite');
return;
}
var dbName = "Database-executsql-callback";
var db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE);

var successCbCount = 0;
var successArgumentCount = 0;
var successCb = function () {
successCbCount += 1;
successArgumentCount = arguments.length;
};
var errorCbCount = 0;
var errorArgumentCount = 0;
var errorCb = function (error) {
errorCbCount += 1;
errorArgumentCount = arguments.length;
};


// 3 asynch tests coming up
stop(2);

// test succesfull query
successCbCount = errorCbCount = 0;
db.executeSql('SELECT 1', [], successCb, errorCb);
// use a new transaction to guarentee tests are run after completion of previous transaction, regardless of when the callbacks are called
db.transaction(function (tx) {
tx.executeSql('SELECT 1', [], function () {
equal(successCbCount, 1, 'expected success callback to be called once for a succesful query');
equal(successArgumentCount, 1, 'expected success callback to be called with two arguments (query results)');
equal(errorCbCount, 0, 'expected error callback not to be called for a succesful query');
start(1);

// test succesfull query
successCbCount = 0;
errorCbCount = 0;
db.executeSql('I am not a query', [], successCb, errorCb);
// use a new transaction to guarentee tests are run after completion of previous transaction, regardless of when the callbacks are called
db.transaction(function (tx) {
tx.executeSql('SELECT 1', [], function () {
equal(successCbCount, 0, 'expected success callback not to be called once for a failed query');
equal(errorCbCount, 1, 'expected error callback to be called once for a failed query');
equal(errorArgumentCount, 1, 'expected error callback to be called with one argument (error details)');
start(1);

// Test a statement that will succeed where the transaction will fail.
// This example's so contrived/stupid that it doesn't even work with the WP8 implementation (but does on Android)...
// Alas the principle does still apply in more reasonable but hard to test circumstances, such as out of disk space during commit.
/*
successCbCount = 0;
errorCbCount = 0;
db.executeSql('COMMIT;', [], successCb, errorCb);
db.transaction(function (tx) {
tx.executeSql('SELECT 1', [], function () {
equal(successCbCount, 0, 'expected success callback not to be called for a valid query whose implicit transaction failes');
equal(errorCbCount, 1, 'expected error callback to be called once for a valid query whose implicit transaction failes');
equal(errorArgumentCount, 1, 'expected error callback to be called with one argument (error details)');
start(1);
})
}, function (err) {
ok(false, 'unexpected error: ' + err.message);
});
*/
})
}, function (err) {
ok(false, 'unexpected error: ' + err.message);
});
})
}, function (err) {
ok(false, 'unexpected error: ' + err.message);
});
});

test(suiteName + ' open same database twice works', function () {
if (isWebSql) {
ok('skipped'); // delete is non-standard
return;
}

stop(2);

var dbName = 'open-twice';

var db1 = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
var db2 = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
db1.readTransaction(function(tx1) {
tx1.executeSql('SELECT 1', [], function(tx1d, results) {
ok(true, 'db1 transaction working');
start(1);
}, function(error) {
ok(false, error);
});
}, function(error) {
ok(false, error);
});
db2.readTransaction(function(tx2) {
tx2.executeSql('SELECT 1', [], function(tx2d, results) {
ok(true, 'db2 transaction working');
start(1);
}, function(error) {
ok(false, error);
});
}, function(error) {
ok(false, error);
});
}, function (error) {
ok(false, error);
});
}, function (error) {
ok(false, error);
});
});

test(suiteName + ' close then re-open allows subsequent queries to run', function () {
if (isWebSql) {
ok('skipped'); // close is non-standard
return;
}

// asynch test coming up
stop(1);

var dbName = "Database-Close-and-Reopen";
var db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
db.close(function () {
db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
db.close(function () {
db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
db.readTransaction(function (tx) {
tx.executeSql('SELECT 1', [], function (tx, results) {
ok(true, 'database re-opened succesfully');
db.close();
start(1);
}, function (error) {
ok(false, error.message);
start(1);
});
}, function (error) {
ok(false, error.message);
start(1);
});
}, function (error) {
ok(false, error.message);
start(1);
});
}, function (error) {
ok(false, error.message);
start(1);
});
}, function (error) {
ok(false, error.message);
start(1);
});
}, function (error) {
ok(false, error.message);
start(1);
});
}, function (error) {
ok(false, error.message);
start(1);
});
});

test(suiteName + ' delete then re-open allows subsequent queries to run', function () {
if (isWebSql) {
ok('skipped'); // delete is non-standard
return;
}

// asynch test coming up
stop(1);

var dbName = "Database-delete-and-Reopen";
var db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
// success CB
window.sqlitePlugin.deleteDatabase(dbName, function () {
db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
db.readTransaction(function (tx) {
tx.executeSql('SELECT 1', [], function (tx, results) {
ok(true, 'database re-opened succesfully');
start(1);
}, function (error) {
ok(false, error);
start(1);
}, function (error) {
ok(false, error);
start(1);
});
}, function (error) {
ok(false, error);
start(1);
});
}, function (error) {
ok(false, error);
start(1);
});
}, function (error) {
ok(false, error);
start(1);
});
}, function (error) {
ok(false, error);
start(1);
});
});

test(suiteName + ' close, then delete then re-open allows subsequent queries to run', function () {
if (isWebSql) {
ok('skipped'); // close and delete are non-standard
return;
}

// asynch test coming up
stop(1);

var dbName = "Database-Close-delete-Reopen";
var db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE);
db.close(function () {
window.sqlitePlugin.deleteDatabase(dbName, function () {
db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE, function () {
db.readTransaction(function (tx) {
tx.executeSql('SELECT 1', [], function (tx, results) {
ok(true, 'database re-opened succesfully');
start(1);
}, function (e) {
ok(false, 'error: ' + e);
start(1);
});
}, function (e) {
ok(false, 'error: ' + e);
start(1);
});
}, function (e) {
ok(false, 'error: ' + e);
start(1);
});
}, function (e) {
ok(false, 'error: ' + e);
start(1);
});
}, function (e) {
ok(false, 'error: ' + e);
start(1);
});
});

// This kills the current android client, possibly due to incomplete implemention of closeDatabaseNow
test(suiteName + ' repeatedly open and delete database succeeds', function () {
if (isWebSql) {
ok('skipped'); // delete not in standard websql
return;
}

// asynch test coming up
stop(5);

var dbName = "repeatedly-open-and-delete";

var db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE);
window.sqlitePlugin.deleteDatabase(dbName, function () {

db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE);
window.sqlitePlugin.deleteDatabase(dbName, function () {

db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE);
window.sqlitePlugin.deleteDatabase(dbName, function () {

db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE);
window.sqlitePlugin.deleteDatabase(dbName, function () {

db = openDatabase(dbName, "1.0", "Demo", DEFAULT_SIZE);
window.sqlitePlugin.deleteDatabase(dbName, function () {
ok(true, 'success 5/5');

start(1);
}, function (error) {
ok(false, 'expected delete 5/5 error callback not to be called for an open database' + error);
start(1);
});

start(1);
}, function (error) {
ok(false, 'expected delete 4/5 error callback not to be called for an open database' + error);
start(1);
});

start(1);
}, function (error) {
ok(false, 'expected delete 3/5 error callback not to be called for an open database' + error);
start(1);
});

start(1);
}, function (error) {
ok(false, 'expected delete 2/5 error callback not to be called for an open database' + error);
start(1);
});

start(1);
}, function (error) {
ok(false, 'expected delete 1/5 error callback not to be called for an open database' + error);
start(5);
});
});

}
})();
</script>
Expand Down

0 comments on commit 65caa9b

Please # to comment.