mirror of
https://github.com/getml/sqlgen.git
synced 2025-12-31 14:39:31 -06:00
64 lines
2.2 KiB
C++
64 lines
2.2 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_is_null {
|
|
|
|
struct Person {
|
|
sqlgen::PrimaryKey<uint32_t> id;
|
|
std::string first_name;
|
|
std::string last_name;
|
|
std::optional<int> age;
|
|
};
|
|
|
|
TEST(postgres, test_is_null) {
|
|
const auto people1 = std::vector<Person>(
|
|
{Person{.id = 0, .first_name = "Homer", .last_name = "Simpson"},
|
|
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
|
|
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
|
|
Person{
|
|
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0},
|
|
Person{.id = 4, .first_name = "Hugo", .last_name = "Simpson"}});
|
|
|
|
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
|
|
.password = "password",
|
|
.host = "localhost",
|
|
.dbname = "postgres"};
|
|
|
|
using namespace sqlgen;
|
|
using namespace sqlgen::literals;
|
|
|
|
const auto conn = postgres::connect(credentials);
|
|
|
|
const auto people2 =
|
|
conn.and_then(drop<Person> | if_exists)
|
|
.and_then(write(std::ref(people1)))
|
|
.and_then(sqlgen::read<std::vector<Person>> |
|
|
where("age"_c.is_null()) | order_by("first_name"_c.desc()))
|
|
.value();
|
|
|
|
const auto people3 =
|
|
conn.and_then(sqlgen::read<std::vector<Person>> |
|
|
where("age"_c.is_not_null()) | order_by("age"_c))
|
|
.value();
|
|
|
|
const std::string expected1 =
|
|
R"([{"id":4,"first_name":"Hugo","last_name":"Simpson"},{"id":0,"first_name":"Homer","last_name":"Simpson"}])";
|
|
|
|
const std::string expected2 =
|
|
R"([{"id":3,"first_name":"Maggie","last_name":"Simpson","age":0},{"id":2,"first_name":"Lisa","last_name":"Simpson","age":8},{"id":1,"first_name":"Bart","last_name":"Simpson","age":10}])";
|
|
|
|
EXPECT_EQ(rfl::json::write(people2), expected1);
|
|
EXPECT_EQ(rfl::json::write(people3), expected2);
|
|
}
|
|
|
|
} // namespace test_is_null
|
|
|
|
#endif
|