Path: blob/main/contrib/kyua/utils/sqlite/exceptions_test.cpp
48198 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#include "utils/sqlite/exceptions.hpp"2930extern "C" {31#include <sqlite3.h>32}3334#include <cstring>35#include <string>3637#include <atf-c++.hpp>3839#include "utils/fs/path.hpp"40#include "utils/optional.ipp"41#include "utils/sqlite/c_gate.hpp"42#include "utils/sqlite/database.hpp"4344namespace fs = utils::fs;45namespace sqlite = utils::sqlite;4647using utils::none;484950ATF_TEST_CASE_WITHOUT_HEAD(error__no_filename);51ATF_TEST_CASE_BODY(error__no_filename)52{53const sqlite::database db = sqlite::database::in_memory();54const sqlite::error e(db.db_filename(), "Some text");55ATF_REQUIRE_EQ("Some text (sqlite db: in-memory or temporary)",56std::string(e.what()));57ATF_REQUIRE_EQ(db.db_filename(), e.db_filename());58}596061ATF_TEST_CASE_WITHOUT_HEAD(error__with_filename);62ATF_TEST_CASE_BODY(error__with_filename)63{64const sqlite::database db = sqlite::database::open(65fs::path("test.db"), sqlite::open_readwrite | sqlite::open_create);66const sqlite::error e(db.db_filename(), "Some text");67ATF_REQUIRE_EQ("Some text (sqlite db: test.db)", std::string(e.what()));68ATF_REQUIRE_EQ(db.db_filename(), e.db_filename());69}707172ATF_TEST_CASE_WITHOUT_HEAD(api_error__explicit);73ATF_TEST_CASE_BODY(api_error__explicit)74{75const sqlite::api_error e(none, "some_function", "Some text");76ATF_REQUIRE_EQ(77"Some text (sqlite op: some_function) "78"(sqlite db: in-memory or temporary)",79std::string(e.what()));80ATF_REQUIRE_EQ("some_function", e.api_function());81}828384ATF_TEST_CASE_WITHOUT_HEAD(api_error__from_database);85ATF_TEST_CASE_BODY(api_error__from_database)86{87sqlite::database db = sqlite::database::open(88fs::path("test.db"), sqlite::open_readwrite | sqlite::open_create);8990// Use the raw sqlite3 API to cause an error. Our C++ wrappers catch all91// errors and reraise them as exceptions, but here we want to handle the raw92// error directly for testing purposes.93sqlite::database_c_gate gate(db);94::sqlite3_stmt* dummy_stmt;95const char* query = "ABCDE INVALID QUERY";96(void)::sqlite3_prepare_v2(gate.c_database(), query, std::strlen(query),97&dummy_stmt, NULL);9899const sqlite::api_error e = sqlite::api_error::from_database(100db, "real_function");101ATF_REQUIRE_MATCH(102".*ABCDE.*\\(sqlite op: real_function\\) \\(sqlite db: test.db\\)",103std::string(e.what()));104ATF_REQUIRE_EQ("real_function", e.api_function());105}106107108ATF_TEST_CASE_WITHOUT_HEAD(invalid_column_error);109ATF_TEST_CASE_BODY(invalid_column_error)110{111const sqlite::invalid_column_error e(none, "some_name");112ATF_REQUIRE_EQ("Unknown column 'some_name' "113"(sqlite db: in-memory or temporary)",114std::string(e.what()));115ATF_REQUIRE_EQ("some_name", e.column_name());116}117118119ATF_INIT_TEST_CASES(tcs)120{121ATF_ADD_TEST_CASE(tcs, error__no_filename);122ATF_ADD_TEST_CASE(tcs, error__with_filename);123124ATF_ADD_TEST_CASE(tcs, api_error__explicit);125ATF_ADD_TEST_CASE(tcs, api_error__from_database);126127ATF_ADD_TEST_CASE(tcs, invalid_column_error);128}129130131