initial matador ng commit
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
#include "catch2/catch_test_macros.hpp"
|
||||
|
||||
#include "SessionFixture.hpp"
|
||||
|
||||
#include "models/airplane.hpp"
|
||||
#include "models/author.hpp"
|
||||
#include "models/book.hpp"
|
||||
#include "models/flight.hpp"
|
||||
|
||||
using namespace matador;
|
||||
using namespace matador::test;
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
|
||||
ses.attach<airplane>("airplanes");
|
||||
ses.attach<flight>("flights");
|
||||
ses.create_schema();
|
||||
|
||||
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");
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session][find]") {
|
||||
ses.attach<airplane>("airplanes");
|
||||
ses.create_schema();
|
||||
|
||||
tables_to_drop.emplace("airplanes");
|
||||
|
||||
auto a380 = ses.insert<airplane>(1, "Boeing", "A380");
|
||||
|
||||
auto result = ses.find<airplane>(2);
|
||||
REQUIRE(!result.is_ok());
|
||||
REQUIRE((result.err().ec() == sql::session_error_code::FailedToFindObject));
|
||||
|
||||
result = ses.find<airplane>(1);
|
||||
|
||||
REQUIRE(result);
|
||||
auto read_a380 = 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();
|
||||
|
||||
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) {
|
||||
ses.insert(plane.release());
|
||||
}
|
||||
|
||||
auto 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();
|
||||
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 relation", "[session][find][one-to-many]") {
|
||||
ses.attach<author>("authors");
|
||||
ses.attach<book>("books");
|
||||
|
||||
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 result = ses.find<author>();
|
||||
REQUIRE(result.is_ok());
|
||||
auto all_authors = 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) {
|
||||
ses.insert(b.release());
|
||||
}
|
||||
|
||||
result = ses.find<author>();
|
||||
REQUIRE(result);
|
||||
|
||||
all_authors = result.release();
|
||||
for (auto it = all_authors.begin(); it != all_authors.end(); ++it) {
|
||||
std::cout << "author: " << it->first_name << " (books: " << it->books.size() << ")\n";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user