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
62 lines (44 loc) · 1.43 KB
/
test_cache.cpp
File metadata and controls
62 lines (44 loc) · 1.43 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
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
#include <vector>
#include "test_helpers.hpp"
namespace test_cache {
struct User {
std::string name;
int age;
};
TEST(postgres, test_cache) {
const auto credentials = sqlgen::postgres::test::make_credentials();
const auto conn = sqlgen::postgres::connect(credentials);
using namespace sqlgen;
using namespace sqlgen::literals;
(sqlgen::drop<User> | if_exists)(conn);
const auto user = User{.name = "John", .age = 30};
sqlgen::write(conn, user);
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
#endif