query/test/orm/sql/ColumnGeneratorTest.cpp

112 lines
3.8 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include "matador/query/generator.hpp"
#include "matador/object/repository.hpp"
#include "../test/models/product.hpp"
#include "../test/models/order.hpp"
#include "../test/models/book.hpp"
#include "../test/models/author.hpp"
#include "matador/sql/table.hpp"
using namespace matador::sql;
using namespace matador::query;
using namespace matador::object;
TEST_CASE("Generate columns from object", "[column][generator]") {
using namespace matador::test;
repository s("main");
auto result = s.attach<product>("product");
REQUIRE( result );
auto columns = generator::columns<product>(s);
const std::vector<std::string> expected_columns = {
"product_name",
"supplier_id",
"category_id",
"quantity_per_unit",
"unit_price",
"units_in_stock",
"units_in_order",
"reorder_level",
"discontinued"
};
REQUIRE(!columns.empty());
REQUIRE(columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) {
REQUIRE(expected_columns[i] == columns[i].name);
}
}
TEST_CASE("Generate columns for object with has many relation", "[column][generator][relation]") {
using namespace matador::test;
repository s("main");
auto result = s.attach<supplier>("supplier")
.and_then( [&s] { return s.attach<category>("categories"); } )
.and_then( [&s] { return s.attach<order_details>("order_details"); } )
.and_then( [&s] { return s.attach<product>("products"); } )
.and_then( [&s] { return s.attach<order>("order"); } );
REQUIRE(result);
auto columns = generator::columns<order>(s, generator::column_generator_options::GenerateAlias);
const auto order_table = std::make_shared<table>("order");
const auto order_details_table = std::make_shared<table>("order_details");
const std::vector<column> expected_columns = {
{ order_table, "order_id", "c01" },
{ order_table, "order_date", "c02" },
{ order_table, "required_date", "c03" },
{ order_table, "shipped_date", "c04" },
{ order_table, "ship_via", "c05" },
{ order_table, "freight", "c06" },
{ order_table, "ship_name", "c07" },
{ order_table, "ship_address", "c08" },
{ order_table, "ship_city", "c09" },
{ order_table, "ship_region", "c10" },
{ order_table, "ship_postal_code", "c11" },
{ order_table, "ship_country", "c12" },
{ order_details_table, "order_details_id", "c13" },
{ order_details_table, "order_id", "c14" },
{ order_details_table, "product_id", "c15" }
};
REQUIRE(!columns.empty());
REQUIRE(columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) {
REQUIRE(expected_columns[i].equals(columns[i]));
}
}
TEST_CASE("Generate columns for object with eager foreign key relation", "[column][generator][eager]") {
using namespace matador::test;
repository s("main");
auto result = s.attach<book>("books")
.and_then( [&s] { return s.attach<author>("authors"); } );
REQUIRE(result);
const auto books_table = std::make_shared<table>("books");
const auto authors_table = std::make_shared<table>("authors");
const std::vector<column> expected_columns {
{ books_table, "id", "c01" },
{ books_table, "title", "c02" },
{ authors_table, "id", "c03" },
{ authors_table, "first_name", "c04" },
{ authors_table, "last_name", "c05" },
{ authors_table, "date_of_birth", "c06" },
{ authors_table, "year_of_birth", "c07" },
{ authors_table, "distinguished", "c08" },
{ books_table, "published_in", "c09" }
};
auto columns = generator::columns<book>(s, generator::column_generator_options::GenerateAlias);
REQUIRE(!columns.empty());
REQUIRE(columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) {
REQUIRE(expected_columns[i].equals(columns[i]));
}
}