storage/test/unit/test_statement_wrapper_automatically.js

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:d5cee5e3104e
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 vim:set ts=2 sw=2 sts=2 et:
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 // This file tests the functions of mozIStorageStatementWrapper
8
9 function setup()
10 {
11 getOpenedDatabase().createTable("test", "id INTEGER PRIMARY KEY, val NONE," +
12 "alt_val NONE");
13 }
14
15 /**
16 * A convenience wrapper for do_check_eq. Calls do_check_eq on aActualVal
17 * and aReturnedVal, with one caveat.
18 *
19 * Date objects are converted before parameter binding to PRTime's (microsecs
20 * since epoch). They are not reconverted when retrieved from the database.
21 * This function abstracts away this reconversion so that you can pass in,
22 * for example:
23 *
24 * checkVal(new Date(), aReturnedVal) // this
25 * checkVal(new Date().valueOf() * 1000.0, aReturnedVal) // instead of this
26 *
27 * Should any other types require conversion in the future, their conversions
28 * may also be abstracted away here.
29 *
30 * @param aActualVal
31 * the value inserted into the database
32 * @param aReturnedVal
33 * the value retrieved from the database
34 */
35 function checkVal(aActualVal, aReturnedVal)
36 {
37 if (aActualVal instanceof Date) aActualVal = aActualVal.valueOf() * 1000.0;
38 do_check_eq(aActualVal, aReturnedVal);
39 }
40
41 /**
42 * Removes all rows from our test table.
43 */
44 function clearTable()
45 {
46 var stmt = createStatement("DELETE FROM test");
47 stmt.execute();
48 stmt.finalize();
49 ensureNumRows(0);
50 }
51
52 /**
53 * Ensures that the number of rows in our test table is equal to aNumRows.
54 * Calls do_check_eq on aNumRows and the value retrieved by SELECT'ing COUNT(*).
55 *
56 * @param aNumRows
57 * the number of rows our test table should contain
58 */
59 function ensureNumRows(aNumRows)
60 {
61 var stmt = createStatement("SELECT COUNT(*) AS number FROM test");
62 do_check_true(stmt.step());
63 do_check_eq(aNumRows, stmt.row.number);
64 stmt.reset();
65 stmt.finalize();
66 }
67
68 /**
69 * Inserts aVal into our test table and checks that insertion was successful by
70 * retrieving the newly inserted value from the database and comparing it
71 * against aVal. aVal is bound to a single parameter.
72 *
73 * @param aVal
74 * value to insert into our test table and check
75 */
76 function insertAndCheckSingleParam(aVal)
77 {
78 clearTable();
79
80 var stmt = createStatement("INSERT INTO test (val) VALUES (:val)");
81 stmt.params.val = aVal;
82 stmt.execute();
83 stmt.finalize();
84
85 ensureNumRows(1);
86
87 stmt = createStatement("SELECT val FROM test WHERE id = 1");
88 do_check_true(stmt.step());
89 checkVal(aVal, stmt.row.val);
90 stmt.reset();
91 stmt.finalize();
92 }
93
94 /**
95 * Inserts aVal into our test table and checks that insertion was successful by
96 * retrieving the newly inserted value from the database and comparing it
97 * against aVal. aVal is bound to two separate parameters, both of which are
98 * checked against aVal.
99 *
100 * @param aVal
101 * value to insert into our test table and check
102 */
103 function insertAndCheckMultipleParams(aVal)
104 {
105 clearTable();
106
107 var stmt = createStatement("INSERT INTO test (val, alt_val) " +
108 "VALUES (:val, :val)");
109 stmt.params.val = aVal;
110 stmt.execute();
111 stmt.finalize();
112
113 ensureNumRows(1);
114
115 stmt = createStatement("SELECT val, alt_val FROM test WHERE id = 1");
116 do_check_true(stmt.step());
117 checkVal(aVal, stmt.row.val);
118 checkVal(aVal, stmt.row.alt_val);
119 stmt.reset();
120 stmt.finalize();
121 }
122
123 /**
124 * A convenience function that prints out a description of aVal using
125 * aVal.toString and aVal.toSource. Output is useful when the test fails.
126 *
127 * @param aVal
128 * a value inserted or to be inserted into our test table
129 */
130 function printValDesc(aVal)
131 {
132 try
133 {
134 var toSource = aVal.toSource();
135 }
136 catch (exc)
137 {
138 toSource = "";
139 }
140 print("Testing value: toString=" + aVal +
141 (toSource ? " toSource=" + toSource : ""));
142 }
143
144 function run_test()
145 {
146 setup();
147
148 // function JSValStorageStatementBinder in
149 // storage/src/mozStorageStatementParams.cpp tells us that the following types
150 // and only the following types are valid as statement parameters:
151 var vals = [
152 1337, // int
153 3.1337, // double
154 "foo", // string
155 true, // boolean
156 null, // null
157 new Date(), // Date object
158 ];
159
160 vals.forEach(function (val)
161 {
162 printValDesc(val);
163 print("Single parameter");
164 insertAndCheckSingleParam(val);
165 print("Multiple parameters");
166 insertAndCheckMultipleParams(val)
167 });
168
169 cleanup();
170 }

mercurial