|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 /* |
|
6 * This file tests the functionality of |
|
7 * mozIStorageAsyncConnection::executeSimpleSQLAsync. |
|
8 */ |
|
9 |
|
10 const INTEGER = 1; |
|
11 const TEXT = "this is test text"; |
|
12 const REAL = 3.23; |
|
13 |
|
14 function asyncClose(db) { |
|
15 let deferred = Promise.defer(); |
|
16 db.asyncClose(function(status) { |
|
17 if (Components.isSuccessCode(status)) { |
|
18 deferred.resolve(); |
|
19 } else { |
|
20 deferred.reject(status); |
|
21 } |
|
22 }); |
|
23 return deferred.promise; |
|
24 } |
|
25 |
|
26 function openAsyncDatabase(file) { |
|
27 let deferred = Promise.defer(); |
|
28 getService().openAsyncDatabase(file, null, function(status, db) { |
|
29 if (Components.isSuccessCode(status)) { |
|
30 deferred.resolve(db.QueryInterface(Ci.mozIStorageAsyncConnection)); |
|
31 } else { |
|
32 deferred.reject(status); |
|
33 } |
|
34 }); |
|
35 return deferred.promise; |
|
36 } |
|
37 |
|
38 function executeSimpleSQLAsync(db, query, onResult) { |
|
39 let deferred = Promise.defer(); |
|
40 db.executeSimpleSQLAsync(query, { |
|
41 handleError: function(error) { |
|
42 deferred.reject(error); |
|
43 }, |
|
44 handleResult: function(result) { |
|
45 if (onResult) { |
|
46 onResult(result); |
|
47 } else { |
|
48 do_throw("No results were expected"); |
|
49 } |
|
50 }, |
|
51 handleCompletion: function(result) { |
|
52 deferred.resolve(result); |
|
53 } |
|
54 }); |
|
55 return deferred.promise; |
|
56 } |
|
57 |
|
58 add_task(function test_create_and_add() { |
|
59 let adb = yield openAsyncDatabase(getTestDB()); |
|
60 |
|
61 let completion = yield executeSimpleSQLAsync(adb, |
|
62 "CREATE TABLE test (id INTEGER, string TEXT, number REAL)"); |
|
63 |
|
64 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion); |
|
65 |
|
66 completion = yield executeSimpleSQLAsync(adb, |
|
67 "INSERT INTO test (id, string, number) " + |
|
68 "VALUES (" + INTEGER + ", \"" + TEXT + "\", " + REAL + ")"); |
|
69 |
|
70 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion); |
|
71 |
|
72 let result = null; |
|
73 |
|
74 completion = yield executeSimpleSQLAsync(adb, |
|
75 "SELECT string, number FROM test WHERE id = 1", |
|
76 function(aResultSet) { |
|
77 result = aResultSet.getNextRow(); |
|
78 do_check_eq(2, result.numEntries); |
|
79 do_check_eq(TEXT, result.getString(0)); |
|
80 do_check_eq(REAL, result.getDouble(1)); |
|
81 } |
|
82 ); |
|
83 |
|
84 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion); |
|
85 do_check_neq(result, null); |
|
86 result = null; |
|
87 |
|
88 yield executeSimpleSQLAsync(adb, "SELECT COUNT(0) FROM test", |
|
89 function(aResultSet) { |
|
90 result = aResultSet.getNextRow(); |
|
91 do_check_eq(1, result.getInt32(0)); |
|
92 }); |
|
93 |
|
94 do_check_neq(result, null); |
|
95 |
|
96 yield asyncClose(adb); |
|
97 }); |
|
98 |
|
99 |
|
100 add_task(function test_asyncClose_does_not_complete_before_statement() { |
|
101 let adb = yield openAsyncDatabase(getTestDB()); |
|
102 let executed = false; |
|
103 |
|
104 let reason = yield executeSimpleSQLAsync(adb, "SELECT * FROM test", |
|
105 function(aResultSet) { |
|
106 let result = aResultSet.getNextRow(); |
|
107 |
|
108 do_check_neq(result, null); |
|
109 do_check_eq(3, result.numEntries); |
|
110 do_check_eq(INTEGER, result.getInt32(0)); |
|
111 do_check_eq(TEXT, result.getString(1)); |
|
112 do_check_eq(REAL, result.getDouble(2)); |
|
113 executed = true; |
|
114 } |
|
115 ); |
|
116 |
|
117 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, reason); |
|
118 |
|
119 // Ensure that the statement executed to completion. |
|
120 do_check_true(executed); |
|
121 |
|
122 yield asyncClose(adb); |
|
123 }); |
|
124 |
|
125 //////////////////////////////////////////////////////////////////////////////// |
|
126 //// Test Runner |
|
127 |
|
128 function run_test() |
|
129 { |
|
130 run_next_test(); |
|
131 } |