mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-06 01:19:58 -06:00
Made sure that nullability rules are applied properly when joining tables (#56)
This commit is contained in:
committed by
GitHub
parent
45c1a52177
commit
ce302f54de
80
tests/postgres/test_full_join.cpp
Normal file
80
tests/postgres/test_full_join.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <optional>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/postgres.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_full_join {
|
||||
|
||||
TEST(postgres, test_full_join) {
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
};
|
||||
|
||||
struct Pet {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string name;
|
||||
uint32_t owner_id;
|
||||
};
|
||||
|
||||
const auto people = std::vector<Person>({
|
||||
Person{.id = 1, .first_name = "Homer", .last_name = "Simpson"},
|
||||
Person{.id = 2, .first_name = "Marge", .last_name = "Simpson"},
|
||||
Person{.id = 3, .first_name = "Bart", .last_name = "Simpson"},
|
||||
Person{.id = 4, .first_name = "Lisa", .last_name = "Simpson"},
|
||||
});
|
||||
|
||||
const auto pets = std::vector<Pet>({
|
||||
Pet{.id = 1, .name = "Santa's Little Helper", .owner_id = 1},
|
||||
Pet{.id = 2, .name = "Snowball", .owner_id = 4},
|
||||
Pet{.id = 3, .name = "Mr. Teeny", .owner_id = 99},
|
||||
});
|
||||
|
||||
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
|
||||
.password = "password",
|
||||
.host = "localhost",
|
||||
.dbname = "postgres"};
|
||||
|
||||
using namespace sqlgen;
|
||||
using namespace sqlgen::literals;
|
||||
|
||||
struct PersonAndPet {
|
||||
std::optional<std::string> first_name;
|
||||
std::optional<std::string> last_name;
|
||||
std::optional<std::string> pet_name;
|
||||
};
|
||||
|
||||
const auto get_all =
|
||||
select_from<Person, "t1">("first_name"_t1 | as<"first_name">,
|
||||
"last_name"_t1 | as<"last_name">,
|
||||
"name"_t2 | as<"pet_name">) |
|
||||
full_join<Pet, "t2">("id"_t1 == "owner_id"_t2) |
|
||||
order_by("id"_t1, "id"_t2) | to<std::vector<PersonAndPet>>;
|
||||
|
||||
const auto result = postgres::connect(credentials)
|
||||
.and_then(drop<Person> | if_exists)
|
||||
.and_then(drop<Pet> | if_exists)
|
||||
.and_then(write(std::ref(people)))
|
||||
.and_then(write(std::ref(pets)))
|
||||
.and_then(get_all)
|
||||
.value();
|
||||
|
||||
const std::string expected_query =
|
||||
R"(SELECT t1."first_name" AS "first_name", t1."last_name" AS "last_name", t2."name" AS "pet_name" FROM "Person" t1 FULL JOIN "Pet" t2 ON t1."id" = t2."owner_id" ORDER BY t1."id", t2."id")";
|
||||
const std::string expected_json =
|
||||
R"([{"first_name":"Homer","last_name":"Simpson","pet_name":"Santa's Little Helper"},{"first_name":"Marge","last_name":"Simpson"},{"first_name":"Bart","last_name":"Simpson"},{"first_name":"Lisa","last_name":"Simpson","pet_name":"Snowball"},{"pet_name":"Mr. Teeny"}])";
|
||||
|
||||
EXPECT_EQ(postgres::to_sql(get_all), expected_query);
|
||||
EXPECT_EQ(rfl::json::write(result), expected_json);
|
||||
}
|
||||
|
||||
} // namespace test_full_join
|
||||
|
||||
#endif
|
||||
@@ -42,7 +42,7 @@ TEST(postgres, test_join) {
|
||||
select_from<Person, "t1">(
|
||||
"id"_t1 | as<"id">, "first_name"_t1 | as<"first_name">,
|
||||
"last_name"_t2 | as<"last_name">, "age"_t2 | as<"age">) |
|
||||
left_join<Person, "t2">("id"_t1 == "id"_t2) | order_by("id"_t1) |
|
||||
inner_join<Person, "t2">("id"_t1 == "id"_t2) | order_by("id"_t1) |
|
||||
to<std::vector<Person>>;
|
||||
|
||||
const auto people = postgres::connect(credentials)
|
||||
@@ -52,7 +52,7 @@ TEST(postgres, test_join) {
|
||||
.value();
|
||||
|
||||
const std::string expected_query =
|
||||
R"(SELECT t1."id" AS "id", t1."first_name" AS "first_name", t2."last_name" AS "last_name", t2."age" AS "age" FROM "Person" t1 LEFT JOIN "Person" t2 ON t1."id" = t2."id" ORDER BY t1."id")";
|
||||
R"(SELECT t1."id" AS "id", t1."first_name" AS "first_name", t2."last_name" AS "last_name", t2."age" AS "age" FROM "Person" t1 INNER JOIN "Person" t2 ON t1."id" = t2."id" ORDER BY t1."id")";
|
||||
const std::string expected =
|
||||
R"([{"id":0,"first_name":"Homer","last_name":"Simpson","age":45.0},{"id":1,"first_name":"Bart","last_name":"Simpson","age":10.0},{"id":2,"first_name":"Lisa","last_name":"Simpson","age":8.0},{"id":3,"first_name":"Maggie","last_name":"Simpson","age":0.0}])";
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ TEST(postgres, test_joins_from) {
|
||||
select_from<Person, "t1">(
|
||||
"child_id"_t2 | as<"id">, "first_name"_t1 | as<"first_name">,
|
||||
"last_name"_t1 | as<"last_name">, "age"_t1 | as<"age">) |
|
||||
left_join<Relationship, "t2">("id"_t1 == "parent_id"_t2);
|
||||
inner_join<Relationship, "t2">("id"_t1 == "parent_id"_t2);
|
||||
|
||||
const auto get_people =
|
||||
select_from<"t1">(get_parents, "last_name"_t1 | as<"last_name">,
|
||||
@@ -80,7 +80,7 @@ TEST(postgres, test_joins_from) {
|
||||
.value();
|
||||
|
||||
const std::string expected_query =
|
||||
R"(SELECT t1."last_name" AS "last_name", t1."first_name" AS "first_name_parent", t2."first_name" AS "first_name_child", (t1."age") - (t2."age") AS "parent_age_at_birth" FROM (SELECT t2."child_id" AS "id", t1."first_name" AS "first_name", t1."last_name" AS "last_name", t1."age" AS "age" FROM "Person" t1 LEFT JOIN "Relationship" t2 ON t1."id" = t2."parent_id") t1 INNER JOIN "Person" t2 ON t1."id" = t2."id" ORDER BY t2."id", t1."id")";
|
||||
R"(SELECT t1."last_name" AS "last_name", t1."first_name" AS "first_name_parent", t2."first_name" AS "first_name_child", (t1."age") - (t2."age") AS "parent_age_at_birth" FROM (SELECT t2."child_id" AS "id", t1."first_name" AS "first_name", t1."last_name" AS "last_name", t1."age" AS "age" FROM "Person" t1 INNER JOIN "Relationship" t2 ON t1."id" = t2."parent_id") t1 INNER JOIN "Person" t2 ON t1."id" = t2."id" ORDER BY t2."id", t1."id")";
|
||||
|
||||
const std::string expected =
|
||||
R"([{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Bart","parent_age_at_birth":35.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Bart","parent_age_at_birth":30.0},{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Lisa","parent_age_at_birth":37.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Lisa","parent_age_at_birth":32.0},{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Maggie","parent_age_at_birth":45.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Maggie","parent_age_at_birth":40.0}])";
|
||||
|
||||
@@ -61,7 +61,7 @@ TEST(postgres, test_joins_nested) {
|
||||
select_from<Relationship, "t1">("parent_id"_t1 | as<"id">,
|
||||
"first_name"_t2 | as<"first_name">,
|
||||
"age"_t2 | as<"age">) |
|
||||
left_join<Person, "t2">("id"_t2 == "child_id"_t1);
|
||||
inner_join<Person, "t2">("id"_t2 == "child_id"_t1);
|
||||
|
||||
const auto get_people =
|
||||
select_from<Person, "t1">(
|
||||
@@ -81,7 +81,7 @@ TEST(postgres, test_joins_nested) {
|
||||
.value();
|
||||
|
||||
const std::string expected_query =
|
||||
R"(SELECT t1."last_name" AS "last_name", t1."first_name" AS "first_name_parent", t2."first_name" AS "first_name_child", (t1."age") - (t2."age") AS "parent_age_at_birth" FROM "Person" t1 INNER JOIN (SELECT t1."parent_id" AS "id", t2."first_name" AS "first_name", t2."age" AS "age" FROM "Relationship" t1 LEFT JOIN "Person" t2 ON t2."id" = t1."child_id") t2 ON t1."id" = t2."id" ORDER BY t1."id", t2."id")";
|
||||
R"(SELECT t1."last_name" AS "last_name", t1."first_name" AS "first_name_parent", t2."first_name" AS "first_name_child", (t1."age") - (t2."age") AS "parent_age_at_birth" FROM "Person" t1 INNER JOIN (SELECT t1."parent_id" AS "id", t2."first_name" AS "first_name", t2."age" AS "age" FROM "Relationship" t1 INNER JOIN "Person" t2 ON t2."id" = t1."child_id") t2 ON t1."id" = t2."id" ORDER BY t1."id", t2."id")";
|
||||
|
||||
const std::string expected =
|
||||
R"([{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Bart","parent_age_at_birth":35.0},{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Lisa","parent_age_at_birth":37.0},{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Maggie","parent_age_at_birth":45.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Bart","parent_age_at_birth":30.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Lisa","parent_age_at_birth":32.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Maggie","parent_age_at_birth":40.0}])";
|
||||
|
||||
@@ -60,7 +60,7 @@ TEST(postgres, test_joins_nested_grouped) {
|
||||
select_from<Relationship, "t1">("parent_id"_t1 | as<"id">,
|
||||
"first_name"_t2 | as<"first_name">,
|
||||
"age"_t2 | as<"age">) |
|
||||
left_join<Person, "t2">("id"_t2 == "child_id"_t1);
|
||||
inner_join<Person, "t2">("id"_t2 == "child_id"_t1);
|
||||
|
||||
const auto get_people =
|
||||
select_from<Person, "t1">(
|
||||
@@ -80,7 +80,7 @@ TEST(postgres, test_joins_nested_grouped) {
|
||||
.value();
|
||||
|
||||
const std::string expected_query =
|
||||
R"(SELECT t1."last_name" AS "last_name", t2."first_name" AS "first_name_child", AVG((t1."age") - (t2."age")) AS "avg_parent_age_at_birth" FROM "Person" t1 INNER JOIN (SELECT t1."parent_id" AS "id", t2."first_name" AS "first_name", t2."age" AS "age" FROM "Relationship" t1 LEFT JOIN "Person" t2 ON t2."id" = t1."child_id") t2 ON t1."id" = t2."id" GROUP BY t1."last_name", t2."first_name" ORDER BY t2."first_name")";
|
||||
R"(SELECT t1."last_name" AS "last_name", t2."first_name" AS "first_name_child", AVG((t1."age") - (t2."age")) AS "avg_parent_age_at_birth" FROM "Person" t1 INNER JOIN (SELECT t1."parent_id" AS "id", t2."first_name" AS "first_name", t2."age" AS "age" FROM "Relationship" t1 INNER JOIN "Person" t2 ON t2."id" = t1."child_id") t2 ON t1."id" = t2."id" GROUP BY t1."last_name", t2."first_name" ORDER BY t2."first_name")";
|
||||
|
||||
const std::string expected =
|
||||
R"([{"last_name":"Simpson","first_name_child":"Bart","avg_parent_age_at_birth":32.5},{"last_name":"Simpson","first_name_child":"Lisa","avg_parent_age_at_birth":34.5},{"last_name":"Simpson","first_name_child":"Maggie","avg_parent_age_at_birth":42.5}])";
|
||||
|
||||
@@ -64,7 +64,7 @@ TEST(postgres, test_joins_two_tables) {
|
||||
"first_name"_t3 | as<"first_name_child">,
|
||||
("age"_t1 - "age"_t3) | as<"parent_age_at_birth">) |
|
||||
inner_join<Relationship, "t2">("id"_t1 == "parent_id"_t2) |
|
||||
left_join<Person, "t3">("id"_t3 == "child_id"_t2) |
|
||||
inner_join<Person, "t3">("id"_t3 == "child_id"_t2) |
|
||||
order_by("id"_t1, "id"_t3) | to<std::vector<ParentAndChild>>;
|
||||
|
||||
const auto people = postgres::connect(credentials)
|
||||
@@ -76,7 +76,7 @@ TEST(postgres, test_joins_two_tables) {
|
||||
.value();
|
||||
|
||||
const std::string expected_query =
|
||||
R"(SELECT t1."last_name" AS "last_name", t1."first_name" AS "first_name_parent", t3."first_name" AS "first_name_child", (t1."age") - (t3."age") AS "parent_age_at_birth" FROM "Person" t1 INNER JOIN "Relationship" t2 ON t1."id" = t2."parent_id" LEFT JOIN "Person" t3 ON t3."id" = t2."child_id" ORDER BY t1."id", t3."id")";
|
||||
R"(SELECT t1."last_name" AS "last_name", t1."first_name" AS "first_name_parent", t3."first_name" AS "first_name_child", (t1."age") - (t3."age") AS "parent_age_at_birth" FROM "Person" t1 INNER JOIN "Relationship" t2 ON t1."id" = t2."parent_id" INNER JOIN "Person" t3 ON t3."id" = t2."child_id" ORDER BY t1."id", t3."id")";
|
||||
const std::string expected =
|
||||
R"([{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Bart","parent_age_at_birth":35.0},{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Lisa","parent_age_at_birth":37.0},{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Maggie","parent_age_at_birth":45.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Bart","parent_age_at_birth":30.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Lisa","parent_age_at_birth":32.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Maggie","parent_age_at_birth":40.0}])";
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ TEST(postgres, test_joins_two_tables_grouped) {
|
||||
"first_name"_t3 | as<"first_name_child">,
|
||||
avg("age"_t1 - "age"_t3) | as<"avg_parent_age_at_birth">) |
|
||||
inner_join<Relationship, "t2">("id"_t1 == "parent_id"_t2) |
|
||||
left_join<Person, "t3">("id"_t3 == "child_id"_t2) |
|
||||
inner_join<Person, "t3">("id"_t3 == "child_id"_t2) |
|
||||
group_by("last_name"_t1, "first_name"_t3) |
|
||||
order_by("last_name"_t1, "first_name"_t3) |
|
||||
to<std::vector<ParentAndChild>>;
|
||||
@@ -76,7 +76,7 @@ TEST(postgres, test_joins_two_tables_grouped) {
|
||||
.value();
|
||||
|
||||
const std::string expected_query =
|
||||
R"(SELECT t1."last_name" AS "last_name", t3."first_name" AS "first_name_child", AVG((t1."age") - (t3."age")) AS "avg_parent_age_at_birth" FROM "Person" t1 INNER JOIN "Relationship" t2 ON t1."id" = t2."parent_id" LEFT JOIN "Person" t3 ON t3."id" = t2."child_id" GROUP BY t1."last_name", t3."first_name" ORDER BY t1."last_name", t3."first_name")";
|
||||
R"(SELECT t1."last_name" AS "last_name", t3."first_name" AS "first_name_child", AVG((t1."age") - (t3."age")) AS "avg_parent_age_at_birth" FROM "Person" t1 INNER JOIN "Relationship" t2 ON t1."id" = t2."parent_id" INNER JOIN "Person" t3 ON t3."id" = t2."child_id" GROUP BY t1."last_name", t3."first_name" ORDER BY t1."last_name", t3."first_name")";
|
||||
|
||||
const std::string expected =
|
||||
R"([{"last_name":"Simpson","first_name_child":"Bart","avg_parent_age_at_birth":32.5},{"last_name":"Simpson","first_name_child":"Lisa","avg_parent_age_at_birth":34.5},{"last_name":"Simpson","first_name_child":"Maggie","avg_parent_age_at_birth":42.5}])";
|
||||
|
||||
80
tests/postgres/test_left_join.cpp
Normal file
80
tests/postgres/test_left_join.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <optional>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/postgres.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_left_join {
|
||||
|
||||
TEST(postgres, test_left_join) {
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
};
|
||||
|
||||
struct Pet {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string name;
|
||||
uint32_t owner_id;
|
||||
};
|
||||
|
||||
const auto people = std::vector<Person>({
|
||||
Person{.id = 1, .first_name = "Homer", .last_name = "Simpson"},
|
||||
Person{.id = 2, .first_name = "Marge", .last_name = "Simpson"},
|
||||
Person{.id = 3, .first_name = "Bart", .last_name = "Simpson"},
|
||||
Person{.id = 4, .first_name = "Lisa", .last_name = "Simpson"},
|
||||
});
|
||||
|
||||
const auto pets = std::vector<Pet>({
|
||||
Pet{.id = 1, .name = "Santa's Little Helper", .owner_id = 1},
|
||||
Pet{.id = 2, .name = "Snowball", .owner_id = 4},
|
||||
Pet{.id = 3, .name = "Mr. Teeny", .owner_id = 99},
|
||||
});
|
||||
|
||||
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
|
||||
.password = "password",
|
||||
.host = "localhost",
|
||||
.dbname = "postgres"};
|
||||
|
||||
using namespace sqlgen;
|
||||
using namespace sqlgen::literals;
|
||||
|
||||
struct PersonWithPet {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
std::optional<std::string> pet_name;
|
||||
};
|
||||
|
||||
const auto get_people_with_pets =
|
||||
select_from<Person, "t1">("first_name"_t1 | as<"first_name">,
|
||||
"last_name"_t1 | as<"last_name">,
|
||||
"name"_t2 | as<"pet_name">) |
|
||||
left_join<Pet, "t2">("id"_t1 == "owner_id"_t2) | order_by("id"_t1) |
|
||||
to<std::vector<PersonWithPet>>;
|
||||
|
||||
const auto result = postgres::connect(credentials)
|
||||
.and_then(drop<Person> | if_exists)
|
||||
.and_then(drop<Pet> | if_exists)
|
||||
.and_then(write(std::ref(people)))
|
||||
.and_then(write(std::ref(pets)))
|
||||
.and_then(get_people_with_pets)
|
||||
.value();
|
||||
|
||||
const std::string expected_query =
|
||||
R"(SELECT t1."first_name" AS "first_name", t1."last_name" AS "last_name", t2."name" AS "pet_name" FROM "Person" t1 LEFT JOIN "Pet" t2 ON t1."id" = t2."owner_id" ORDER BY t1."id")";
|
||||
const std::string expected_json =
|
||||
R"([{"first_name":"Homer","last_name":"Simpson","pet_name":"Santa's Little Helper"},{"first_name":"Marge","last_name":"Simpson"},{"first_name":"Bart","last_name":"Simpson"},{"first_name":"Lisa","last_name":"Simpson","pet_name":"Snowball"}])";
|
||||
|
||||
EXPECT_EQ(postgres::to_sql(get_people_with_pets), expected_query);
|
||||
EXPECT_EQ(rfl::json::write(result), expected_json);
|
||||
}
|
||||
|
||||
} // namespace test_left_join
|
||||
|
||||
#endif
|
||||
80
tests/postgres/test_right_join.cpp
Normal file
80
tests/postgres/test_right_join.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <optional>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/postgres.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_right_join {
|
||||
|
||||
TEST(postgres, test_right_join) {
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
};
|
||||
|
||||
struct Pet {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string name;
|
||||
uint32_t owner_id;
|
||||
};
|
||||
|
||||
const auto people = std::vector<Person>({
|
||||
Person{.id = 1, .first_name = "Homer", .last_name = "Simpson"},
|
||||
Person{.id = 2, .first_name = "Marge", .last_name = "Simpson"},
|
||||
Person{.id = 3, .first_name = "Bart", .last_name = "Simpson"},
|
||||
Person{.id = 4, .first_name = "Lisa", .last_name = "Simpson"},
|
||||
});
|
||||
|
||||
const auto pets = std::vector<Pet>({
|
||||
Pet{.id = 1, .name = "Santa's Little Helper", .owner_id = 1},
|
||||
Pet{.id = 2, .name = "Snowball", .owner_id = 4},
|
||||
Pet{.id = 3, .name = "Mr. Teeny", .owner_id = 99},
|
||||
});
|
||||
|
||||
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
|
||||
.password = "password",
|
||||
.host = "localhost",
|
||||
.dbname = "postgres"};
|
||||
|
||||
using namespace sqlgen;
|
||||
using namespace sqlgen::literals;
|
||||
|
||||
struct PetWithOwner {
|
||||
std::string pet_name;
|
||||
std::optional<std::string> owner_first_name;
|
||||
std::optional<std::string> owner_last_name;
|
||||
};
|
||||
|
||||
const auto get_pets_with_owners =
|
||||
select_from<Person, "t1">("name"_t2 | as<"pet_name">,
|
||||
"first_name"_t1 | as<"owner_first_name">,
|
||||
"last_name"_t1 | as<"owner_last_name">) |
|
||||
right_join<Pet, "t2">("id"_t1 == "owner_id"_t2) | order_by("id"_t2) |
|
||||
to<std::vector<PetWithOwner>>;
|
||||
|
||||
const auto result = postgres::connect(credentials)
|
||||
.and_then(drop<Person> | if_exists)
|
||||
.and_then(drop<Pet> | if_exists)
|
||||
.and_then(write(std::ref(people)))
|
||||
.and_then(write(std::ref(pets)))
|
||||
.and_then(get_pets_with_owners)
|
||||
.value();
|
||||
|
||||
const std::string expected_query =
|
||||
R"(SELECT t2."name" AS "pet_name", t1."first_name" AS "owner_first_name", t1."last_name" AS "owner_last_name" FROM "Person" t1 RIGHT JOIN "Pet" t2 ON t1."id" = t2."owner_id" ORDER BY t2."id")";
|
||||
const std::string expected_json =
|
||||
R"([{"pet_name":"Santa's Little Helper","owner_first_name":"Homer","owner_last_name":"Simpson"},{"pet_name":"Snowball","owner_first_name":"Lisa","owner_last_name":"Simpson"},{"pet_name":"Mr. Teeny"}])";
|
||||
|
||||
EXPECT_EQ(postgres::to_sql(get_pets_with_owners), expected_query);
|
||||
EXPECT_EQ(rfl::json::write(result), expected_json);
|
||||
}
|
||||
|
||||
} // namespace test_right_join
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user