added session test with has one relation
This commit is contained in:
parent
fb57545cce
commit
4199c5029c
|
|
@ -5,6 +5,7 @@
|
||||||
#include "matador/sql/session.hpp"
|
#include "matador/sql/session.hpp"
|
||||||
|
|
||||||
#include "models/airplane.hpp"
|
#include "models/airplane.hpp"
|
||||||
|
#include "models/flight.hpp"
|
||||||
|
|
||||||
class SessionFixture
|
class SessionFixture
|
||||||
{
|
{
|
||||||
|
|
@ -16,9 +17,8 @@ public:
|
||||||
|
|
||||||
~SessionFixture()
|
~SessionFixture()
|
||||||
{
|
{
|
||||||
drop_table_if_exists("flight");
|
drop_table_if_exists("flights");
|
||||||
drop_table_if_exists("airplane");
|
drop_table_if_exists("airplanes");
|
||||||
drop_table_if_exists("person");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
@ -38,16 +38,19 @@ using namespace matador;
|
||||||
|
|
||||||
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
|
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
|
||||||
using namespace matador;
|
using namespace matador;
|
||||||
ses.attach<test::airplane>("airplane");
|
ses.attach<test::airplane>("airplanes");
|
||||||
|
ses.attach<test::flight>("flights");
|
||||||
ses.create_schema();
|
ses.create_schema();
|
||||||
ses.insert<test::airplane>(1, "Boeing", "A380");
|
auto plane = ses.insert<test::airplane>(1, "Boeing", "A380");
|
||||||
|
auto f = ses.insert<test::flight>(2, plane, "sully");
|
||||||
|
|
||||||
REQUIRE(true);
|
auto result = ses.find<test::flight>(2);
|
||||||
|
REQUIRE(result.is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session][find]") {
|
TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session][find]") {
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
ses.attach<airplane>("airplane");
|
ses.attach<airplane>("airplanes");
|
||||||
ses.create_schema();
|
ses.create_schema();
|
||||||
auto a380 = ses.insert<airplane>(1, "Boeing", "A380");
|
auto a380 = ses.insert<airplane>(1, "Boeing", "A380");
|
||||||
|
|
||||||
|
|
@ -64,7 +67,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session
|
||||||
|
|
||||||
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][find]") {
|
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][find]") {
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
ses.attach<airplane>("airplane");
|
ses.attach<airplane>("airplanes");
|
||||||
ses.create_schema();
|
ses.create_schema();
|
||||||
|
|
||||||
std::vector<std::unique_ptr<airplane>> planes;
|
std::vector<std::unique_ptr<airplane>> planes;
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,8 @@ public:
|
||||||
template<class ContainerType>
|
template<class ContainerType>
|
||||||
void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/)
|
void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/)
|
||||||
{
|
{
|
||||||
join_columns_.join_column = inverse_join_column;
|
join_columns_.join_column = join_column;
|
||||||
join_columns_.inverse_join_column = join_column;
|
join_columns_.inverse_join_column = inverse_join_column;
|
||||||
}
|
}
|
||||||
template<class ContainerType>
|
template<class ContainerType>
|
||||||
void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const utils::foreign_attributes &/*attr*/) {}
|
void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const utils::foreign_attributes &/*attr*/) {}
|
||||||
|
|
@ -209,7 +209,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
append_join({table_info_stack_.top().name, table_info_stack_.top().prototype.primary_key()->name()}, {id, join_column});
|
append_join({table_info_stack_.top().name, table_info_stack_.top().prototype.primary_key()->name()}, {id, join_column});
|
||||||
append_join({info->name, pk->name()}, {id, inverse_join_column});
|
append_join({id, inverse_join_column}, {info->name, pk->name()});
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ContainerType>
|
template<class ContainerType>
|
||||||
|
|
@ -234,8 +234,8 @@ public:
|
||||||
|
|
||||||
const auto join_columns = join_column_collector_.collect<typename ContainerType::value_type::value_type>();
|
const auto join_columns = join_column_collector_.collect<typename ContainerType::value_type::value_type>();
|
||||||
|
|
||||||
append_join({table_info_stack_.top().name, table_info_stack_.top().prototype.primary_key()->name()}, {id, join_columns.join_column});
|
append_join({table_info_stack_.top().name, table_info_stack_.top().prototype.primary_key()->name()}, {id, join_columns.inverse_join_column});
|
||||||
append_join({info->name, pk->name()}, {id, join_columns.inverse_join_column});
|
append_join({id, join_columns.join_column}, {info->name, pk->name()});
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -150,7 +150,7 @@ entity<Type> session::insert(Type *obj)
|
||||||
}
|
}
|
||||||
c->query(*schema_)
|
c->query(*schema_)
|
||||||
.insert()
|
.insert()
|
||||||
.into(info->name, column_generator::generate<Type>(*schema_))
|
.into(info->name, column_generator::generate<Type>(*schema_, true))
|
||||||
.values(*obj)
|
.values(*obj)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ void entity_query_builder::push(const std::string &column_name)
|
||||||
void entity_query_builder::append_join(const column &left, const column &right)
|
void entity_query_builder::append_join(const column &left, const column &right)
|
||||||
{
|
{
|
||||||
entity_query_data_.joins.push_back({
|
entity_query_data_.joins.push_back({
|
||||||
{ left.table },
|
{ right.table },
|
||||||
make_condition(left == right)
|
make_condition(left == right)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,59 @@
|
||||||
#include <matador/sql/connection.hpp>
|
#include <matador/sql/connection.hpp>
|
||||||
#include <matador/sql/entity_query_builder.hpp>
|
#include <matador/sql/entity_query_builder.hpp>
|
||||||
|
|
||||||
|
#include "models/airplane.hpp"
|
||||||
#include "models/author.hpp"
|
#include "models/author.hpp"
|
||||||
#include "models/book.hpp"
|
#include "models/book.hpp"
|
||||||
|
#include "models/flight.hpp"
|
||||||
#include "models/recipe.hpp"
|
#include "models/recipe.hpp"
|
||||||
#include "models/order.hpp"
|
#include "models/order.hpp"
|
||||||
#include "models/student.hpp"
|
#include "models/student.hpp"
|
||||||
|
|
||||||
using namespace matador::sql;
|
using namespace matador::sql;
|
||||||
|
|
||||||
|
TEST_CASE("Create sql query data for entity with eager has one", "[query][entity][builder]") {
|
||||||
|
using namespace matador::test;
|
||||||
|
connection db("noop://noop.db");
|
||||||
|
schema scm("noop");
|
||||||
|
scm.attach<airplane>("airplanes");
|
||||||
|
scm.attach<flight>("flights");
|
||||||
|
|
||||||
|
entity_query_builder eqb(scm);
|
||||||
|
|
||||||
|
auto data = eqb.build<flight>(17);
|
||||||
|
|
||||||
|
REQUIRE(data.is_ok());
|
||||||
|
REQUIRE(data->root_table_name == "flights");
|
||||||
|
REQUIRE(data->joins.size() == 1);
|
||||||
|
const std::vector<column> expected_columns {
|
||||||
|
{ "flights", "id", "c01" },
|
||||||
|
{ "airplanes", "id", "c02" },
|
||||||
|
{ "airplanes", "brand", "c03" },
|
||||||
|
{ "airplanes", "model", "c04" },
|
||||||
|
{ "flights", "pilot_name", "c05" },
|
||||||
|
};
|
||||||
|
REQUIRE(data->columns.size() == expected_columns.size());
|
||||||
|
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
||||||
|
REQUIRE(expected_columns[i].equals(data->columns[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
||||||
|
{ "airplanes", R"("flights"."airplane_id" = "airplanes"."id")"}
|
||||||
|
};
|
||||||
|
|
||||||
|
query_context qc;
|
||||||
|
size_t index{0};
|
||||||
|
for (const auto &jd : data->joins) {
|
||||||
|
REQUIRE(jd.join_table.name == expected_join_data[index].first);
|
||||||
|
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
|
||||||
|
++index;
|
||||||
|
}
|
||||||
|
|
||||||
|
REQUIRE(data->where_clause);
|
||||||
|
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
||||||
|
REQUIRE(cond == R"("flights"."id" = 17)");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("Create sql query data for entity with eager belongs to", "[query][entity][builder]") {
|
TEST_CASE("Create sql query data for entity with eager belongs to", "[query][entity][builder]") {
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
connection db("noop://noop.db");
|
connection db("noop://noop.db");
|
||||||
|
|
@ -42,7 +87,7 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
||||||
{ "books", R"("books"."author_id" = "authors"."id")"}
|
{ "authors", R"("books"."author_id" = "authors"."id")"}
|
||||||
};
|
};
|
||||||
|
|
||||||
query_context qc;
|
query_context qc;
|
||||||
|
|
@ -108,7 +153,7 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
||||||
{ "orders", R"("orders"."order_id" = "order_details"."order_id")"}
|
{ "order_details", R"("orders"."order_id" = "order_details"."order_id")"}
|
||||||
};
|
};
|
||||||
|
|
||||||
query_context qc;
|
query_context qc;
|
||||||
|
|
@ -151,8 +196,8 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
||||||
{ "ingredients", R"("ingredients"."id" = "recipe_ingredients"."ingredient_id")"},
|
{ "recipe_ingredients", R"("ingredients"."id" = "recipe_ingredients"."ingredient_id")"},
|
||||||
{ "recipes", R"("recipes"."id" = "recipe_ingredients"."recipe_id")"}
|
{ "recipes", R"("recipe_ingredients"."recipe_id" = "recipes"."id")"}
|
||||||
};
|
};
|
||||||
|
|
||||||
query_context qc;
|
query_context qc;
|
||||||
|
|
@ -195,8 +240,8 @@ TEST_CASE("Create sql query data for entity with eager many to many (inverse par
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
||||||
{ "courses", R"("courses"."id" = "student_courses"."course_id")"},
|
{ "student_courses", R"("courses"."id" = "student_courses"."course_id")"},
|
||||||
{ "students", R"("students"."id" = "student_courses"."student_id")"}
|
{ "students", R"("student_courses"."student_id" = "students"."id")"}
|
||||||
};
|
};
|
||||||
|
|
||||||
query_context qc;
|
query_context qc;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue