Path: blob/main/contrib/kyua/store/write_backend_test.cpp
39478 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 "store/write_backend.hpp"2930#include <atf-c++.hpp>3132#include "store/exceptions.hpp"33#include "store/metadata.hpp"34#include "utils/datetime.hpp"35#include "utils/env.hpp"36#include "utils/fs/path.hpp"37#include "utils/logging/operations.hpp"38#include "utils/sqlite/database.hpp"39#include "utils/sqlite/exceptions.hpp"40#include "utils/sqlite/statement.ipp"4142namespace datetime = utils::datetime;43namespace fs = utils::fs;44namespace logging = utils::logging;45namespace sqlite = utils::sqlite;464748ATF_TEST_CASE(detail__initialize__ok);49ATF_TEST_CASE_HEAD(detail__initialize__ok)50{51logging::set_inmemory();52set_md_var("require.files", store::detail::schema_file().c_str());53}54ATF_TEST_CASE_BODY(detail__initialize__ok)55{56sqlite::database db = sqlite::database::in_memory();57const datetime::timestamp before = datetime::timestamp::now();58const store::metadata md = store::detail::initialize(db);59const datetime::timestamp after = datetime::timestamp::now();6061ATF_REQUIRE(md.timestamp() >= before.to_seconds());62ATF_REQUIRE(md.timestamp() <= after.to_microseconds());63ATF_REQUIRE_EQ(store::detail::current_schema_version, md.schema_version());6465// Query some known tables to ensure they were created.66db.exec("SELECT * FROM metadata");6768// And now query some know values.69sqlite::statement stmt = db.create_statement(70"SELECT COUNT(*) FROM metadata");71ATF_REQUIRE(stmt.step());72ATF_REQUIRE_EQ(1, stmt.column_int(0));73ATF_REQUIRE(!stmt.step());74}757677ATF_TEST_CASE_WITHOUT_HEAD(detail__initialize__missing_schema);78ATF_TEST_CASE_BODY(detail__initialize__missing_schema)79{80utils::setenv("KYUA_STOREDIR", "/non-existent");81store::detail::current_schema_version = 712;8283sqlite::database db = sqlite::database::in_memory();84ATF_REQUIRE_THROW_RE(store::error,85"Cannot read.*'/non-existent/schema_v712.sql'",86store::detail::initialize(db));87}888990ATF_TEST_CASE_WITHOUT_HEAD(detail__initialize__sqlite_error);91ATF_TEST_CASE_BODY(detail__initialize__sqlite_error)92{93utils::setenv("KYUA_STOREDIR", ".");94store::detail::current_schema_version = 712;9596atf::utils::create_file("schema_v712.sql", "foo_bar_baz;\n");9798sqlite::database db = sqlite::database::in_memory();99ATF_REQUIRE_THROW_RE(store::error, "Failed to initialize.*:.*foo_bar_baz",100store::detail::initialize(db));101}102103104ATF_TEST_CASE_WITHOUT_HEAD(detail__schema_file__builtin);105ATF_TEST_CASE_BODY(detail__schema_file__builtin)106{107utils::unsetenv("KYUA_STOREDIR");108ATF_REQUIRE_EQ(fs::path(KYUA_STOREDIR) / "schema_v3.sql",109store::detail::schema_file());110}111112113ATF_TEST_CASE_WITHOUT_HEAD(detail__schema_file__overriden);114ATF_TEST_CASE_BODY(detail__schema_file__overriden)115{116utils::setenv("KYUA_STOREDIR", "/tmp/test");117store::detail::current_schema_version = 123;118ATF_REQUIRE_EQ(fs::path("/tmp/test/schema_v123.sql"),119store::detail::schema_file());120}121122123ATF_TEST_CASE(write_backend__open_rw__ok_if_empty);124ATF_TEST_CASE_HEAD(write_backend__open_rw__ok_if_empty)125{126logging::set_inmemory();127set_md_var("require.files", store::detail::schema_file().c_str());128}129ATF_TEST_CASE_BODY(write_backend__open_rw__ok_if_empty)130{131{132sqlite::database db = sqlite::database::open(133fs::path("test.db"), sqlite::open_readwrite | sqlite::open_create);134}135store::write_backend backend = store::write_backend::open_rw(136fs::path("test.db"));137backend.database().exec("SELECT * FROM metadata");138}139140141ATF_TEST_CASE(write_backend__open_rw__error_if_not_empty);142ATF_TEST_CASE_HEAD(write_backend__open_rw__error_if_not_empty)143{144logging::set_inmemory();145set_md_var("require.files", store::detail::schema_file().c_str());146}147ATF_TEST_CASE_BODY(write_backend__open_rw__error_if_not_empty)148{149{150sqlite::database db = sqlite::database::open(151fs::path("test.db"), sqlite::open_readwrite | sqlite::open_create);152store::detail::initialize(db);153}154ATF_REQUIRE_THROW_RE(store::error, "test.db already exists",155store::write_backend::open_rw(fs::path("test.db")));156}157158159ATF_TEST_CASE(write_backend__open_rw__create_missing);160ATF_TEST_CASE_HEAD(write_backend__open_rw__create_missing)161{162logging::set_inmemory();163set_md_var("require.files", store::detail::schema_file().c_str());164}165ATF_TEST_CASE_BODY(write_backend__open_rw__create_missing)166{167store::write_backend backend = store::write_backend::open_rw(168fs::path("test.db"));169backend.database().exec("SELECT * FROM metadata");170}171172173ATF_TEST_CASE(write_backend__close);174ATF_TEST_CASE_HEAD(write_backend__close)175{176logging::set_inmemory();177set_md_var("require.files", store::detail::schema_file().c_str());178}179ATF_TEST_CASE_BODY(write_backend__close)180{181store::write_backend backend = store::write_backend::open_rw(182fs::path("test.db"));183backend.database().exec("SELECT * FROM metadata");184backend.close();185ATF_REQUIRE_THROW(utils::sqlite::error,186backend.database().exec("SELECT * FROM metadata"));187}188189190ATF_INIT_TEST_CASES(tcs)191{192ATF_ADD_TEST_CASE(tcs, detail__initialize__ok);193ATF_ADD_TEST_CASE(tcs, detail__initialize__missing_schema);194ATF_ADD_TEST_CASE(tcs, detail__initialize__sqlite_error);195196ATF_ADD_TEST_CASE(tcs, detail__schema_file__builtin);197ATF_ADD_TEST_CASE(tcs, detail__schema_file__overriden);198199ATF_ADD_TEST_CASE(tcs, write_backend__open_rw__ok_if_empty);200ATF_ADD_TEST_CASE(tcs, write_backend__open_rw__error_if_not_empty);201ATF_ADD_TEST_CASE(tcs, write_backend__open_rw__create_missing);202ATF_ADD_TEST_CASE(tcs, write_backend__close);203}204205206