tor-browser

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

test_connection_executeAsync.js (5202B)


      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 /*
      6 * This file tests the functionality of mozIStorageConnection::executeAsync for
      7 * both mozIStorageStatement and mozIStorageAsyncStatement.
      8 *
      9 * A single database connection is used for the entirety of the test, which is
     10 * a legacy thing, but we otherwise use the modern promise-based driver and
     11 * async helpers.
     12 */
     13 
     14 const INTEGER = 1;
     15 const TEXT = "this is test text";
     16 const REAL = 3.23;
     17 const BLOB = [1, 2];
     18 
     19 add_task(async function test_first_create_and_add() {
     20  // synchronously open the database and let gDBConn hold onto it because we
     21  // use this database
     22  let db = getOpenedDatabase();
     23  // synchronously set up our table *that will be used for the rest of the file*
     24  db.executeSimpleSQL(
     25    "CREATE TABLE test (" +
     26      "id INTEGER, " +
     27      "string TEXT, " +
     28      "number REAL, " +
     29      "nuller NULL, " +
     30      "blober BLOB" +
     31      ")"
     32  );
     33 
     34  let stmts = [];
     35  stmts[0] = db.createStatement(
     36    "INSERT INTO test (id, string, number, nuller, blober) VALUES (?, ?, ?, ?, ?)"
     37  );
     38  stmts[0].bindByIndex(0, INTEGER);
     39  stmts[0].bindByIndex(1, TEXT);
     40  stmts[0].bindByIndex(2, REAL);
     41  stmts[0].bindByIndex(3, null);
     42  stmts[0].bindBlobByIndex(4, BLOB, BLOB.length);
     43  stmts[1] = getOpenedDatabase().createAsyncStatement(
     44    "INSERT INTO test (string, number, nuller, blober) VALUES (?, ?, ?, ?)"
     45  );
     46  stmts[1].bindByIndex(0, TEXT);
     47  stmts[1].bindByIndex(1, REAL);
     48  stmts[1].bindByIndex(2, null);
     49  stmts[1].bindBlobByIndex(3, BLOB, BLOB.length);
     50 
     51  // asynchronously execute the statements
     52  let execResult = await executeMultipleStatementsAsync(db, stmts, function () {
     53    ok(false, "we only did inserts so we should not have gotten results!");
     54  });
     55  equal(
     56    Ci.mozIStorageStatementCallback.REASON_FINISHED,
     57    execResult,
     58    "execution should have finished successfully."
     59  );
     60 
     61  // Check that the result is in the table
     62  let stmt = db.createStatement(
     63    "SELECT string, number, nuller, blober FROM test WHERE id = ?"
     64  );
     65  stmt.bindByIndex(0, INTEGER);
     66  try {
     67    Assert.ok(stmt.executeStep());
     68    Assert.equal(TEXT, stmt.getString(0));
     69    Assert.equal(REAL, stmt.getDouble(1));
     70    Assert.ok(stmt.getIsNull(2));
     71    let count = { value: 0 };
     72    let blob = { value: null };
     73    stmt.getBlob(3, count, blob);
     74    Assert.equal(BLOB.length, count.value);
     75    for (let i = 0; i < BLOB.length; i++) {
     76      Assert.equal(BLOB[i], blob.value[i]);
     77    }
     78  } finally {
     79    stmt.finalize();
     80  }
     81 
     82  // Make sure we have two rows in the table
     83  stmt = db.createStatement("SELECT COUNT(1) FROM test");
     84  try {
     85    Assert.ok(stmt.executeStep());
     86    Assert.equal(2, stmt.getInt32(0));
     87  } finally {
     88    stmt.finalize();
     89  }
     90 
     91  stmts[0].finalize();
     92  stmts[1].finalize();
     93 });
     94 
     95 add_task(async function test_last_multiple_bindings_on_statements() {
     96  // This tests to make sure that we pass all the statements multiply bound
     97  // parameters when we call executeAsync.
     98  const AMOUNT_TO_ADD = 5;
     99  const ITERATIONS = 5;
    100 
    101  let stmts = [];
    102  let db = getOpenedDatabase();
    103  let sqlString =
    104    "INSERT INTO test (id, string, number, nuller, blober) " +
    105    "VALUES (:int, :text, :real, :null, :blob)";
    106  // We run the same statement twice, and should insert 2 * AMOUNT_TO_ADD.
    107  for (let i = 0; i < ITERATIONS; i++) {
    108    // alternate the type of statement we create
    109    if (i % 2) {
    110      stmts[i] = db.createStatement(sqlString);
    111    } else {
    112      stmts[i] = db.createAsyncStatement(sqlString);
    113    }
    114 
    115    let params = stmts[i].newBindingParamsArray();
    116    for (let j = 0; j < AMOUNT_TO_ADD; j++) {
    117      let bp = params.newBindingParams();
    118      bp.bindByName("int", INTEGER);
    119      bp.bindByName("text", TEXT);
    120      bp.bindByName("real", REAL);
    121      bp.bindByName("null", null);
    122      bp.bindBlobByName("blob", BLOB);
    123      params.addParams(bp);
    124    }
    125    stmts[i].bindParameters(params);
    126  }
    127 
    128  // Get our current number of rows in the table.
    129  let currentRows = 0;
    130  let countStmt = getOpenedDatabase().createStatement(
    131    "SELECT COUNT(1) AS count FROM test"
    132  );
    133  try {
    134    Assert.ok(countStmt.executeStep());
    135    currentRows = countStmt.row.count;
    136  } finally {
    137    countStmt.reset();
    138  }
    139 
    140  // Execute asynchronously.
    141  let execResult = await executeMultipleStatementsAsync(db, stmts, function () {
    142    ok(false, "we only did inserts so we should not have gotten results!");
    143  });
    144  equal(
    145    Ci.mozIStorageStatementCallback.REASON_FINISHED,
    146    execResult,
    147    "execution should have finished successfully."
    148  );
    149 
    150  // Check to make sure we added all of our rows.
    151  try {
    152    Assert.ok(countStmt.executeStep());
    153    Assert.equal(currentRows + ITERATIONS * AMOUNT_TO_ADD, countStmt.row.count);
    154  } finally {
    155    countStmt.finalize();
    156  }
    157 
    158  stmts.forEach(stmt => stmt.finalize());
    159 
    160  // we are the last test using this connection and since it has gone async
    161  // we *must* call asyncClose on it.
    162  await asyncClose(db);
    163  gDBConn = null;
    164 });
    165 
    166 // If you add a test down here you will need to move the asyncClose or clean
    167 // things up a little more.