added SchemaTest, fixed bug in class shipment, added contains methods to class schema and enabled some tests
This commit is contained in:
+14
-13
@@ -27,7 +27,7 @@ META_TABLE(recipe_ingredients, RECIPE_INGREDIENT, recipe_id, ingredient_id);
|
||||
META_TABLE(airplanes, AIRPLANE, id, brand, model);
|
||||
META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name);
|
||||
META_TABLE(shipments, SHIPMENT, id, tracking_number)
|
||||
META_TABLE(packages, PACKAGE, id, weight, shipment)
|
||||
META_TABLE(packages, PACKAGE, id, weight, shipment_id)
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") {
|
||||
auto result = repo.attach<airplane>("airplane")
|
||||
@@ -593,10 +593,8 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
|
||||
|
||||
select_result = query::select({r.id, r.name, ri.ingredient_id, i.name})
|
||||
.from(r)
|
||||
.join_left(ri)
|
||||
.on(r.id == ri.recipe_id)
|
||||
.join_left(i)
|
||||
.on(ri.ingredient_id == i.id)
|
||||
.join_left(ri).on(r.id == ri.recipe_id)
|
||||
.join_left(i).on(ri.ingredient_id == i.id)
|
||||
.where(r.id == 8)
|
||||
.fetch_all(db);
|
||||
REQUIRE(result.is_ok());
|
||||
@@ -621,8 +619,6 @@ void print(std::ostream& out, const record& row) {
|
||||
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", "[query][has_many][eager]") {
|
||||
auto result = repo.attach<package>("packages")
|
||||
.and_then( [this] { return repo.attach<shipment>("shipments"); } );
|
||||
// auto result = repo.attach<shipment>("shipments")
|
||||
// .and_then( [this] { return repo.attach<package>("packages"); } );
|
||||
REQUIRE(result.is_ok());
|
||||
tables_to_drop.emplace("shipments");
|
||||
tables_to_drop.emplace("packages");
|
||||
@@ -675,10 +671,10 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
|
||||
REQUIRE(count.is_ok());
|
||||
REQUIRE(*count == 5);
|
||||
|
||||
auto pkgs = query::select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment, SHIPMENT.tracking_number})
|
||||
auto pkgs = query::select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id, SHIPMENT.tracking_number})
|
||||
.from(PACKAGE)
|
||||
.join_left(SHIPMENT)
|
||||
.on( PACKAGE.shipment == SHIPMENT.id )
|
||||
.on( PACKAGE.shipment_id == SHIPMENT.id )
|
||||
.where( PACKAGE.weight > 20.0 && SHIPMENT.tracking_number == "0815" )
|
||||
.group_by({PACKAGE.id, SHIPMENT.tracking_number})
|
||||
.order_by(PACKAGE.weight).asc()
|
||||
@@ -694,23 +690,28 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
|
||||
auto shipment_records = query::select({SHIPMENT.tracking_number, SHIPMENT.id, PACKAGE.weight, PACKAGE.weight})
|
||||
.from(SHIPMENT)
|
||||
.join_left(PACKAGE)
|
||||
.on( SHIPMENT.id == PACKAGE.shipment )
|
||||
.on( SHIPMENT.id == PACKAGE.shipment_id )
|
||||
.fetch_all(db);
|
||||
|
||||
REQUIRE(shipment_records.is_ok());
|
||||
|
||||
printer.print(*shipment_records);
|
||||
|
||||
auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment})
|
||||
auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id})
|
||||
.from(SHIPMENT)
|
||||
.join_left(PACKAGE)
|
||||
.on( SHIPMENT.id == PACKAGE.shipment )
|
||||
.on( SHIPMENT.id == PACKAGE.shipment_id )
|
||||
.order_by(SHIPMENT.id).asc()
|
||||
.fetch_all<shipment>(db);
|
||||
|
||||
REQUIRE(shipment_result.is_ok());
|
||||
|
||||
size_t index{0};
|
||||
std::vector<size_t> packages_sizes{2, 3};
|
||||
std::cout << "\n";
|
||||
for (const auto &s : *shipment_result) {
|
||||
std::cout << s.id << " " << s.tracking_number << " packages: " << s.packages.size() << std::endl;
|
||||
REQUIRE(s.id == shipments.at(index)->id);
|
||||
REQUIRE(s.tracking_number == shipments.at(index)->tracking_number);
|
||||
REQUIRE(s.packages.size() == packages_sizes.at(index++));
|
||||
}
|
||||
}
|
||||
@@ -48,15 +48,15 @@ TEST_CASE("Test add type to prototype tree", "[schema_node][add]") {
|
||||
|
||||
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);
|
||||
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]") {
|
||||
|
||||
@@ -35,7 +35,7 @@ struct package {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "weight", weight);
|
||||
field::belongs_to(op, "shipment", delivery, utils::CascadeAllFetchLazy);
|
||||
field::belongs_to(op, "shipment_id", delivery, utils::CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ add_executable(OrmTests
|
||||
utils/auto_reset_event.cpp
|
||||
utils/auto_reset_event.hpp
|
||||
query/GeneratorTests.cpp
|
||||
query/SchemaTest.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(OrmTests matador-orm matador-core Catch2::Catch2WithMain)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/query/schema.hpp"
|
||||
|
||||
#include "../../models/shipment.hpp"
|
||||
|
||||
using namespace matador::query;
|
||||
using namespace matador::test;
|
||||
|
||||
TEST_CASE("Test schema with belongs-to to has-many relation" "[schema][relation][belongs_to]") {
|
||||
schema repo;
|
||||
auto result = repo.attach<package>("packages")
|
||||
.and_then( [&repo] { return repo.attach<shipment>("shipments"); } );
|
||||
REQUIRE(result.is_ok());
|
||||
REQUIRE(repo.size() == 2);
|
||||
REQUIRE(repo.contains<shipment>());
|
||||
REQUIRE(repo.contains<package>());
|
||||
|
||||
auto it = repo.find<shipment>();
|
||||
REQUIRE(it != repo.end());
|
||||
REQUIRE(it->second.name() == "shipments");
|
||||
it = repo.find<package>();
|
||||
REQUIRE(it != repo.end());
|
||||
REQUIRE(it->second.name() == "packages");
|
||||
}
|
||||
|
||||
TEST_CASE("Test schema with has-many to belongs-to relation" "[schema][relation][has_many]") {
|
||||
schema repo;
|
||||
auto result = repo.attach<shipment>("shipments")
|
||||
.and_then( [&repo] { return repo.attach<package>("packages"); } );
|
||||
REQUIRE(result.is_ok());
|
||||
REQUIRE(repo.size() == 2);
|
||||
REQUIRE(repo.contains<shipment>());
|
||||
REQUIRE(repo.contains<package>());
|
||||
|
||||
auto it = repo.find<shipment>();
|
||||
REQUIRE(it != repo.end());
|
||||
REQUIRE(it->second.name() == "shipments");
|
||||
it = repo.find<package>();
|
||||
REQUIRE(it != repo.end());
|
||||
REQUIRE(it->second.name() == "packages");
|
||||
}
|
||||
Reference in New Issue
Block a user