Added several tests for static_assert (plus fixes for aggregates)

This commit is contained in:
rbock
2015-10-03 20:11:55 +02:00
parent bd385f7377
commit bd55f3db5b
8 changed files with 77 additions and 4 deletions

View File

@@ -60,6 +60,7 @@ namespace sqlpp
{
using _traits = make_traits<floating_point, tag::is_expression, tag::is_selectable>;
using _nodes = detail::type_vector<Expr, aggregate_function>;
using _is_aggregate_expression = std::true_type;
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value,
"avg() used with flag other than 'distinct'");

View File

@@ -62,6 +62,7 @@ namespace sqlpp
using _traits = make_traits<integral, tag::is_expression /*, tag::is_selectable*/>;
using _nodes = detail::type_vector<Expr, aggregate_function>;
using _is_aggregate_expression = std::true_type;
using _can_be_null = std::false_type;
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value,

View File

@@ -59,6 +59,7 @@ namespace sqlpp
{
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;
using _nodes = detail::type_vector<Expr, aggregate_function>;
using _is_aggregate_expression = std::true_type;
using _auto_alias_t = max_alias_t;

View File

@@ -59,6 +59,7 @@ namespace sqlpp
{
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;
using _nodes = detail::type_vector<Expr, aggregate_function>;
using _is_aggregate_expression = std::true_type;
using _auto_alias_t = min_alias_t;

View File

@@ -60,6 +60,7 @@ namespace sqlpp
{
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;
using _nodes = detail::type_vector<Expr, aggregate_function>;
using _is_aggregate_expression = std::true_type;
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value,
"sum() used with flag other than 'distinct'");

View File

@@ -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 <typename NameProvider, typename Member>

View File

@@ -44,6 +44,7 @@ namespace sqlpp
{
using _traits = make_traits<boolean, tag::is_expression, tag::is_wrapped_value>;
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<integral, tag::is_expression, tag::is_wrapped_value>;
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<floating_point, tag::is_expression, tag::is_wrapped_value>;
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<text, tag::is_expression, tag::is_wrapped_value>;
using _nodes = detail::type_vector<>;
using _is_aggregate_expression = std::true_type;
using _value_t = std::string;

View File

@@ -31,15 +31,80 @@
#include <sqlpp11/functions.h>
#include <sqlpp11/connection.h>
namespace sqlpp
{
namespace test
{
template <typename T>
void print_type_on_error(std::true_type, const T&)
{
}
template <typename T>
void print_type_on_error(std::false_type, const T& t)
{
t._print_me_;
}
template <typename Assert, typename Expression>
void run_check(const Expression&)
{
using Context = MockDb::_serializer_context_t;
using CheckResult = std::is_same<sqlpp::run_check_t<Context, Expression>, Assert>;
static_assert(CheckResult::value, "Unexpected run_check result");
print_type_on_error(CheckResult{}, sqlpp::run_check_t<Context, Expression>{});
}
template <typename Expression>
void run_check(const Expression&)
{
using Context = MockDb::_serializer_context_t;
using CheckResult = std::is_same<sqlpp::run_check_t<Context, Expression>, consistent_t>;
static_assert(CheckResult::value, "Unexpected run_check result");
print_type_on_error(CheckResult{}, sqlpp::run_check_t<Context, Expression>{});
}
}
}
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<sqlpp::assert_aggregates_t>(select(t.beta).from(t).where(true).group_by(t.alpha));
run_check<sqlpp::assert_aggregates_t>(select((t.alpha + t.delta).as(whatever)).from(t).where(true).group_by(t.alpha));
run_check<sqlpp::assert_aggregates_t>(
select((t.alpha + t.delta).as(whatever)).from(t).where(true).group_by(t.alpha, t.alpha + t.delta * 17));
return 0;
}