Path: blob/main/contrib/kyua/utils/sqlite/test_utils.hpp
48178 views
// Copyright 2011 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file utils/sqlite/test_utils.hpp29/// Utilities for tests of the sqlite modules.30///31/// This file is intended to be included once, and only once, for every test32/// program that needs it. All the code is herein contained to simplify the33/// dependency chain in the build rules.3435#if !defined(UTILS_SQLITE_TEST_UTILS_HPP)36# define UTILS_SQLITE_TEST_UTILS_HPP37#else38# error "test_utils.hpp can only be included once"39#endif4041#include <iostream>4243#include <atf-c++.hpp>4445#include "utils/defs.hpp"46#include "utils/sqlite/c_gate.hpp"47#include "utils/sqlite/exceptions.hpp"484950namespace {515253/// Checks that a given expression raises a particular sqlite::api_error.54///55/// We cannot make any assumptions regarding the error text provided by SQLite,56/// so we resort to checking only which API function raised the error (because57/// our code is the one hardcoding these strings).58///59/// \param exp_api_function The name of the SQLite 3 C API function that causes60/// the error.61/// \param statement The statement to execute.62#define REQUIRE_API_ERROR(exp_api_function, statement) \63do { \64try { \65statement; \66ATF_FAIL("api_error not raised by " #statement); \67} catch (const utils::sqlite::api_error& api_error) { \68ATF_REQUIRE_EQ(exp_api_function, api_error.api_function()); \69} \70} while (0)717273/// Gets the pointer to the internal sqlite3 of a database object.74///75/// This is pure syntactic sugar to simplify typing in the test cases.76///77/// \param db The SQLite database.78///79/// \return The internal sqlite3 of the input database.80static inline ::sqlite3*81raw(utils::sqlite::database& db)82{83return utils::sqlite::database_c_gate(db).c_database();84}858687/// SQL commands to create a test table.88///89/// See create_test_table() for more details.90static const char* create_test_table_sql =91"CREATE TABLE test (prime INTEGER PRIMARY KEY);"92"INSERT INTO test (prime) VALUES (1);\n"93"INSERT INTO test (prime) VALUES (2);\n"94"INSERT INTO test (prime) VALUES (7);\n"95"INSERT INTO test (prime) VALUES (5);\n"96"INSERT INTO test (prime) VALUES (3);\n";979899static void create_test_table(::sqlite3*) UTILS_UNUSED;100101102/// Creates a 'test' table in a database.103///104/// The created 'test' table is populated with a few rows. If there are any105/// problems creating the database, this fails the test case.106///107/// Use the verify_test_table() function on the same database to verify that108/// the table is present and contains the expected data.109///110/// \param db The database in which to create the test table.111static void112create_test_table(::sqlite3* db)113{114std::cout << "Creating 'test' table in the database\n";115ATF_REQUIRE_EQ(SQLITE_OK, ::sqlite3_exec(db, create_test_table_sql,116NULL, NULL, NULL));117}118119120static void verify_test_table(::sqlite3*) UTILS_UNUSED;121122123/// Verifies that the specified database contains the 'test' table.124///125/// This function ensures that the provided database contains the 'test' table126/// created by the create_test_table() function on the same database. If it127/// doesn't, this fails the caller test case.128///129/// \param db The database to validate.130static void131verify_test_table(::sqlite3* db)132{133std::cout << "Verifying that the 'test' table is in the database\n";134char **result;135int rows, columns;136ATF_REQUIRE_EQ(SQLITE_OK, ::sqlite3_get_table(db,137"SELECT * FROM test ORDER BY prime", &result, &rows, &columns, NULL));138ATF_REQUIRE_EQ(5, rows);139ATF_REQUIRE_EQ(1, columns);140ATF_REQUIRE_EQ("prime", std::string(result[0]));141ATF_REQUIRE_EQ("1", std::string(result[1]));142ATF_REQUIRE_EQ("2", std::string(result[2]));143ATF_REQUIRE_EQ("3", std::string(result[3]));144ATF_REQUIRE_EQ("5", std::string(result[4]));145ATF_REQUIRE_EQ("7", std::string(result[5]));146::sqlite3_free_table(result);147}148149150} // anonymous namespace151152153