From bd55f3db5ba98df1f6340f43b9f111ce83bc4b00 Mon Sep 17 00:00:00 2001 From: rbock Date: Sat, 3 Oct 2015 20:11:55 +0200 Subject: [PATCH] Added several tests for static_assert (plus fixes for aggregates) --- include/sqlpp11/avg.h | 1 + include/sqlpp11/count.h | 1 + include/sqlpp11/max.h | 1 + include/sqlpp11/min.h | 1 + include/sqlpp11/sum.h | 1 + include/sqlpp11/type_traits.h | 1 - include/sqlpp11/wrap_operand.h | 4 ++ tests/Aggregates.cpp | 71 ++++++++++++++++++++++++++++++++-- 8 files changed, 77 insertions(+), 4 deletions(-) diff --git a/include/sqlpp11/avg.h b/include/sqlpp11/avg.h index 9a4aa642..1c7913d3 100644 --- a/include/sqlpp11/avg.h +++ b/include/sqlpp11/avg.h @@ -60,6 +60,7 @@ namespace sqlpp { using _traits = make_traits; using _nodes = detail::type_vector; + using _is_aggregate_expression = std::true_type; static_assert(is_noop::value or std::is_same::value, "avg() used with flag other than 'distinct'"); diff --git a/include/sqlpp11/count.h b/include/sqlpp11/count.h index 31353d21..6c50c599 100644 --- a/include/sqlpp11/count.h +++ b/include/sqlpp11/count.h @@ -62,6 +62,7 @@ namespace sqlpp using _traits = make_traits; using _nodes = detail::type_vector; + using _is_aggregate_expression = std::true_type; using _can_be_null = std::false_type; static_assert(is_noop::value or std::is_same::value, diff --git a/include/sqlpp11/max.h b/include/sqlpp11/max.h index 48091ed1..27792def 100644 --- a/include/sqlpp11/max.h +++ b/include/sqlpp11/max.h @@ -59,6 +59,7 @@ namespace sqlpp { using _traits = make_traits, tag::is_expression, tag::is_selectable>; using _nodes = detail::type_vector; + using _is_aggregate_expression = std::true_type; using _auto_alias_t = max_alias_t; diff --git a/include/sqlpp11/min.h b/include/sqlpp11/min.h index 50f884bb..ea398289 100644 --- a/include/sqlpp11/min.h +++ b/include/sqlpp11/min.h @@ -59,6 +59,7 @@ namespace sqlpp { using _traits = make_traits, tag::is_expression, tag::is_selectable>; using _nodes = detail::type_vector; + using _is_aggregate_expression = std::true_type; using _auto_alias_t = min_alias_t; diff --git a/include/sqlpp11/sum.h b/include/sqlpp11/sum.h index c5ee6534..cc963789 100644 --- a/include/sqlpp11/sum.h +++ b/include/sqlpp11/sum.h @@ -60,6 +60,7 @@ namespace sqlpp { using _traits = make_traits, tag::is_expression, tag::is_selectable>; using _nodes = detail::type_vector; + using _is_aggregate_expression = std::true_type; static_assert(is_noop::value or std::is_same::value, "sum() used with flag other than 'distinct'"); diff --git a/include/sqlpp11/type_traits.h b/include/sqlpp11/type_traits.h index fc4de04a..f70ea3aa 100644 --- a/include/sqlpp11/type_traits.h +++ b/include/sqlpp11/type_traits.h @@ -292,7 +292,6 @@ namespace sqlpp { using _nodes = detail::type_vector<>; using _contains_aggregate_function = std::true_type; - using _is_aggregate_expression = std::true_type; }; template diff --git a/include/sqlpp11/wrap_operand.h b/include/sqlpp11/wrap_operand.h index 9ef712c1..3470e7d9 100644 --- a/include/sqlpp11/wrap_operand.h +++ b/include/sqlpp11/wrap_operand.h @@ -44,6 +44,7 @@ namespace sqlpp { using _traits = make_traits; using _nodes = detail::type_vector<>; + using _is_aggregate_expression = std::true_type; using _value_t = bool; @@ -86,6 +87,7 @@ namespace sqlpp { using _traits = make_traits; using _nodes = detail::type_vector<>; + using _is_aggregate_expression = std::true_type; using _value_t = int64_t; @@ -128,6 +130,7 @@ namespace sqlpp { using _traits = make_traits; using _nodes = detail::type_vector<>; + using _is_aggregate_expression = std::true_type; using _value_t = double; @@ -170,6 +173,7 @@ namespace sqlpp { using _traits = make_traits; using _nodes = detail::type_vector<>; + using _is_aggregate_expression = std::true_type; using _value_t = std::string; diff --git a/tests/Aggregates.cpp b/tests/Aggregates.cpp index 2c2b71f3..ab872d4f 100644 --- a/tests/Aggregates.cpp +++ b/tests/Aggregates.cpp @@ -31,15 +31,80 @@ #include #include +namespace sqlpp +{ + namespace test + { + template + void print_type_on_error(std::true_type, const T&) + { + } + + template + void print_type_on_error(std::false_type, const T& t) + { + t._print_me_; + } + + template + void run_check(const Expression&) + { + using Context = MockDb::_serializer_context_t; + using CheckResult = std::is_same, Assert>; + static_assert(CheckResult::value, "Unexpected run_check result"); + print_type_on_error(CheckResult{}, sqlpp::run_check_t{}); + } + + template + void run_check(const Expression&) + { + using Context = MockDb::_serializer_context_t; + using CheckResult = std::is_same, consistent_t>; + static_assert(CheckResult::value, "Unexpected run_check result"); + print_type_on_error(CheckResult{}, sqlpp::run_check_t{}); + } + } +} + +SQLPP_ALIAS_PROVIDER(whatever); + int Aggregates(int, char**) { - MockDb db = {}; - MockDb::_serializer_context_t printer; + using sqlpp::test::run_check; // test::TabFoo f; test::TabBar t; - db(select(t.alpha).from(t).where(true).group_by(t.alpha)); + // If there is no group_by, we can select whatever we want + run_check(select(all_of(t)).from(t).where(true)); + run_check(select(t.alpha).from(t).where(true)); + run_check(select(count(t.alpha)).from(t).where(true)); + + // If there is a static group_by, selected columns must be made of group_by expressions, or aggregate expression (e.g. + // count(t.id)) or values to be valid + run_check(select(t.alpha).from(t).where(true).group_by(t.alpha)); + run_check(select((t.alpha + 42).as(whatever)).from(t).where(true).group_by(t.alpha)); + run_check(select((t.alpha + 42).as(whatever)).from(t).where(true).group_by(t.alpha, t.alpha + t.delta * 17)); + run_check( + select((t.alpha + t.delta * 17).as(whatever)).from(t).where(true).group_by(t.alpha, t.alpha + t.delta * 17)); + run_check(select((t.beta + "fortytwo").as(whatever)).from(t).where(true).group_by(t.beta)); + + run_check(select(avg(t.alpha)).from(t).where(true).group_by(t.beta)); + run_check(select(count(t.alpha)).from(t).where(true).group_by(t.beta)); + run_check(select(max(t.alpha)).from(t).where(true).group_by(t.beta)); + run_check(select(min(t.alpha)).from(t).where(true).group_by(t.beta)); + run_check(select(sum(t.alpha)).from(t).where(true).group_by(t.beta)); + + run_check(select((t.alpha + count(t.delta)).as(whatever)).from(t).where(true).group_by(t.alpha)); + + run_check(select(sqlpp::value(1).as(whatever)).from(t).where(true).group_by(t.alpha)); + run_check(select(sqlpp::value("whatever").as(whatever)).from(t).where(true).group_by(t.alpha)); + + // Otherwise, they are invalid + run_check(select(t.beta).from(t).where(true).group_by(t.alpha)); + run_check(select((t.alpha + t.delta).as(whatever)).from(t).where(true).group_by(t.alpha)); + run_check( + select((t.alpha + t.delta).as(whatever)).from(t).where(true).group_by(t.alpha, t.alpha + t.delta * 17)); return 0; }