query/test/backends/QueryTest.cpp

729 lines
23 KiB
C++

#include "catch2/catch_test_macros.hpp"
#include "matador/query/builder.hpp"
#include "matador/query/criteria.hpp"
#include "matador/query/generator.hpp"
#include "matador/query/query.hpp"
#include "matador/query/meta_table_macro.hpp"
#include "QueryFixture.hpp"
#include "models/airplane.hpp"
#include "models/author.hpp"
#include "models/book.hpp"
#include "models/department.hpp"
#include "models/flight.hpp"
#include "models/person.hpp"
#include "models/recipe.hpp"
#include "models/shipment.hpp"
#include "models/model_metas.hpp"
#include "../test/utils/record_printer.hpp"
using namespace matador::object;
using namespace matador::query;
using namespace matador::sql;
using namespace matador::test;
using namespace matador::query::meta;
TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") {
auto result = repo.attach<airplane>("airplane")
.and_then([this] { return repo.attach<flight>("flight");})
.and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok());
check_table_exists("airplane");
check_table_exists("flight");
}
TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[query][where]") {
REQUIRE(repo.attach<person>("persons"));
auto result = repo.create(db);
REQUIRE(result.is_ok());
check_table_exists(PERSON.table_name());
person george{7, "george", 45};
george.image.emplace_back(37);
auto res = query::insert()
.into(PERSON, {PERSON.id, PERSON.name, PERSON.age, PERSON.image})
.values(george)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
// fetch person as record
auto result_record = query::select({PERSON.id, PERSON.name, PERSON.age, PERSON.image})
.from(PERSON)
.where(PERSON.id == 7)
.fetch_all(db);
REQUIRE(result_record.is_ok());
for (const auto &i: *result_record) {
REQUIRE(i.size() == 4);
REQUIRE(i.at(0).name() == "persons.id");
REQUIRE(i.at(0).is_integer());
REQUIRE(i.at(0).as<uint32_t>() == george.id);
REQUIRE(i.at(1).name() == "persons.name");
REQUIRE(i.at(1).is_varchar());
REQUIRE(i.at(1).as<std::string>() == george.name);
REQUIRE(i.at(2).name() == "persons.age");
REQUIRE(i.at(2).is_integer());
REQUIRE(i.at(2).as<uint32_t>() == george.age);
}
// fetch person as person
auto result_person = query::select({PERSON.id, PERSON.name, PERSON.age, PERSON.image})
.from(PERSON)
.where(PERSON.id == 7)
.fetch_all<person>(db);
REQUIRE(result_person.is_ok());
for (const auto &i: *result_person) {
REQUIRE(i.id == 7);
REQUIRE(i.name == "george");
REQUIRE(i.age == 45);
}
}
TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]") {
auto res = query::create()
.table("person")
.columns({
column("id", matador::utils::basic_type::UInt32),
column("name", matador::utils::basic_type::Varchar, 255),
column("color", matador::utils::basic_type::Varchar, 63)
})
.constraints({
constraint("PK_person").primary_key("id")
})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
REQUIRE(db.exists("person"));
tables_to_drop.emplace("person");
res = query::insert()
.into("person", {{"id", ""}, {"name", ""}, {"color", ""}})
.values({7, "george", "green"})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
// fetch person as record
auto result_record = query::select({"id", "name", "color"})
.from("person")
.where("id"_col == 7)
.fetch_all(db);
REQUIRE(result_record.is_ok());
for (const auto &i: *result_record) {
REQUIRE(i.size() == 3);
REQUIRE(i.at(0).name() == "id");
REQUIRE(i.at(0).is_integer());
REQUIRE(i.at(0).as<uint32_t>() == 7);
REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).is_varchar());
REQUIRE(i.at(1).as<std::string>() == "george");
REQUIRE(i.at(2).name() == "color");
REQUIRE(i.at(2).is_varchar());
REQUIRE(i.at(2).as<std::string>() == "green");
}
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][foreign]") {
auto result = repo.attach<airplane>("airplanes")
.and_then( [this] { return repo.attach<flight>("flights");} )
.and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok());
REQUIRE(db.exists(AIRPLANE.table_name()));
REQUIRE(db.exists(FLIGHT.table_name()));
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
};
for (const auto &plane: planes) {
auto res = query::insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.values(*plane)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto count = query::select({count_all()})
.from(AIRPLANE)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 3);
flight f4711{4, planes.at(1), "hans"};
auto res = query::insert()
.into(FLIGHT, {FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.values(f4711)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
auto f = query::select({FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.from(FLIGHT)
.fetch_one(db);
REQUIRE(f.is_ok());
REQUIRE(f.value()->at(0).as<uint32_t>() == 4);
REQUIRE(f.value()->at(1).as<uint32_t>() == 2);
REQUIRE(f.value()->at(2).as<std::string>() == "hans");
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left", "[query][foreign][join_left]") {
auto result = repo.attach<airplane>("airplanes")
.and_then( [this] { return repo.attach<flight>("flights");} )
.and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok());
REQUIRE(db.exists(AIRPLANE.table_name()));
REQUIRE(db.exists(FLIGHT.table_name()));
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
};
for (const auto &plane: planes) {
auto res = query::insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.values(*plane)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto count = query::select({count_all()})
.from(AIRPLANE)
.fetch_value<int>(db).value();
REQUIRE(count == 3);
std::vector flights{
object_ptr(new flight{4, planes.at(0), "hans"}),
object_ptr(new flight{5, planes.at(0), "otto"}),
object_ptr(new flight{6, planes.at(1), "george"}),
object_ptr(new flight{7, planes.at(2), "paul"})
};
for (const auto &f: flights) {
auto res = query::insert()
.into(FLIGHT, {FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.values(*f)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto flight_result = query::select({FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.from(FLIGHT)
.fetch_one(db);
REQUIRE(flight_result.is_ok());
REQUIRE(flight_result->has_value());
REQUIRE(flight_result.value()->at(0).as<uint32_t>() == 4);
REQUIRE(flight_result.value()->at(1).as<uint32_t>() == 1);
REQUIRE(flight_result.value()->at(2).as<std::string>() == "hans");
const auto f = FLIGHT.as("f");
const auto ap = AIRPLANE.as("ap");
auto select_result = query::select({f.id, ap.brand, ap.model, f.pilot_name})
.from(f)
.join_left(ap)
.on(f.airplane_id == ap.id)
.order_by(f.id).asc()
.fetch_all(db);
REQUIRE(select_result.is_ok());
std::vector<std::pair<uint32_t, std::string>> expected_result {
{4, "hans"},
{5, "otto"},
{6, "george"},
{7, "paul"}
};
size_t index{0};
for (const auto &r: *select_result) {
REQUIRE(r.size() == 4);
REQUIRE(r.at(0).as<uint32_t>() == expected_result[index].first);
REQUIRE(r.at(3).as<std::string>() == expected_result[index++].second);
}
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single entity", "[query][join_left][find]") {
auto result = repo.attach<airplane>("airplanes")
.and_then([this] { return repo.attach<flight>("flights");})
.and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok());
REQUIRE(db.exists(AIRPLANE.table_name()));
REQUIRE(db.exists(FLIGHT.table_name()));
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
};
for (const auto &plane: planes) {
auto res = query::insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.values(*plane)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto count = query::select({count_all()})
.from(AIRPLANE)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(count->has_value());
REQUIRE(count->value() == 3);
std::vector flights{
object_ptr(new flight{4, planes.at(0), "hans"}),
object_ptr(new flight{5, planes.at(0), "otto"}),
object_ptr(new flight{6, planes.at(1), "george"}),
object_ptr(new flight{7, planes.at(2), "paul"})
};
for (const auto &f: flights) {
auto res = query::insert()
.into(FLIGHT, {FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.values(*f)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto flight_result = query::select({FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.from(FLIGHT)
.fetch_one(db);
REQUIRE(flight_result.is_ok());
REQUIRE(flight_result->has_value());
REQUIRE(flight_result.value()->at(0).as<uint32_t>() == 4);
REQUIRE(flight_result.value()->at(1).as<uint32_t>() == 1);
REQUIRE(flight_result.value()->at(2).as<std::string>() == "hans");
const auto f = FLIGHT.as("f");
const auto ap = AIRPLANE.as("ap");
auto select_result = query::select({f.id, f.airplane_id, ap.brand, ap.model, f.pilot_name})
.from(f)
.join_left(ap)
.on(f.airplane_id == ap.id)
.where(f.id == 4)
.fetch_one<flight>(db);
auto expected_flight = flights[0];
REQUIRE(select_result.is_ok());
REQUIRE(*select_result);
auto f1 = select_result.release();
REQUIRE(f1->id == expected_flight->id);
REQUIRE(f1->pilot_name == expected_flight->pilot_name);
REQUIRE(f1->plane.get());
REQUIRE(f1->plane->id == 1);
REQUIRE(f1->plane->model == "A380");
REQUIRE(f1->plane->brand == "Airbus");
}
TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship", "[query][join][many_to_many]") {
auto result = repo.attach<ingredient>("ingredients")
.and_then( [this] { return repo.attach<recipe>("recipes"); } )
.and_then([this] {return repo.create(db); });
REQUIRE(db.exists(RECIPE.table_name()));
REQUIRE(db.exists(INGREDIENT.table_name()));
REQUIRE(db.exists(RECIPE_INGREDIENT.table_name()));
std::vector<ingredient> ingredients {
{1, "Apple"},
{2, "Strawberry"},
{3, "Pineapple"},
{4, "Sugar"},
{5, "Flour"},
{6, "Butter"},
{7, "Beans"}
};
for (const auto &i: ingredients) {
auto res = query::insert()
.into(INGREDIENT, {INGREDIENT.id, INGREDIENT.name})
.values(i)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
std::vector<recipe> recipes{
{7, "Apple Crumble"},
{8, "Beans Chili"},
{9, "Fruit Salad"}
};
for (const auto &r: recipes) {
auto res = query::insert()
.into(RECIPE, {RECIPE.id, RECIPE.name})
.values(r)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
std::vector<std::pair<int, int>> recipe_ingredients {
{ 7, 1 },
{ 7, 4 },
{ 7, 5 },
{ 8, 6 },
{ 8, 7 },
{ 9, 1 },
{ 9, 2 },
{ 9, 3 }
};
for (const auto & [recipe_id, ingredient_id]: recipe_ingredients) {
auto res = query::insert()
.into(RECIPE_INGREDIENT, {RECIPE_INGREDIENT.recipe_id, RECIPE_INGREDIENT.ingredient_id})
.values({recipe_id, ingredient_id})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
const auto r = RECIPE.as("r");
const auto ri= RECIPE_INGREDIENT.as("ri");
const auto i = INGREDIENT.as("i");
auto select_result = query::select({ r.id, r.name, ri.ingredient_id})
.from(r)
.join_left(ri)
.on(r.id == ri.recipe_id)
.fetch_all(db);
REQUIRE(select_result.is_ok());
std::vector<std::tuple<uint32_t, std::string, uint32_t>> expected_result_one_join {
{7, "Apple Crumble", 1},
{7, "Apple Crumble", 4},
{7, "Apple Crumble", 5},
{8, "Beans Chili", 6},
{8, "Beans Chili", 7},
{9, "Fruit Salad", 1},
{9, "Fruit Salad", 2},
{9, "Fruit Salad", 3}
};
size_t index{0};
for (const auto &row: *select_result) {
REQUIRE(row.size() == 3);
REQUIRE(row.at(0).as<uint32_t>().value() == std::get<0>(expected_result_one_join[index]));
REQUIRE(row.at(1).as<std::string>().value() == std::get<1>(expected_result_one_join[index]));
REQUIRE(row.at(2).as<uint32_t>().value() == std::get<2>(expected_result_one_join[index]));
++index;
}
select_result = query::select({r.id, r.name, ri.ingredient_id, i.name})
.from(r)
.join_left(ri)
.on(r.id == ri.recipe_id)
.join_left(i)
.on(ri.ingredient_id == i.id)
.fetch_all(db);
REQUIRE(result.is_ok());
std::vector<std::tuple<uint32_t, std::string, uint32_t, std::string>> expected_result_two_joins {
{7, "Apple Crumble", 1, "Apple"},
{7, "Apple Crumble", 4, "Sugar"},
{7, "Apple Crumble", 5, "Flour"},
{8, "Beans Chili", 6, "Butter"},
{8, "Beans Chili", 7, "Beans"},
{9, "Fruit Salad", 1, "Apple"},
{9, "Fruit Salad", 2, "Strawberry"},
{9, "Fruit Salad", 3, "Pineapple"}
};
index = 0;
for (const auto &row: *select_result) {
REQUIRE(row.size() == 4);
REQUIRE(row.at(0).as<uint32_t>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(row.at(1).as<std::string>().value() == std::get<1>(expected_result_two_joins[index]));
REQUIRE(row.at(2).as<uint32_t>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(row.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
}
select_result = query::select({r.id, r.name, ri.ingredient_id, i.name})
.from(r)
.join_left(ri).on(r.id == ri.recipe_id)
.join_left(i).on(ri.ingredient_id == i.id)
.where(r.id == 8)
.fetch_all(db);
REQUIRE(result.is_ok());
index = 3;
for (const auto &row: *select_result) {
REQUIRE(row.size() == 4);
REQUIRE(row.at(0).as<uint32_t>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(row.at(1).as<std::string>().value() == std::get<1>(expected_result_two_joins[index]));
REQUIRE(row.at(2).as<uint32_t>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(row.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
}
}
void print(std::ostream& out, const record& row) {
for (const auto& f : row) {
out << f << " ";
}
}
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", "[query][has_many][eager]") {
auto result = repo.attach<package>("packages")
.and_then([this] { return repo.attach<shipment>("shipments"); })
.and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok());
const std::vector shipments {
object_ptr{new shipment{1, "4711"}},
object_ptr{new shipment{2, "0815"}}
};
for (const auto &sh: shipments) {
auto res = query::insert()
.into(SHIPMENT, SHIPMENT)
.values(*sh)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto count = query::select({count_all()})
.from(SHIPMENT)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 2);
std::vector packages {
object_ptr{new package{3, 15.4, shipments.at(0)}},
object_ptr{new package{4, 1.3, shipments.at(0)}},
object_ptr{new package{5, 30.9, shipments.at(1)}},
object_ptr{new package{6, 22.8, shipments.at(1)}},
object_ptr{new package{7, 17.2, shipments.at(1)}}
};
for (const auto &pkg: packages) {
auto res = query::insert()
.into(PACKAGE, PACKAGE)
.values(*pkg)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
count = query::select({count_all()})
.from(PACKAGE)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 5);
auto pkgs = query::select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id, SHIPMENT.tracking_number})
.from(PACKAGE)
.join_left(SHIPMENT)
.on( PACKAGE.shipment_id == SHIPMENT.id )
.where( PACKAGE.weight > 20.0 && SHIPMENT.tracking_number == "0815" )
.group_by({PACKAGE.id, SHIPMENT.tracking_number})
.order_by(PACKAGE.weight).asc()
.limit( 5 )
.offset( 0 )
.fetch_all(db);
REQUIRE(pkgs.is_ok());
record_printer printer(std::cout);
printer.print(*pkgs);
auto shipment_records = query::select({SHIPMENT.tracking_number, SHIPMENT.id, PACKAGE.weight, PACKAGE.weight})
.from(SHIPMENT)
.join_left(PACKAGE)
.on( SHIPMENT.id == PACKAGE.shipment_id )
.fetch_all(db);
REQUIRE(shipment_records.is_ok());
printer.print(*shipment_records);
auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id})
.from(SHIPMENT)
.join_left(PACKAGE)
.on( SHIPMENT.id == PACKAGE.shipment_id )
.order_by(SHIPMENT.id).asc()
.fetch_all<shipment>(db);
REQUIRE(shipment_result.is_ok());
size_t index{0};
std::vector<size_t> packages_sizes{2, 3};
std::cout << "\n";
for (const auto &s : *shipment_result) {
REQUIRE(s.id == shipments.at(index)->id);
REQUIRE(s.tracking_number == shipments.at(index)->tracking_number);
REQUIRE(s.packages.size() == packages_sizes.at(index++));
}
}
TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation", "[query][belongs_to][lazy]") {
auto result = repo.attach<department>("departments")
.and_then([this] { return repo.attach<employee>("employees"); })
.and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok());
const std::vector deps {
object_ptr{new department{1, "Human Resources"}},
object_ptr{new department{2, "Invoices"}}
};
const std::vector emps {
object_ptr{new employee{3, "Hans", "Wurst", deps[0]}},
object_ptr{new employee{4, "Steven", "Spielberg", deps[0]}},
object_ptr{new employee{5, "Julia", "Roberts", deps[0]}},
object_ptr{new employee{6, "Otto", "Walkes", deps[1]}},
object_ptr{new employee{7, "Miss", "Marple", deps[1]}},
};
for (const auto &dep: deps) {
auto res = query::insert()
.into(DEPARTMENT, {DEPARTMENT.id, DEPARTMENT.name})
.values(*dep)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto count = query::select({count_all()})
.from(DEPARTMENT)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 2);
for (const auto &emp: emps) {
auto res = query::insert()
.into(EMPLOYEE, {EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id})
.values(*emp)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
count = query::select({count_all()})
.from(EMPLOYEE)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 5);
auto emps_result = query::select({EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, DEPARTMENT.id, DEPARTMENT.name})
.from(EMPLOYEE)
.join_left(DEPARTMENT)
.on(EMPLOYEE.dep_id == DEPARTMENT.id)
.order_by(EMPLOYEE.id).asc()
.fetch_all<employee>(db);
REQUIRE(emps_result.is_ok());
size_t index{0};
for (const auto &e : *emps_result) {
REQUIRE(e.id == emps.at(index)->id);
REQUIRE(e.first_name == emps.at(index)->first_name);
REQUIRE(e.last_name == emps.at(index)->last_name);
REQUIRE(e.dep->id == emps.at(index)->dep->id);
REQUIRE(e.dep->name == emps.at(index)->dep->name);
++index;
}
}
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation", "[query][belongs_to][eager]") {
auto result = repo.attach<author>("authors")
.and_then( [this] { return repo.attach<book>("books"); } )
.and_then([this] {
return repo.create(db);
} );
const std::vector authors {
object_ptr{new author{1, "Michael", "Crichton", "23.10.1942", 1975, true, {}}},
object_ptr{new author{ 2, "Steven", "King", "21.9.1947", 1956, false, {}}}
};
const std::vector books {
object_ptr{new book{3, "Jurassic Park", authors[0], 1990}},
object_ptr{new book{4, "Timeline", authors[0], 1999}},
object_ptr{new book{5, "The Andromeda Strain", authors[0], 1969}},
object_ptr{new book{6, "Congo", authors[0], 1980}},
object_ptr{new book{7, "Prey", authors[0], 2002}},
object_ptr{new book{8, "Carrie", authors[1], 1974}},
object_ptr{new book{9, "The Shining", authors[1], 1977}},
object_ptr{new book{10, "It", authors[1], 1986}},
object_ptr{new book{11, "Misery", authors[1], 1987}},
object_ptr{new book{12, "The Dark Tower: The Gunslinger", authors[1], 1982}},
};
for (const auto &a: authors) {
auto res = query::insert()
.into(AUTHOR, {AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
.values(*a)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto count = query::select({count_all()})
.from(AUTHOR)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 2);
for (const auto &b: books) {
auto res = query::insert()
.into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in})
.values(*b)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
count = query::select({count_all()})
.from(BOOK)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 10);
auto books_result = query::select({BOOK.id, BOOK.title, AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished, BOOK.published_in})
.from(BOOK)
.join_left(AUTHOR)
.on(BOOK.author_id == AUTHOR.id)
.order_by(BOOK.id).asc()
.fetch_all<book>(db);
REQUIRE(books_result.is_ok());
size_t index{0};
for (const auto &b : *books_result) {
REQUIRE(b.id == books.at(index)->id);
REQUIRE(b.title == books.at(index)->title);
REQUIRE(b.book_author->id == books.at(index)->book_author->id);
REQUIRE(b.book_author->first_name == books.at(index)->book_author->first_name);
REQUIRE(b.book_author->last_name == books.at(index)->book_author->last_name);
REQUIRE(b.book_author->date_of_birth == books.at(index)->book_author->date_of_birth);
REQUIRE(b.book_author->year_of_birth == books.at(index)->book_author->year_of_birth);
REQUIRE(b.book_author->distinguished == books.at(index)->book_author->distinguished);
REQUIRE(b.published_in == books.at(index)->published_in);
++index;
}
}