mirror of
https://github.com/rbock/sqlpp11.git
synced 2026-01-05 20:50:34 -06:00
Added several tests for static_assert (plus fixes for aggregates)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user