mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-03 16:09:47 -06:00
Added a caching strategy (#83)
This commit is contained in:
committed by
GitHub
parent
4cc3e871c3
commit
86ee6e2bcc
56
tests/postgres/test_cache.cpp
Normal file
56
tests/postgres/test_cache.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#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>
|
||||
|
||||
namespace test_cache {
|
||||
|
||||
struct User {
|
||||
std::string name;
|
||||
int age;
|
||||
};
|
||||
|
||||
TEST(postgres, test_cache) {
|
||||
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
|
||||
.password = "password",
|
||||
.host = "localhost",
|
||||
.dbname = "postgres"};
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
} // namespace test_cache
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user