Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/kyua/store/metadata_test.cpp
39478 views
1
// Copyright 2011 The Kyua Authors.
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
6
// met:
7
//
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
// * Neither the name of Google Inc. nor the names of its contributors
14
// may be used to endorse or promote products derived from this software
15
// without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
#include "store/metadata.hpp"
30
31
#include <atf-c++.hpp>
32
33
#include "store/exceptions.hpp"
34
#include "store/write_backend.hpp"
35
#include "utils/fs/path.hpp"
36
#include "utils/logging/operations.hpp"
37
#include "utils/sqlite/database.hpp"
38
39
namespace logging = utils::logging;
40
namespace sqlite = utils::sqlite;
41
42
43
namespace {
44
45
46
/// Creates a test in-memory database.
47
///
48
/// When using this function, you must define a 'require.files' property in this
49
/// case pointing to store::detail::schema_file().
50
///
51
/// The database created by this function mimics a real complete database, but
52
/// without any predefined values. I.e. for our particular case, the metadata
53
/// table is empty.
54
///
55
/// \return A SQLite database instance.
56
static sqlite::database
57
create_database(void)
58
{
59
sqlite::database db = sqlite::database::in_memory();
60
store::detail::initialize(db);
61
db.exec("DELETE FROM metadata");
62
return db;
63
}
64
65
66
} // anonymous namespace
67
68
69
ATF_TEST_CASE(fetch_latest__ok);
70
ATF_TEST_CASE_HEAD(fetch_latest__ok)
71
{
72
logging::set_inmemory();
73
set_md_var("require.files", store::detail::schema_file().c_str());
74
}
75
ATF_TEST_CASE_BODY(fetch_latest__ok)
76
{
77
sqlite::database db = create_database();
78
db.exec("INSERT INTO metadata (schema_version, timestamp) "
79
"VALUES (512, 5678)");
80
db.exec("INSERT INTO metadata (schema_version, timestamp) "
81
"VALUES (256, 1234)");
82
83
const store::metadata metadata = store::metadata::fetch_latest(db);
84
ATF_REQUIRE_EQ(5678L, metadata.timestamp());
85
ATF_REQUIRE_EQ(512, metadata.schema_version());
86
}
87
88
89
ATF_TEST_CASE(fetch_latest__empty_metadata);
90
ATF_TEST_CASE_HEAD(fetch_latest__empty_metadata)
91
{
92
logging::set_inmemory();
93
set_md_var("require.files", store::detail::schema_file().c_str());
94
}
95
ATF_TEST_CASE_BODY(fetch_latest__empty_metadata)
96
{
97
sqlite::database db = create_database();
98
ATF_REQUIRE_THROW_RE(store::integrity_error, "metadata.*empty",
99
store::metadata::fetch_latest(db));
100
}
101
102
103
ATF_TEST_CASE_WITHOUT_HEAD(fetch_latest__no_timestamp);
104
ATF_TEST_CASE_BODY(fetch_latest__no_timestamp)
105
{
106
sqlite::database db = sqlite::database::in_memory();
107
db.exec("CREATE TABLE metadata (schema_version INTEGER)");
108
db.exec("INSERT INTO metadata VALUES (3)");
109
110
ATF_REQUIRE_THROW_RE(store::integrity_error,
111
"Invalid metadata.*timestamp",
112
store::metadata::fetch_latest(db));
113
}
114
115
116
ATF_TEST_CASE_WITHOUT_HEAD(fetch_latest__no_schema_version);
117
ATF_TEST_CASE_BODY(fetch_latest__no_schema_version)
118
{
119
sqlite::database db = sqlite::database::in_memory();
120
db.exec("CREATE TABLE metadata (timestamp INTEGER)");
121
db.exec("INSERT INTO metadata VALUES (3)");
122
123
ATF_REQUIRE_THROW_RE(store::integrity_error,
124
"Invalid metadata.*schema_version",
125
store::metadata::fetch_latest(db));
126
}
127
128
129
ATF_TEST_CASE(fetch_latest__invalid_timestamp);
130
ATF_TEST_CASE_HEAD(fetch_latest__invalid_timestamp)
131
{
132
logging::set_inmemory();
133
set_md_var("require.files", store::detail::schema_file().c_str());
134
}
135
ATF_TEST_CASE_BODY(fetch_latest__invalid_timestamp)
136
{
137
sqlite::database db = create_database();
138
db.exec("INSERT INTO metadata (schema_version, timestamp) "
139
"VALUES (3, 'foo')");
140
141
ATF_REQUIRE_THROW_RE(store::integrity_error,
142
"timestamp.*invalid type",
143
store::metadata::fetch_latest(db));
144
}
145
146
147
ATF_INIT_TEST_CASES(tcs)
148
{
149
ATF_ADD_TEST_CASE(tcs, fetch_latest__ok);
150
ATF_ADD_TEST_CASE(tcs, fetch_latest__empty_metadata);
151
ATF_ADD_TEST_CASE(tcs, fetch_latest__no_timestamp);
152
ATF_ADD_TEST_CASE(tcs, fetch_latest__no_schema_version);
153
ATF_ADD_TEST_CASE(tcs, fetch_latest__invalid_timestamp);
154
}
155
156