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