storage/test/test_statement_scoper.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/test_statement_scoper.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,104 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "storage_test_harness.h"
    1.11 +
    1.12 +#include "mozStorageHelper.h"
    1.13 +
    1.14 +/**
    1.15 + * This file test our statement scoper in mozStorageHelper.h.
    1.16 + */
    1.17 +
    1.18 +void
    1.19 +test_automatic_reset()
    1.20 +{
    1.21 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    1.22 +
    1.23 +  // Need to create a table to populate sqlite_master with an entry.
    1.24 +  (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
    1.25 +    "CREATE TABLE test (id INTEGER PRIMARY KEY)"
    1.26 +  ));
    1.27 +
    1.28 +  nsCOMPtr<mozIStorageStatement> stmt;
    1.29 +  (void)db->CreateStatement(NS_LITERAL_CSTRING(
    1.30 +    "SELECT * FROM sqlite_master"
    1.31 +  ), getter_AddRefs(stmt));
    1.32 +
    1.33 +  // Reality check - make sure we start off in the right state.
    1.34 +  int32_t state = -1;
    1.35 +  (void)stmt->GetState(&state);
    1.36 +  do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);
    1.37 +
    1.38 +  // Start executing the statement, which will put it into an executing state.
    1.39 +  {
    1.40 +    mozStorageStatementScoper scoper(stmt);
    1.41 +    bool hasMore;
    1.42 +    do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)));
    1.43 +
    1.44 +    // Reality check that we are executing.
    1.45 +    state = -1;
    1.46 +    (void)stmt->GetState(&state);
    1.47 +    do_check_true(state ==
    1.48 +                  mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING);
    1.49 +  }
    1.50 +
    1.51 +  // And we should be ready again.
    1.52 +  state = -1;
    1.53 +  (void)stmt->GetState(&state);
    1.54 +  do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);
    1.55 +}
    1.56 +
    1.57 +void
    1.58 +test_Abandon()
    1.59 +{
    1.60 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    1.61 +
    1.62 +  // Need to create a table to populate sqlite_master with an entry.
    1.63 +  (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
    1.64 +    "CREATE TABLE test (id INTEGER PRIMARY KEY)"
    1.65 +  ));
    1.66 +
    1.67 +  nsCOMPtr<mozIStorageStatement> stmt;
    1.68 +  (void)db->CreateStatement(NS_LITERAL_CSTRING(
    1.69 +    "SELECT * FROM sqlite_master"
    1.70 +  ), getter_AddRefs(stmt));
    1.71 +
    1.72 +  // Reality check - make sure we start off in the right state.
    1.73 +  int32_t state = -1;
    1.74 +  (void)stmt->GetState(&state);
    1.75 +  do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);
    1.76 +
    1.77 +  // Start executing the statement, which will put it into an executing state.
    1.78 +  {
    1.79 +    mozStorageStatementScoper scoper(stmt);
    1.80 +    bool hasMore;
    1.81 +    do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)));
    1.82 +
    1.83 +    // Reality check that we are executing.
    1.84 +    state = -1;
    1.85 +    (void)stmt->GetState(&state);
    1.86 +    do_check_true(state ==
    1.87 +                  mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING);
    1.88 +
    1.89 +    // And call Abandon.  We should not reset now when we fall out of scope.
    1.90 +    scoper.Abandon();
    1.91 +  }
    1.92 +
    1.93 +  // And we should still be executing.
    1.94 +  state = -1;
    1.95 +  (void)stmt->GetState(&state);
    1.96 +  do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING);
    1.97 +}
    1.98 +
    1.99 +void (*gTests[])(void) = {
   1.100 +  test_automatic_reset,
   1.101 +  test_Abandon,
   1.102 +};
   1.103 +
   1.104 +const char *file = __FILE__;
   1.105 +#define TEST_NAME "statement scoper"
   1.106 +#define TEST_FILE file
   1.107 +#include "storage_test_harness_tail.h"

mercurial