forked from getml/sqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cache.cpp
More file actions
53 lines (39 loc) · 1.23 KB
/
test_cache.cpp
File metadata and controls
53 lines (39 loc) · 1.23 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
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/sqlite.hpp>
#include <vector>
namespace test_cache {
struct User {
std::string name;
int age;
};
TEST(sqlite, test_cache) {
const auto conn = sqlgen::sqlite::connect();
const auto user = User{.name = "John", .age = 30};
sqlgen::write(conn, user);
using namespace sqlgen;
using namespace sqlgen::literals;
const auto query = sqlgen::read<User> | where("name"_c == "John");
const auto cached_query = sqlgen::cache<100>(query);
auto test_cache_population = [&]() {
const auto user1 = conn.and_then(cache<100>(query)).value();
EXPECT_EQ(cached_query.cache(conn).size(), 1);
const auto user2 = cached_query(conn).value();
const auto user3 = cached_query(conn).value();
EXPECT_EQ(user1.name, "John");
EXPECT_EQ(user1.age, 30);
EXPECT_EQ(user2.name, "John");
EXPECT_EQ(user2.age, 30);
EXPECT_EQ(cached_query.cache(conn).size(), 1);
EXPECT_EQ(user3.name, "John");
EXPECT_EQ(user3.age, 30);
};
test_cache_population();
// Test cache invalidation
cached_query.clear(conn);
EXPECT_EQ(cached_query.cache(conn).size(), 0);
test_cache_population();
}
} // namespace test_cache