insert has one progress
This commit is contained in:
@@ -13,10 +13,21 @@ using namespace matador::query;
|
||||
using namespace matador::object;
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation", "[session][insert][has_one]") {
|
||||
session sess{conn};
|
||||
user_pk_generator u;
|
||||
u.name = "John Doe";
|
||||
sess.persist(u);
|
||||
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); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
REQUIRE(u.id > 0);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
const auto u = make_object<user_identity>("user1", "password");
|
||||
const auto us = make_object<user_session_identity>("session1", u);
|
||||
|
||||
REQUIRE(u.is_transient());
|
||||
REQUIRE(us.is_transient());
|
||||
REQUIRE(ses.insert(us).is_ok());
|
||||
REQUIRE(us.is_persistent());
|
||||
REQUIRE(u.is_persistent());
|
||||
|
||||
REQUIRE(u->session->session_token == "session1");
|
||||
}
|
||||
@@ -17,5 +17,7 @@ META_TABLE(authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_bi
|
||||
META_TABLE(books, BOOK, id, title, author_id, published_in)
|
||||
META_TABLE(orders, ORDER, order_id, order_date, required_date, shipped_date, ship_via, freight, ship_name, ship_address, ship_city, ship_region, ship_postal_code, ship_country)
|
||||
META_TABLE(courses, COURSE, id, title)
|
||||
META_TABLE(users, USER, id, name, password, session_id)
|
||||
META_TABLE(user_sessions, USER_SESSION, id, session_token, user_id)
|
||||
|
||||
#endif //MATADOR_MODEL_METAS_HPP
|
||||
+16
-2
@@ -15,14 +15,22 @@ template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct user_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
std::string password;
|
||||
object::object_ptr<user_session_pk_generator<PkAttribute>> session;
|
||||
|
||||
user_pk_generator() = default;
|
||||
user_pk_generator(std::string name, std::string password)
|
||||
: name(std::move(name)), password(std::move(password)) {}
|
||||
user_pk_generator(const unsigned int id, std::string name, std::string password)
|
||||
: id(id), name(std::move(name)), password(std::move(password)) {}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "name", name, UniqueVarChar255);
|
||||
field::attribute(op, "password", password, UniqueVarChar255);
|
||||
field::has_one(op, "session", session, CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
@@ -33,11 +41,17 @@ struct user_session_pk_generator {
|
||||
std::string session_token;
|
||||
object::object_ptr<user_pk_generator<PkAttribute>> user_;
|
||||
|
||||
user_session_pk_generator() = default;
|
||||
user_session_pk_generator(std::string session_token, object::object_ptr<user_pk_generator<PkAttribute>> user)
|
||||
: session_token(std::move(session_token)), user_(std::move(user)) {}
|
||||
user_session_pk_generator(const unsigned int id, std::string session_token, object::object_ptr<user_pk_generator<PkAttribute>> user)
|
||||
: id(id), session_token(std::move(session_token)), user_(std::move(user)) {}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "session_token", session_token, VarChar255);
|
||||
field::belongs_to(op, "user_id", user_, CascadeAllFetchLazy);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "../../models/recipe.hpp"
|
||||
#include "../../models/order.hpp"
|
||||
#include "../../models/student.hpp"
|
||||
#include "../../models/user.hpp"
|
||||
#include "../../models/model_metas.hpp"
|
||||
|
||||
using namespace matador::object;
|
||||
@@ -384,4 +385,22 @@ TEST_CASE("Test eager relationship", "[query][entity][builder]") {
|
||||
// .str(db);
|
||||
//
|
||||
// std::cout << ctx << std::endl;
|
||||
}
|
||||
|
||||
TEST_CASE("Test has one relationship", "[query][entity][builder][has_one]") {
|
||||
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<user>("users")
|
||||
.and_then( [&scm] { return scm.attach<user_session>("user_sessions"); } );
|
||||
REQUIRE(result);
|
||||
|
||||
select_query_builder eqb(scm);
|
||||
|
||||
auto q = eqb.build<user>();
|
||||
REQUIRE(q.is_ok());
|
||||
const auto sql = q->str(db);
|
||||
|
||||
}
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
#include "utils/RecordingObserver.hpp"
|
||||
|
||||
#include <thread>
|
||||
|
||||
using namespace matador::test;
|
||||
using namespace matador::sql;
|
||||
using namespace matador::utils;
|
||||
|
||||
Reference in New Issue
Block a user