xref: /freebsd/contrib/kyua/utils/sqlite/test_utils.hpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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.
16 //
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.
28 
29 /// \file utils/sqlite/test_utils.hpp
30 /// Utilities for tests of the sqlite modules.
31 ///
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.
35 
36 #if !defined(UTILS_SQLITE_TEST_UTILS_HPP)
37 #   define UTILS_SQLITE_TEST_UTILS_HPP
38 #else
39 #   error "test_utils.hpp can only be included once"
40 #endif
41 
42 #include <iostream>
43 
44 #include <atf-c++.hpp>
45 
46 #include "utils/defs.hpp"
47 #include "utils/sqlite/c_gate.hpp"
48 #include "utils/sqlite/exceptions.hpp"
49 
50 
51 namespace {
52 
53 
54 /// Checks that a given expression raises a particular sqlite::api_error.
55 ///
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).
59 ///
60 /// \param exp_api_function The name of the SQLite 3 C API function that causes
61 ///     the error.
62 /// \param statement The statement to execute.
63 #define REQUIRE_API_ERROR(exp_api_function, statement) \
64     do { \
65         try { \
66             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()); \
70         } \
71     } while (0)
72 
73 
74 /// Gets the pointer to the internal sqlite3 of a database object.
75 ///
76 /// This is pure syntactic sugar to simplify typing in the test cases.
77 ///
78 /// \param db The SQLite database.
79 ///
80 /// \return The internal sqlite3 of the input database.
81 static inline ::sqlite3*
raw(utils::sqlite::database & db)82 raw(utils::sqlite::database& db)
83 {
84     return utils::sqlite::database_c_gate(db).c_database();
85 }
86 
87 
88 /// SQL commands to create a test table.
89 ///
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";
98 
99 
100 static void create_test_table(::sqlite3*) UTILS_UNUSED;
101 
102 
103 /// Creates a 'test' table in a database.
104 ///
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.
107 ///
108 /// Use the verify_test_table() function on the same database to verify that
109 /// the table is present and contains the expected data.
110 ///
111 /// \param db The database in which to create the test table.
112 static void
create_test_table(::sqlite3 * db)113 create_test_table(::sqlite3* db)
114 {
115     std::cout << "Creating 'test' table in the database\n";
116     ATF_REQUIRE_EQ(SQLITE_OK, ::sqlite3_exec(db, create_test_table_sql,
117                                              NULL, NULL, NULL));
118 }
119 
120 
121 static void verify_test_table(::sqlite3*) UTILS_UNUSED;
122 
123 
124 /// Verifies that the specified database contains the 'test' table.
125 ///
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.
129 ///
130 /// \param db The database to validate.
131 static void
verify_test_table(::sqlite3 * db)132 verify_test_table(::sqlite3* db)
133 {
134     std::cout << "Verifying that the 'test' table is in the database\n";
135     char **result;
136     int rows, columns;
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);
148 }
149 
150 
151 }  // anonymous namespace
152