michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "storage_test_harness.h" michael@0: michael@0: #include "mozStorageHelper.h" michael@0: michael@0: /** michael@0: * This file test our Transaction helper in mozStorageHelper.h. michael@0: */ michael@0: michael@0: void michael@0: test_HasTransaction() michael@0: { michael@0: nsCOMPtr db(getMemoryDatabase()); michael@0: michael@0: // First test that it holds the transaction after it should have gotten one. michael@0: { michael@0: mozStorageTransaction transaction(db, false); michael@0: do_check_true(transaction.HasTransaction()); michael@0: (void)transaction.Commit(); michael@0: // And that it does not have a transaction after we have committed. michael@0: do_check_false(transaction.HasTransaction()); michael@0: } michael@0: michael@0: // Check that no transaction is had after a rollback. michael@0: { michael@0: mozStorageTransaction transaction(db, false); michael@0: do_check_true(transaction.HasTransaction()); michael@0: (void)transaction.Rollback(); michael@0: do_check_false(transaction.HasTransaction()); michael@0: } michael@0: michael@0: // Check that we do not have a transaction if one is already obtained. michael@0: mozStorageTransaction outerTransaction(db, false); michael@0: do_check_true(outerTransaction.HasTransaction()); michael@0: { michael@0: mozStorageTransaction innerTransaction(db, false); michael@0: do_check_false(innerTransaction.HasTransaction()); michael@0: } michael@0: } michael@0: michael@0: void michael@0: test_Commit() michael@0: { michael@0: nsCOMPtr db(getMemoryDatabase()); michael@0: michael@0: // Create a table in a transaction, call Commit, and make sure that it does michael@0: // exists after the transaction falls out of scope. michael@0: { michael@0: mozStorageTransaction transaction(db, false); michael@0: (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( michael@0: "CREATE TABLE test (id INTEGER PRIMARY KEY)" michael@0: )); michael@0: (void)transaction.Commit(); michael@0: } michael@0: michael@0: bool exists = false; michael@0: (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists); michael@0: do_check_true(exists); michael@0: } michael@0: michael@0: void michael@0: test_Rollback() michael@0: { michael@0: nsCOMPtr db(getMemoryDatabase()); michael@0: michael@0: // Create a table in a transaction, call Rollback, and make sure that it does michael@0: // not exists after the transaction falls out of scope. michael@0: { michael@0: mozStorageTransaction transaction(db, true); michael@0: (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( michael@0: "CREATE TABLE test (id INTEGER PRIMARY KEY)" michael@0: )); michael@0: (void)transaction.Rollback(); michael@0: } michael@0: michael@0: bool exists = true; michael@0: (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists); michael@0: do_check_false(exists); michael@0: } michael@0: michael@0: void michael@0: test_AutoCommit() michael@0: { michael@0: nsCOMPtr db(getMemoryDatabase()); michael@0: michael@0: // Create a table in a transaction, and make sure that it exists after the michael@0: // transaction falls out of scope. This means the Commit was successful. michael@0: { michael@0: mozStorageTransaction transaction(db, true); michael@0: (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( michael@0: "CREATE TABLE test (id INTEGER PRIMARY KEY)" michael@0: )); michael@0: } michael@0: michael@0: bool exists = false; michael@0: (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists); michael@0: do_check_true(exists); michael@0: } michael@0: michael@0: void michael@0: test_AutoRollback() michael@0: { michael@0: nsCOMPtr db(getMemoryDatabase()); michael@0: michael@0: // Create a table in a transaction, and make sure that it does not exists michael@0: // after the transaction falls out of scope. This means the Rollback was michael@0: // successful. michael@0: { michael@0: mozStorageTransaction transaction(db, false); michael@0: (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( michael@0: "CREATE TABLE test (id INTEGER PRIMARY KEY)" michael@0: )); michael@0: } michael@0: michael@0: bool exists = true; michael@0: (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists); michael@0: do_check_false(exists); michael@0: } michael@0: michael@0: void michael@0: test_SetDefaultAction() michael@0: { michael@0: nsCOMPtr db(getMemoryDatabase()); michael@0: michael@0: // First we test that rollback happens when we first set it to automatically michael@0: // commit. michael@0: { michael@0: mozStorageTransaction transaction(db, true); michael@0: (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( michael@0: "CREATE TABLE test1 (id INTEGER PRIMARY KEY)" michael@0: )); michael@0: transaction.SetDefaultAction(false); michael@0: } michael@0: bool exists = true; michael@0: (void)db->TableExists(NS_LITERAL_CSTRING("test1"), &exists); michael@0: do_check_false(exists); michael@0: michael@0: // Now we do the opposite and test that a commit happens when we first set it michael@0: // to automatically rollback. michael@0: { michael@0: mozStorageTransaction transaction(db, false); michael@0: (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING( michael@0: "CREATE TABLE test2 (id INTEGER PRIMARY KEY)" michael@0: )); michael@0: transaction.SetDefaultAction(true); michael@0: } michael@0: exists = false; michael@0: (void)db->TableExists(NS_LITERAL_CSTRING("test2"), &exists); michael@0: do_check_true(exists); michael@0: } michael@0: michael@0: void michael@0: test_null_database_connection() michael@0: { michael@0: // We permit the use of the Transaction helper when passing a null database michael@0: // in, so we need to make sure this still works without crashing. michael@0: mozStorageTransaction transaction(nullptr, false); michael@0: michael@0: do_check_false(transaction.HasTransaction()); michael@0: do_check_true(NS_SUCCEEDED(transaction.Commit())); michael@0: do_check_true(NS_SUCCEEDED(transaction.Rollback())); michael@0: } michael@0: michael@0: void (*gTests[])(void) = { michael@0: test_HasTransaction, michael@0: test_Commit, michael@0: test_Rollback, michael@0: test_AutoCommit, michael@0: test_AutoRollback, michael@0: test_SetDefaultAction, michael@0: test_null_database_connection, michael@0: }; michael@0: michael@0: const char *file = __FILE__; michael@0: #define TEST_NAME "transaction helper" michael@0: #define TEST_FILE file michael@0: #include "storage_test_harness_tail.h"