xrpld
Loading...
Searching...
No Matches
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
28namespace xrpl {
29class SociDB_test final : public TestSuite
30{
31private:
32 static void
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
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
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 }
68 {
69 return std::filesystem::current_path() / "socidb_test_databases";
70 }
71
72public:
74 {
75 try
76 {
78 }
79 catch (std::exception const&) // NOLINT(bugprone-empty-catch)
80 {
81 }
82 }
83 ~SociDB_test() override
84 {
85 try
86 {
88 }
89 catch (std::exception const&) // NOLINT(bugprone-empty-catch)
90 {
91 }
92 }
93 void
95 {
96 // confirm that files are given the correct extensions
97 testcase("sqliteFileNames");
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
115 {
116 testcase("open");
117 BasicConfig c;
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
171 {
172 testcase("select");
173 BasicConfig c;
175 DBConfig const sc(c, "SociTestDB");
178 std::vector<std::int64_t> const bid({-10, -20, -30});
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
244 {
245 testcase("deleteWithSubselect");
246 BasicConfig c;
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
302};
303
305
306} // namespace xrpl
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Holds unparsed configuration information.
void overwrite(std::string const &section, 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.
void legacy(std::string const &section, std::string value)
Set a value that is not a key/value pair.
DBConfig is used when a client wants to delay opening a soci::session after parsing the config parame...
Definition SociDB.h:44
void open(soci::session &s) const
Definition SociDB.cpp:85
std::string connectionString() const
Definition SociDB.cpp:79
static void setupDatabaseDir(std::filesystem::path const &dbPath)
static void cleanupDatabaseDir(std::filesystem::path const &dbPath)
static std::filesystem::path getDatabasePath()
void testSQLiteDeleteWithSubselect()
static void setupSQLiteConfig(BasicConfig &config, std::filesystem::path const &dbPath)
~SociDB_test() override
void run() override
Runs the suite.
T create_directory(T... args)
T current_path(T... args)
T distance(T... args)
T emplace_back(T... args)
T ends_with(T... args)
T exists(T... args)
T find(T... args)
T is_directory(T... args)
T is_empty(T... args)
T max(T... args)
T memset(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T remove(T... args)
T reserve(T... args)
T size(T... args)
static constexpr auto kBackend
Definition Constants.h:93
static constexpr auto kSqdb
Definition Constants.h:60
static constexpr auto kDatabasePath
Definition Constants.h:13