storage/test/test_transaction_helper.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/test_transaction_helper.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,182 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "storage_test_harness.h"
    1.11 +
    1.12 +#include "mozStorageHelper.h"
    1.13 +
    1.14 +/**
    1.15 + * This file test our Transaction helper in mozStorageHelper.h.
    1.16 + */
    1.17 +
    1.18 +void
    1.19 +test_HasTransaction()
    1.20 +{
    1.21 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    1.22 +
    1.23 +  // First test that it holds the transaction after it should have gotten one.
    1.24 +  {
    1.25 +    mozStorageTransaction transaction(db, false);
    1.26 +    do_check_true(transaction.HasTransaction());
    1.27 +    (void)transaction.Commit();
    1.28 +    // And that it does not have a transaction after we have committed.
    1.29 +    do_check_false(transaction.HasTransaction());
    1.30 +  }
    1.31 +
    1.32 +  // Check that no transaction is had after a rollback.
    1.33 +  {
    1.34 +    mozStorageTransaction transaction(db, false);
    1.35 +    do_check_true(transaction.HasTransaction());
    1.36 +    (void)transaction.Rollback();
    1.37 +    do_check_false(transaction.HasTransaction());
    1.38 +  }
    1.39 +
    1.40 +  // Check that we do not have a transaction if one is already obtained.
    1.41 +  mozStorageTransaction outerTransaction(db, false);
    1.42 +  do_check_true(outerTransaction.HasTransaction());
    1.43 +  {
    1.44 +    mozStorageTransaction innerTransaction(db, false);
    1.45 +    do_check_false(innerTransaction.HasTransaction());
    1.46 +  }
    1.47 +}
    1.48 +
    1.49 +void
    1.50 +test_Commit()
    1.51 +{
    1.52 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    1.53 +
    1.54 +  // Create a table in a transaction, call Commit, and make sure that it does
    1.55 +  // exists after the transaction falls out of scope.
    1.56 +  {
    1.57 +    mozStorageTransaction transaction(db, false);
    1.58 +    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
    1.59 +      "CREATE TABLE test (id INTEGER PRIMARY KEY)"
    1.60 +    ));
    1.61 +    (void)transaction.Commit();
    1.62 +  }
    1.63 +
    1.64 +  bool exists = false;
    1.65 +  (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
    1.66 +  do_check_true(exists);
    1.67 +}
    1.68 +
    1.69 +void
    1.70 +test_Rollback()
    1.71 +{
    1.72 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    1.73 +
    1.74 +  // Create a table in a transaction, call Rollback, and make sure that it does
    1.75 +  // not exists after the transaction falls out of scope.
    1.76 +  {
    1.77 +    mozStorageTransaction transaction(db, true);
    1.78 +    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
    1.79 +      "CREATE TABLE test (id INTEGER PRIMARY KEY)"
    1.80 +    ));
    1.81 +    (void)transaction.Rollback();
    1.82 +  }
    1.83 +
    1.84 +  bool exists = true;
    1.85 +  (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
    1.86 +  do_check_false(exists);
    1.87 +}
    1.88 +
    1.89 +void
    1.90 +test_AutoCommit()
    1.91 +{
    1.92 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    1.93 +
    1.94 +  // Create a table in a transaction, and make sure that it exists after the
    1.95 +  // transaction falls out of scope.  This means the Commit was successful.
    1.96 +  {
    1.97 +    mozStorageTransaction transaction(db, true);
    1.98 +    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
    1.99 +      "CREATE TABLE test (id INTEGER PRIMARY KEY)"
   1.100 +    ));
   1.101 +  }
   1.102 +
   1.103 +  bool exists = false;
   1.104 +  (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
   1.105 +  do_check_true(exists);
   1.106 +}
   1.107 +
   1.108 +void
   1.109 +test_AutoRollback()
   1.110 +{
   1.111 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
   1.112 +
   1.113 +  // Create a table in a transaction, and make sure that it does not exists
   1.114 +  // after the transaction falls out of scope.  This means the Rollback was
   1.115 +  // successful.
   1.116 +  {
   1.117 +    mozStorageTransaction transaction(db, false);
   1.118 +    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
   1.119 +      "CREATE TABLE test (id INTEGER PRIMARY KEY)"
   1.120 +    ));
   1.121 +  }
   1.122 +
   1.123 +  bool exists = true;
   1.124 +  (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
   1.125 +  do_check_false(exists);
   1.126 +}
   1.127 +
   1.128 +void
   1.129 +test_SetDefaultAction()
   1.130 +{
   1.131 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
   1.132 +
   1.133 +  // First we test that rollback happens when we first set it to automatically
   1.134 +  // commit.
   1.135 +  {
   1.136 +    mozStorageTransaction transaction(db, true);
   1.137 +    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
   1.138 +      "CREATE TABLE test1 (id INTEGER PRIMARY KEY)"
   1.139 +    ));
   1.140 +    transaction.SetDefaultAction(false);
   1.141 +  }
   1.142 +  bool exists = true;
   1.143 +  (void)db->TableExists(NS_LITERAL_CSTRING("test1"), &exists);
   1.144 +  do_check_false(exists);
   1.145 +
   1.146 +  // Now we do the opposite and test that a commit happens when we first set it
   1.147 +  // to automatically rollback.
   1.148 +  {
   1.149 +    mozStorageTransaction transaction(db, false);
   1.150 +    (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
   1.151 +      "CREATE TABLE test2 (id INTEGER PRIMARY KEY)"
   1.152 +    ));
   1.153 +    transaction.SetDefaultAction(true);
   1.154 +  }
   1.155 +  exists = false;
   1.156 +  (void)db->TableExists(NS_LITERAL_CSTRING("test2"), &exists);
   1.157 +  do_check_true(exists);
   1.158 +}
   1.159 +
   1.160 +void
   1.161 +test_null_database_connection()
   1.162 +{
   1.163 +  // We permit the use of the Transaction helper when passing a null database
   1.164 +  // in, so we need to make sure this still works without crashing.
   1.165 +  mozStorageTransaction transaction(nullptr, false);
   1.166 +
   1.167 +  do_check_false(transaction.HasTransaction());
   1.168 +  do_check_true(NS_SUCCEEDED(transaction.Commit()));
   1.169 +  do_check_true(NS_SUCCEEDED(transaction.Rollback()));
   1.170 +}
   1.171 +
   1.172 +void (*gTests[])(void) = {
   1.173 +  test_HasTransaction,
   1.174 +  test_Commit,
   1.175 +  test_Rollback,
   1.176 +  test_AutoCommit,
   1.177 +  test_AutoRollback,
   1.178 +  test_SetDefaultAction,
   1.179 +  test_null_database_connection,
   1.180 +};
   1.181 +
   1.182 +const char *file = __FILE__;
   1.183 +#define TEST_NAME "transaction helper"
   1.184 +#define TEST_FILE file
   1.185 +#include "storage_test_harness_tail.h"

mercurial