xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
core
SociDB_test.cpp
1
#include <test/jtx/TestSuite.h>
2
3
#include <xrpl/basics/contract.h>
4
#include <xrpl/beast/unit_test/suite.h>
5
#include <xrpl/config/BasicConfig.h>
6
#include <xrpl/config/Constants.h>
7
#include <xrpl/rdb/SociDB.h>
8
9
#include <boost/optional/optional.hpp>
// IWYU pragma: keep
10
11
#include <soci/boost-optional.h>
// IWYU pragma: keep
12
#include <soci/into.h>
13
#include <soci/session.h>
14
#include <soci/use.h>
15
16
#include <
algorithm
>
17
#include <
cstdint
>
18
#include <
cstring
>
19
#include <
exception
>
20
#include <
filesystem
>
21
#include <
iterator
>
22
#include <
limits
>
23
#include <
stdexcept
>
24
#include <
string_view
>
25
#include <
utility
>
26
#include <
vector
>
27
28
namespace
xrpl
{
29
class
SociDB_test
final :
public
TestSuite
30
{
31
private
:
32
static
void
33
setupSQLiteConfig
(
BasicConfig
& config,
std::filesystem::path
const
& dbPath)
34
{
35
config.
overwrite
(
Sections::kSqdb
,
Keys::kBackend
,
"sqlite"
);
36
auto
const
value = dbPath.
string
();
37
if
(!value.empty())
38
config.
legacy
(
Sections::kDatabasePath
, value);
39
}
40
41
static
void
42
cleanupDatabaseDir
(
std::filesystem::path
const
& dbPath)
43
{
44
using namespace
std::filesystem
;
45
if
(!
exists
(dbPath) || !
is_directory
(dbPath) || !
is_empty
(dbPath))
46
return
;
47
remove
(dbPath);
48
}
49
50
static
void
51
setupDatabaseDir
(
std::filesystem::path
const
& dbPath)
52
{
53
using namespace
std::filesystem
;
54
if
(!
exists
(dbPath))
55
{
56
create_directory
(dbPath);
57
return
;
58
}
59
60
if
(!
is_directory
(dbPath))
61
{
62
// someone created a file where we want to put out directory
63
Throw<std::runtime_error>
(
"Cannot create directory: "
+ dbPath.
string
());
64
}
65
}
66
static
std::filesystem::path
67
getDatabasePath
()
68
{
69
return
std::filesystem::current_path
() /
"socidb_test_databases"
;
70
}
71
72
public
:
73
SociDB_test
()
74
{
75
try
76
{
77
setupDatabaseDir
(
getDatabasePath
());
78
}
79
catch
(
std::exception
const
&)
// NOLINT(bugprone-empty-catch)
80
{
81
}
82
}
83
~SociDB_test
()
override
84
{
85
try
86
{
87
cleanupDatabaseDir
(
getDatabasePath
());
88
}
89
catch
(
std::exception
const
&)
// NOLINT(bugprone-empty-catch)
90
{
91
}
92
}
93
void
94
testSQLiteFileNames
()
95
{
96
// confirm that files are given the correct extensions
97
testcase
(
"sqliteFileNames"
);
98
BasicConfig
c;
99
setupSQLiteConfig
(c,
getDatabasePath
());
100
std::vector<std::pair<std::string, std::string>
>
const
d({
101
{
"peerfinder"
,
".sqlite"
},
102
{
"state"
,
".db"
},
103
{
"random"
,
".db"
},
104
{
"validators"
,
".sqlite"
},
105
});
106
107
for
(
auto
const
& i : d)
108
{
109
DBConfig
const
sc(c, i.first);
110
BEAST_EXPECT(sc.
connectionString
().
ends_with
(i.first + i.second));
111
}
112
}
113
void
114
testSQLiteSession
()
115
{
116
testcase
(
"open"
);
117
BasicConfig
c;
118
setupSQLiteConfig
(c,
getDatabasePath
());
119
DBConfig
const
sc(c,
"SociTestDB"
);
120
std::vector<std::string>
const
stringData({
"String1"
,
"String2"
,
"String3"
});
121
std::vector<int>
const
intData({1, 2, 3});
122
auto
const
checkValues = [
this
, &stringData, &intData](soci::session& s) {
123
// Check values in db
124
std::vector<std::string>
stringResult(20 * stringData.size());
125
std::vector<int>
intResult(20 * intData.size());
126
s <<
"SELECT StringData, IntData FROM SociTestTable;"
, soci::into(stringResult),
127
soci::into(intResult);
128
BEAST_EXPECT(
129
stringResult.
size
() == stringData.size() && intResult.
size
() == intData.size());
130
for
(
int
i = 0; i < stringResult.
size
(); ++i)
131
{
132
auto
const
si =
std::distance
(
133
stringData.begin(),
std::ranges::find
(stringData, stringResult[i]));
134
auto
const
ii =
135
std::distance
(intData.begin(),
std::ranges::find
(intData, intResult[i]));
136
BEAST_EXPECT(si == ii && si < stringResult.
size
());
137
}
138
};
139
140
{
141
soci::session s;
142
sc.
open
(s);
143
s <<
"CREATE TABLE IF NOT EXISTS SociTestTable ("
144
" Key INTEGER PRIMARY KEY,"
145
" StringData TEXT,"
146
" IntData INTEGER"
147
");"
;
148
149
s <<
"INSERT INTO SociTestTable (StringData, IntData) VALUES "
150
"(:stringData, :intData);"
,
151
soci::use(stringData), soci::use(intData);
152
checkValues(s);
153
}
154
{
155
// Check values in db after session was closed
156
soci::session s;
157
sc.
open
(s);
158
checkValues(s);
159
}
160
{
161
namespace
bfs =
std::filesystem
;
162
// Remove the database
163
bfs::path
const
dbPath(sc.
connectionString
());
164
if
(bfs::is_regular_file(dbPath))
165
bfs::remove(dbPath);
166
}
167
}
168
169
void
170
testSQLiteSelect
()
171
{
172
testcase
(
"select"
);
173
BasicConfig
c;
174
setupSQLiteConfig
(c,
getDatabasePath
());
175
DBConfig
const
sc(c,
"SociTestDB"
);
176
std::vector<std::uint64_t>
const
ubid(
177
{(
std::uint64_t
)
std::numeric_limits<std::int64_t>::max
(), 20, 30});
178
std::vector<std::int64_t>
const
bid({-10, -20, -30});
179
std::vector<std::uint32_t>
const
uid({
std::numeric_limits<std::uint32_t>::max
(), 2, 3});
180
std::vector<std::int32_t>
const
id({-1, -2, -3});
181
182
{
183
soci::session s;
184
sc.
open
(s);
185
186
s <<
"DROP TABLE IF EXISTS STT;"
;
187
188
s <<
"CREATE TABLE STT ("
189
" I INTEGER,"
190
" UI INTEGER UNSIGNED,"
191
" BI BIGINT,"
192
" UBI BIGINT UNSIGNED"
193
");"
;
194
195
s <<
"INSERT INTO STT (I, UI, BI, UBI) VALUES "
196
"(:id, :idu, :bid, :bidu);"
,
197
soci::use(
id
), soci::use(uid), soci::use(bid), soci::use(ubid);
198
199
try
200
{
201
std::int32_t
ig = 0;
202
std::uint32_t
uig = 0;
203
std::int64_t
big = 0;
204
std::uint64_t
ubig = 0;
205
s <<
"SELECT I, UI, BI, UBI from STT;"
, soci::into(ig), soci::into(uig),
206
soci::into(big), soci::into(ubig);
207
BEAST_EXPECT(ig ==
id
[0] && uig == uid[0] && big == bid[0] && ubig == ubid[0]);
208
}
209
catch
(
std::exception
&)
210
{
211
fail
();
212
}
213
try
214
{
215
// SOCI requires boost::optional (not std::optional) as
216
// parameters.
217
boost::optional<std::int32_t> ig;
218
// Known bug: https://github.com/SOCI/soci/issues/926
219
// boost::optional<std::uint32_t> uig;
220
uint32_t
uig = 0;
221
boost::optional<std::int64_t> big;
222
boost::optional<std::uint64_t> ubig;
223
s <<
"SELECT I, UI, BI, UBI from STT;"
, soci::into(ig), soci::into(uig),
224
soci::into(big), soci::into(ubig);
225
BEAST_EXPECT(*ig ==
id
[0] && uig == uid[0] && *big == bid[0] && *ubig == ubid[0]);
226
}
227
catch
(
std::exception
&)
228
{
229
fail
();
230
}
231
// There are too many issues when working with soci::row and
232
// boost::tuple. DO NOT USE soci row!
233
}
234
{
235
namespace
bfs =
std::filesystem
;
236
// Remove the database
237
bfs::path
const
dbPath(sc.
connectionString
());
238
if
(bfs::is_regular_file(dbPath))
239
bfs::remove(dbPath);
240
}
241
}
242
void
243
testSQLiteDeleteWithSubselect
()
244
{
245
testcase
(
"deleteWithSubselect"
);
246
BasicConfig
c;
247
setupSQLiteConfig
(c,
getDatabasePath
());
248
DBConfig
const
sc(c,
"SociTestDB"
);
249
{
250
soci::session s;
251
sc.
open
(s);
252
253
std::string_view
const
dbInit[] = {
254
"BEGIN TRANSACTION;"
,
255
"CREATE TABLE Ledgers ( \
256
LedgerHash CHARACTER(64) PRIMARY KEY, \
257
LedgerSeq BIGINT UNSIGNED \
258
);"
,
259
"CREATE INDEX SeqLedger ON Ledgers(LedgerSeq);"
,
260
};
261
for
(
auto
const
c : dbInit)
262
s << c;
263
char
lh[65];
264
memset
(lh,
'a'
, 64);
265
lh[64] =
'\0'
;
266
int
toIncIndex = 63;
267
int
const
numRows = 16;
268
std::vector<std::string>
ledgerHashes;
269
std::vector<int>
ledgerIndexes;
270
ledgerHashes.
reserve
(numRows);
271
ledgerIndexes.
reserve
(numRows);
272
for
(
int
i = 0; i < numRows; ++i)
273
{
274
++lh[toIncIndex];
275
if
(lh[toIncIndex] ==
'z'
)
276
--toIncIndex;
277
ledgerHashes.
emplace_back
(lh);
278
ledgerIndexes.
emplace_back
(i);
279
}
280
s <<
"INSERT INTO Ledgers (LedgerHash, LedgerSeq) VALUES "
281
"(:lh, :li);"
,
282
soci::use(ledgerHashes), soci::use(ledgerIndexes);
283
284
std::vector<int>
ledgersLS(numRows * 2);
285
s <<
"SELECT LedgerSeq FROM Ledgers;"
, soci::into(ledgersLS);
286
BEAST_EXPECT(ledgersLS.
size
() == numRows);
287
}
288
namespace
bfs =
std::filesystem
;
289
// Remove the database
290
bfs::path
const
dbPath(sc.
connectionString
());
291
if
(bfs::is_regular_file(dbPath))
292
bfs::remove(dbPath);
293
}
294
void
295
run
()
override
296
{
297
testSQLiteFileNames
();
298
testSQLiteSession
();
299
testSQLiteSelect
();
300
testSQLiteDeleteWithSubselect
();
301
}
302
};
303
304
BEAST_DEFINE_TESTSUITE
(SociDB, core,
xrpl
);
305
306
}
// namespace xrpl
algorithm
std::string_view
beast::unit_test::Suite::fail
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition
suite.h:554
beast::unit_test::Suite::testcase
TestcaseT testcase
Memberspace for declaring test cases.
Definition
suite.h:155
xrpl::BasicConfig
Holds unparsed configuration information.
Definition
BasicConfig.h:222
xrpl::BasicConfig::overwrite
void overwrite(std::string const §ion, std::string const &key, std::string const &value)
Overwrite a key/value pair with a command line argument If the section does not exist it is created.
Definition
BasicConfig.cpp:137
xrpl::BasicConfig::legacy
void legacy(std::string const §ion, std::string value)
Set a value that is not a key/value pair.
Definition
BasicConfig.cpp:153
xrpl::DBConfig
DBConfig is used when a client wants to delay opening a soci::session after parsing the config parame...
Definition
SociDB.h:44
xrpl::DBConfig::open
void open(soci::session &s) const
Definition
SociDB.cpp:85
xrpl::DBConfig::connectionString
std::string connectionString() const
Definition
SociDB.cpp:79
xrpl::SociDB_test::setupDatabaseDir
static void setupDatabaseDir(std::filesystem::path const &dbPath)
Definition
SociDB_test.cpp:51
xrpl::SociDB_test::cleanupDatabaseDir
static void cleanupDatabaseDir(std::filesystem::path const &dbPath)
Definition
SociDB_test.cpp:42
xrpl::SociDB_test::testSQLiteSelect
void testSQLiteSelect()
Definition
SociDB_test.cpp:170
xrpl::SociDB_test::testSQLiteFileNames
void testSQLiteFileNames()
Definition
SociDB_test.cpp:94
xrpl::SociDB_test::testSQLiteSession
void testSQLiteSession()
Definition
SociDB_test.cpp:114
xrpl::SociDB_test::getDatabasePath
static std::filesystem::path getDatabasePath()
Definition
SociDB_test.cpp:67
xrpl::SociDB_test::testSQLiteDeleteWithSubselect
void testSQLiteDeleteWithSubselect()
Definition
SociDB_test.cpp:243
xrpl::SociDB_test::SociDB_test
SociDB_test()
Definition
SociDB_test.cpp:73
xrpl::SociDB_test::setupSQLiteConfig
static void setupSQLiteConfig(BasicConfig &config, std::filesystem::path const &dbPath)
Definition
SociDB_test.cpp:33
xrpl::SociDB_test::~SociDB_test
~SociDB_test() override
Definition
SociDB_test.cpp:83
xrpl::SociDB_test::run
void run() override
Runs the suite.
Definition
SociDB_test.cpp:295
xrpl::TestSuite
Definition
TestSuite.h:12
std::filesystem::create_directory
T create_directory(T... args)
cstdint
cstring
std::filesystem::current_path
T current_path(T... args)
std::distance
T distance(T... args)
std::vector::emplace_back
T emplace_back(T... args)
std::string::ends_with
T ends_with(T... args)
exception
std::filesystem::exists
T exists(T... args)
filesystem
std::ranges::find
T find(T... args)
std::uint64_t
std::filesystem::is_directory
T is_directory(T... args)
std::filesystem::is_empty
T is_empty(T... args)
iterator
limits
std::numeric_limits::max
T max(T... args)
std::memset
T memset(T... args)
std::filesystem
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
std::filesystem::path
std::filesystem::remove
T remove(T... args)
std::vector::reserve
T reserve(T... args)
std::vector::size
T size(T... args)
stdexcept
std::filesystem::path::string
T string(T... args)
string_view
xrpl::Keys::kBackend
static constexpr auto kBackend
Definition
Constants.h:93
xrpl::Sections::kSqdb
static constexpr auto kSqdb
Definition
Constants.h:60
xrpl::Sections::kDatabasePath
static constexpr auto kDatabasePath
Definition
Constants.h:13
utility
vector
Generated by
1.17.0