LCOV - code coverage report
Current view: directory - storage/test - test_transaction_helper.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 76 76 100.0 %
Date: 2012-06-02 Functions: 7 7 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
       2                 :  * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
       3                 :  * ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is storage test code.
      17                 :  *
      18                 :  * The Initial Developer of the Original Code is
      19                 :  * Mozilla Corporation.
      20                 :  * Portions created by the Initial Developer are Copyright (C) 2009
      21                 :  * the Initial Developer. All Rights Reserved.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *   Shawn Wilsher <me@shawnwilsher.com> (Original Author)
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #include "storage_test_harness.h"
      41                 : 
      42                 : #include "mozStorageHelper.h"
      43                 : 
      44                 : /**
      45                 :  * This file test our Transaction helper in mozStorageHelper.h.
      46                 :  */
      47                 : 
      48                 : void
      49               1 : test_HasTransaction()
      50                 : {
      51               2 :   nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
      52                 : 
      53                 :   // First test that it holds the transaction after it should have gotten one.
      54                 :   {
      55               2 :     mozStorageTransaction transaction(db, false);
      56               1 :     do_check_true(transaction.HasTransaction());
      57               1 :     (void)transaction.Commit();
      58                 :     // And that it does not have a transaction after we have committed.
      59               1 :     do_check_false(transaction.HasTransaction());
      60                 :   }
      61                 : 
      62                 :   // Check that no transaction is had after a rollback.
      63                 :   {
      64               2 :     mozStorageTransaction transaction(db, false);
      65               1 :     do_check_true(transaction.HasTransaction());
      66               1 :     (void)transaction.Rollback();
      67               1 :     do_check_false(transaction.HasTransaction());
      68                 :   }
      69                 : 
      70                 :   // Check that we do not have a transaction if one is already obtained.
      71               2 :   mozStorageTransaction outerTransaction(db, false);
      72               1 :   do_check_true(outerTransaction.HasTransaction());
      73                 :   {
      74               2 :     mozStorageTransaction innerTransaction(db, false);
      75               1 :     do_check_false(innerTransaction.HasTransaction());
      76                 :   }
      77               1 : }
      78                 : 
      79                 : void
      80               1 : test_Commit()
      81                 : {
      82               2 :   nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
      83                 : 
      84                 :   // Create a table in a transaction, call Commit, and make sure that it does
      85                 :   // exists after the transaction falls out of scope.
      86                 :   {
      87               2 :     mozStorageTransaction transaction(db, false);
      88               2 :     (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
      89                 :       "CREATE TABLE test (id INTEGER PRIMARY KEY)"
      90               1 :     ));
      91               1 :     (void)transaction.Commit();
      92                 :   }
      93                 : 
      94               1 :   bool exists = false;
      95               1 :   (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
      96               1 :   do_check_true(exists);
      97               1 : }
      98                 : 
      99                 : void
     100               1 : test_Rollback()
     101                 : {
     102               2 :   nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
     103                 : 
     104                 :   // Create a table in a transaction, call Rollback, and make sure that it does
     105                 :   // not exists after the transaction falls out of scope.
     106                 :   {
     107               2 :     mozStorageTransaction transaction(db, true);
     108               2 :     (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
     109                 :       "CREATE TABLE test (id INTEGER PRIMARY KEY)"
     110               1 :     ));
     111               1 :     (void)transaction.Rollback();
     112                 :   }
     113                 : 
     114               1 :   bool exists = true;
     115               1 :   (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
     116               1 :   do_check_false(exists);
     117               1 : }
     118                 : 
     119                 : void
     120               1 : test_AutoCommit()
     121                 : {
     122               2 :   nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
     123                 : 
     124                 :   // Create a table in a transaction, and make sure that it exists after the
     125                 :   // transaction falls out of scope.  This means the Commit was successful.
     126                 :   {
     127               2 :     mozStorageTransaction transaction(db, true);
     128               2 :     (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
     129                 :       "CREATE TABLE test (id INTEGER PRIMARY KEY)"
     130               1 :     ));
     131                 :   }
     132                 : 
     133               1 :   bool exists = false;
     134               1 :   (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
     135               1 :   do_check_true(exists);
     136               1 : }
     137                 : 
     138                 : void
     139               1 : test_AutoRollback()
     140                 : {
     141               2 :   nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
     142                 : 
     143                 :   // Create a table in a transaction, and make sure that it does not exists
     144                 :   // after the transaction falls out of scope.  This means the Rollback was
     145                 :   // successful.
     146                 :   {
     147               2 :     mozStorageTransaction transaction(db, false);
     148               2 :     (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
     149                 :       "CREATE TABLE test (id INTEGER PRIMARY KEY)"
     150               1 :     ));
     151                 :   }
     152                 : 
     153               1 :   bool exists = true;
     154               1 :   (void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
     155               1 :   do_check_false(exists);
     156               1 : }
     157                 : 
     158                 : void
     159               1 : test_SetDefaultAction()
     160                 : {
     161               2 :   nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
     162                 : 
     163                 :   // First we test that rollback happens when we first set it to automatically
     164                 :   // commit.
     165                 :   {
     166               2 :     mozStorageTransaction transaction(db, true);
     167               2 :     (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
     168                 :       "CREATE TABLE test1 (id INTEGER PRIMARY KEY)"
     169               1 :     ));
     170               1 :     transaction.SetDefaultAction(false);
     171                 :   }
     172               1 :   bool exists = true;
     173               1 :   (void)db->TableExists(NS_LITERAL_CSTRING("test1"), &exists);
     174               1 :   do_check_false(exists);
     175                 : 
     176                 :   // Now we do the opposite and test that a commit happens when we first set it
     177                 :   // to automatically rollback.
     178                 :   {
     179               2 :     mozStorageTransaction transaction(db, false);
     180               2 :     (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
     181                 :       "CREATE TABLE test2 (id INTEGER PRIMARY KEY)"
     182               1 :     ));
     183               1 :     transaction.SetDefaultAction(true);
     184                 :   }
     185               1 :   exists = false;
     186               1 :   (void)db->TableExists(NS_LITERAL_CSTRING("test2"), &exists);
     187               1 :   do_check_true(exists);
     188               1 : }
     189                 : 
     190                 : void
     191               1 : test_null_database_connection()
     192                 : {
     193                 :   // We permit the use of the Transaction helper when passing a null database
     194                 :   // in, so we need to make sure this still works without crashing.
     195               2 :   mozStorageTransaction transaction(nsnull, false);
     196                 : 
     197               1 :   do_check_false(transaction.HasTransaction());
     198               1 :   do_check_true(NS_SUCCEEDED(transaction.Commit()));
     199               1 :   do_check_true(NS_SUCCEEDED(transaction.Rollback()));
     200               1 : }
     201                 : 
     202                 : void (*gTests[])(void) = {
     203                 :   test_HasTransaction,
     204                 :   test_Commit,
     205                 :   test_Rollback,
     206                 :   test_AutoCommit,
     207                 :   test_AutoRollback,
     208                 :   test_SetDefaultAction,
     209                 :   test_null_database_connection,
     210                 : };
     211                 : 
     212                 : const char *file = __FILE__;
     213                 : #define TEST_NAME "transaction helper"
     214                 : #define TEST_FILE file
     215                 : #include "storage_test_harness_tail.h"

Generated by: LCOV version 1.7