xref: /minix3/external/bsd/kyua-cli/dist/utils/sqlite/test_utils.hpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
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 /// \file utils/sqlite/test_utils.hpp
30*11be35a1SLionel Sambuc /// Utilities for tests of the sqlite modules.
31*11be35a1SLionel Sambuc ///
32*11be35a1SLionel Sambuc /// This file is intended to be included once, and only once, for every test
33*11be35a1SLionel Sambuc /// program that needs it.  All the code is herein contained to simplify the
34*11be35a1SLionel Sambuc /// dependency chain in the build rules.
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc #if !defined(UTILS_SQLITE_TEST_UTILS_HPP)
37*11be35a1SLionel Sambuc #   define UTILS_SQLITE_TEST_UTILS_HPP
38*11be35a1SLionel Sambuc #else
39*11be35a1SLionel Sambuc #   error "test_utils.hpp can only be included once"
40*11be35a1SLionel Sambuc #endif
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc #include <iostream>
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc #include <atf-c++.hpp>
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc #include "utils/defs.hpp"
47*11be35a1SLionel Sambuc #include "utils/sqlite/c_gate.hpp"
48*11be35a1SLionel Sambuc #include "utils/sqlite/exceptions.hpp"
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc namespace {
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc /// Checks that a given expression raises a particular sqlite::api_error.
55*11be35a1SLionel Sambuc ///
56*11be35a1SLionel Sambuc /// We cannot make any assumptions regarding the error text provided by SQLite,
57*11be35a1SLionel Sambuc /// so we resort to checking only which API function raised the error (because
58*11be35a1SLionel Sambuc /// our code is the one hardcoding these strings).
59*11be35a1SLionel Sambuc ///
60*11be35a1SLionel Sambuc /// \param exp_api_function The name of the SQLite 3 C API function that causes
61*11be35a1SLionel Sambuc ///     the error.
62*11be35a1SLionel Sambuc /// \param statement The statement to execute.
63*11be35a1SLionel Sambuc #define REQUIRE_API_ERROR(exp_api_function, statement) \
64*11be35a1SLionel Sambuc     do { \
65*11be35a1SLionel Sambuc         try { \
66*11be35a1SLionel Sambuc             statement; \
67*11be35a1SLionel Sambuc             ATF_FAIL("api_error not raised by " #statement); \
68*11be35a1SLionel Sambuc         } catch (const utils::sqlite::api_error& api_error) { \
69*11be35a1SLionel Sambuc             ATF_REQUIRE_EQ(exp_api_function, api_error.api_function()); \
70*11be35a1SLionel Sambuc         } \
71*11be35a1SLionel Sambuc     } while (0)
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc /// Gets the pointer to the internal sqlite3 of a database object.
75*11be35a1SLionel Sambuc ///
76*11be35a1SLionel Sambuc /// This is pure syntactic sugar to simplify typing in the test cases.
77*11be35a1SLionel Sambuc ///
78*11be35a1SLionel Sambuc /// \param db The SQLite database.
79*11be35a1SLionel Sambuc ///
80*11be35a1SLionel Sambuc /// \return The internal sqlite3 of the input database.
81*11be35a1SLionel Sambuc static inline ::sqlite3*
raw(utils::sqlite::database & db)82*11be35a1SLionel Sambuc raw(utils::sqlite::database& db)
83*11be35a1SLionel Sambuc {
84*11be35a1SLionel Sambuc     return utils::sqlite::database_c_gate(db).c_database();
85*11be35a1SLionel Sambuc }
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc /// SQL commands to create a test table.
89*11be35a1SLionel Sambuc ///
90*11be35a1SLionel Sambuc /// See create_test_table() for more details.
91*11be35a1SLionel Sambuc static const char* create_test_table_sql =
92*11be35a1SLionel Sambuc     "CREATE TABLE test (prime INTEGER PRIMARY KEY);"
93*11be35a1SLionel Sambuc     "INSERT INTO test (prime) VALUES (1);\n"
94*11be35a1SLionel Sambuc     "INSERT INTO test (prime) VALUES (2);\n"
95*11be35a1SLionel Sambuc     "INSERT INTO test (prime) VALUES (7);\n"
96*11be35a1SLionel Sambuc     "INSERT INTO test (prime) VALUES (5);\n"
97*11be35a1SLionel Sambuc     "INSERT INTO test (prime) VALUES (3);\n";
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc static void create_test_table(::sqlite3*) UTILS_UNUSED;
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc 
103*11be35a1SLionel Sambuc /// Creates a 'test' table in a database.
104*11be35a1SLionel Sambuc ///
105*11be35a1SLionel Sambuc /// The created 'test' table is populated with a few rows.  If there are any
106*11be35a1SLionel Sambuc /// problems creating the database, this fails the test case.
107*11be35a1SLionel Sambuc ///
108*11be35a1SLionel Sambuc /// Use the verify_test_table() function on the same database to verify that
109*11be35a1SLionel Sambuc /// the table is present and contains the expected data.
110*11be35a1SLionel Sambuc ///
111*11be35a1SLionel Sambuc /// \param db The database in which to create the test table.
112*11be35a1SLionel Sambuc static void
create_test_table(::sqlite3 * db)113*11be35a1SLionel Sambuc create_test_table(::sqlite3* db)
114*11be35a1SLionel Sambuc {
115*11be35a1SLionel Sambuc     std::cout << "Creating 'test' table in the database\n";
116*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(SQLITE_OK, ::sqlite3_exec(db, create_test_table_sql,
117*11be35a1SLionel Sambuc                                              NULL, NULL, NULL));
118*11be35a1SLionel Sambuc }
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc 
121*11be35a1SLionel Sambuc static void verify_test_table(::sqlite3*) UTILS_UNUSED;
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc /// Verifies that the specified database contains the 'test' table.
125*11be35a1SLionel Sambuc ///
126*11be35a1SLionel Sambuc /// This function ensures that the provided database contains the 'test' table
127*11be35a1SLionel Sambuc /// created by the create_test_table() function on the same database.  If it
128*11be35a1SLionel Sambuc /// doesn't, this fails the caller test case.
129*11be35a1SLionel Sambuc ///
130*11be35a1SLionel Sambuc /// \param db The database to validate.
131*11be35a1SLionel Sambuc static void
verify_test_table(::sqlite3 * db)132*11be35a1SLionel Sambuc verify_test_table(::sqlite3* db)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc     std::cout << "Verifying that the 'test' table is in the database\n";
135*11be35a1SLionel Sambuc     char **result;
136*11be35a1SLionel Sambuc     int rows, columns;
137*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(SQLITE_OK, ::sqlite3_get_table(db,
138*11be35a1SLionel Sambuc         "SELECT * FROM test ORDER BY prime", &result, &rows, &columns, NULL));
139*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(5, rows);
140*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, columns);
141*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("prime", std::string(result[0]));
142*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("1", std::string(result[1]));
143*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("2", std::string(result[2]));
144*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("3", std::string(result[3]));
145*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("5", std::string(result[4]));
146*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("7", std::string(result[5]));
147*11be35a1SLionel Sambuc     ::sqlite3_free_table(result);
148*11be35a1SLionel Sambuc }
149*11be35a1SLionel Sambuc 
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc }  // anonymous namespace
152