1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include "utils/sqlite/transaction.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc #include <atf-c++.hpp>
32*11be35a1SLionel Sambuc
33*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
34*11be35a1SLionel Sambuc #include "utils/sqlite/database.hpp"
35*11be35a1SLionel Sambuc #include "utils/sqlite/exceptions.hpp"
36*11be35a1SLionel Sambuc #include "utils/sqlite/statement.ipp"
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc namespace sqlite = utils::sqlite;
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc namespace {
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc /// Ensures that a table has a single specific value in a column.
45*11be35a1SLionel Sambuc ///
46*11be35a1SLionel Sambuc /// \param db The SQLite database.
47*11be35a1SLionel Sambuc /// \param table_name The table to be checked.
48*11be35a1SLionel Sambuc /// \param column_name The column to be checked.
49*11be35a1SLionel Sambuc /// \param exp_value The value expected to be found in the column.
50*11be35a1SLionel Sambuc ///
51*11be35a1SLionel Sambuc /// \return True if the column contains a single value and it matches exp_value;
52*11be35a1SLionel Sambuc /// false if not. If the query fails, the calling test is marked as bad.
53*11be35a1SLionel Sambuc static bool
check_in_table(sqlite::database & db,const char * table_name,const char * column_name,int exp_value)54*11be35a1SLionel Sambuc check_in_table(sqlite::database& db, const char* table_name,
55*11be35a1SLionel Sambuc const char* column_name, int exp_value)
56*11be35a1SLionel Sambuc {
57*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement(
58*11be35a1SLionel Sambuc F("SELECT * FROM %s WHERE %s == %s") % table_name % column_name %
59*11be35a1SLionel Sambuc exp_value);
60*11be35a1SLionel Sambuc if (!stmt.step())
61*11be35a1SLionel Sambuc return false;
62*11be35a1SLionel Sambuc if (stmt.step())
63*11be35a1SLionel Sambuc ATF_FAIL("More than one value found in table");
64*11be35a1SLionel Sambuc return true;
65*11be35a1SLionel Sambuc }
66*11be35a1SLionel Sambuc
67*11be35a1SLionel Sambuc
68*11be35a1SLionel Sambuc } // anonymous namespace
69*11be35a1SLionel Sambuc
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(automatic_rollback);
ATF_TEST_CASE_BODY(automatic_rollback)72*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(automatic_rollback)
73*11be35a1SLionel Sambuc {
74*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
75*11be35a1SLionel Sambuc db.exec("CREATE TABLE t (col INTEGER PRIMARY KEY)");
76*11be35a1SLionel Sambuc db.exec("INSERT INTO t VALUES (3)");
77*11be35a1SLionel Sambuc {
78*11be35a1SLionel Sambuc sqlite::transaction tx = db.begin_transaction();
79*11be35a1SLionel Sambuc db.exec("INSERT INTO t VALUES (5)");
80*11be35a1SLionel Sambuc }
81*11be35a1SLionel Sambuc ATF_REQUIRE( check_in_table(db, "t", "col", 3));
82*11be35a1SLionel Sambuc ATF_REQUIRE(!check_in_table(db, "t", "col", 5));
83*11be35a1SLionel Sambuc }
84*11be35a1SLionel Sambuc
85*11be35a1SLionel Sambuc
86*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(explicit_commit);
ATF_TEST_CASE_BODY(explicit_commit)87*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(explicit_commit)
88*11be35a1SLionel Sambuc {
89*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
90*11be35a1SLionel Sambuc db.exec("CREATE TABLE t (col INTEGER PRIMARY KEY)");
91*11be35a1SLionel Sambuc db.exec("INSERT INTO t VALUES (3)");
92*11be35a1SLionel Sambuc {
93*11be35a1SLionel Sambuc sqlite::transaction tx = db.begin_transaction();
94*11be35a1SLionel Sambuc db.exec("INSERT INTO t VALUES (5)");
95*11be35a1SLionel Sambuc tx.commit();
96*11be35a1SLionel Sambuc }
97*11be35a1SLionel Sambuc ATF_REQUIRE(check_in_table(db, "t", "col", 3));
98*11be35a1SLionel Sambuc ATF_REQUIRE(check_in_table(db, "t", "col", 5));
99*11be35a1SLionel Sambuc }
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc
102*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(explicit_rollback);
ATF_TEST_CASE_BODY(explicit_rollback)103*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(explicit_rollback)
104*11be35a1SLionel Sambuc {
105*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
106*11be35a1SLionel Sambuc db.exec("CREATE TABLE t (col INTEGER PRIMARY KEY)");
107*11be35a1SLionel Sambuc db.exec("INSERT INTO t VALUES (3)");
108*11be35a1SLionel Sambuc {
109*11be35a1SLionel Sambuc sqlite::transaction tx = db.begin_transaction();
110*11be35a1SLionel Sambuc db.exec("INSERT INTO t VALUES (5)");
111*11be35a1SLionel Sambuc tx.rollback();
112*11be35a1SLionel Sambuc }
113*11be35a1SLionel Sambuc ATF_REQUIRE( check_in_table(db, "t", "col", 3));
114*11be35a1SLionel Sambuc ATF_REQUIRE(!check_in_table(db, "t", "col", 5));
115*11be35a1SLionel Sambuc }
116*11be35a1SLionel Sambuc
117*11be35a1SLionel Sambuc
118*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(nested_fail);
ATF_TEST_CASE_BODY(nested_fail)119*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(nested_fail)
120*11be35a1SLionel Sambuc {
121*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
122*11be35a1SLionel Sambuc {
123*11be35a1SLionel Sambuc sqlite::transaction tx = db.begin_transaction();
124*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(sqlite::error, db.begin_transaction());
125*11be35a1SLionel Sambuc }
126*11be35a1SLionel Sambuc }
127*11be35a1SLionel Sambuc
128*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)129*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
130*11be35a1SLionel Sambuc {
131*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, automatic_rollback);
132*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, explicit_commit);
133*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, explicit_rollback);
134*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, nested_fail);
135*11be35a1SLionel Sambuc }
136