tor-browser

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

test_connection_executeSimpleSQLAsync.js (2443B)


      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
      7 * mozIStorageAsyncConnection::executeSimpleSQLAsync.
      8 */
      9 
     10 const INTEGER = 1;
     11 const TEXT = "this is test text";
     12 const REAL = 3.23;
     13 
     14 add_task(async function test_create_and_add() {
     15  let adb = await openAsyncDatabase(getTestDB());
     16 
     17  let completion = await executeSimpleSQLAsync(
     18    adb,
     19    "CREATE TABLE test (id INTEGER, string TEXT, number REAL)"
     20  );
     21 
     22  Assert.equal(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
     23 
     24  completion = await executeSimpleSQLAsync(
     25    adb,
     26    "INSERT INTO test (id, string, number) " +
     27      "VALUES (" +
     28      INTEGER +
     29      ', "' +
     30      TEXT +
     31      '", ' +
     32      REAL +
     33      ")"
     34  );
     35 
     36  Assert.equal(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
     37 
     38  let result = null;
     39 
     40  completion = await executeSimpleSQLAsync(
     41    adb,
     42    "SELECT string, number FROM test WHERE id = 1",
     43    function (aResultSet) {
     44      result = aResultSet.getNextRow();
     45      Assert.equal(2, result.numEntries);
     46      Assert.equal(TEXT, result.getString(0));
     47      Assert.equal(REAL, result.getDouble(1));
     48    }
     49  );
     50 
     51  Assert.equal(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
     52  Assert.notEqual(result, null);
     53  result = null;
     54 
     55  await executeSimpleSQLAsync(
     56    adb,
     57    "SELECT COUNT(0) FROM test",
     58    function (aResultSet) {
     59      result = aResultSet.getNextRow();
     60      Assert.equal(1, result.getInt32(0));
     61    }
     62  );
     63 
     64  Assert.notEqual(result, null);
     65 
     66  await asyncClose(adb);
     67 });
     68 
     69 add_task(async function test_asyncClose_does_not_complete_before_statement() {
     70  let adb = await openAsyncDatabase(getTestDB());
     71  let executed = false;
     72 
     73  let reason = await executeSimpleSQLAsync(
     74    adb,
     75    "SELECT * FROM test",
     76    function (aResultSet) {
     77      let result = aResultSet.getNextRow();
     78 
     79      Assert.notEqual(result, null);
     80      Assert.equal(3, result.numEntries);
     81      Assert.equal(INTEGER, result.getInt32(0));
     82      Assert.equal(TEXT, result.getString(1));
     83      Assert.equal(REAL, result.getDouble(2));
     84      executed = true;
     85    }
     86  );
     87 
     88  Assert.equal(Ci.mozIStorageStatementCallback.REASON_FINISHED, reason);
     89 
     90  // Ensure that the statement executed to completion.
     91  Assert.ok(executed);
     92 
     93  await asyncClose(adb);
     94 });