1 // Copyright 2011 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /// \file utils/sqlite/test_utils.hpp
30 /// Utilities for tests of the sqlite modules.
32 /// This file is intended to be included once, and only once, for every test
33 /// program that needs it. All the code is herein contained to simplify the
34 /// dependency chain in the build rules.
36 #if !defined(UTILS_SQLITE_TEST_UTILS_HPP)
37 # define UTILS_SQLITE_TEST_UTILS_HPP
39 # error "test_utils.hpp can only be included once"
44 #include <atf-c++.hpp>
46 #include "utils/defs.hpp"
47 #include "utils/sqlite/c_gate.hpp"
48 #include "utils/sqlite/exceptions.hpp"
54 /// Checks that a given expression raises a particular sqlite::api_error.
56 /// We cannot make any assumptions regarding the error text provided by SQLite,
57 /// so we resort to checking only which API function raised the error (because
58 /// our code is the one hardcoding these strings).
60 /// \param exp_api_function The name of the SQLite 3 C API function that causes
62 /// \param statement The statement to execute.
63 #define REQUIRE_API_ERROR(exp_api_function, statement) \
67 ATF_FAIL("api_error not raised by " #statement); \
68 } catch (const utils::sqlite::api_error& api_error) { \
69 ATF_REQUIRE_EQ(exp_api_function, api_error.api_function()); \
74 /// Gets the pointer to the internal sqlite3 of a database object.
76 /// This is pure syntactic sugar to simplify typing in the test cases.
78 /// \param db The SQLite database.
80 /// \return The internal sqlite3 of the input database.
81 static inline ::sqlite3
*
82 raw(utils::sqlite::database
& db
)
84 return utils::sqlite::database_c_gate(db
).c_database();
88 /// SQL commands to create a test table.
90 /// See create_test_table() for more details.
91 static const char* create_test_table_sql
=
92 "CREATE TABLE test (prime INTEGER PRIMARY KEY);"
93 "INSERT INTO test (prime) VALUES (1);\n"
94 "INSERT INTO test (prime) VALUES (2);\n"
95 "INSERT INTO test (prime) VALUES (7);\n"
96 "INSERT INTO test (prime) VALUES (5);\n"
97 "INSERT INTO test (prime) VALUES (3);\n";
100 static void create_test_table(::sqlite3
*) UTILS_UNUSED
;
103 /// Creates a 'test' table in a database.
105 /// The created 'test' table is populated with a few rows. If there are any
106 /// problems creating the database, this fails the test case.
108 /// Use the verify_test_table() function on the same database to verify that
109 /// the table is present and contains the expected data.
111 /// \param db The database in which to create the test table.
113 create_test_table(::sqlite3
* db
)
115 std::cout
<< "Creating 'test' table in the database\n";
116 ATF_REQUIRE_EQ(SQLITE_OK
, ::sqlite3_exec(db
, create_test_table_sql
,
121 static void verify_test_table(::sqlite3
*) UTILS_UNUSED
;
124 /// Verifies that the specified database contains the 'test' table.
126 /// This function ensures that the provided database contains the 'test' table
127 /// created by the create_test_table() function on the same database. If it
128 /// doesn't, this fails the caller test case.
130 /// \param db The database to validate.
132 verify_test_table(::sqlite3
* db
)
134 std::cout
<< "Verifying that the 'test' table is in the database\n";
137 ATF_REQUIRE_EQ(SQLITE_OK
, ::sqlite3_get_table(db
,
138 "SELECT * FROM test ORDER BY prime", &result
, &rows
, &columns
, NULL
));
139 ATF_REQUIRE_EQ(5, rows
);
140 ATF_REQUIRE_EQ(1, columns
);
141 ATF_REQUIRE_EQ("prime", std::string(result
[0]));
142 ATF_REQUIRE_EQ("1", std::string(result
[1]));
143 ATF_REQUIRE_EQ("2", std::string(result
[2]));
144 ATF_REQUIRE_EQ("3", std::string(result
[3]));
145 ATF_REQUIRE_EQ("5", std::string(result
[4]));
146 ATF_REQUIRE_EQ("7", std::string(result
[5]));
147 ::sqlite3_free_table(result
);
151 } // anonymous namespace