285 lines
11 KiB
C++
285 lines
11 KiB
C++
#include "catch2/catch_test_macros.hpp"
|
|
|
|
#include "SessionFixture.hpp"
|
|
|
|
#include "models/airplane.hpp"
|
|
#include "models/author.hpp"
|
|
#include "models/book.hpp"
|
|
#include "models/department.hpp"
|
|
#include "models/flight.hpp"
|
|
#include "models/recipe.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
using namespace matador;
|
|
using namespace matador::object;
|
|
using namespace matador::test;
|
|
|
|
TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") {
|
|
const auto result = ses.attach<airplane>("airplanes")
|
|
.and_then([this] { return ses.create_schema(); } );
|
|
REQUIRE(result.is_ok());
|
|
|
|
tables_to_drop.emplace("airplanes");
|
|
|
|
auto plane = ses.insert<airplane>(1, "Boeing", "A380");
|
|
REQUIRE(plane.is_ok());
|
|
|
|
const auto res = ses.find<airplane>(1);
|
|
REQUIRE(res.is_ok());
|
|
const auto& read_airplane = *res;
|
|
REQUIRE(read_airplane->id == (*plane)->id);
|
|
REQUIRE(read_airplane->brand == (*plane)->brand);
|
|
REQUIRE(read_airplane->model == (*plane)->model);
|
|
}
|
|
|
|
TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
|
|
const auto res = ses.attach<airplane>("airplanes")
|
|
.and_then([this] { return ses.create_schema(); } );
|
|
REQUIRE(res.is_ok());
|
|
|
|
tables_to_drop.emplace("airplanes");
|
|
|
|
auto result = ses.insert<airplane>(1, "Boeing", "747");
|
|
REQUIRE(result.is_ok());
|
|
|
|
const auto plane = result.value();
|
|
result = ses.find<airplane>(1);
|
|
REQUIRE(result.is_ok());
|
|
auto read_airplane = *result;
|
|
REQUIRE(read_airplane->id == plane->id);
|
|
REQUIRE(read_airplane->brand == plane->brand);
|
|
REQUIRE(read_airplane->model == plane->model);
|
|
|
|
read_airplane->brand = "Airbus";
|
|
read_airplane->model = "A380";
|
|
|
|
auto update_result = ses.update(read_airplane);
|
|
REQUIRE(update_result.is_ok());
|
|
|
|
result = ses.find<airplane>(1);
|
|
REQUIRE(result.is_ok());
|
|
read_airplane = *result;
|
|
REQUIRE(read_airplane->id == plane->id);
|
|
REQUIRE(read_airplane->brand == "Airbus");
|
|
REQUIRE(read_airplane->model == "A380");
|
|
}
|
|
|
|
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
|
|
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");
|
|
REQUIRE(plane.is_ok());
|
|
auto f = ses.insert<flight>(2, *plane, "sully");
|
|
REQUIRE(f.is_ok());
|
|
|
|
const auto res = ses.find<flight>(2);
|
|
REQUIRE(res.is_ok());
|
|
const auto& rf = *res;
|
|
REQUIRE(rf->id == (*f)->id);
|
|
REQUIRE(rf->pilot_name == (*f)->pilot_name);
|
|
REQUIRE(rf->plane);
|
|
REQUIRE(rf->plane->id == (*plane)->id);
|
|
REQUIRE(rf->plane->brand == (*plane)->brand);
|
|
REQUIRE(rf->plane->model == (*plane)->model);
|
|
}
|
|
|
|
TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session][find]") {
|
|
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 find_result = ses.find<airplane>(2);
|
|
REQUIRE(!find_result.is_ok());
|
|
REQUIRE((find_result.err().ec() == orm::error_code::FailedToFindObject));
|
|
|
|
find_result = ses.find<airplane>(1);
|
|
|
|
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]") {
|
|
const auto result = ses.attach<airplane>("airplanes")
|
|
.and_then([this] { return ses.create_schema(); } );
|
|
REQUIRE(result.is_ok());
|
|
|
|
tables_to_drop.emplace("airplanes");
|
|
|
|
std::vector<std::unique_ptr<airplane>> planes;
|
|
planes.emplace_back(new airplane(1, "Airbus", "A380"));
|
|
planes.emplace_back(new airplane(2, "Boeing", "707"));
|
|
planes.emplace_back(new airplane(3, "Boeing", "747"));
|
|
|
|
for (auto &&plane: planes) {
|
|
auto res = ses.insert(plane.release());
|
|
REQUIRE(res.is_ok());
|
|
}
|
|
|
|
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(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]));
|
|
REQUIRE(i.brand == std::get<1>(expected_result[index]));
|
|
REQUIRE(i.model == std::get<2>(expected_result[index]));
|
|
++index;
|
|
}
|
|
}
|
|
|
|
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many lazy relation", "[session][find][one-to-many][eager]") {
|
|
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");
|
|
|
|
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) {
|
|
auto res = ses.insert(a.release());
|
|
REQUIRE(res.is_ok());
|
|
}
|
|
|
|
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";
|
|
author_repo.emplace_back(it.release());
|
|
}
|
|
REQUIRE(author_repo.size() == 2);
|
|
|
|
std::vector<std::unique_ptr<book>> books;
|
|
books.emplace_back( new book{3, "Jurassic Park", author_repo[0], 1990} );
|
|
books.emplace_back( new book{4, "Timeline", author_repo[0], 1999} );
|
|
books.emplace_back( new book{5, "The Andromeda Strain", author_repo[0], 1969} );
|
|
books.emplace_back( new book{6, "Congo", author_repo[0], 1980} );
|
|
books.emplace_back( new book{7, "Prey", author_repo[0], 2002} );
|
|
books.emplace_back( new book{8, "Carrie", author_repo[1], 1974} );
|
|
books.emplace_back( new book{9, "The Shining", author_repo[1], 1977} );
|
|
books.emplace_back( new book{10, "It", author_repo[1], 1986} );
|
|
books.emplace_back( new book{11, "Misery", author_repo[1], 1987} );
|
|
books.emplace_back( new book{12, "The Dark Tower: The Gunslinger", author_repo[1], 1982} );
|
|
|
|
for (auto &&b: books) {
|
|
auto res = ses.insert(b.release());
|
|
REQUIRE(res.is_ok());
|
|
}
|
|
|
|
find_result = ses.find<author>();
|
|
REQUIRE(find_result);
|
|
|
|
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";
|
|
}
|
|
}
|
|
|
|
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many eager relation", "[session][find][one-to-many][eager]") {
|
|
auto result = ses.attach<department>("departments")
|
|
.and_then( [this] { return ses.attach<employee>("employees"); } )
|
|
.and_then( [this] { return ses.create_schema(); } );
|
|
|
|
tables_to_drop.emplace("departments");
|
|
tables_to_drop.emplace("employees");
|
|
|
|
std::vector<std::unique_ptr<department>> departments;
|
|
departments.emplace_back(new department{1, "Insurance", {}});
|
|
departments.emplace_back(new department{ 2, "Invoice", {}});
|
|
|
|
for (auto &&a: departments) {
|
|
auto res = ses.insert(a.release());
|
|
REQUIRE(res.is_ok());
|
|
}
|
|
|
|
auto find_result = ses.find<department>();
|
|
REQUIRE(find_result.is_ok());
|
|
auto all_departments = find_result.release();
|
|
std::vector<object_ptr<department>> departments_repo;
|
|
for (auto it = all_departments.begin(); it != all_departments.end(); ++it) {
|
|
std::cout << "department: " << it->name << " (employees: " << it->employees.size() << ")\n";
|
|
departments_repo.emplace_back(it.release());
|
|
}
|
|
REQUIRE(departments_repo.size() == 2);
|
|
|
|
std::vector<std::unique_ptr<employee>> employees;
|
|
employees.emplace_back( new employee{3, "George", "Orwell", departments_repo[0]} );
|
|
employees.emplace_back( new employee{4, "Chris", "Tucker", departments_repo[0]} );
|
|
employees.emplace_back( new employee{5, "Steven", "King", departments_repo[0]} );
|
|
employees.emplace_back( new employee{6, "John", "Wayne", departments_repo[0]} );
|
|
employees.emplace_back( new employee{7, "Clint", "Eastwood", departments_repo[0]} );
|
|
employees.emplace_back( new employee{8, "Emma", "Thompson", departments_repo[1]} );
|
|
employees.emplace_back( new employee{9, "Ed", "Wood", departments_repo[1]} );
|
|
employees.emplace_back( new employee{10, "Steven", "Spielberg", departments_repo[1]} );
|
|
employees.emplace_back( new employee{11, "Jane", "Fonda", departments_repo[1]} );
|
|
employees.emplace_back( new employee{12, "Julia", "Roberts", departments_repo[1]} );
|
|
|
|
for (auto &&b: employees) {
|
|
auto res = ses.insert(b.release());
|
|
REQUIRE(res.is_ok());
|
|
}
|
|
|
|
find_result = ses.find<department>();
|
|
REQUIRE(find_result);
|
|
|
|
all_departments = find_result.release();
|
|
for (auto it = all_departments.begin(); it != all_departments.end(); ++it) {
|
|
std::cout << "department: " << it->name << " (id: " << it->id << ", employees: " << it->employees.size() << ")\n";
|
|
for (const auto& emp : it->employees) {
|
|
std::cout << "\temployee: " << emp->first_name << " " << emp->last_name << "( " << emp->id << ")\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-many eager relation", "[session][find][many-to-many][eager]") {
|
|
auto result = ses.attach<recipe>("recipes")
|
|
.and_then( [this] { return ses.attach<ingredient>("ingredients"); } )
|
|
.and_then( [this] { return ses.create_schema(); } );
|
|
|
|
tables_to_drop.emplace("recipes");
|
|
tables_to_drop.emplace("ingredients");
|
|
tables_to_drop.emplace("recipe_ingredients");
|
|
|
|
std::vector<std::unique_ptr<ingredient>> ingredients;
|
|
ingredients.push_back(std::make_unique<ingredient>(1, "Apple"));
|
|
ingredients.push_back(std::make_unique<ingredient>(2, "Strawberry"));
|
|
ingredients.push_back(std::make_unique<ingredient>(3, "Pineapple"));
|
|
ingredients.push_back(std::make_unique<ingredient>(4, "Sugar"));
|
|
ingredients.push_back(std::make_unique<ingredient>(5, "Flour"));
|
|
ingredients.push_back(std::make_unique<ingredient>(6, "Butter"));
|
|
ingredients.push_back(std::make_unique<ingredient>(7, "Beans"));
|
|
|
|
for (auto &i: ingredients) {
|
|
auto res = ses.insert(i.release());
|
|
REQUIRE(res.is_ok());
|
|
}
|
|
|
|
std::vector<std::unique_ptr<recipe>> recipes;
|
|
recipes.push_back(std::make_unique<recipe>(7, "Apple Crumble"));
|
|
recipes.push_back(std::make_unique<recipe>(8, "Beans Chili"));
|
|
recipes.push_back(std::make_unique<recipe>(9, "Fruit Salad"));
|
|
} |