added belongs to and has one tests
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
#include "catch2/catch_test_macros.hpp"
|
||||
|
||||
#include "SessionFixture.hpp"
|
||||
|
||||
#include "connection.hpp"
|
||||
|
||||
#include "matador/query/session.hpp"
|
||||
|
||||
#include "models/author.hpp"
|
||||
|
||||
using namespace matador::test;
|
||||
using namespace matador::query;
|
||||
using namespace matador::object;
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with belongs to relation with identity", "[session][insert][belongs_to][identity]") {
|
||||
const auto result = schema.attach<book_identity>("books")
|
||||
.and_then( [this] { return schema.attach<author_identity>("authors"); } )
|
||||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
auto s_king = make_object<author_identity>("Steven King");
|
||||
|
||||
const auto carrie = make_object<book_identity>("Carrie", s_king, 1974);
|
||||
|
||||
REQUIRE(carrie.is_transient());
|
||||
REQUIRE(ses.insert(carrie).is_ok());
|
||||
REQUIRE(carrie.is_persistent());
|
||||
|
||||
auto found_author = ses.find<author_identity>(s_king->id);
|
||||
REQUIRE(found_author);
|
||||
REQUIRE(found_author.value()->name == "Steven King");
|
||||
REQUIRE(found_author.value()->books.size() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with belongs to relation with table sequence", "[session][insert][belongs_to][table_sequence]") {
|
||||
const auto result = schema.attach<book_table>("books")
|
||||
.and_then( [this] { return schema.attach<author_table>("authors"); } )
|
||||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
auto s_king = make_object<author_table>("Steven King");
|
||||
|
||||
const auto carrie = make_object<book_table>("Carrie", s_king, 1974);
|
||||
|
||||
REQUIRE(carrie.is_transient());
|
||||
REQUIRE(ses.insert(carrie).is_ok());
|
||||
REQUIRE(carrie.is_persistent());
|
||||
|
||||
auto found_author = ses.find<author_table>(s_king->id);
|
||||
REQUIRE(found_author);
|
||||
REQUIRE(found_author.value()->name == "Steven King");
|
||||
REQUIRE(found_author.value()->books.size() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with belongs to relation with sequence", "[session][insert][belongs_to][sequence]") {
|
||||
const auto result = schema.attach<book_sequence>("books")
|
||||
.and_then( [this] { return schema.attach<author_sequence>("authors"); } )
|
||||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
auto s_king = make_object<author_sequence>("Steven King");
|
||||
|
||||
const auto carrie = make_object<book_sequence>("Carrie", s_king, 1974);
|
||||
|
||||
REQUIRE(carrie.is_transient());
|
||||
REQUIRE(ses.insert(carrie).is_ok());
|
||||
REQUIRE(carrie.is_persistent());
|
||||
|
||||
auto found_author = ses.find<author_sequence>(s_king->id);
|
||||
REQUIRE(found_author);
|
||||
REQUIRE(found_author.value()->name == "Steven King");
|
||||
REQUIRE(found_author.value()->books.size() == 1);
|
||||
}
|
||||
|
||||
@@ -15,56 +15,6 @@ using namespace matador::object;
|
||||
using namespace matador::test;
|
||||
|
||||
namespace matador::test {
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct book_pk_generator;
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct author_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
collection<object_ptr<book_pk_generator<PkAttribute>>> books;
|
||||
|
||||
author_pk_generator() = default;
|
||||
explicit author_pk_generator(std::string name)
|
||||
: name(std::move(name)) {}
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "name", name, VarChar63);
|
||||
field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct book_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string title;
|
||||
object_ptr<author_pk_generator<PkAttribute>> book_author;
|
||||
unsigned short published_in{};
|
||||
|
||||
book_pk_generator() = default;
|
||||
book_pk_generator(std::string title, object_ptr<author_pk_generator<PkAttribute>> author, const unsigned short published_in)
|
||||
: title(std::move(title)), book_author(std::move(author)), published_in(published_in) {}
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "title", title, VarChar511);
|
||||
field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager);
|
||||
field::attribute(op, "published_in", published_in);
|
||||
}
|
||||
};
|
||||
|
||||
using author_identity = author_pk_generator<utils::Identity>;
|
||||
using author_sequence = author_pk_generator<utils::Sequence>;
|
||||
using author_table = author_pk_generator<utils::Table>;
|
||||
using book_identity = book_pk_generator<utils::Identity>;
|
||||
using book_sequence = book_pk_generator<utils::Sequence>;
|
||||
using book_table = book_pk_generator<utils::Table>;
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct colorlist_pk_generator {
|
||||
unsigned int id{};
|
||||
@@ -88,7 +38,6 @@ struct colorlist_pk_generator {
|
||||
using colorlist_identity = colorlist_pk_generator<utils::Identity>;
|
||||
using colorlist_sequence = colorlist_pk_generator<utils::Sequence>;
|
||||
using colorlist_table = colorlist_pk_generator<utils::Table>;
|
||||
|
||||
}
|
||||
|
||||
template<typename AuthorType, typename BookType>
|
||||
|
||||
@@ -30,6 +30,10 @@ struct ingredient_pk_generator {
|
||||
: name(std::move(name)) {
|
||||
}
|
||||
|
||||
ingredient_pk_generator(std::string name, std::vector<object_ptr<recipe_pk_generator<PkAttribute>>> recps)
|
||||
: name(std::move(name))
|
||||
, recipes(std::move(recps)){}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
@@ -51,8 +55,7 @@ struct recipe_pk_generator {
|
||||
}
|
||||
recipe_pk_generator(std::string name, std::vector<object_ptr<ingredient_pk_generator<PkAttribute>>> ings)
|
||||
: name(std::move(name))
|
||||
, ingredients(std::move(ings)){
|
||||
}
|
||||
, ingredients(std::move(ings)){}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
@@ -218,3 +221,43 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many to many relat
|
||||
REQUIRE(ing_result.is_ok());
|
||||
REQUIRE(ing_result.value()->recipes.size() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many to many relation sequence reverse", "[session][insert][sequence][has_many_to_many][reverse]") {
|
||||
auto result = schema.attach<recipe_sequence>("recipes")
|
||||
.and_then( [this] { return schema.attach<ingredient_sequence>("ingredients"); } )
|
||||
.and_then([this] { return schema.create(db); } );
|
||||
|
||||
query::session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
std::vector recipes {
|
||||
make_object<recipe_sequence>("Apple Pie"),
|
||||
make_object<recipe_sequence>("Strawberry Cake"),
|
||||
make_object<recipe_sequence>("Pineapple Pie")
|
||||
};
|
||||
|
||||
std::vector ingredients {
|
||||
make_object<ingredient_sequence>("Apple", std::vector{recipes[0], recipes[2]}),
|
||||
make_object<ingredient_sequence>("Strawberry", std::vector{recipes[2]}),
|
||||
make_object<ingredient_sequence>("Pineapple", std::vector{recipes[2]}),
|
||||
make_object<ingredient_sequence>("Sugar", std::vector{recipes[0]}),
|
||||
make_object<ingredient_sequence>("Flour", std::vector{recipes[0]}),
|
||||
make_object<ingredient_sequence>("Butter", std::vector{recipes[1]}),
|
||||
make_object<ingredient_sequence>("Beans", std::vector{recipes[1]})
|
||||
};
|
||||
|
||||
for (auto &i: ingredients) {
|
||||
REQUIRE(i.is_transient());
|
||||
auto res = ses.insert(i);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(res->is_persistent());
|
||||
}
|
||||
|
||||
auto recipe_result = ses.find<ingredient_sequence>(1);
|
||||
REQUIRE(recipe_result.is_ok());
|
||||
REQUIRE(recipe_result->is_persistent());
|
||||
REQUIRE(recipe_result.value()->recipes.size() == 2);
|
||||
|
||||
const auto ing_result = ses.find<recipe_sequence>(recipes[0]->id);
|
||||
REQUIRE(ing_result.is_ok());
|
||||
REQUIRE(ing_result.value()->ingredients.size() == 3);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "catch2/catch_test_macros.hpp"
|
||||
|
||||
#include "SessionFixture.hpp"
|
||||
|
||||
#include "connection.hpp"
|
||||
|
||||
#include "matador/query/session.hpp"
|
||||
|
||||
#include "models/user.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]") {
|
||||
session sess{conn};
|
||||
user_pk_generator u;
|
||||
u.name = "John Doe";
|
||||
sess.persist(u);
|
||||
|
||||
REQUIRE(u.id > 0);
|
||||
}
|
||||
@@ -41,6 +41,57 @@ struct author {
|
||||
field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct book_pk_generator;
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct author_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
object::collection<object::object_ptr<book_pk_generator<PkAttribute>>> books;
|
||||
|
||||
author_pk_generator() = default;
|
||||
explicit author_pk_generator(std::string name)
|
||||
: name(std::move(name)) {}
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "name", name, VarChar63);
|
||||
field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct book_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string title;
|
||||
object::object_ptr<author_pk_generator<PkAttribute>> book_author;
|
||||
unsigned short published_in{};
|
||||
|
||||
book_pk_generator() = default;
|
||||
book_pk_generator(std::string title, object::object_ptr<author_pk_generator<PkAttribute>> author, const unsigned short published_in)
|
||||
: title(std::move(title)), book_author(std::move(author)), published_in(published_in) {}
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "title", title, VarChar511);
|
||||
field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager);
|
||||
field::attribute(op, "published_in", published_in);
|
||||
}
|
||||
};
|
||||
|
||||
using author_identity = author_pk_generator<utils::Identity>;
|
||||
using author_sequence = author_pk_generator<utils::Sequence>;
|
||||
using author_table = author_pk_generator<utils::Table>;
|
||||
using book_identity = book_pk_generator<utils::Identity>;
|
||||
using book_sequence = book_pk_generator<utils::Sequence>;
|
||||
using book_table = book_pk_generator<utils::Table>;
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_AUTHOR_HPP
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef MATADOR_USER_HPP
|
||||
#define MATADOR_USER_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 user_session_pk_generator;
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct user_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
object::object_ptr<user_session_pk_generator<PkAttribute>> session;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, UniqueVarChar255);
|
||||
field::has_one(op, "session", session, CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct user_session_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string session_token;
|
||||
object::object_ptr<user_pk_generator<PkAttribute>> user_;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "session_token", session_token, VarChar255);
|
||||
field::belongs_to(op, "user_id", user_, CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
using user = user_pk_generator<utils::Manual>;
|
||||
using user_identity = user_pk_generator<utils::Identity>;
|
||||
using user_sequence = user_pk_generator<utils::Sequence>;
|
||||
using user_table = user_pk_generator<utils::Table>;
|
||||
using user_session = user_session_pk_generator<utils::Manual>;
|
||||
using user_session_identity = user_session_pk_generator<utils::Identity>;
|
||||
using user_session_sequence = user_session_pk_generator<utils::Sequence>;
|
||||
using user_session_table = user_session_pk_generator<utils::Table>;
|
||||
}
|
||||
|
||||
#endif //MATADOR_USER_HPP
|
||||
Reference in New Issue
Block a user