added table_column tests
This commit is contained in:
parent
4b015c8ced
commit
36a160e1c7
|
|
@ -14,12 +14,15 @@ add_executable(OrmTests
|
||||||
backend/test_statement.cpp
|
backend/test_statement.cpp
|
||||||
backend/test_statement.hpp
|
backend/test_statement.hpp
|
||||||
orm/SessionQueryBuilderTest.cpp
|
orm/SessionQueryBuilderTest.cpp
|
||||||
|
query/ColumnGeneratorTest.cpp
|
||||||
query/CriteriaTests.cpp
|
query/CriteriaTests.cpp
|
||||||
|
query/GeneratorTests.cpp
|
||||||
query/QueryBuilderTest.cpp
|
query/QueryBuilderTest.cpp
|
||||||
query/QueryFixture.cpp
|
query/QueryFixture.cpp
|
||||||
query/QueryFixture.hpp
|
query/QueryFixture.hpp
|
||||||
query/QueryTest.cpp
|
query/QueryTest.cpp
|
||||||
query/ColumnGeneratorTest.cpp
|
query/SchemaTest.cpp
|
||||||
|
query/TableColumnTest.cpp
|
||||||
sql/ColumnTest.cpp
|
sql/ColumnTest.cpp
|
||||||
sql/ConnectionPoolFixture.hpp
|
sql/ConnectionPoolFixture.hpp
|
||||||
sql/ConnectionPoolTest.cpp
|
sql/ConnectionPoolTest.cpp
|
||||||
|
|
@ -27,8 +30,6 @@ add_executable(OrmTests
|
||||||
sql/StatementCacheTest.cpp
|
sql/StatementCacheTest.cpp
|
||||||
utils/auto_reset_event.cpp
|
utils/auto_reset_event.cpp
|
||||||
utils/auto_reset_event.hpp
|
utils/auto_reset_event.hpp
|
||||||
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,75 @@
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include "matador/query/table_column.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using matador::query::table_column;
|
||||||
|
using matador::query::operator""_col;
|
||||||
|
|
||||||
|
TEST_CASE("table_column: basic construction without table or alias", "[table_column]") {
|
||||||
|
const table_column c{"id"};
|
||||||
|
|
||||||
|
CHECK(c.column_name() == "id");
|
||||||
|
CHECK(c.canonical_name() == "id");
|
||||||
|
|
||||||
|
// `name()` should be safe in expression contexts (no alias leakage)
|
||||||
|
CHECK(c.name() == "id");
|
||||||
|
|
||||||
|
CHECK(c.alias().empty());
|
||||||
|
CHECK_FALSE(c.has_alias());
|
||||||
|
|
||||||
|
// result label defaults to raw column name when no alias is set
|
||||||
|
CHECK(c.result_name() == "id");
|
||||||
|
|
||||||
|
CHECK_FALSE(c.is_function());
|
||||||
|
CHECK(c.function() == matador::sql::sql_function_t::None);
|
||||||
|
|
||||||
|
// default attributes should mean "nullable" (unless NotNull is set)
|
||||||
|
CHECK(c.is_nullable());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("table_column: alias keeps expression name canonical but changes result label", "[table_column]") {
|
||||||
|
const table_column base{"id"};
|
||||||
|
const table_column aliased = base.as("user_id");
|
||||||
|
|
||||||
|
// original unchanged
|
||||||
|
CHECK(base.alias().empty());
|
||||||
|
CHECK(base.result_name() == "id");
|
||||||
|
CHECK(base.name() == "id");
|
||||||
|
|
||||||
|
// aliased column
|
||||||
|
CHECK(aliased.alias() == "user_id");
|
||||||
|
CHECK(aliased.has_alias());
|
||||||
|
CHECK(aliased.result_name() == "user_id");
|
||||||
|
|
||||||
|
// expression reference remains canonical (table-less here)
|
||||||
|
CHECK(aliased.canonical_name() == "id");
|
||||||
|
CHECK(aliased.name() == "id");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("table_column: function construction sets function flags", "[table_column]") {
|
||||||
|
const table_column c{matador::sql::sql_function_t::Count, "id"};
|
||||||
|
|
||||||
|
CHECK(c.column_name() == "id");
|
||||||
|
CHECK(c.is_function());
|
||||||
|
CHECK(c.function() == matador::sql::sql_function_t::Count);
|
||||||
|
|
||||||
|
// name() remains the expression reference (canonical for table-less)
|
||||||
|
CHECK(c.name() == "id");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("table_column: user-defined literal _col parses dotted input", "[table_column]") {
|
||||||
|
const table_column a = "id"_col;
|
||||||
|
CHECK(a.column_name() == "id");
|
||||||
|
CHECK(a.name() == "id");
|
||||||
|
|
||||||
|
// Dotted input keeps only the column part (table qualifier is ignored by the literal)
|
||||||
|
const table_column b = "users.id"_col;
|
||||||
|
CHECK(b.column_name() == "id");
|
||||||
|
CHECK(b.name() == "id");
|
||||||
|
|
||||||
|
// Multiple dots are rejected
|
||||||
|
CHECK_THROWS_AS(("a.b.c"_col), std::invalid_argument);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue