storage/test/unit/test_storage_statement.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:14800841bdf7
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 // This file tests the functions of mozIStorageStatement
6
7 function setup()
8 {
9 getOpenedDatabase().createTable("test", "id INTEGER PRIMARY KEY, name TEXT");
10 }
11
12 function test_parameterCount_none()
13 {
14 var stmt = createStatement("SELECT * FROM test");
15 do_check_eq(0, stmt.parameterCount);
16 stmt.reset();
17 stmt.finalize();
18 }
19
20 function test_parameterCount_one()
21 {
22 var stmt = createStatement("SELECT * FROM test WHERE id = ?1");
23 do_check_eq(1, stmt.parameterCount);
24 stmt.reset();
25 stmt.finalize();
26 }
27
28 function test_getParameterName()
29 {
30 var stmt = createStatement("SELECT * FROM test WHERE id = :id");
31 do_check_eq(":id", stmt.getParameterName(0));
32 stmt.reset();
33 stmt.finalize();
34 }
35
36 function test_getParameterIndex_different()
37 {
38 var stmt = createStatement("SELECT * FROM test WHERE id = :id OR name = :name");
39 do_check_eq(0, stmt.getParameterIndex("id"));
40 do_check_eq(1, stmt.getParameterIndex("name"));
41 stmt.reset();
42 stmt.finalize();
43 }
44
45 function test_getParameterIndex_same()
46 {
47 var stmt = createStatement("SELECT * FROM test WHERE id = :test OR name = :test");
48 do_check_eq(0, stmt.getParameterIndex("test"));
49 stmt.reset();
50 stmt.finalize();
51 }
52
53 function test_columnCount()
54 {
55 var stmt = createStatement("SELECT * FROM test WHERE id = ?1 OR name = ?2");
56 do_check_eq(2, stmt.columnCount);
57 stmt.reset();
58 stmt.finalize();
59 }
60
61 function test_getColumnName()
62 {
63 var stmt = createStatement("SELECT name, id FROM test");
64 do_check_eq("id", stmt.getColumnName(1));
65 do_check_eq("name", stmt.getColumnName(0));
66 stmt.reset();
67 stmt.finalize();
68 }
69
70 function test_getColumnIndex_same_case()
71 {
72 var stmt = createStatement("SELECT name, id FROM test");
73 do_check_eq(0, stmt.getColumnIndex("name"));
74 do_check_eq(1, stmt.getColumnIndex("id"));
75 stmt.reset();
76 stmt.finalize();
77 }
78
79 function test_getColumnIndex_different_case()
80 {
81 var stmt = createStatement("SELECT name, id FROM test");
82 try {
83 do_check_eq(0, stmt.getColumnIndex("NaMe"));
84 do_throw("should not get here");
85 } catch (e) {
86 do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
87 }
88 try {
89 do_check_eq(1, stmt.getColumnIndex("Id"));
90 do_throw("should not get here");
91 } catch (e) {
92 do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
93 }
94 stmt.reset();
95 stmt.finalize();
96 }
97
98 function test_state_ready()
99 {
100 var stmt = createStatement("SELECT name, id FROM test");
101 do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
102 stmt.reset();
103 stmt.finalize();
104 }
105
106 function test_state_executing()
107 {
108 var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");
109 stmt.execute();
110 stmt.execute();
111 stmt.finalize();
112
113 stmt = createStatement("SELECT name, id FROM test");
114 stmt.executeStep();
115 do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING,
116 stmt.state);
117 stmt.executeStep();
118 do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING,
119 stmt.state);
120 stmt.reset();
121 do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
122 stmt.finalize();
123 }
124
125 function test_state_after_finalize()
126 {
127 var stmt = createStatement("SELECT name, id FROM test");
128 stmt.executeStep();
129 stmt.finalize();
130 do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_INVALID, stmt.state);
131 }
132
133 function test_getColumnDecltype()
134 {
135 var stmt = createStatement("SELECT name, id FROM test");
136 do_check_eq("TEXT", stmt.getColumnDecltype(0));
137 do_check_eq("INTEGER", stmt.getColumnDecltype(1));
138 try {
139 do_check_eq("GARBAGE", stmt.getColumnDecltype(2));
140 do_throw("should not get here");
141 } catch (e) {
142 do_check_eq(Cr.NS_ERROR_ILLEGAL_VALUE, e.result);
143 }
144 stmt.finalize();
145 }
146
147 function test_failed_execute()
148 {
149 var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");
150 stmt.execute();
151 stmt.finalize();
152 var id = getOpenedDatabase().lastInsertRowID;
153 stmt = createStatement("INSERT INTO test(id, name) VALUES(:id, 'bar')");
154 stmt.params.id = id;
155 try {
156 // Should throw a constraint error
157 stmt.execute();
158 do_throw("Should have seen a constraint error");
159 }
160 catch (e) {
161 do_check_eq(getOpenedDatabase().lastError, Ci.mozIStorageError.CONSTRAINT);
162 }
163 do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
164 // Should succeed without needing to reset the statement manually
165 stmt.finalize();
166 }
167
168 var tests = [test_parameterCount_none, test_parameterCount_one,
169 test_getParameterName, test_getParameterIndex_different,
170 test_getParameterIndex_same, test_columnCount,
171 test_getColumnName, test_getColumnIndex_same_case,
172 test_getColumnIndex_different_case, test_state_ready,
173 test_state_executing, test_state_after_finalize,
174 test_getColumnDecltype,
175 test_failed_execute,
176 ];
177
178 function run_test()
179 {
180 setup();
181
182 for (var i = 0; i < tests.length; i++)
183 tests[i]();
184
185 cleanup();
186 }
187

mercurial