added has_one eager functionality and tests
This commit is contained in:
@@ -7,12 +7,13 @@
|
||||
#include "matador/query/session.hpp"
|
||||
|
||||
#include "models/user.hpp"
|
||||
#include "models/country.hpp"
|
||||
|
||||
using namespace matador::test;
|
||||
using namespace matador::query;
|
||||
using namespace matador::object;
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation", "[session][insert][has_one]") {
|
||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation lazy", "[session][insert][has_one][lazy]") {
|
||||
const auto result = schema.attach<user_identity>("users")
|
||||
.and_then( [this] { return schema.attach<user_session_identity>("user_sessions"); } )
|
||||
.and_then([this] { return schema.create(db); } );
|
||||
@@ -33,4 +34,27 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation", "[s
|
||||
REQUIRE(user_result.is_ok());
|
||||
us = user_result.value()->session;
|
||||
REQUIRE(us->session_token == "session1");
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation eager", "[session][insert][has_one][eager]") {
|
||||
const auto result = schema.attach<country_identity>("countries")
|
||||
.and_then( [this] { return schema.attach<capital_identity>("capitals"); } )
|
||||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
const auto ger = make_object<country_identity>("Germany");
|
||||
auto ger_cap = make_object<capital_identity>("Berlin", ger);
|
||||
|
||||
REQUIRE(ger.is_transient());
|
||||
REQUIRE(ger_cap.is_transient());
|
||||
REQUIRE(ses.insert(ger_cap).is_ok());
|
||||
REQUIRE(ger_cap.is_persistent());
|
||||
REQUIRE(ger.is_persistent());
|
||||
|
||||
const auto ger_result = ses.find<country_identity>(ger->id);
|
||||
REQUIRE(ger_result.is_ok());
|
||||
ger_cap = ger_result.value()->capital;
|
||||
REQUIRE(ger_cap->name == "Berlin");
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
#ifndef MATADOR_COUNTRY_HPP
|
||||
#define MATADOR_COUNTRY_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct capital_pk_generator;
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct country_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
object::object_ptr<capital_pk_generator<PkAttribute>> capital;
|
||||
|
||||
country_pk_generator() = default;
|
||||
explicit country_pk_generator(std::string name)
|
||||
: name(std::move(name)) {}
|
||||
country_pk_generator(const unsigned int id, std::string name)
|
||||
: id(id)
|
||||
, name(std::move(name)) {}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "name", name, UniqueVarChar255);
|
||||
field::has_one(op, "capital", capital, "country_id", CascadeAllFetchEager);
|
||||
}
|
||||
};
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct capital_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
object::object_ptr<country_pk_generator<PkAttribute>> country;
|
||||
|
||||
capital_pk_generator() = default;
|
||||
capital_pk_generator(std::string name, object::object_ptr<country_pk_generator<PkAttribute>> country)
|
||||
: name(std::move(name))
|
||||
, country(std::move(country)) {}
|
||||
capital_pk_generator(const unsigned int id, std::string name, object::object_ptr<country_pk_generator<PkAttribute>> country)
|
||||
: id(id)
|
||||
, name(std::move(name))
|
||||
, country(std::move(country)) {}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "name", name, VarChar255);
|
||||
field::belongs_to(op, "country_id", country, CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
using country = country_pk_generator<utils::Manual>;
|
||||
using country_identity = country_pk_generator<utils::Identity>;
|
||||
using country_sequence = country_pk_generator<utils::Sequence>;
|
||||
using country_table = country_pk_generator<utils::Table>;
|
||||
using capital = capital_pk_generator<utils::Manual>;
|
||||
using capital_identity = capital_pk_generator<utils::Identity>;
|
||||
using capital_sequence = capital_pk_generator<utils::Sequence>;
|
||||
using capital_table = capital_pk_generator<utils::Table>;
|
||||
}
|
||||
|
||||
#endif //MATADOR_COUNTRY_HPP
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "../../models/order.hpp"
|
||||
#include "../../models/student.hpp"
|
||||
#include "../../models/user.hpp"
|
||||
#include "../../models/country.hpp"
|
||||
#include "../../models/model_metas.hpp"
|
||||
|
||||
using namespace matador::object;
|
||||
@@ -386,7 +387,7 @@ TEST_CASE("Test eager relationship", "[query][entity][builder]") {
|
||||
// std::cout << ctx << std::endl;
|
||||
}
|
||||
|
||||
TEST_CASE("Test has one relationship", "[query][entity][builder][has_one]") {
|
||||
TEST_CASE("Test has one lazy relationship", "[query][entity][builder][has_one][lazy]") {
|
||||
using namespace matador::test;
|
||||
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
||||
connection db("noop://noop.db");
|
||||
@@ -401,5 +402,27 @@ TEST_CASE("Test has one relationship", "[query][entity][builder][has_one]") {
|
||||
auto q = eqb.build<user>();
|
||||
REQUIRE(q.is_ok());
|
||||
const auto sql = q->str(db);
|
||||
const std::string expected_sql = R"(SELECT "t01"."id", "t01"."name", "t01"."password" FROM "users" "t01" ORDER BY "t01"."id" ASC)";
|
||||
|
||||
REQUIRE(expected_sql == sql);
|
||||
}
|
||||
|
||||
TEST_CASE("Test has one eager relationship", "[query][entity][builder][has_one][eager]") {
|
||||
using namespace matador::test;
|
||||
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
||||
connection db("noop://noop.db");
|
||||
|
||||
schema scm;
|
||||
auto result = scm.attach<country>("countries")
|
||||
.and_then( [&scm] { return scm.attach<capital>("capitals"); } );
|
||||
REQUIRE(result);
|
||||
|
||||
select_query_builder eqb(scm);
|
||||
|
||||
auto q = eqb.build<country>();
|
||||
REQUIRE(q.is_ok());
|
||||
const auto sql = q->str(db);
|
||||
const std::string expected_sql = R"(SELECT "t01"."id", "t01"."name", "t02"."id", "t02"."name", "t02"."country_id" FROM "countries" "t01" LEFT JOIN "capitals" "t02" ON "t01"."id" = "t02"."country_id" ORDER BY "t01"."id" ASC)";
|
||||
|
||||
REQUIRE(expected_sql == sql);
|
||||
}
|
||||
Reference in New Issue
Block a user