storage/test/test_transaction_helper.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "storage_test_harness.h"
michael@0 8
michael@0 9 #include "mozStorageHelper.h"
michael@0 10
michael@0 11 /**
michael@0 12 * This file test our Transaction helper in mozStorageHelper.h.
michael@0 13 */
michael@0 14
michael@0 15 void
michael@0 16 test_HasTransaction()
michael@0 17 {
michael@0 18 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
michael@0 19
michael@0 20 // First test that it holds the transaction after it should have gotten one.
michael@0 21 {
michael@0 22 mozStorageTransaction transaction(db, false);
michael@0 23 do_check_true(transaction.HasTransaction());
michael@0 24 (void)transaction.Commit();
michael@0 25 // And that it does not have a transaction after we have committed.
michael@0 26 do_check_false(transaction.HasTransaction());
michael@0 27 }
michael@0 28
michael@0 29 // Check that no transaction is had after a rollback.
michael@0 30 {
michael@0 31 mozStorageTransaction transaction(db, false);
michael@0 32 do_check_true(transaction.HasTransaction());
michael@0 33 (void)transaction.Rollback();
michael@0 34 do_check_false(transaction.HasTransaction());
michael@0 35 }
michael@0 36
michael@0 37 // Check that we do not have a transaction if one is already obtained.
michael@0 38 mozStorageTransaction outerTransaction(db, false);
michael@0 39 do_check_true(outerTransaction.HasTransaction());
michael@0 40 {
michael@0 41 mozStorageTransaction innerTransaction(db, false);
michael@0 42 do_check_false(innerTransaction.HasTransaction());
michael@0 43 }
michael@0 44 }
michael@0 45
michael@0 46 void
michael@0 47 test_Commit()
michael@0 48 {
michael@0 49 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
michael@0 50
michael@0 51 // Create a table in a transaction, call Commit, and make sure that it does
michael@0 52 // exists after the transaction falls out of scope.
michael@0 53 {
michael@0 54 mozStorageTransaction transaction(db, false);
michael@0 55 (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
michael@0 56 "CREATE TABLE test (id INTEGER PRIMARY KEY)"
michael@0 57 ));
michael@0 58 (void)transaction.Commit();
michael@0 59 }
michael@0 60
michael@0 61 bool exists = false;
michael@0 62 (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
michael@0 63 do_check_true(exists);
michael@0 64 }
michael@0 65
michael@0 66 void
michael@0 67 test_Rollback()
michael@0 68 {
michael@0 69 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
michael@0 70
michael@0 71 // Create a table in a transaction, call Rollback, and make sure that it does
michael@0 72 // not exists after the transaction falls out of scope.
michael@0 73 {
michael@0 74 mozStorageTransaction transaction(db, true);
michael@0 75 (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
michael@0 76 "CREATE TABLE test (id INTEGER PRIMARY KEY)"
michael@0 77 ));
michael@0 78 (void)transaction.Rollback();
michael@0 79 }
michael@0 80
michael@0 81 bool exists = true;
michael@0 82 (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
michael@0 83 do_check_false(exists);
michael@0 84 }
michael@0 85
michael@0 86 void
michael@0 87 test_AutoCommit()
michael@0 88 {
michael@0 89 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
michael@0 90
michael@0 91 // Create a table in a transaction, and make sure that it exists after the
michael@0 92 // transaction falls out of scope. This means the Commit was successful.
michael@0 93 {
michael@0 94 mozStorageTransaction transaction(db, true);
michael@0 95 (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
michael@0 96 "CREATE TABLE test (id INTEGER PRIMARY KEY)"
michael@0 97 ));
michael@0 98 }
michael@0 99
michael@0 100 bool exists = false;
michael@0 101 (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
michael@0 102 do_check_true(exists);
michael@0 103 }
michael@0 104
michael@0 105 void
michael@0 106 test_AutoRollback()
michael@0 107 {
michael@0 108 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
michael@0 109
michael@0 110 // Create a table in a transaction, and make sure that it does not exists
michael@0 111 // after the transaction falls out of scope. This means the Rollback was
michael@0 112 // successful.
michael@0 113 {
michael@0 114 mozStorageTransaction transaction(db, false);
michael@0 115 (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
michael@0 116 "CREATE TABLE test (id INTEGER PRIMARY KEY)"
michael@0 117 ));
michael@0 118 }
michael@0 119
michael@0 120 bool exists = true;
michael@0 121 (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
michael@0 122 do_check_false(exists);
michael@0 123 }
michael@0 124
michael@0 125 void
michael@0 126 test_SetDefaultAction()
michael@0 127 {
michael@0 128 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
michael@0 129
michael@0 130 // First we test that rollback happens when we first set it to automatically
michael@0 131 // commit.
michael@0 132 {
michael@0 133 mozStorageTransaction transaction(db, true);
michael@0 134 (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
michael@0 135 "CREATE TABLE test1 (id INTEGER PRIMARY KEY)"
michael@0 136 ));
michael@0 137 transaction.SetDefaultAction(false);
michael@0 138 }
michael@0 139 bool exists = true;
michael@0 140 (void)db->TableExists(NS_LITERAL_CSTRING("test1"), &exists);
michael@0 141 do_check_false(exists);
michael@0 142
michael@0 143 // Now we do the opposite and test that a commit happens when we first set it
michael@0 144 // to automatically rollback.
michael@0 145 {
michael@0 146 mozStorageTransaction transaction(db, false);
michael@0 147 (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
michael@0 148 "CREATE TABLE test2 (id INTEGER PRIMARY KEY)"
michael@0 149 ));
michael@0 150 transaction.SetDefaultAction(true);
michael@0 151 }
michael@0 152 exists = false;
michael@0 153 (void)db->TableExists(NS_LITERAL_CSTRING("test2"), &exists);
michael@0 154 do_check_true(exists);
michael@0 155 }
michael@0 156
michael@0 157 void
michael@0 158 test_null_database_connection()
michael@0 159 {
michael@0 160 // We permit the use of the Transaction helper when passing a null database
michael@0 161 // in, so we need to make sure this still works without crashing.
michael@0 162 mozStorageTransaction transaction(nullptr, false);
michael@0 163
michael@0 164 do_check_false(transaction.HasTransaction());
michael@0 165 do_check_true(NS_SUCCEEDED(transaction.Commit()));
michael@0 166 do_check_true(NS_SUCCEEDED(transaction.Rollback()));
michael@0 167 }
michael@0 168
michael@0 169 void (*gTests[])(void) = {
michael@0 170 test_HasTransaction,
michael@0 171 test_Commit,
michael@0 172 test_Rollback,
michael@0 173 test_AutoCommit,
michael@0 174 test_AutoRollback,
michael@0 175 test_SetDefaultAction,
michael@0 176 test_null_database_connection,
michael@0 177 };
michael@0 178
michael@0 179 const char *file = __FILE__;
michael@0 180 #define TEST_NAME "transaction helper"
michael@0 181 #define TEST_FILE file
michael@0 182 #include "storage_test_harness_tail.h"

mercurial