moved schema to query namespace
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include "../../models/department.hpp"
|
||||
#include "../../models/recipe.hpp"
|
||||
|
||||
struct node {
|
||||
};
|
||||
|
||||
using namespace matador;
|
||||
|
||||
struct person {
|
||||
virtual ~person() = default;
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &/*op*/) {
|
||||
}
|
||||
};
|
||||
|
||||
struct student final : person {
|
||||
};
|
||||
|
||||
struct teacher final : person {
|
||||
};
|
||||
|
||||
struct names {
|
||||
unsigned int id{};
|
||||
std::vector<std::string> names_list;
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::has_many(op, "name_list", names_list, "names_id", utils::fetch_type::Eager);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("Test empty prototype tree", "[schema_node][empty]") {
|
||||
const object::repository repo;
|
||||
|
||||
REQUIRE(repo.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("Test add type to prototype tree", "[schema_node][add]") {
|
||||
object::repository repo;
|
||||
|
||||
REQUIRE(repo.empty());
|
||||
|
||||
auto res = repo.attach<person>("person");
|
||||
REQUIRE(res.is_ok());
|
||||
res = repo.attach<student, person>("student");
|
||||
REQUIRE(res.is_ok());
|
||||
res = repo.attach<teacher, person>("teacher");
|
||||
REQUIRE(res.is_ok());
|
||||
|
||||
REQUIRE(!repo.empty());
|
||||
REQUIRE(repo.size() == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("Test next and previous of schema node", "[schema_node][next][previous]") {
|
||||
object::repository repo;
|
||||
|
||||
REQUIRE(repo.empty());
|
||||
|
||||
auto res = repo.attach<person>("person");
|
||||
REQUIRE(res.is_ok());
|
||||
|
||||
REQUIRE(repo.size() == 1);
|
||||
|
||||
auto it = repo.begin();
|
||||
REQUIRE(it->name() == "person");
|
||||
REQUIRE((--it)->name() == "person");
|
||||
REQUIRE(++it == repo.end());
|
||||
}
|
||||
|
||||
TEST_CASE("Test automatic creating of a relation table with foreign key", "[schema][relation_table][foreign_key]") {
|
||||
object::repository repo;
|
||||
|
||||
REQUIRE(repo.empty());
|
||||
|
||||
auto res = repo.attach<test::department>("department");
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(repo.size() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("Test automatic creating of a relation table with values", "[schema][relation_table][values]") {
|
||||
object::repository repo;
|
||||
|
||||
REQUIRE(repo.empty());
|
||||
|
||||
auto res = repo.attach<names>("names");
|
||||
REQUIRE(res.is_ok());
|
||||
}
|
||||
|
||||
TEST_CASE("Test one to many", "[relation][one-to-many]") {
|
||||
object::repository repo;
|
||||
|
||||
REQUIRE(repo.empty());
|
||||
|
||||
auto res = repo.attach<test::department>("departments")
|
||||
.and_then([&repo] { return repo.attach<test::employee>("employees"); });
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(repo.size() == 2);
|
||||
REQUIRE(repo.contains("departments"));
|
||||
REQUIRE(repo.contains("employees"));
|
||||
|
||||
auto info = repo.basic_info("departments")->get();
|
||||
REQUIRE(!info.endpoints_empty());
|
||||
REQUIRE(info.endpoints_size() == 1);
|
||||
std::cout << *info.object();
|
||||
info = repo.basic_info("employees")->get();
|
||||
REQUIRE(!info.endpoints_empty());
|
||||
REQUIRE(info.endpoints_size() == 1);
|
||||
std::cout << *info.object();
|
||||
}
|
||||
|
||||
TEST_CASE("Test one to many reverse", "[relation][one-to-many][reverse]") {
|
||||
object::repository repo;
|
||||
|
||||
REQUIRE(repo.empty());
|
||||
|
||||
auto res = repo.attach<test::employee>("employees")
|
||||
.and_then([&repo] { return repo.attach<test::department>("departments"); });
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(repo.size() == 2);
|
||||
REQUIRE(repo.contains("departments"));
|
||||
REQUIRE(repo.contains("employees"));
|
||||
|
||||
auto info = repo.basic_info("departments")->get();
|
||||
REQUIRE(!info.endpoints_empty());
|
||||
REQUIRE(info.endpoints_size() == 1);
|
||||
std::cout << *info.object();
|
||||
info = repo.basic_info("employees")->get();
|
||||
REQUIRE(!info.endpoints_empty());
|
||||
REQUIRE(info.endpoints_size() == 1);
|
||||
std::cout << *info.object();
|
||||
}
|
||||
|
||||
TEST_CASE("Test many to many relation", "[relation][many-to-many]") {
|
||||
object::repository repo;
|
||||
|
||||
REQUIRE(repo.empty());
|
||||
|
||||
auto result = repo.attach<test::recipe>("recipes")
|
||||
.and_then([&repo] { return repo.attach<test::ingredient>("ingredients"); });
|
||||
REQUIRE(result);
|
||||
|
||||
REQUIRE(repo.size() == 3);
|
||||
REQUIRE(repo.contains("ingredients"));
|
||||
REQUIRE(repo.contains("recipes"));
|
||||
REQUIRE(repo.contains("recipe_ingredients"));
|
||||
|
||||
std::cout << *repo.basic_info("ingredients")->get().object();
|
||||
std::cout << *repo.basic_info("recipes")->get().object();
|
||||
std::cout << *repo.basic_info("recipe_ingredients")->get().object();
|
||||
|
||||
|
||||
auto info = repo.basic_info("ingredients")->get();
|
||||
REQUIRE(!info.endpoints_empty());
|
||||
REQUIRE(info.endpoints_size() == 1);
|
||||
std::cout << *info.object();
|
||||
info = repo.basic_info("recipes")->get();
|
||||
REQUIRE(!info.endpoints_empty());
|
||||
REQUIRE(info.endpoints_size() == 1);
|
||||
std::cout << *info.object();
|
||||
info = repo.basic_info("recipe_ingredients")->get();
|
||||
REQUIRE(!info.endpoints_empty());
|
||||
REQUIRE(info.endpoints_size() == 2);
|
||||
std::cout << *info.object();
|
||||
}
|
||||
Reference in New Issue
Block a user