1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 #include "storage_test_harness.h"
6 #include "mozStorageConnection.h"
10 using namespace mozilla
;
11 using namespace mozilla::storage
;
13 ////////////////////////////////////////////////////////////////////////////////
17 * Commit hook to detect transactions.
20 * An integer pointer that will be incremented for each commit.
22 int commit_hook(void* aArg
) {
23 int* arg
= static_cast<int*>(aArg
);
29 * Executes the passed-in statements and checks if a transaction is created.
30 * When done statements are finalized and database connection is closed.
33 * The database connection.
35 * Vector of statements.
37 * Number of statements.
38 * @param aTransactionExpected
39 * Whether a transaction is expected or not.
41 void check_transaction(mozIStorageConnection
* aDB
,
42 const nsTArray
<RefPtr
<mozIStorageBaseStatement
>>& aStmts
,
43 bool aTransactionExpected
) {
44 // -- install a transaction commit hook.
46 static_cast<Connection
*>(aDB
)->setCommitHook(commit_hook
, &commit
);
48 RefPtr
<AsyncStatementSpinner
> asyncSpin(new AsyncStatementSpinner());
49 nsCOMPtr
<mozIStoragePendingStatement
> asyncPend
;
51 aDB
->ExecuteAsync(aStmts
, asyncSpin
, getter_AddRefs(asyncPend
)));
52 do_check_true(asyncPend
);
54 // -- complete the execution
55 asyncSpin
->SpinUntilCompleted();
57 // -- uninstall the transaction commit hook.
58 static_cast<Connection
*>(aDB
)->setCommitHook(nullptr);
60 // -- check transaction
61 do_check_eq(aTransactionExpected
, !!commit
);
63 // -- check that only one transaction was created.
64 if (aTransactionExpected
) {
65 do_check_eq(1, commit
);
69 for (uint32_t i
= 0; i
< aStmts
.Length(); ++i
) {
70 aStmts
[i
]->Finalize();
72 blocking_async_close(aDB
);
75 ////////////////////////////////////////////////////////////////////////////////
79 * Test that executing multiple readonly AsyncStatements doesn't create a
82 TEST(storage_asyncStatementExecution_transaction
, MultipleAsyncReadStatements
)
84 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
86 // -- create statements and execute them
87 nsCOMPtr
<mozIStorageAsyncStatement
> stmt1
;
88 db
->CreateAsyncStatement("SELECT * FROM sqlite_master"_ns
,
89 getter_AddRefs(stmt1
));
91 nsCOMPtr
<mozIStorageAsyncStatement
> stmt2
;
92 db
->CreateAsyncStatement("SELECT * FROM sqlite_master"_ns
,
93 getter_AddRefs(stmt2
));
95 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
96 ToRefPtr(std::move(stmt1
)),
97 ToRefPtr(std::move(stmt2
)),
100 check_transaction(db
, stmts
.Clone(), false);
104 * Test that executing multiple readonly Statements doesn't create a
107 TEST(storage_asyncStatementExecution_transaction
, MultipleReadStatements
)
109 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
111 // -- create statements and execute them
112 nsCOMPtr
<mozIStorageStatement
> stmt1
;
113 db
->CreateStatement("SELECT * FROM sqlite_master"_ns
, getter_AddRefs(stmt1
));
115 nsCOMPtr
<mozIStorageStatement
> stmt2
;
116 db
->CreateStatement("SELECT * FROM sqlite_master"_ns
, getter_AddRefs(stmt2
));
118 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
119 ToRefPtr(std::move(stmt1
)),
120 ToRefPtr(std::move(stmt2
)),
123 check_transaction(db
, stmts
, false);
127 * Test that executing multiple AsyncStatements causing writes creates a
130 TEST(storage_asyncStatementExecution_transaction
,
131 MultipleAsyncReadWriteStatements
)
133 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
135 // -- create statements and execute them
136 nsCOMPtr
<mozIStorageAsyncStatement
> stmt1
;
137 db
->CreateAsyncStatement("SELECT * FROM sqlite_master"_ns
,
138 getter_AddRefs(stmt1
));
140 nsCOMPtr
<mozIStorageAsyncStatement
> stmt2
;
141 db
->CreateAsyncStatement("CREATE TABLE test (id INTEGER PRIMARY KEY)"_ns
,
142 getter_AddRefs(stmt2
));
144 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
145 ToRefPtr(std::move(stmt1
)),
146 ToRefPtr(std::move(stmt2
)),
149 check_transaction(db
, stmts
, true);
153 * Test that executing multiple Statements causing writes creates a transaction.
155 TEST(storage_asyncStatementExecution_transaction
, MultipleReadWriteStatements
)
157 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
159 // -- create statements and execute them
160 nsCOMPtr
<mozIStorageStatement
> stmt1
;
161 db
->CreateStatement("SELECT * FROM sqlite_master"_ns
, getter_AddRefs(stmt1
));
163 nsCOMPtr
<mozIStorageStatement
> stmt2
;
164 db
->CreateStatement("CREATE TABLE test (id INTEGER PRIMARY KEY)"_ns
,
165 getter_AddRefs(stmt2
));
167 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
168 ToRefPtr(std::move(stmt1
)),
169 ToRefPtr(std::move(stmt2
)),
172 check_transaction(db
, stmts
, true);
176 * Test that executing multiple AsyncStatements causing writes creates a
177 * single transaction.
179 TEST(storage_asyncStatementExecution_transaction
, MultipleAsyncWriteStatements
)
181 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
183 // -- create statements and execute them
184 nsCOMPtr
<mozIStorageAsyncStatement
> stmt1
;
185 db
->CreateAsyncStatement("CREATE TABLE test1 (id INTEGER PRIMARY KEY)"_ns
,
186 getter_AddRefs(stmt1
));
188 nsCOMPtr
<mozIStorageAsyncStatement
> stmt2
;
189 db
->CreateAsyncStatement("CREATE TABLE test2 (id INTEGER PRIMARY KEY)"_ns
,
190 getter_AddRefs(stmt2
));
192 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
193 ToRefPtr(std::move(stmt1
)),
194 ToRefPtr(std::move(stmt2
)),
197 check_transaction(db
, stmts
, true);
201 * Test that executing multiple Statements causing writes creates a
202 * single transaction.
204 TEST(storage_asyncStatementExecution_transaction
, MultipleWriteStatements
)
206 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
208 // -- create statements and execute them
209 nsCOMPtr
<mozIStorageStatement
> stmt1
;
210 db
->CreateStatement("CREATE TABLE test1 (id INTEGER PRIMARY KEY)"_ns
,
211 getter_AddRefs(stmt1
));
213 nsCOMPtr
<mozIStorageStatement
> stmt2
;
214 db
->CreateStatement("CREATE TABLE test2 (id INTEGER PRIMARY KEY)"_ns
,
215 getter_AddRefs(stmt2
));
217 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
218 ToRefPtr(std::move(stmt1
)),
219 ToRefPtr(std::move(stmt2
)),
222 check_transaction(db
, stmts
, true);
226 * Test that executing a single read-only AsyncStatement doesn't create a
229 TEST(storage_asyncStatementExecution_transaction
, SingleAsyncReadStatement
)
231 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
233 // -- create statements and execute them
234 nsCOMPtr
<mozIStorageAsyncStatement
> stmt
;
235 db
->CreateAsyncStatement("SELECT * FROM sqlite_master"_ns
,
236 getter_AddRefs(stmt
));
238 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
239 ToRefPtr(std::move(stmt
)),
242 check_transaction(db
, stmts
, false);
246 * Test that executing a single read-only Statement doesn't create a
249 TEST(storage_asyncStatementExecution_transaction
, SingleReadStatement
)
251 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
253 // -- create statements and execute them
254 nsCOMPtr
<mozIStorageStatement
> stmt
;
255 db
->CreateStatement("SELECT * FROM sqlite_master"_ns
, getter_AddRefs(stmt
));
257 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
258 ToRefPtr(std::move(stmt
)),
261 check_transaction(db
, stmts
, false);
265 * Test that executing a single AsyncStatement causing writes creates a
268 TEST(storage_asyncStatementExecution_transaction
, SingleAsyncWriteStatement
)
270 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
272 // -- create statements and execute them
273 nsCOMPtr
<mozIStorageAsyncStatement
> stmt
;
274 db
->CreateAsyncStatement("CREATE TABLE test (id INTEGER PRIMARY KEY)"_ns
,
275 getter_AddRefs(stmt
));
277 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
278 ToRefPtr(std::move(stmt
)),
281 check_transaction(db
, stmts
, true);
285 * Test that executing a single Statement causing writes creates a transaction.
287 TEST(storage_asyncStatementExecution_transaction
, SingleWriteStatement
)
289 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
291 // -- create statements and execute them
292 nsCOMPtr
<mozIStorageStatement
> stmt
;
293 db
->CreateStatement("CREATE TABLE test (id INTEGER PRIMARY KEY)"_ns
,
294 getter_AddRefs(stmt
));
296 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
297 ToRefPtr(std::move(stmt
)),
300 check_transaction(db
, stmts
, true);
304 * Test that executing a single read-only AsyncStatement with multiple params
305 * doesn't create a transaction.
307 TEST(storage_asyncStatementExecution_transaction
,
308 MultipleParamsAsyncReadStatement
)
310 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
312 // -- create statements and execute them
313 nsCOMPtr
<mozIStorageAsyncStatement
> stmt
;
314 db
->CreateAsyncStatement("SELECT :param FROM sqlite_master"_ns
,
315 getter_AddRefs(stmt
));
317 // -- bind multiple BindingParams
318 nsCOMPtr
<mozIStorageBindingParamsArray
> paramsArray
;
319 stmt
->NewBindingParamsArray(getter_AddRefs(paramsArray
));
320 for (int32_t i
= 0; i
< 2; i
++) {
321 nsCOMPtr
<mozIStorageBindingParams
> params
;
322 paramsArray
->NewBindingParams(getter_AddRefs(params
));
323 params
->BindInt32ByName("param"_ns
, 1);
324 paramsArray
->AddParams(params
);
326 stmt
->BindParameters(paramsArray
);
327 paramsArray
= nullptr;
329 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
330 ToRefPtr(std::move(stmt
)),
333 check_transaction(db
, stmts
, false);
337 * Test that executing a single read-only Statement with multiple params
338 * doesn't create a transaction.
340 TEST(storage_asyncStatementExecution_transaction
, MultipleParamsReadStatement
)
342 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
344 // -- create statements and execute them
345 nsCOMPtr
<mozIStorageStatement
> stmt
;
346 db
->CreateStatement("SELECT :param FROM sqlite_master"_ns
,
347 getter_AddRefs(stmt
));
349 // -- bind multiple BindingParams
350 nsCOMPtr
<mozIStorageBindingParamsArray
> paramsArray
;
351 stmt
->NewBindingParamsArray(getter_AddRefs(paramsArray
));
352 for (int32_t i
= 0; i
< 2; i
++) {
353 nsCOMPtr
<mozIStorageBindingParams
> params
;
354 paramsArray
->NewBindingParams(getter_AddRefs(params
));
355 params
->BindInt32ByName("param"_ns
, 1);
356 paramsArray
->AddParams(params
);
358 stmt
->BindParameters(paramsArray
);
359 paramsArray
= nullptr;
361 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
362 ToRefPtr(std::move(stmt
)),
365 check_transaction(db
, stmts
, false);
369 * Test that executing a single write AsyncStatement with multiple params
370 * creates a transaction.
372 TEST(storage_asyncStatementExecution_transaction
,
373 MultipleParamsAsyncWriteStatement
)
375 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
377 // -- create a table for writes
378 nsCOMPtr
<mozIStorageStatement
> tableStmt
;
379 db
->CreateStatement("CREATE TABLE test (id INTEGER PRIMARY KEY)"_ns
,
380 getter_AddRefs(tableStmt
));
381 tableStmt
->Execute();
382 tableStmt
->Finalize();
384 // -- create statements and execute them
385 nsCOMPtr
<mozIStorageAsyncStatement
> stmt
;
386 db
->CreateAsyncStatement("DELETE FROM test WHERE id = :param"_ns
,
387 getter_AddRefs(stmt
));
389 // -- bind multiple BindingParams
390 nsCOMPtr
<mozIStorageBindingParamsArray
> paramsArray
;
391 stmt
->NewBindingParamsArray(getter_AddRefs(paramsArray
));
392 for (int32_t i
= 0; i
< 2; i
++) {
393 nsCOMPtr
<mozIStorageBindingParams
> params
;
394 paramsArray
->NewBindingParams(getter_AddRefs(params
));
395 params
->BindInt32ByName("param"_ns
, 1);
396 paramsArray
->AddParams(params
);
398 stmt
->BindParameters(paramsArray
);
399 paramsArray
= nullptr;
401 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
402 ToRefPtr(std::move(stmt
)),
405 check_transaction(db
, stmts
, true);
409 * Test that executing a single write Statement with multiple params
410 * creates a transaction.
412 TEST(storage_asyncStatementExecution_transaction
, MultipleParamsWriteStatement
)
414 nsCOMPtr
<mozIStorageConnection
> db(getMemoryDatabase());
416 // -- create a table for writes
417 nsCOMPtr
<mozIStorageStatement
> tableStmt
;
418 db
->CreateStatement("CREATE TABLE test (id INTEGER PRIMARY KEY)"_ns
,
419 getter_AddRefs(tableStmt
));
420 tableStmt
->Execute();
421 tableStmt
->Finalize();
423 // -- create statements and execute them
424 nsCOMPtr
<mozIStorageStatement
> stmt
;
425 db
->CreateStatement("DELETE FROM test WHERE id = :param"_ns
,
426 getter_AddRefs(stmt
));
428 // -- bind multiple BindingParams
429 nsCOMPtr
<mozIStorageBindingParamsArray
> paramsArray
;
430 stmt
->NewBindingParamsArray(getter_AddRefs(paramsArray
));
431 for (int32_t i
= 0; i
< 2; i
++) {
432 nsCOMPtr
<mozIStorageBindingParams
> params
;
433 paramsArray
->NewBindingParams(getter_AddRefs(params
));
434 params
->BindInt32ByName("param"_ns
, 1);
435 paramsArray
->AddParams(params
);
437 stmt
->BindParameters(paramsArray
);
438 paramsArray
= nullptr;
440 nsTArray
<RefPtr
<mozIStorageBaseStatement
>> stmts
= {
441 ToRefPtr(std::move(stmt
)),
444 check_transaction(db
, stmts
, true);