storage/test/unit/test_connection_executeSimpleSQLAsync.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/unit/test_connection_executeSimpleSQLAsync.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 + * This file tests the functionality of
    1.10 + * mozIStorageAsyncConnection::executeSimpleSQLAsync.
    1.11 + */
    1.12 +
    1.13 +const INTEGER = 1;
    1.14 +const TEXT = "this is test text";
    1.15 +const REAL = 3.23;
    1.16 +
    1.17 +function asyncClose(db) {
    1.18 +  let deferred = Promise.defer();
    1.19 +  db.asyncClose(function(status) {
    1.20 +    if (Components.isSuccessCode(status)) {
    1.21 +      deferred.resolve();
    1.22 +    } else {
    1.23 +      deferred.reject(status);
    1.24 +    }
    1.25 +  });
    1.26 +  return deferred.promise;
    1.27 +}
    1.28 +
    1.29 +function openAsyncDatabase(file) {
    1.30 +  let deferred = Promise.defer();
    1.31 +  getService().openAsyncDatabase(file, null, function(status, db) {
    1.32 +    if (Components.isSuccessCode(status)) {
    1.33 +      deferred.resolve(db.QueryInterface(Ci.mozIStorageAsyncConnection));
    1.34 +    } else {
    1.35 +      deferred.reject(status);
    1.36 +    }
    1.37 +  });
    1.38 +  return deferred.promise;
    1.39 +}
    1.40 +
    1.41 +function executeSimpleSQLAsync(db, query, onResult) {
    1.42 +  let deferred = Promise.defer();
    1.43 +  db.executeSimpleSQLAsync(query, {
    1.44 +    handleError: function(error) {
    1.45 +      deferred.reject(error);
    1.46 +    },
    1.47 +    handleResult: function(result) {
    1.48 +      if (onResult) {
    1.49 +        onResult(result);
    1.50 +      } else {
    1.51 +        do_throw("No results were expected");
    1.52 +      }
    1.53 +    },
    1.54 +    handleCompletion: function(result) {
    1.55 +      deferred.resolve(result);
    1.56 +    }
    1.57 +  });
    1.58 +  return deferred.promise;
    1.59 +}
    1.60 +
    1.61 +add_task(function test_create_and_add() {
    1.62 +  let adb = yield openAsyncDatabase(getTestDB());
    1.63 +
    1.64 +  let completion = yield executeSimpleSQLAsync(adb,
    1.65 +    "CREATE TABLE test (id INTEGER, string TEXT, number REAL)");
    1.66 +
    1.67 +  do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
    1.68 +
    1.69 +  completion = yield executeSimpleSQLAsync(adb,
    1.70 +    "INSERT INTO test (id, string, number) " +
    1.71 +    "VALUES (" + INTEGER + ", \"" + TEXT + "\", " + REAL + ")");
    1.72 +
    1.73 +  do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
    1.74 +
    1.75 +  let result = null;
    1.76 +
    1.77 +  completion = yield executeSimpleSQLAsync(adb,
    1.78 +    "SELECT string, number FROM test WHERE id = 1",
    1.79 +    function(aResultSet) {
    1.80 +      result = aResultSet.getNextRow();
    1.81 +      do_check_eq(2, result.numEntries);
    1.82 +      do_check_eq(TEXT, result.getString(0));
    1.83 +      do_check_eq(REAL, result.getDouble(1));
    1.84 +    }
    1.85 +  );
    1.86 +
    1.87 +  do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
    1.88 +  do_check_neq(result, null);
    1.89 +  result = null;
    1.90 +
    1.91 +  yield executeSimpleSQLAsync(adb, "SELECT COUNT(0) FROM test",
    1.92 +    function(aResultSet) {
    1.93 +      result = aResultSet.getNextRow();
    1.94 +      do_check_eq(1, result.getInt32(0));
    1.95 +    });
    1.96 +
    1.97 +  do_check_neq(result, null);
    1.98 +
    1.99 +  yield asyncClose(adb);
   1.100 +});
   1.101 +
   1.102 +
   1.103 +add_task(function test_asyncClose_does_not_complete_before_statement() {
   1.104 +  let adb = yield openAsyncDatabase(getTestDB());
   1.105 +  let executed = false;
   1.106 +
   1.107 +  let reason = yield executeSimpleSQLAsync(adb, "SELECT * FROM test",
   1.108 +    function(aResultSet) {
   1.109 +      let result = aResultSet.getNextRow();
   1.110 +
   1.111 +      do_check_neq(result, null);
   1.112 +      do_check_eq(3, result.numEntries);
   1.113 +      do_check_eq(INTEGER, result.getInt32(0));
   1.114 +      do_check_eq(TEXT, result.getString(1));
   1.115 +      do_check_eq(REAL, result.getDouble(2));
   1.116 +      executed = true;
   1.117 +    }
   1.118 +  );
   1.119 +
   1.120 +  do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, reason);
   1.121 +
   1.122 +  // Ensure that the statement executed to completion.
   1.123 +  do_check_true(executed);
   1.124 +
   1.125 +  yield asyncClose(adb);
   1.126 +});
   1.127 +
   1.128 +////////////////////////////////////////////////////////////////////////////////
   1.129 +//// Test Runner
   1.130 +
   1.131 +function run_test()
   1.132 +{
   1.133 +  run_next_test();
   1.134 +}

mercurial