|
| 1 | +#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY |
| 2 | + |
| 3 | +#include <gtest/gtest.h> |
| 4 | + |
| 5 | +#include <rfl.hpp> |
| 6 | +#include <rfl/json.hpp> |
| 7 | +#include <sqlgen.hpp> |
| 8 | +#include <sqlgen/postgres.hpp> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +namespace test_boolean_conditions { |
| 12 | + |
| 13 | +struct Person { |
| 14 | + sqlgen::PrimaryKey<uint32_t> id; |
| 15 | + std::string first_name; |
| 16 | + std::string last_name; |
| 17 | + bool has_children; |
| 18 | +}; |
| 19 | + |
| 20 | +TEST(postgres, test_boolean_conditions) { |
| 21 | + const auto people1 = std::vector<Person>({Person{.id = 0, |
| 22 | + .first_name = "Homer", |
| 23 | + .last_name = "Simpson", |
| 24 | + .has_children = true}, |
| 25 | + Person{.id = 1, |
| 26 | + .first_name = "Bart", |
| 27 | + .last_name = "Simpson", |
| 28 | + .has_children = false}, |
| 29 | + Person{.id = 2, |
| 30 | + .first_name = "Lisa", |
| 31 | + .last_name = "Simpson", |
| 32 | + .has_children = false}, |
| 33 | + Person{.id = 3, |
| 34 | + .first_name = "Maggie", |
| 35 | + .last_name = "Simpson", |
| 36 | + .has_children = false}}); |
| 37 | + |
| 38 | + const auto credentials = sqlgen::postgres::Credentials{.user = "postgres", |
| 39 | + .password = "password", |
| 40 | + .host = "localhost", |
| 41 | + .dbname = "postgres"}; |
| 42 | + |
| 43 | + using namespace sqlgen; |
| 44 | + using namespace sqlgen::literals; |
| 45 | + |
| 46 | + const auto conn = |
| 47 | + postgres::connect(credentials).and_then(drop<Person> | if_exists); |
| 48 | + |
| 49 | + const auto homer = |
| 50 | + sqlgen::write(conn, people1) |
| 51 | + .and_then(sqlgen::read<Person> | where("has_children"_c == true)) |
| 52 | + .value(); |
| 53 | + |
| 54 | + const auto json1 = rfl::json::write(people1.at(0)); |
| 55 | + const auto json2 = rfl::json::write(homer); |
| 56 | + |
| 57 | + EXPECT_EQ(json1, json2); |
| 58 | +} |
| 59 | + |
| 60 | +} // namespace test_boolean_conditions |
| 61 | + |
| 62 | +#endif |
0 commit comments