fixed backend test compilation

This commit is contained in:
Sascha Kühl
2025-02-12 16:01:52 +01:00
parent 42acaf24ce
commit 5d41a592bb
36 changed files with 727 additions and 575 deletions
+50 -38
View File
@@ -7,52 +7,62 @@
#include "models/book.hpp"
#include "models/flight.hpp"
#include <iostream>
using namespace matador;
using namespace matador::object;
using namespace matador::test;
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
ses.attach<airplane>("airplanes");
ses.attach<flight>("flights");
ses.create_schema();
const auto result = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.attach<flight>("flights"); } )
.and_then([this] { return ses.create_schema(); } );
REQUIRE(result.is_ok());
tables_to_drop.emplace("airplanes");
tables_to_drop.emplace("flights");
auto plane = ses.insert<airplane>(1, "Boeing", "A380");
auto f = ses.insert<flight>(2, plane, "sully");
REQUIRE(plane.is_ok());
auto f = ses.insert<flight>(2, *plane, "sully");
REQUIRE(f.is_ok());
const auto result = ses.find<flight>(2);
REQUIRE(result.is_ok());
REQUIRE(result->get()->id == f->id);
REQUIRE(result->get()->pilot_name == f->pilot_name);
REQUIRE(result->get()->airplane);
REQUIRE(result->get()->airplane->id == plane->id);
REQUIRE(result->get()->airplane->brand == plane->brand);
REQUIRE(result->get()->airplane->model == plane->model);
const auto res = ses.find<flight>(2);
REQUIRE(res.is_ok());
auto rf = *res;
REQUIRE(rf->id == (*f)->id);
REQUIRE(rf->pilot_name == (*f)->pilot_name);
REQUIRE(rf->airplane);
REQUIRE(rf->airplane->id == (*plane)->id);
REQUIRE(rf->airplane->brand == (*plane)->brand);
REQUIRE(rf->airplane->model == (*plane)->model);
}
TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session][find]") {
ses.attach<airplane>("airplanes");
ses.create_schema();
const auto result = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.create_schema(); } );
REQUIRE(result.is_ok());
tables_to_drop.emplace("airplanes");
auto a380 = ses.insert<airplane>(1, "Boeing", "A380");
REQUIRE(a380.is_ok());
auto result = ses.find<airplane>(2);
auto res = ses.find<airplane>(2);
REQUIRE(!result.is_ok());
REQUIRE((result.err().ec() == sql::session_error_code::FailedToFindObject));
REQUIRE((result.err().ec() == orm::error_code::FailedToFindObject));
result = ses.find<airplane>(1);
auto find_result = ses.find<airplane>(1);
REQUIRE(result);
auto read_a380 = result.value();
REQUIRE(a380->id == read_a380->id);
REQUIRE(find_result);
auto read_a380 = find_result.value();
REQUIRE((*a380)->id == read_a380->id);
}
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][find]") {
ses.attach<airplane>("airplanes");
ses.create_schema();
const auto result = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.create_schema(); } );
REQUIRE(result.is_ok());
tables_to_drop.emplace("airplanes");
@@ -62,18 +72,19 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][f
planes.emplace_back(new airplane(3, "Boeing", "747"));
for (auto &&plane: planes) {
ses.insert(plane.release());
auto res = ses.insert(plane.release());
REQUIRE(res.is_ok());
}
auto result = ses.find<airplane>();
auto find_result = ses.find<airplane>();
std::vector<std::tuple<unsigned long, std::string, std::string>> expected_result {
{1, "Airbus", "A380"},
{2, "Boeing", "707"},
{3, "Boeing", "747"}
};
REQUIRE(result);
auto all_planes = result.release();
REQUIRE(find_result);
auto all_planes = find_result.release();
size_t index {0};
for (const auto &i: all_planes) {
REQUIRE(i.id == std::get<0>(expected_result[index]));
@@ -84,25 +95,25 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][f
}
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many relation", "[session][find][one-to-many]") {
ses.attach<author>("authors");
ses.attach<book>("books");
auto result = ses.attach<author>("authors")
.and_then( [this] { return ses.attach<book>("books"); } )
.and_then( [this] { return ses.create_schema(); } );
tables_to_drop.emplace("authors");
tables_to_drop.emplace("books");
ses.create_schema();
std::vector<std::unique_ptr<author>> authors;
authors.emplace_back(new author{1, "Michael", "Crichton", "23.10.1942", 1975, true, {}});
authors.emplace_back(new author{ 2, "Steven", "King", "21.9.1947", 1956, false, {}});
for (auto &&a: authors) {
ses.insert(a.release());
auto res = ses.insert(a.release());
REQUIRE(res.is_ok());
}
auto result = ses.find<author>();
REQUIRE(result.is_ok());
auto all_authors = result.release();
auto find_result = ses.find<author>();
REQUIRE(find_result.is_ok());
auto all_authors = find_result.release();
std::vector<object_ptr<author>> author_repo;
for (auto it = all_authors.begin(); it != all_authors.end(); ++it) {
std::cout << "author: " << it->first_name << " (books: " << it->books.size() << ")\n";
@@ -123,13 +134,14 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
books.emplace_back( new book{12, "The Dark Tower: The Gunslinger", author_repo[1], 1982} );
for (auto &&b: books) {
ses.insert(b.release());
auto res = ses.insert(b.release());
REQUIRE(res.is_ok());
}
result = ses.find<author>();
REQUIRE(result);
find_result = ses.find<author>();
REQUIRE(find_result);
all_authors = result.release();
all_authors = find_result.release();
for (auto it = all_authors.begin(); it != all_authors.end(); ++it) {
std::cout << "author: " << it->first_name << " (books: " << it->books.size() << ")\n";
}