tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_statement_wrapper_automatically.js (4580B)


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