-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sqlite.cpp
More file actions
65 lines (52 loc) · 1.77 KB
/
test_sqlite.cpp
File metadata and controls
65 lines (52 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "test_settings.hpp"
#ifdef ENABLE_TESTS
#include "../utils_lib/external/doctest.hpp"
#include "../utils_lib/utils_sqlite.hpp"
#include "../utils_lib/utils_json.hpp"
#include "../utils_lib/utils_io.hpp"
#include "../utils_lib/utils_algorithm.hpp"
#include "../utils_lib/utils_random.hpp"
struct Entry {
int id;
std::string key;
std::string val;
};
TEST_CASE("Test utils::sqlite") {
UTILS_PROFILE_SCOPE("utils::sqlite");
static const std::string dbfile = "test_db.sqlite";
// Delete to be sure
bool db_file_exists = utils::io::fs::exists(dbfile);
if (db_file_exists) {
utils::io::fs::remove(dbfile);
}
auto storage = utils::sqlite::make_storage(std::string(dbfile),
utils::sqlite::make_table("items",
utils::sqlite::make_column("id", &Entry::id,
utils::sqlite::autoincrement(),
utils::sqlite::primary_key()),
utils::sqlite::make_column("key", &Entry::key),
utils::sqlite::make_column("val", &Entry::val)
)
);
// Check if saved
storage.sync_schema();
db_file_exists = utils::io::fs::exists(dbfile);
REQUIRE(db_file_exists);
REQUIRE(utils::algorithm::contains(storage.table_names(), "items"));
utils::json out;
out["test_1"] = "some value";
out["test_2"] = "some other value 3.14/*-?";
storage.transaction([&] () mutable {
for (auto& [key, value] : out.items()) {
storage.insert(std::move(Entry{-1, key, value.dump()}));
}
return true;
});
CHECK(storage.count<Entry>() == 2);
CHECK(storage.group_concat(&Entry::key) == "test_1,test_2");
// Cleanup
if (db_file_exists) {
utils::io::fs::remove(dbfile);
}
}
#endif