1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/storage/test/unit/test_statement_wrapper_automatically.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,170 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + vim:set ts=2 sw=2 sts=2 et: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// This file tests the functions of mozIStorageStatementWrapper 1.11 + 1.12 +function setup() 1.13 +{ 1.14 + getOpenedDatabase().createTable("test", "id INTEGER PRIMARY KEY, val NONE," + 1.15 + "alt_val NONE"); 1.16 +} 1.17 + 1.18 +/** 1.19 + * A convenience wrapper for do_check_eq. Calls do_check_eq on aActualVal 1.20 + * and aReturnedVal, with one caveat. 1.21 + * 1.22 + * Date objects are converted before parameter binding to PRTime's (microsecs 1.23 + * since epoch). They are not reconverted when retrieved from the database. 1.24 + * This function abstracts away this reconversion so that you can pass in, 1.25 + * for example: 1.26 + * 1.27 + * checkVal(new Date(), aReturnedVal) // this 1.28 + * checkVal(new Date().valueOf() * 1000.0, aReturnedVal) // instead of this 1.29 + * 1.30 + * Should any other types require conversion in the future, their conversions 1.31 + * may also be abstracted away here. 1.32 + * 1.33 + * @param aActualVal 1.34 + * the value inserted into the database 1.35 + * @param aReturnedVal 1.36 + * the value retrieved from the database 1.37 + */ 1.38 +function checkVal(aActualVal, aReturnedVal) 1.39 +{ 1.40 + if (aActualVal instanceof Date) aActualVal = aActualVal.valueOf() * 1000.0; 1.41 + do_check_eq(aActualVal, aReturnedVal); 1.42 +} 1.43 + 1.44 +/** 1.45 + * Removes all rows from our test table. 1.46 + */ 1.47 +function clearTable() 1.48 +{ 1.49 + var stmt = createStatement("DELETE FROM test"); 1.50 + stmt.execute(); 1.51 + stmt.finalize(); 1.52 + ensureNumRows(0); 1.53 +} 1.54 + 1.55 +/** 1.56 + * Ensures that the number of rows in our test table is equal to aNumRows. 1.57 + * Calls do_check_eq on aNumRows and the value retrieved by SELECT'ing COUNT(*). 1.58 + * 1.59 + * @param aNumRows 1.60 + * the number of rows our test table should contain 1.61 + */ 1.62 +function ensureNumRows(aNumRows) 1.63 +{ 1.64 + var stmt = createStatement("SELECT COUNT(*) AS number FROM test"); 1.65 + do_check_true(stmt.step()); 1.66 + do_check_eq(aNumRows, stmt.row.number); 1.67 + stmt.reset(); 1.68 + stmt.finalize(); 1.69 +} 1.70 + 1.71 +/** 1.72 + * Inserts aVal into our test table and checks that insertion was successful by 1.73 + * retrieving the newly inserted value from the database and comparing it 1.74 + * against aVal. aVal is bound to a single parameter. 1.75 + * 1.76 + * @param aVal 1.77 + * value to insert into our test table and check 1.78 + */ 1.79 +function insertAndCheckSingleParam(aVal) 1.80 +{ 1.81 + clearTable(); 1.82 + 1.83 + var stmt = createStatement("INSERT INTO test (val) VALUES (:val)"); 1.84 + stmt.params.val = aVal; 1.85 + stmt.execute(); 1.86 + stmt.finalize(); 1.87 + 1.88 + ensureNumRows(1); 1.89 + 1.90 + stmt = createStatement("SELECT val FROM test WHERE id = 1"); 1.91 + do_check_true(stmt.step()); 1.92 + checkVal(aVal, stmt.row.val); 1.93 + stmt.reset(); 1.94 + stmt.finalize(); 1.95 +} 1.96 + 1.97 +/** 1.98 + * Inserts aVal into our test table and checks that insertion was successful by 1.99 + * retrieving the newly inserted value from the database and comparing it 1.100 + * against aVal. aVal is bound to two separate parameters, both of which are 1.101 + * checked against aVal. 1.102 + * 1.103 + * @param aVal 1.104 + * value to insert into our test table and check 1.105 + */ 1.106 +function insertAndCheckMultipleParams(aVal) 1.107 +{ 1.108 + clearTable(); 1.109 + 1.110 + var stmt = createStatement("INSERT INTO test (val, alt_val) " + 1.111 + "VALUES (:val, :val)"); 1.112 + stmt.params.val = aVal; 1.113 + stmt.execute(); 1.114 + stmt.finalize(); 1.115 + 1.116 + ensureNumRows(1); 1.117 + 1.118 + stmt = createStatement("SELECT val, alt_val FROM test WHERE id = 1"); 1.119 + do_check_true(stmt.step()); 1.120 + checkVal(aVal, stmt.row.val); 1.121 + checkVal(aVal, stmt.row.alt_val); 1.122 + stmt.reset(); 1.123 + stmt.finalize(); 1.124 +} 1.125 + 1.126 +/** 1.127 + * A convenience function that prints out a description of aVal using 1.128 + * aVal.toString and aVal.toSource. Output is useful when the test fails. 1.129 + * 1.130 + * @param aVal 1.131 + * a value inserted or to be inserted into our test table 1.132 + */ 1.133 +function printValDesc(aVal) 1.134 +{ 1.135 + try 1.136 + { 1.137 + var toSource = aVal.toSource(); 1.138 + } 1.139 + catch (exc) 1.140 + { 1.141 + toSource = ""; 1.142 + } 1.143 + print("Testing value: toString=" + aVal + 1.144 + (toSource ? " toSource=" + toSource : "")); 1.145 +} 1.146 + 1.147 +function run_test() 1.148 +{ 1.149 + setup(); 1.150 + 1.151 + // function JSValStorageStatementBinder in 1.152 + // storage/src/mozStorageStatementParams.cpp tells us that the following types 1.153 + // and only the following types are valid as statement parameters: 1.154 + var vals = [ 1.155 + 1337, // int 1.156 + 3.1337, // double 1.157 + "foo", // string 1.158 + true, // boolean 1.159 + null, // null 1.160 + new Date(), // Date object 1.161 + ]; 1.162 + 1.163 + vals.forEach(function (val) 1.164 + { 1.165 + printValDesc(val); 1.166 + print("Single parameter"); 1.167 + insertAndCheckSingleParam(val); 1.168 + print("Multiple parameters"); 1.169 + insertAndCheckMultipleParams(val) 1.170 + }); 1.171 + 1.172 + cleanup(); 1.173 +}