tor-browser

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

test_storage_statement.js (4938B)


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