mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-01 15:09:46 -06:00
77 lines
2.5 KiB
C++
77 lines
2.5 KiB
C++
#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_create_view_as {
|
|
|
|
struct Person {
|
|
static constexpr const char* tablename = "PEOPLE";
|
|
|
|
sqlgen::PrimaryKey<uint32_t> id;
|
|
std::string first_name;
|
|
std::string last_name;
|
|
int age;
|
|
};
|
|
|
|
struct Name {
|
|
static constexpr const char* viewname = "NAMES";
|
|
static constexpr bool is_view = true;
|
|
|
|
std::string first_name;
|
|
std::string last_name;
|
|
};
|
|
|
|
TEST(postgres, test_create_view_as) {
|
|
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
|
|
.password = "password",
|
|
.host = "localhost",
|
|
.dbname = "postgres"};
|
|
|
|
using namespace sqlgen;
|
|
using namespace sqlgen::literals;
|
|
|
|
const auto people = std::vector<Person>(
|
|
{Person{
|
|
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
|
|
Person{
|
|
.id = 1, .first_name = "Marge", .last_name = "Simpson", .age = 40},
|
|
Person{.id = 2, .first_name = "Bart", .last_name = "Simpson", .age = 10},
|
|
Person{.id = 3, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
|
|
Person{
|
|
.id = 4, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});
|
|
|
|
const auto names_query = select_from<Person>("first_name"_c, "last_name"_c);
|
|
|
|
const auto get_names = create_or_replace_view_as<Name>(names_query);
|
|
|
|
const auto conn = sqlgen::postgres::connect(credentials);
|
|
|
|
const auto names = conn.and_then(drop<Name> | if_exists)
|
|
.and_then(drop<Person> | if_exists)
|
|
.and_then(write(std::ref(people)))
|
|
.and_then(get_names)
|
|
.and_then(sqlgen::read<std::vector<Name>>)
|
|
.value();
|
|
|
|
conn.and_then(drop<Name> | if_exists).value();
|
|
|
|
const std::string expected_query =
|
|
R"(CREATE OR REPLACE VIEW "NAMES" AS SELECT "first_name", "last_name" FROM "PEOPLE")";
|
|
|
|
const std::string expected =
|
|
R"([{"first_name":"Homer","last_name":"Simpson"},{"first_name":"Marge","last_name":"Simpson"},{"first_name":"Bart","last_name":"Simpson"},{"first_name":"Lisa","last_name":"Simpson"},{"first_name":"Maggie","last_name":"Simpson"}])";
|
|
|
|
EXPECT_EQ(postgres::to_sql(get_names), expected_query);
|
|
EXPECT_EQ(rfl::json::write(names), expected);
|
|
}
|
|
|
|
} // namespace test_create_view_as
|
|
|
|
#endif
|