Path: blob/main/contrib/kyua/utils/sqlite/transaction_test.cpp
48199 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/transaction.hpp"2930#include <atf-c++.hpp>3132#include "utils/format/macros.hpp"33#include "utils/sqlite/database.hpp"34#include "utils/sqlite/exceptions.hpp"35#include "utils/sqlite/statement.ipp"3637namespace sqlite = utils::sqlite;383940namespace {414243/// Ensures that a table has a single specific value in a column.44///45/// \param db The SQLite database.46/// \param table_name The table to be checked.47/// \param column_name The column to be checked.48/// \param exp_value The value expected to be found in the column.49///50/// \return True if the column contains a single value and it matches exp_value;51/// false if not. If the query fails, the calling test is marked as bad.52static bool53check_in_table(sqlite::database& db, const char* table_name,54const char* column_name, int exp_value)55{56sqlite::statement stmt = db.create_statement(57F("SELECT * FROM %s WHERE %s == %s") % table_name % column_name %58exp_value);59if (!stmt.step())60return false;61if (stmt.step())62ATF_FAIL("More than one value found in table");63return true;64}656667} // anonymous namespace686970ATF_TEST_CASE_WITHOUT_HEAD(automatic_rollback);71ATF_TEST_CASE_BODY(automatic_rollback)72{73sqlite::database db = sqlite::database::in_memory();74db.exec("CREATE TABLE t (col INTEGER PRIMARY KEY)");75db.exec("INSERT INTO t VALUES (3)");76{77sqlite::transaction tx = db.begin_transaction();78db.exec("INSERT INTO t VALUES (5)");79}80ATF_REQUIRE( check_in_table(db, "t", "col", 3));81ATF_REQUIRE(!check_in_table(db, "t", "col", 5));82}838485ATF_TEST_CASE_WITHOUT_HEAD(explicit_commit);86ATF_TEST_CASE_BODY(explicit_commit)87{88sqlite::database db = sqlite::database::in_memory();89db.exec("CREATE TABLE t (col INTEGER PRIMARY KEY)");90db.exec("INSERT INTO t VALUES (3)");91{92sqlite::transaction tx = db.begin_transaction();93db.exec("INSERT INTO t VALUES (5)");94tx.commit();95}96ATF_REQUIRE(check_in_table(db, "t", "col", 3));97ATF_REQUIRE(check_in_table(db, "t", "col", 5));98}99100101ATF_TEST_CASE_WITHOUT_HEAD(explicit_rollback);102ATF_TEST_CASE_BODY(explicit_rollback)103{104sqlite::database db = sqlite::database::in_memory();105db.exec("CREATE TABLE t (col INTEGER PRIMARY KEY)");106db.exec("INSERT INTO t VALUES (3)");107{108sqlite::transaction tx = db.begin_transaction();109db.exec("INSERT INTO t VALUES (5)");110tx.rollback();111}112ATF_REQUIRE( check_in_table(db, "t", "col", 3));113ATF_REQUIRE(!check_in_table(db, "t", "col", 5));114}115116117ATF_TEST_CASE_WITHOUT_HEAD(nested_fail);118ATF_TEST_CASE_BODY(nested_fail)119{120sqlite::database db = sqlite::database::in_memory();121{122sqlite::transaction tx = db.begin_transaction();123ATF_REQUIRE_THROW(sqlite::error, db.begin_transaction());124}125}126127128ATF_INIT_TEST_CASES(tcs)129{130ATF_ADD_TEST_CASE(tcs, automatic_rollback);131ATF_ADD_TEST_CASE(tcs, explicit_commit);132ATF_ADD_TEST_CASE(tcs, explicit_rollback);133ATF_ADD_TEST_CASE(tcs, nested_fail);134}135136137