added SchemaTest, fixed bug in class shipment, added contains methods to class schema and enabled some tests
This commit is contained in:
parent
930b9d1aa4
commit
ac53526a27
|
|
@ -102,12 +102,25 @@ public:
|
||||||
const_iterator begin() const;
|
const_iterator begin() const;
|
||||||
const_iterator end() const;
|
const_iterator end() const;
|
||||||
|
|
||||||
iterator find(const std::type_index& ti) { return schema_nodes_.find(ti); }
|
[[nodiscard]] size_t size() const;
|
||||||
const_iterator find(const std::type_index& ti) const { return schema_nodes_.find(ti); }
|
[[nodiscard]] bool empty() const;
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
iterator find() { return find(typeid(Type)); }
|
||||||
|
iterator find(const std::type_index& ti);
|
||||||
iterator find(const std::string& name);
|
iterator find(const std::string& name);
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
const_iterator find() const { return find(typeid(Type)); }
|
||||||
|
const_iterator find(const std::type_index& ti) const;
|
||||||
const_iterator find(const std::string& name) const;
|
const_iterator find(const std::string& name) const;
|
||||||
|
|
||||||
|
[[nodiscard]] bool contains(const std::type_index &index) const;
|
||||||
|
template < typename Type >
|
||||||
|
[[nodiscard]] bool contains() const {
|
||||||
|
return contains(std::type_index(typeid(Type)));
|
||||||
|
}
|
||||||
|
|
||||||
const object::repository &repo() const { return repo_; }
|
const object::repository &repo() const { return repo_; }
|
||||||
object::repository &repo() { return repo_; }
|
object::repository &repo() { return repo_; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -200,6 +200,22 @@ schema::const_iterator schema::end() const {
|
||||||
return schema_nodes_.end();
|
return schema_nodes_.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t schema::size() const {
|
||||||
|
return schema_nodes_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool schema::empty() const {
|
||||||
|
return schema_nodes_.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
schema::iterator schema::find(const std::type_index &ti) {
|
||||||
|
return schema_nodes_.find(ti);
|
||||||
|
}
|
||||||
|
|
||||||
|
schema::const_iterator schema::find(const std::type_index &ti) const {
|
||||||
|
return schema_nodes_.find(ti);
|
||||||
|
}
|
||||||
|
|
||||||
schema::iterator schema::find(const std::string &name) {
|
schema::iterator schema::find(const std::string &name) {
|
||||||
const auto result = repo_.basic_info(name);
|
const auto result = repo_.basic_info(name);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
|
|
@ -218,6 +234,10 @@ schema::const_iterator schema::find(const std::string &name) const {
|
||||||
return schema_nodes_.find(result->get().type_index());
|
return schema_nodes_.find(result->get().type_index());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool schema::contains(const std::type_index &index) const {
|
||||||
|
return schema_nodes_.count(index) == 1;
|
||||||
|
}
|
||||||
|
|
||||||
void schema::dump(std::ostream &os) const {
|
void schema::dump(std::ostream &os) const {
|
||||||
repo_.dump(os);
|
repo_.dump(os);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ META_TABLE(recipe_ingredients, RECIPE_INGREDIENT, recipe_id, ingredient_id);
|
||||||
META_TABLE(airplanes, AIRPLANE, id, brand, model);
|
META_TABLE(airplanes, AIRPLANE, id, brand, model);
|
||||||
META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name);
|
META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name);
|
||||||
META_TABLE(shipments, SHIPMENT, id, tracking_number)
|
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]") {
|
TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") {
|
||||||
auto result = repo.attach<airplane>("airplane")
|
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})
|
select_result = query::select({r.id, r.name, ri.ingredient_id, i.name})
|
||||||
.from(r)
|
.from(r)
|
||||||
.join_left(ri)
|
.join_left(ri).on(r.id == ri.recipe_id)
|
||||||
.on(r.id == ri.recipe_id)
|
.join_left(i).on(ri.ingredient_id == i.id)
|
||||||
.join_left(i)
|
|
||||||
.on(ri.ingredient_id == i.id)
|
|
||||||
.where(r.id == 8)
|
.where(r.id == 8)
|
||||||
.fetch_all(db);
|
.fetch_all(db);
|
||||||
REQUIRE(result.is_ok());
|
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]") {
|
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", "[query][has_many][eager]") {
|
||||||
auto result = repo.attach<package>("packages")
|
auto result = repo.attach<package>("packages")
|
||||||
.and_then( [this] { return repo.attach<shipment>("shipments"); } );
|
.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());
|
REQUIRE(result.is_ok());
|
||||||
tables_to_drop.emplace("shipments");
|
tables_to_drop.emplace("shipments");
|
||||||
tables_to_drop.emplace("packages");
|
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.is_ok());
|
||||||
REQUIRE(*count == 5);
|
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)
|
.from(PACKAGE)
|
||||||
.join_left(SHIPMENT)
|
.join_left(SHIPMENT)
|
||||||
.on( PACKAGE.shipment == SHIPMENT.id )
|
.on( PACKAGE.shipment_id == SHIPMENT.id )
|
||||||
.where( PACKAGE.weight > 20.0 && SHIPMENT.tracking_number == "0815" )
|
.where( PACKAGE.weight > 20.0 && SHIPMENT.tracking_number == "0815" )
|
||||||
.group_by({PACKAGE.id, SHIPMENT.tracking_number})
|
.group_by({PACKAGE.id, SHIPMENT.tracking_number})
|
||||||
.order_by(PACKAGE.weight).asc()
|
.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})
|
auto shipment_records = query::select({SHIPMENT.tracking_number, SHIPMENT.id, PACKAGE.weight, PACKAGE.weight})
|
||||||
.from(SHIPMENT)
|
.from(SHIPMENT)
|
||||||
.join_left(PACKAGE)
|
.join_left(PACKAGE)
|
||||||
.on( SHIPMENT.id == PACKAGE.shipment )
|
.on( SHIPMENT.id == PACKAGE.shipment_id )
|
||||||
.fetch_all(db);
|
.fetch_all(db);
|
||||||
|
|
||||||
REQUIRE(shipment_records.is_ok());
|
REQUIRE(shipment_records.is_ok());
|
||||||
|
|
||||||
printer.print(*shipment_records);
|
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)
|
.from(SHIPMENT)
|
||||||
.join_left(PACKAGE)
|
.join_left(PACKAGE)
|
||||||
.on( SHIPMENT.id == PACKAGE.shipment )
|
.on( SHIPMENT.id == PACKAGE.shipment_id )
|
||||||
|
.order_by(SHIPMENT.id).asc()
|
||||||
.fetch_all<shipment>(db);
|
.fetch_all<shipment>(db);
|
||||||
|
|
||||||
REQUIRE(shipment_result.is_ok());
|
REQUIRE(shipment_result.is_ok());
|
||||||
|
|
||||||
|
size_t index{0};
|
||||||
|
std::vector<size_t> packages_sizes{2, 3};
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
for (const auto &s : *shipment_result) {
|
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());
|
REQUIRE(repo.empty());
|
||||||
|
|
||||||
// auto res = repo.attach<person>("person");
|
auto res = repo.attach<person>("person");
|
||||||
// REQUIRE(res.is_ok());
|
REQUIRE(res.is_ok());
|
||||||
// res = repo.attach<student, person>("student");
|
res = repo.attach<student, person>("student");
|
||||||
// REQUIRE(res.is_ok());
|
REQUIRE(res.is_ok());
|
||||||
// res = repo.attach<teacher, person>("teacher");
|
res = repo.attach<teacher, person>("teacher");
|
||||||
// REQUIRE(res.is_ok());
|
REQUIRE(res.is_ok());
|
||||||
//
|
|
||||||
// REQUIRE(!repo.empty());
|
REQUIRE(!repo.empty());
|
||||||
// REQUIRE(repo.size() == 3);
|
REQUIRE(repo.size() == 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Test next and previous of schema node", "[schema_node][next][previous]") {
|
TEST_CASE("Test next and previous of schema node", "[schema_node][next][previous]") {
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ struct package {
|
||||||
namespace field = matador::access;
|
namespace field = matador::access;
|
||||||
field::primary_key(op, "id", id);
|
field::primary_key(op, "id", id);
|
||||||
field::attribute(op, "weight", weight);
|
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.cpp
|
||||||
utils/auto_reset_event.hpp
|
utils/auto_reset_event.hpp
|
||||||
query/GeneratorTests.cpp
|
query/GeneratorTests.cpp
|
||||||
|
query/SchemaTest.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(OrmTests matador-orm matador-core Catch2::Catch2WithMain)
|
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");
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue