Path: blob/main/contrib/kyua/store/read_transaction_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/read_transaction.hpp"2930#include <map>31#include <string>3233#include <atf-c++.hpp>3435#include "model/context.hpp"36#include "model/metadata.hpp"37#include "model/test_program.hpp"38#include "model/test_result.hpp"39#include "store/exceptions.hpp"40#include "store/read_backend.hpp"41#include "store/write_backend.hpp"42#include "store/write_transaction.hpp"43#include "utils/datetime.hpp"44#include "utils/fs/path.hpp"45#include "utils/logging/operations.hpp"46#include "utils/optional.ipp"47#include "utils/sqlite/database.hpp"48#include "utils/sqlite/statement.ipp"4950namespace datetime = utils::datetime;51namespace fs = utils::fs;52namespace logging = utils::logging;53namespace sqlite = utils::sqlite;545556ATF_TEST_CASE(get_context__missing);57ATF_TEST_CASE_HEAD(get_context__missing)58{59logging::set_inmemory();60set_md_var("require.files", store::detail::schema_file().c_str());61}62ATF_TEST_CASE_BODY(get_context__missing)63{64store::write_backend::open_rw(fs::path("test.db")); // Create database.65store::read_backend backend = store::read_backend::open_ro(66fs::path("test.db"));6768store::read_transaction tx = backend.start_read();69ATF_REQUIRE_THROW_RE(store::error, "context: no data", tx.get_context());70}717273ATF_TEST_CASE(get_context__invalid_cwd);74ATF_TEST_CASE_HEAD(get_context__invalid_cwd)75{76logging::set_inmemory();77set_md_var("require.files", store::detail::schema_file().c_str());78}79ATF_TEST_CASE_BODY(get_context__invalid_cwd)80{81{82store::write_backend backend = store::write_backend::open_rw(83fs::path("test.db"));8485sqlite::statement stmt = backend.database().create_statement(86"INSERT INTO contexts (cwd) VALUES (:cwd)");87const char buffer[10] = "foo bar";88stmt.bind(":cwd", sqlite::blob(buffer, sizeof(buffer)));89stmt.step_without_results();90}9192store::read_backend backend = store::read_backend::open_ro(93fs::path("test.db"));94store::read_transaction tx = backend.start_read();95ATF_REQUIRE_THROW_RE(store::error, "context: .*cwd.*not a string",96tx.get_context());97}9899100ATF_TEST_CASE(get_context__invalid_env_vars);101ATF_TEST_CASE_HEAD(get_context__invalid_env_vars)102{103logging::set_inmemory();104set_md_var("require.files", store::detail::schema_file().c_str());105}106ATF_TEST_CASE_BODY(get_context__invalid_env_vars)107{108{109store::write_backend backend = store::write_backend::open_rw(110fs::path("test-bad-name.db"));111backend.database().exec("INSERT INTO contexts (cwd) "112"VALUES ('/foo/bar')");113const char buffer[10] = "foo bar";114115sqlite::statement stmt = backend.database().create_statement(116"INSERT INTO env_vars (var_name, var_value) "117"VALUES (:var_name, 'abc')");118stmt.bind(":var_name", sqlite::blob(buffer, sizeof(buffer)));119stmt.step_without_results();120}121{122store::read_backend backend = store::read_backend::open_ro(123fs::path("test-bad-name.db"));124store::read_transaction tx = backend.start_read();125ATF_REQUIRE_THROW_RE(store::error, "context: .*var_name.*not a string",126tx.get_context());127}128129{130store::write_backend backend = store::write_backend::open_rw(131fs::path("test-bad-value.db"));132backend.database().exec("INSERT INTO contexts (cwd) "133"VALUES ('/foo/bar')");134const char buffer[10] = "foo bar";135136sqlite::statement stmt = backend.database().create_statement(137"INSERT INTO env_vars (var_name, var_value) "138"VALUES ('abc', :var_value)");139stmt.bind(":var_value", sqlite::blob(buffer, sizeof(buffer)));140stmt.step_without_results();141}142{143store::read_backend backend = store::read_backend::open_ro(144fs::path("test-bad-value.db"));145store::read_transaction tx = backend.start_read();146ATF_REQUIRE_THROW_RE(store::error, "context: .*var_value.*not a string",147tx.get_context());148}149}150151152ATF_TEST_CASE(get_results__none);153ATF_TEST_CASE_HEAD(get_results__none)154{155logging::set_inmemory();156set_md_var("require.files", store::detail::schema_file().c_str());157}158ATF_TEST_CASE_BODY(get_results__none)159{160store::write_backend::open_rw(fs::path("test.db")); // Create database.161store::read_backend backend = store::read_backend::open_ro(162fs::path("test.db"));163store::read_transaction tx = backend.start_read();164store::results_iterator iter = tx.get_results();165ATF_REQUIRE(!iter);166}167168169ATF_TEST_CASE(get_results__many);170ATF_TEST_CASE_HEAD(get_results__many)171{172logging::set_inmemory();173set_md_var("require.files", store::detail::schema_file().c_str());174}175ATF_TEST_CASE_BODY(get_results__many)176{177store::write_backend backend = store::write_backend::open_rw(178fs::path("test.db"));179180store::write_transaction tx = backend.start_write();181182const model::context context(fs::path("/foo/bar"),183std::map< std::string, std::string >());184tx.put_context(context);185186const datetime::timestamp start_time1 = datetime::timestamp::from_values(1872012, 01, 30, 22, 10, 00, 0);188const datetime::timestamp end_time1 = datetime::timestamp::from_values(1892012, 01, 30, 22, 15, 30, 1234);190const datetime::timestamp start_time2 = datetime::timestamp::from_values(1912012, 01, 30, 22, 15, 40, 987);192const datetime::timestamp end_time2 = datetime::timestamp::from_values(1932012, 01, 30, 22, 16, 0, 0);194195atf::utils::create_file("unused.txt", "unused file\n");196197const model::test_program test_program_1 = model::test_program_builder(198"plain", fs::path("a/prog1"), fs::path("/the/root"), "suite1")199.add_test_case("main")200.build();201const model::test_result result_1(model::test_result_passed);202{203const int64_t tp_id = tx.put_test_program(test_program_1);204const int64_t tc_id = tx.put_test_case(test_program_1, "main", tp_id);205atf::utils::create_file("prog1.out", "stdout of prog1\n");206tx.put_test_case_file("__STDOUT__", fs::path("prog1.out"), tc_id);207tx.put_test_case_file("unused.txt", fs::path("unused.txt"), tc_id);208tx.put_result(result_1, tc_id, start_time1, end_time1);209}210211const model::test_program test_program_2 = model::test_program_builder(212"plain", fs::path("b/prog2"), fs::path("/the/root"), "suite2")213.add_test_case("main")214.build();215const model::test_result result_2(model::test_result_failed,216"Some text");217{218const int64_t tp_id = tx.put_test_program(test_program_2);219const int64_t tc_id = tx.put_test_case(test_program_2, "main", tp_id);220atf::utils::create_file("prog2.err", "stderr of prog2\n");221tx.put_test_case_file("__STDERR__", fs::path("prog2.err"), tc_id);222tx.put_test_case_file("unused.txt", fs::path("unused.txt"), tc_id);223tx.put_result(result_2, tc_id, start_time2, end_time2);224}225226tx.commit();227backend.close();228229store::read_backend backend2 = store::read_backend::open_ro(230fs::path("test.db"));231store::read_transaction tx2 = backend2.start_read();232store::results_iterator iter = tx2.get_results();233ATF_REQUIRE(iter);234ATF_REQUIRE_EQ(test_program_1, *iter.test_program());235ATF_REQUIRE_EQ("main", iter.test_case_name());236ATF_REQUIRE_EQ("stdout of prog1\n", iter.stdout_contents());237ATF_REQUIRE(iter.stderr_contents().empty());238ATF_REQUIRE_EQ(result_1, iter.result());239ATF_REQUIRE_EQ(start_time1, iter.start_time());240ATF_REQUIRE_EQ(end_time1, iter.end_time());241ATF_REQUIRE(++iter);242ATF_REQUIRE_EQ(test_program_2, *iter.test_program());243ATF_REQUIRE_EQ("main", iter.test_case_name());244ATF_REQUIRE(iter.stdout_contents().empty());245ATF_REQUIRE_EQ("stderr of prog2\n", iter.stderr_contents());246ATF_REQUIRE_EQ(result_2, iter.result());247ATF_REQUIRE_EQ(start_time2, iter.start_time());248ATF_REQUIRE_EQ(end_time2, iter.end_time());249ATF_REQUIRE(!++iter);250}251252253ATF_INIT_TEST_CASES(tcs)254{255ATF_ADD_TEST_CASE(tcs, get_context__missing);256ATF_ADD_TEST_CASE(tcs, get_context__invalid_cwd);257ATF_ADD_TEST_CASE(tcs, get_context__invalid_env_vars);258259ATF_ADD_TEST_CASE(tcs, get_results__none);260ATF_ADD_TEST_CASE(tcs, get_results__many);261}262263264