#include #include "matador/sql/column_generator.hpp" #include "matador/object/schema.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::object; TEST_CASE("Generate columns from object", "[column][generator]") { using namespace matador::test; schema s("main"); auto result = s.attach("product"); REQUIRE( result ); auto columns = column_generator::generate(s); const std::vector 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; schema s("main"); auto result = s.attach("supplier") .and_then( [&s] { return s.attach("categories"); } ) .and_then( [&s] { return s.attach("order_details"); } ) .and_then( [&s] { return s.attach("products"); } ) .and_then( [&s] { return s.attach("order"); } ); REQUIRE(result); auto columns = column_generator::generate(s); const auto order_table = std::make_shared("order"); const auto order_details_table = std::make_shared
("order_details"); const std::vector 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; schema s("main"); auto result = s.attach("books") .and_then( [&s] { return s.attach("authors"); } ); REQUIRE(result); const auto books_table = std::make_shared
("books"); const auto authors_table = std::make_shared
("authors"); const std::vector 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 = column_generator::generate(s); 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])); } }