From 8e688f3c3447e31520a06b40d3563b608c2eba67 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 3 Aug 2024 12:48:22 +0200 Subject: [PATCH] More tests --- include/sqlpp11/core/operator/any.h | 5 +- .../core/operator/arithmetic_expression.h | 111 ++++++--- .../core/operator/between_expression.h | 13 +- .../sqlpp11/core/operator/bit_expression.h | 2 +- include/sqlpp11/core/operator/case.h | 8 +- .../sqlpp11/core/operator/enable_comparison.h | 7 + include/sqlpp11/core/operator/in_expression.h | 45 ++-- .../core/operator/logical_expression.h | 30 ++- include/sqlpp11/core/query/dynamic.h | 22 +- tests/core/serialize/operator/any.cpp | 4 +- .../operator/arithmetic_expression.cpp | 210 ++++-------------- .../core/serialize/operator/as_expression.cpp | 96 +------- .../serialize/operator/assign_expression.cpp | 116 ++-------- .../serialize/operator/between_expression.cpp | 97 ++------ .../serialize/operator/bit_expression.cpp | 147 +++--------- .../serialize/operator/case_expression.cpp | 96 ++------ .../operator/comparison_expression.cpp | 198 ++++------------- .../serialize/operator/exists_expression.cpp | 71 +----- .../core/serialize/operator/in_expression.cpp | 123 +++------- .../serialize/operator/logical_expression.cpp | 124 +++++------ .../operator/sort_order_expression.cpp | 95 ++------ 21 files changed, 458 insertions(+), 1162 deletions(-) diff --git a/include/sqlpp11/core/operator/any.h b/include/sqlpp11/core/operator/any.h index 467310b7..65d1b353 100644 --- a/include/sqlpp11/core/operator/any.h +++ b/include/sqlpp11/core/operator/any.h @@ -74,8 +74,9 @@ namespace sqlpp template Context& serialize(Context& context, const any_t& t) + { + if (t._condition) + { + serialize_operand(context, t._expr); } else { @@ -97,6 +111,12 @@ namespace sqlpp return {condition, std::move(t)}; } + template > + auto dynamic(bool condition, assign_expression t) -> dynamic_t> + { + return {condition, std::move(t)}; + } + template > auto dynamic(bool condition, sort_order_expression t) -> dynamic_t> { diff --git a/tests/core/serialize/operator/any.cpp b/tests/core/serialize/operator/any.cpp index 89f4be1f..7761526d 100644 --- a/tests/core/serialize/operator/any.cpp +++ b/tests/core/serialize/operator/any.cpp @@ -32,8 +32,8 @@ int main(int, char* []) { const auto val = sqlpp::value(17); - SQLPP_COMPARE(any(select(val.as(v))), "ANY(SELECT 17 AS v)"); - SQLPP_COMPARE(val == any(select(val.as(v))), "17 = ANY(SELECT 17 AS v)"); + SQLPP_COMPARE(any(select(val.as(v))), "ANY (SELECT 17 AS v)"); + SQLPP_COMPARE(val == any(select(val.as(v))), "17 = ANY (SELECT 17 AS v)"); return 0; } diff --git a/tests/core/serialize/operator/arithmetic_expression.cpp b/tests/core/serialize/operator/arithmetic_expression.cpp index f83e26ba..6a5e0f9a 100644 --- a/tests/core/serialize/operator/arithmetic_expression.cpp +++ b/tests/core/serialize/operator/arithmetic_expression.cpp @@ -23,173 +23,51 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "MockDb.h" -#include "Sample.h" +#include "../compare.h" #include -#include "../../include/test_helpers.h" - -namespace +int main(int, char* []) { - template - constexpr bool is_same_type() - { - return std::is_same::value; - } -} - -#warning: implement serialize instead of type tests here! -SQLPP_ALIAS_PROVIDER(r_not_null); -SQLPP_ALIAS_PROVIDER(r_maybe_null); -SQLPP_ALIAS_PROVIDER(r_opt_not_null); -SQLPP_ALIAS_PROVIDER(r_opt_maybe_null); - -template -void test_arithmetic_expressions(Value v) -{ - using ValueType = sqlpp::numeric; - using OptValueType = ::sqlpp::optional; - - auto value = sqlpp::value(v); - auto opt_value = sqlpp::value(::sqlpp::make_optional(v)); - - // Arithmetically combining non-optional values - static_assert(is_same_type, ValueType>(), ""); - static_assert(is_same_type, ValueType>(), ""); - static_assert(is_same_type, ValueType>(), ""); - static_assert(is_same_type, ValueType>(), ""); - - // Arithmetically combining non-optional with optional values - static_assert(is_same_type, OptValueType>(), ""); - static_assert(is_same_type, OptValueType>(), ""); - static_assert(is_same_type, OptValueType>(), ""); - static_assert(is_same_type, OptValueType>(), ""); - - // Arithmetically combining optional with non-optional values - static_assert(is_same_type, OptValueType>(), ""); - static_assert(is_same_type, OptValueType>(), ""); - static_assert(is_same_type, OptValueType>(), ""); - static_assert(is_same_type, OptValueType>(), ""); - - // Arithmetically combining optional with optional values - static_assert(is_same_type, OptValueType>(), ""); - static_assert(is_same_type, OptValueType>(), ""); - static_assert(is_same_type, OptValueType>(), ""); - static_assert(is_same_type, OptValueType>(), ""); - - // Same with negate. - static_assert(is_same_type, ValueType>(), ""); - static_assert(is_same_type, OptValueType>(), ""); - - // Arithmetic expressions enable the `as` member function. - static_assert(sqlpp::has_enabled_as::value, ""); - static_assert(sqlpp::has_enabled_as::value, ""); - - // Arithmetic expressions enable comparison member functions. - static_assert(sqlpp::has_enabled_comparison::value, ""); - - // Arithmetic expressions have their arguments as nodes - using L = typename std::decay::type; - using R = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); -} - -template -void test_modulus_expressions(Value v) -{ - using ValueType = sqlpp::numeric; - using OptValueType = ::sqlpp::optional; - - auto value = sqlpp::value(v); - auto opt_value = sqlpp::value(::sqlpp::make_optional(v)); - - // Modulus combining non-optional values - static_assert(is_same_type, ValueType>(), ""); - - // Modulus combining non-optional with optional values - static_assert(is_same_type, OptValueType>(), ""); - - // Modulus combining optional with non-optional values - static_assert(is_same_type, OptValueType>(), ""); - - // Modulus combining optional with optional values - static_assert(is_same_type, OptValueType>(), ""); - - // Modulus expressions enable the `as` member function. - static_assert(sqlpp::has_enabled_as::value, ""); - - // Modulus expressions enable comparison member functions. - static_assert(sqlpp::has_enabled_comparison::value, ""); - - // Modulus expressions have their arguments as nodes - using L = typename std::decay::type; - using R = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); -} - -template -void test_concatenation_expressions(Value v) -{ - using ValueType = sqlpp::text; - using OptValueType = ::sqlpp::optional; - - auto value = sqlpp::value(v); - auto opt_value = sqlpp::value(::sqlpp::make_optional(v)); - - // Concatenating non-optional values - static_assert(is_same_type, ValueType>(), ""); - - // Concatenating non-optional with optional values - static_assert(is_same_type, OptValueType>(), ""); - - // Concatenating optional with non-optional values - static_assert(is_same_type, OptValueType>(), ""); - - // Concatenating optional with optional values - static_assert(is_same_type, OptValueType>(), ""); - - // Modulus expressions enable the `as` member function. - static_assert(sqlpp::has_enabled_as::value, ""); - - // Modulus expressions enable comparison member functions. - static_assert(sqlpp::has_enabled_comparison::value, ""); - - // Modulus expressions have their arguments as nodes - using L = typename std::decay::type; - using R = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); -} - -int main() -{ - // integral - test_arithmetic_expressions(int8_t{7}); - test_arithmetic_expressions(int16_t{7}); - test_arithmetic_expressions(int32_t{7}); - test_arithmetic_expressions(int64_t{7}); - test_modulus_expressions(int8_t{7}); - test_modulus_expressions(int16_t{7}); - test_modulus_expressions(int32_t{7}); - test_modulus_expressions(int64_t{7}); - - // unsigned integral - test_arithmetic_expressions(uint8_t{7}); - test_arithmetic_expressions(uint16_t{7}); - test_arithmetic_expressions(uint32_t{7}); - test_arithmetic_expressions(uint64_t{7}); - test_modulus_expressions(uint8_t{7}); - test_modulus_expressions(uint16_t{7}); - test_modulus_expressions(uint32_t{7}); - test_modulus_expressions(uint64_t{7}); - - // floating point - test_arithmetic_expressions(float{7.7}); - test_arithmetic_expressions(double{7.7}); - - // text - test_concatenation_expressions('7'); - test_concatenation_expressions("seven"); - test_concatenation_expressions(std::string("seven")); - test_concatenation_expressions(::sqlpp::string_view("seven")); + const auto val = sqlpp::value(1); + const auto expr = sqlpp::value(17) + 4; + + // Operands are enclosed in parenheses where required. + SQLPP_COMPARE(val + val, "1 + 1"); + SQLPP_COMPARE(val - val, "1 - 1"); + SQLPP_COMPARE(val * val, "1 * 1"); + SQLPP_COMPARE(val / val, "1 / 1"); + SQLPP_COMPARE(val % val, "1 % 1"); + + SQLPP_COMPARE(val + expr, "1 + (17 + 4)"); + SQLPP_COMPARE(val - expr, "1 - (17 + 4)"); + SQLPP_COMPARE(val * expr, "1 * (17 + 4)"); + SQLPP_COMPARE(val / expr, "1 / (17 + 4)"); + SQLPP_COMPARE(val % expr, "1 % (17 + 4)"); + + SQLPP_COMPARE(expr + val, "(17 + 4) + 1"); + SQLPP_COMPARE(expr - val, "(17 + 4) - 1"); + SQLPP_COMPARE(expr * val, "(17 + 4) * 1"); + SQLPP_COMPARE(expr / val, "(17 + 4) / 1"); + SQLPP_COMPARE(expr % val, "(17 + 4) % 1"); + + SQLPP_COMPARE(expr + expr, "(17 + 4) + (17 + 4)"); + SQLPP_COMPARE(expr - expr, "(17 + 4) - (17 + 4)"); + SQLPP_COMPARE(expr * expr, "(17 + 4) * (17 + 4)"); + SQLPP_COMPARE(expr / expr, "(17 + 4) / (17 + 4)"); + SQLPP_COMPARE(expr % expr, "(17 + 4) % (17 + 4)"); + + // Same for unary expressions. + SQLPP_COMPARE(-val, "-1"); + SQLPP_COMPARE(-expr, "-(17 + 4)"); + + const auto text = sqlpp::value("a"); + const auto text_expr = sqlpp::value("b") + "c"; + + // Same for concatenation. + SQLPP_COMPARE(text + text, "'a' || 'a'"); + SQLPP_COMPARE(text + text_expr, "'a' || ('b' || 'c')"); + SQLPP_COMPARE(text_expr + text, "('b' || 'c') || 'a'"); + SQLPP_COMPARE(text_expr + text_expr, "('b' || 'c') || ('b' || 'c')"); + + return 0; } diff --git a/tests/core/serialize/operator/as_expression.cpp b/tests/core/serialize/operator/as_expression.cpp index 2075549c..fe3b24d1 100644 --- a/tests/core/serialize/operator/as_expression.cpp +++ b/tests/core/serialize/operator/as_expression.cpp @@ -23,96 +23,24 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "../compare.h" #include -#warning: implement serialize instead of type tests here! -SQLPP_ALIAS_PROVIDER(cheese); +SQLPP_ALIAS_PROVIDER(v); -template -using is_select_column_value_type = std::is_same, ValueType>; - -template -void test_as_expression(Value v) +int main(int, char* []) { - using ValueType = sqlpp::value_type_of_t; - using OptValueType = ::sqlpp::optional; + const auto val = sqlpp::value(17); + const auto expr = sqlpp::value(17) + 4; - auto v_not_null= sqlpp::value(v); - auto v_maybe_null= sqlpp::value(::sqlpp::make_optional(v)); - auto v_dynamic_not_null = dynamic(true, sqlpp::value(v)); - auto v_dynamic_maybe_null = dynamic(true, sqlpp::value(::sqlpp::make_optional(v))); + SQLPP_COMPARE(val.as(v), "17 AS v"); + SQLPP_COMPARE(expr.as(v), "(17 + 4) AS v"); - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); + SQLPP_COMPARE(dynamic(true, val).as(v), "17 AS v"); + SQLPP_COMPARE(dynamic(true, expr).as(v), "(17 + 4) AS v"); - static_assert(not sqlpp::has_name::value, ""); - static_assert(not sqlpp::has_name::value, ""); - static_assert(not sqlpp::has_name::value, ""); - static_assert(not sqlpp::has_name::value, ""); + SQLPP_COMPARE(dynamic(false, val).as(v), "NULL AS v"); + SQLPP_COMPARE(dynamic(false, expr).as(v), "NULL AS v"); - static_assert(is_select_column_value_type::value, ""); - static_assert(is_select_column_value_type::value, ""); - static_assert(is_select_column_value_type::value, ""); - static_assert(is_select_column_value_type::value, ""); - - static_assert(sqlpp::select_column_has_name::value, ""); - static_assert(sqlpp::select_column_has_name::value, ""); - static_assert(sqlpp::select_column_has_name::value, ""); - static_assert(sqlpp::select_column_has_name::value, ""); - - // AS expressions have do not enable the `as` member function. - static_assert(not sqlpp::has_enabled_as::value, ""); - - // AS expressions do not enable comparison member functions. - static_assert(not sqlpp::has_enabled_comparison::value, ""); - - // AS expressions have their arguments as nodes. - using L = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); + return 0; } - -int main() -{ - // boolean - test_as_expression(bool{true}); - - // integral - test_as_expression(int8_t{7}); - test_as_expression(int16_t{7}); - test_as_expression(int32_t{7}); - test_as_expression(int64_t{7}); - - // unsigned integral - test_as_expression(uint8_t{7}); - test_as_expression(uint16_t{7}); - test_as_expression(uint32_t{7}); - test_as_expression(uint64_t{7}); - - // floating point - test_as_expression(float{7.7}); - test_as_expression(double{7.7}); - - // text - test_as_expression('7'); - test_as_expression("seven"); - test_as_expression(std::string("seven")); - test_as_expression(::sqlpp::string_view("seven")); - - // blob - test_as_expression(std::vector{}); - - // date - test_as_expression(::sqlpp::chrono::day_point{}); - - // timestamp - test_as_expression(::sqlpp::chrono::microsecond_point{}); - using minute_point = std::chrono::time_point; - test_as_expression(minute_point{}); - - // time_of_day - test_as_expression(std::chrono::microseconds{}); - -} - diff --git a/tests/core/serialize/operator/assign_expression.cpp b/tests/core/serialize/operator/assign_expression.cpp index 044f4562..c0bae7c5 100644 --- a/tests/core/serialize/operator/assign_expression.cpp +++ b/tests/core/serialize/operator/assign_expression.cpp @@ -23,110 +23,26 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "MockDb.h" +#include "../compare.h" #include "Sample.h" #include -#warning: implement serialize instead of type tests here! -namespace +int main(int, char* []) { - auto db = MockDb{}; + constexpr auto t = test::TabFoo{}; + const auto val = sqlpp::value(17); - template - using is_bool = std::is_same, sqlpp::boolean>; + // Operands in assignments are enclosed in parentheses as required. + SQLPP_COMPARE(t.id = val, "id = 17"); + SQLPP_COMPARE(t.id = val + 4, "id = (17 + 4)"); - template - using is_maybe_bool = std::is_same, ::sqlpp::optional>; + // Active dynamic assignments are just as above. + SQLPP_COMPARE(dynamic(true, t.id = val), "id = 17"); + SQLPP_COMPARE(dynamic(true, t.id = val + 4), "id = (17 + 4)"); + + // This should be skipped by insert and update and should therefore never be called. + SQLPP_COMPARE(dynamic(false, t.id = val), "NULL"); + SQLPP_COMPARE(dynamic(false, t.id = val + 4), "NULL"); + + return 0; } - -template -void test_assign_expression(const Column& col, const Value& v) -{ - auto v_not_null = sqlpp::value(v); - auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v)); - - using ValueType = decltype(v_not_null); - using OptValueType = decltype(v_maybe_null); - - // Assignments have no value - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - - // Assignments have no name - static_assert(not sqlpp::has_name::value, ""); - static_assert(not sqlpp::has_name::value, ""); - static_assert(not sqlpp::has_name::value, ""); - - // Assignment nodes - static_assert(std::is_same, - sqlpp::detail::type_vector>::value, - ""); - static_assert(std::is_same, - sqlpp::detail::type_vector>::value, - ""); - static_assert(std::is_same, - sqlpp::detail::type_vector>::value, - ""); - - // Assign expressions do not have the `as` member function. - static_assert(not sqlpp::has_enabled_as::value, ""); - - // Assign expressions do not enable comparison member functions. - static_assert(not sqlpp::has_enabled_comparison::value, ""); - - // Assign expressions have their arguments as nodes. - using L = typename std::decay::type; - using R = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); -} - -#warning: test that non-nullable columns cannot be assigned optional values -#warning: test that non-default columns cannot be assigned to default_value - -int main() -{ - const auto bar = test::TabBar{}; - const auto foo = test::TabFoo{}; - const auto date_time = test::TabDateTime{}; - - // boolean - test_assign_expression(foo.boolN, bool{true}); - - // integral - test_assign_expression(foo.intN, int8_t{7}); - test_assign_expression(foo.intN, int16_t{7}); - test_assign_expression(foo.intN, int32_t{7}); - test_assign_expression(foo.intN, int64_t{7}); - - // unsigned integral - test_assign_expression(foo.uIntN, uint8_t{7}); - test_assign_expression(foo.uIntN, uint16_t{7}); - test_assign_expression(foo.uIntN, uint32_t{7}); - test_assign_expression(foo.uIntN, uint64_t{7}); - - // floating point - test_assign_expression(foo.doubleN, float{7.7}); - test_assign_expression(foo.doubleN, double{7.7}); - - // text - test_assign_expression(bar.textN, '7'); - test_assign_expression(bar.textN, "seven"); - test_assign_expression(bar.textN, std::string("seven")); - test_assign_expression(bar.textN, ::sqlpp::string_view("seven")); - - // blob - test_assign_expression(foo.blobN, std::vector{}); - - // date - test_assign_expression(date_time.dayPointN, ::sqlpp::chrono::day_point{}); - - // timestamp - test_assign_expression(date_time.timePointN, ::sqlpp::chrono::microsecond_point{}); - using minute_point = std::chrono::time_point; - test_assign_expression(date_time.timePointN, minute_point{}); - - // time_of_day - test_assign_expression(date_time.timeOfDayN, std::chrono::microseconds{}); -} - diff --git a/tests/core/serialize/operator/between_expression.cpp b/tests/core/serialize/operator/between_expression.cpp index 2d33ea03..3c844b1b 100644 --- a/tests/core/serialize/operator/between_expression.cpp +++ b/tests/core/serialize/operator/between_expression.cpp @@ -23,91 +23,24 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "MockDb.h" -#include "Sample.h" +#include "../compare.h" #include -#warning: implement serialize instead of type tests here! -namespace +int main(int, char* []) { - auto db = MockDb{}; + const auto val = sqlpp::value(1); + const auto expr = sqlpp::value(17) + 4; - template - using is_bool = std::is_same, sqlpp::boolean>; + // Operands are enclosed in parenheses where required + SQLPP_COMPARE(val.between(val, val), "1 BETWEEN 1 AND 1"); + SQLPP_COMPARE(val.between(val, expr), "1 BETWEEN 1 AND (17 + 4)"); + SQLPP_COMPARE(val.between(expr, val), "1 BETWEEN (17 + 4) AND 1"); + SQLPP_COMPARE(val.between(expr, expr), "1 BETWEEN (17 + 4) AND (17 + 4)"); + SQLPP_COMPARE(expr.between(val, val), "(17 + 4) BETWEEN 1 AND 1"); + SQLPP_COMPARE(expr.between(val, expr), "(17 + 4) BETWEEN 1 AND (17 + 4)"); + SQLPP_COMPARE(expr.between(expr, val), "(17 + 4) BETWEEN (17 + 4) AND 1"); + SQLPP_COMPARE(expr.between(expr, expr), "(17 + 4) BETWEEN (17 + 4) AND (17 + 4)"); - template - using is_maybe_bool = std::is_same, ::sqlpp::optional>; + SQLPP_COMPARE(val.between(val, val) and true, "(1 BETWEEN 1 AND 1) AND 1"); + return 0; } - -template -void test_between_expression(Value v) -{ - auto v_not_null = sqlpp::value(v); - auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v)); - - // Variations of nullable and non-nullable values - static_assert(is_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - - // Between expressions have the `as` member function. - static_assert(sqlpp::has_enabled_as::value, ""); - - // Between expressions do not enable comparison member functions. - static_assert(not sqlpp::has_enabled_comparison::value, ""); - - // Between expressions have their arguments as nodes. - using L = typename std::decay::type; - using M = Value; - using R = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); -} - -int main() -{ - // boolean - test_between_expression(bool{true}); - - // integral - test_between_expression(int8_t{7}); - test_between_expression(int16_t{7}); - test_between_expression(int32_t{7}); - test_between_expression(int64_t{7}); - - // unsigned integral - test_between_expression(uint8_t{7}); - test_between_expression(uint16_t{7}); - test_between_expression(uint32_t{7}); - test_between_expression(uint64_t{7}); - - // floating point - test_between_expression(float{7.7}); - test_between_expression(double{7.7}); - - // text - test_between_expression('7'); - test_between_expression("seven"); - test_between_expression(std::string("seven")); - test_between_expression(::sqlpp::string_view("seven")); - - // blob - test_between_expression(std::vector{}); - - // date - test_between_expression(::sqlpp::chrono::day_point{}); - - // timestamp - test_between_expression(::sqlpp::chrono::microsecond_point{}); - using minute_point = std::chrono::time_point; - test_between_expression(minute_point{}); - - // time_of_day - test_between_expression(std::chrono::microseconds{}); - -} - diff --git a/tests/core/serialize/operator/bit_expression.cpp b/tests/core/serialize/operator/bit_expression.cpp index 470fc9a9..b8bf3253 100644 --- a/tests/core/serialize/operator/bit_expression.cpp +++ b/tests/core/serialize/operator/bit_expression.cpp @@ -23,123 +23,42 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "MockDb.h" -#include "Sample.h" +#include "../compare.h" #include -#warning: implement serialize instead of type tests here! -namespace +int main(int, char* []) { - auto db = MockDb{}; + const auto val = sqlpp::value(1); + const auto expr = sqlpp::value(17) + 4; - template - using is_integral = std::is_same, sqlpp::integral>; + // Operands are enclosed in parentheses where required. + SQLPP_COMPARE(val & val, "1 & 1"); + SQLPP_COMPARE(val | val, "1 | 1"); + SQLPP_COMPARE(val ^ val, "1 ^ 1"); + SQLPP_COMPARE(val << val, "1 << 1"); + SQLPP_COMPARE(val >> val, "1 >> 1"); - template - using is_maybe_integral = std::is_same, ::sqlpp::optional>; + SQLPP_COMPARE(val & expr, "1 & (17 + 4)"); + SQLPP_COMPARE(val | expr, "1 | (17 + 4)"); + SQLPP_COMPARE(val ^ expr, "1 ^ (17 + 4)"); + SQLPP_COMPARE(val << expr, "1 << (17 + 4)"); + SQLPP_COMPARE(val >> expr, "1 >> (17 + 4)"); + + SQLPP_COMPARE(expr & val, "(17 + 4) & 1"); + SQLPP_COMPARE(expr | val, "(17 + 4) | 1"); + SQLPP_COMPARE(expr ^ val, "(17 + 4) ^ 1"); + SQLPP_COMPARE(expr << val, "(17 + 4) << 1"); + SQLPP_COMPARE(expr >> val, "(17 + 4) >> 1"); + + SQLPP_COMPARE(expr & expr, "(17 + 4) & (17 + 4)"); + SQLPP_COMPARE(expr | expr, "(17 + 4) | (17 + 4)"); + SQLPP_COMPARE(expr ^ expr, "(17 + 4) ^ (17 + 4)"); + SQLPP_COMPARE(expr << expr, "(17 + 4) << (17 + 4)"); + SQLPP_COMPARE(expr >> expr, "(17 + 4) >> (17 + 4)"); + + // Same for unary operators + SQLPP_COMPARE(~val, "~1"); + SQLPP_COMPARE(~expr, "~(17 + 4)"); + + return 0; } - -template -void test_bit_expression(Value v) -{ - auto v_not_null = sqlpp::value(v); - auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v)); - - // Compare non-nullable with non-nullable. - static_assert(is_integral::value, ""); - static_assert(is_integral> v_not_null)>::value, ""); - static_assert(is_integral::value, ""); - static_assert(is_integral::value, ""); - static_assert(is_integral::value, ""); - - // Compare non-nullable with nullable. - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral> v_maybe_null)>::value, ""); - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral::value, ""); - - // Compare nullable with non-nullable. - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral> v_not_null)>::value, ""); - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral::value, ""); - - // Compare nullable with nullable. - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral> v_maybe_null)>::value, ""); - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral::value, ""); - - // Compare with null. - static_assert(is_integral::value, ""); - static_assert(is_maybe_integral::value, ""); - - // Comparison expressions have the `as` member function. - static_assert(sqlpp::has_enabled_as::value, ""); - static_assert(sqlpp::has_enabled_as::value, ""); - - // Comparison expressions do not enable comparison member functions. - static_assert(not sqlpp::has_enabled_comparison::value, ""); - - // Comparison expressions have their arguments as nodes. - using L = typename std::decay::type; - using R = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); -} - -template -void test_bit_shift_expression(Left l, Right r) -{ - auto l_not_null = sqlpp::value(l); - auto l_maybe_null = sqlpp::value(::sqlpp::make_optional(l)); - auto r_not_null = sqlpp::value(r); - auto r_maybe_null = sqlpp::value(::sqlpp::make_optional(r)); - - // Compare non-nullable with non-nullable. - static_assert(is_integral::value, ""); - static_assert(is_integral> r_not_null)>::value, ""); - - // Compare non-nullable with nullable. - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral> r_maybe_null)>::value, ""); - - // Compare nullable with non-nullable. - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral> r_not_null)>::value, ""); - - // Compare nullable with nullable. - static_assert(is_maybe_integral::value, ""); - static_assert(is_maybe_integral> r_maybe_null)>::value, ""); - - // Comparison expressions have the `as` member function. - static_assert(sqlpp::has_enabled_as::value, ""); - - // Comparison expressions do not enable comparison member functions. - static_assert(not sqlpp::has_enabled_comparison::value, ""); - - // Comparison expressions have their arguments as nodes. - using L = typename std::decay::type; - using R = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); -} - - -int main() -{ - // bit expression require integral operands - test_bit_expression(int8_t{7}); - test_bit_expression(int16_t{7}); - test_bit_expression(int32_t{7}); - test_bit_expression(int64_t{7}); - - // bit shift operations can have unsigned rhs operands - test_bit_shift_expression(int8_t{7}, uint8_t{7}); - test_bit_shift_expression(int8_t{7}, uint16_t{7}); - test_bit_shift_expression(int8_t{7}, uint32_t{7}); - test_bit_shift_expression(int8_t{7}, uint64_t{7}); -} - diff --git a/tests/core/serialize/operator/case_expression.cpp b/tests/core/serialize/operator/case_expression.cpp index ff9f8d26..b754aa9d 100644 --- a/tests/core/serialize/operator/case_expression.cpp +++ b/tests/core/serialize/operator/case_expression.cpp @@ -23,90 +23,26 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "../compare.h" #include - template - using is_same_type = std::is_same, sqlpp::value_type_of_t>; +SQLPP_ALIAS_PROVIDER(v); -#warning: implement serialize instead of type tests here! -template -void test_case_expression(Value v) +int main(int, char* []) { - auto c_not_null = sqlpp::value(true); - auto c_maybe_null = sqlpp::value(::sqlpp::make_optional(false)); + const auto cond = sqlpp::value(true); + const auto val = sqlpp::value(11); + const auto expr = sqlpp::value(17) + 4; - auto v_not_null = sqlpp::value(v); - auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v)); + // Case operands use parentheses where required. + SQLPP_COMPARE(case_when(cond).then(val).else_(val), "CASE WHEN 1 THEN 11 ELSE 11 END"); + SQLPP_COMPARE(case_when(cond).then(val).else_(expr), "CASE WHEN 1 THEN 11 ELSE (17 + 4) END"); + SQLPP_COMPARE(case_when(cond).then(expr).else_(val), "CASE WHEN 1 THEN (17 + 4) ELSE 11 END"); + SQLPP_COMPARE(case_when(cond).then(expr).else_(expr), "CASE WHEN 1 THEN (17 + 4) ELSE (17 + 4) END"); + SQLPP_COMPARE(case_when(false or cond).then(val).else_(val), "CASE WHEN (0 OR 1) THEN 11 ELSE 11 END"); + SQLPP_COMPARE(case_when(false or cond).then(val).else_(expr), "CASE WHEN (0 OR 1) THEN 11 ELSE (17 + 4) END"); + SQLPP_COMPARE(case_when(false or cond).then(expr).else_(val), "CASE WHEN (0 OR 1) THEN (17 + 4) ELSE 11 END"); + SQLPP_COMPARE(case_when(false or cond).then(expr).else_(expr), "CASE WHEN (0 OR 1) THEN (17 + 4) ELSE (17 + 4) END"); - using ValueType = sqlpp::value_type_of_t; - using OptValueType = sqlpp::value_type_of_t; - - // Variations of nullable and non-nullable values - static_assert(is_same_type::value, ""); - static_assert(is_same_type::value, ""); - static_assert(is_same_type::value, ""); - static_assert(is_same_type::value, ""); - static_assert(is_same_type::value, ""); - static_assert(is_same_type::value, ""); - static_assert(is_same_type::value, ""); - static_assert(is_same_type::value, ""); - - // Incomplete case expressions have no value. - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - - // Case expressions have the `as` member function. - static_assert(sqlpp::has_enabled_as::value, ""); - - // Case expressions enable comparison member functions. - static_assert(sqlpp::has_enabled_comparison::value, ""); - - // Between expressions have their arguments as nodes. - using L = typename std::decay::type; - using M = typename std::decay::type; - using R = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); + return 0; } - -int main() -{ - // boolean - test_case_expression(bool{true}); - - // integral - test_case_expression(int8_t{7}); - test_case_expression(int16_t{7}); - test_case_expression(int32_t{7}); - test_case_expression(int64_t{7}); - - // unsigned integral - test_case_expression(uint8_t{7}); - test_case_expression(uint16_t{7}); - test_case_expression(uint32_t{7}); - test_case_expression(uint64_t{7}); - - // floating point - test_case_expression(float{7.7}); - test_case_expression(double{7.7}); - - // text - test_case_expression('7'); - test_case_expression("seven"); - test_case_expression(std::string("seven")); - test_case_expression(::sqlpp::string_view("seven")); - - // blob - test_case_expression(std::vector{}); - - // date - test_case_expression(::sqlpp::chrono::day_point{}); - - // timestamp - test_case_expression(::sqlpp::chrono::microsecond_point{}); - using minute_point = std::chrono::time_point; - test_case_expression(minute_point{}); - - // time_of_day - test_case_expression(std::chrono::microseconds{}); -} - diff --git a/tests/core/serialize/operator/comparison_expression.cpp b/tests/core/serialize/operator/comparison_expression.cpp index 8d33571c..c78d5f38 100644 --- a/tests/core/serialize/operator/comparison_expression.cpp +++ b/tests/core/serialize/operator/comparison_expression.cpp @@ -23,159 +23,57 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "MockDb.h" -#include "Sample.h" +#include "../compare.h" #include -#warning: implement serialize instead of type tests here! -namespace +int main(int, char* []) { - auto db = MockDb{}; + const auto val = sqlpp::value(1); + const auto expr = sqlpp::value(17) + 4; - template - using is_bool = std::is_same, sqlpp::boolean>; + // Operands are enclosed in parentheses where required. + SQLPP_COMPARE(val < val, "1 < 1"); + SQLPP_COMPARE(val <= val, "1 <= 1"); + SQLPP_COMPARE(val == val, "1 = 1"); + SQLPP_COMPARE(val != val, "1 <> 1"); + SQLPP_COMPARE(val >= val, "1 >= 1"); + SQLPP_COMPARE(val > val, "1 > 1"); + SQLPP_COMPARE(val.is_distinct_from(val), "1 IS DISTINCT FROM 1"); + SQLPP_COMPARE(val.is_not_distinct_from(val), "1 IS NOT DISTINCT FROM 1"); - template - using is_maybe_bool = std::is_same, ::sqlpp::optional>; + SQLPP_COMPARE(val < expr, "1 < (17 + 4)"); + SQLPP_COMPARE(val <= expr, "1 <= (17 + 4)"); + SQLPP_COMPARE(val == expr, "1 = (17 + 4)"); + SQLPP_COMPARE(val != expr, "1 <> (17 + 4)"); + SQLPP_COMPARE(val >= expr, "1 >= (17 + 4)"); + SQLPP_COMPARE(val > expr, "1 > (17 + 4)"); + SQLPP_COMPARE(val.is_distinct_from(expr), "1 IS DISTINCT FROM (17 + 4)"); + SQLPP_COMPARE(val.is_not_distinct_from(expr), "1 IS NOT DISTINCT FROM (17 + 4)"); + + SQLPP_COMPARE(expr < val, "(17 + 4) < 1"); + SQLPP_COMPARE(expr <= val, "(17 + 4) <= 1"); + SQLPP_COMPARE(expr == val, "(17 + 4) = 1"); + SQLPP_COMPARE(expr != val, "(17 + 4) <> 1"); + SQLPP_COMPARE(expr >= val, "(17 + 4) >= 1"); + SQLPP_COMPARE(expr > val, "(17 + 4) > 1"); + SQLPP_COMPARE(expr.is_distinct_from(val), "(17 + 4) IS DISTINCT FROM 1"); + SQLPP_COMPARE(expr.is_not_distinct_from(val), "(17 + 4) IS NOT DISTINCT FROM 1"); + + SQLPP_COMPARE(expr < expr, "(17 + 4) < (17 + 4)"); + SQLPP_COMPARE(expr <= expr, "(17 + 4) <= (17 + 4)"); + SQLPP_COMPARE(expr == expr, "(17 + 4) = (17 + 4)"); + SQLPP_COMPARE(expr != expr, "(17 + 4) <> (17 + 4)"); + SQLPP_COMPARE(expr >= expr, "(17 + 4) >= (17 + 4)"); + SQLPP_COMPARE(expr > expr, "(17 + 4) > (17 + 4)"); + SQLPP_COMPARE(expr.is_distinct_from(expr), "(17 + 4) IS DISTINCT FROM (17 + 4)"); + SQLPP_COMPARE(expr.is_not_distinct_from(expr), "(17 + 4) IS NOT DISTINCT FROM (17 + 4)"); + + // Same for unary operators + SQLPP_COMPARE(val.is_null(), "1 IS NULL"); + SQLPP_COMPARE(val.is_not_null(), "1 IS NOT NULL"); + + SQLPP_COMPARE(expr.is_null(), "(17 + 4) IS NULL"); + SQLPP_COMPARE(expr.is_not_null(), "(17 + 4) IS NOT NULL"); + + return 0; } - -template -void test_comparison_expression(Value v) -{ - auto v_not_null = sqlpp::value(v); - auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v)); - -#warning : Should also implement between as member functions? - - // Compare non-nullable with non-nullable. - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - static_assert(is_bool= v_not_null)>::value, ""); - static_assert(is_bool v_not_null)>::value, ""); - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - - // Compare non-nullable with nullable. - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool= v_maybe_null)>::value, ""); - static_assert(is_maybe_bool v_maybe_null)>::value, ""); - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - - // Compare nullable with non-nullable. - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool= v_not_null)>::value, ""); - static_assert(is_maybe_bool v_not_null)>::value, ""); - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - - // Compare nullable with nullable. - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool= v_maybe_null)>::value, ""); - static_assert(is_maybe_bool v_maybe_null)>::value, ""); - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - - // Compare with null. - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - - // Comparison expressions have the `as` member function. - static_assert(sqlpp::has_enabled_as::value, ""); - static_assert(sqlpp::has_enabled_as::value, ""); - - // Comparison expressions do not enable comparison member functions. - static_assert(not sqlpp::has_enabled_comparison::value, ""); - - // Comparison expressions have their arguments as nodes. - using L = typename std::decay::type; - using R = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); -} - -template -void test_like(Value v) -{ - auto v_not_null= sqlpp::value(v); - auto v_maybe_null= sqlpp::value(::sqlpp::make_optional(v)); - - // Compare non-nullable with non-nullable. - static_assert(is_bool::value, ""); - - // Compare non-nullable with nullable. - static_assert(is_maybe_bool::value, ""); - - // Compare nullable with non-nullable. - static_assert(is_maybe_bool::value, ""); - - // Compare nullable with nullable. - static_assert(is_maybe_bool::value, ""); -} - -int main() -{ - // boolean - test_comparison_expression(bool{true}); - - // integral - test_comparison_expression(int8_t{7}); - test_comparison_expression(int16_t{7}); - test_comparison_expression(int32_t{7}); - test_comparison_expression(int64_t{7}); - - // unsigned integral - test_comparison_expression(uint8_t{7}); - test_comparison_expression(uint16_t{7}); - test_comparison_expression(uint32_t{7}); - test_comparison_expression(uint64_t{7}); - - // floating point - test_comparison_expression(float{7.7}); - test_comparison_expression(double{7.7}); - - // text - test_comparison_expression('7'); - test_comparison_expression("seven"); - test_comparison_expression(std::string("seven")); - test_comparison_expression(::sqlpp::string_view("seven")); - - // blob - test_comparison_expression(std::vector{}); - - // date - test_comparison_expression(::sqlpp::chrono::day_point{}); - - // timestamp - test_comparison_expression(::sqlpp::chrono::microsecond_point{}); - using minute_point = std::chrono::time_point; - test_comparison_expression(minute_point{}); - - // time_of_day - test_comparison_expression(std::chrono::microseconds{}); - - // text - test_like('7'); - test_like("seven"); - test_like(std::string("seven")); - test_like(::sqlpp::string_view("seven")); - -} - diff --git a/tests/core/serialize/operator/exists_expression.cpp b/tests/core/serialize/operator/exists_expression.cpp index 5159afd8..1a8f01ff 100644 --- a/tests/core/serialize/operator/exists_expression.cpp +++ b/tests/core/serialize/operator/exists_expression.cpp @@ -23,75 +23,16 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "../compare.h" #include -#warning: implement serialize instead of type tests here! -SQLPP_ALIAS_PROVIDER(r_not_null); -SQLPP_ALIAS_PROVIDER(r_maybe_null); +SQLPP_ALIAS_PROVIDER(v); -template -void test_exists(Value v) +int main(int, char* []) { - // Selectable values. - const auto v_not_null = sqlpp::value(v).as(r_not_null); - const auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v)).as(r_maybe_null); + const auto val = sqlpp::value(17); - // EXISTS expression can be used in basic comparison expressions, which use remove_exists_t to look inside. - static_assert(std::is_same, sqlpp::boolean>::value, ""); - static_assert(std::is_same, sqlpp::boolean>::value, ""); + SQLPP_COMPARE(exists(select(val.as(v))), "EXISTS (SELECT 17 AS v)"); - // EXISTS expressions enable `as` member function. - static_assert(sqlpp::has_enabled_as::value, ""); - - // EXISTS expressions do not enable comparison member functions. - static_assert(not sqlpp::has_enabled_comparison::value, ""); - - // EXISTS expressions have the SELECT as node. - using S = decltype(select(v_not_null)); - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); - -#warning: Note that a sub select may require tables from the enclosing select. This is currently not correctly implemented. We need to test that. + return 0; } - -int main() -{ - // boolean - test_exists(bool{true}); - - // integral - test_exists(int8_t{7}); - test_exists(int16_t{7}); - test_exists(int32_t{7}); - test_exists(int64_t{7}); - - // unsigned integral - test_exists(uint8_t{7}); - test_exists(uint16_t{7}); - test_exists(uint32_t{7}); - test_exists(uint64_t{7}); - - // floating point - test_exists(float{7.7}); - test_exists(double{7.7}); - - // text - test_exists('7'); - test_exists("seven"); - test_exists(std::string("seven")); - test_exists(::sqlpp::string_view("seven")); - - // blob - test_exists(std::vector{}); - - // date - test_exists(::sqlpp::chrono::day_point{}); - - // timestamp - test_exists(::sqlpp::chrono::microsecond_point{}); - using minute_point = std::chrono::time_point; - test_exists(minute_point{}); - - // time_of_day - test_exists(std::chrono::microseconds{}); -} - diff --git a/tests/core/serialize/operator/in_expression.cpp b/tests/core/serialize/operator/in_expression.cpp index f2013a0a..12984426 100644 --- a/tests/core/serialize/operator/in_expression.cpp +++ b/tests/core/serialize/operator/in_expression.cpp @@ -23,101 +23,38 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "../compare.h" #include -#warning: implement serialize instead of type tests here! -namespace +SQLPP_ALIAS_PROVIDER(v); + +int main(int, char* []) { - template - using is_bool = std::is_same, sqlpp::boolean>; + const auto val = sqlpp::value(17); + const auto expr = sqlpp::value(17) + 4; + using expr_t = typename std::decay::type; - template - using is_maybe_bool = std::is_same, ::sqlpp::optional>; + // IN expression with single select or other singe expression: No extra parentheses. + SQLPP_COMPARE(val.in(val), "17 IN (17)"); + SQLPP_COMPARE(val.in(expr), "17 IN (17 + 4)"); + SQLPP_COMPARE(val.in(select(val.as(v))), "17 IN (SELECT 17 AS v)"); + + SQLPP_COMPARE(val.not_in(val), "17 NOT IN (17)"); + SQLPP_COMPARE(val.not_in(expr), "17 NOT IN (17 + 4)"); + SQLPP_COMPARE(val.not_in(select(val.as(v))), "17 NOT IN (SELECT 17 AS v)"); + + // IN expressions with multiple arguments require inner parentheses. + SQLPP_COMPARE(val.in(1, select(val.as(v))), "17 IN (1, (SELECT 17 AS v))"); + SQLPP_COMPARE(val.in(std::vector{17, 18, 19}), "17 IN (17, 18, 19)"); + SQLPP_COMPARE(val.in(std::vector{expr, expr, expr}), "17 IN ((17 + 4), (17 + 4), (17 + 4))"); + + SQLPP_COMPARE(val.not_in(1, select(val.as(v))), "17 NOT IN (1, (SELECT 17 AS v))"); + SQLPP_COMPARE(val.not_in(std::vector{17, 18, 19}), "17 NOT IN (17, 18, 19)"); + SQLPP_COMPARE(val.not_in(std::vector{expr, expr, expr}), "17 NOT IN ((17 + 4), (17 + 4), (17 + 4))"); + + // IN expressions with no arguments would be an error in SQL, but the library interprets the intent gracefully. + SQLPP_COMPARE(val.in(std::vector{}), "0"); + SQLPP_COMPARE(val.not_in(std::vector{}), "1"); + + return 0; } - -template -void test_in_expression(Value v) -{ - using OptValue = ::sqlpp::optional; - - auto v_not_null = sqlpp::value(v); - auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v)); - - // Compare non-nullable with non-nullable. - static_assert(is_bool::value, ""); - static_assert(is_bool{}))>::value, ""); - static_assert(is_bool::value, ""); - - // Compare non-nullable with nullable. - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool{}))>::value, ""); - static_assert(is_maybe_bool::value, ""); - - // Compare nullable with non-nullable. - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool{}))>::value, ""); - static_assert(is_maybe_bool::value, ""); - - // Compare nullable with nullable. - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool{}))>::value, ""); - static_assert(is_maybe_bool::value, ""); - - // IN expressions have the `as` member function. - static_assert(sqlpp::has_enabled_as{}))>::value, ""); - - // IN expressions do not enable comparison member functions. - static_assert(not sqlpp::has_enabled_comparison{}))>::value, ""); - - // IN expressions have their arguments as nodes. - using L = typename std::decay::type; - using R1= Value; - using R2= OptValue; - static_assert(std::is_same{}))>, sqlpp::detail::type_vector>::value, ""); - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); -} - -int main() -{ - // boolean - test_in_expression(bool{true}); -#warning reactivate -#if 0 - // integral - test_in_expression(int8_t{7}); - test_in_expression(int16_t{7}); - test_in_expression(int32_t{7}); - test_in_expression(int64_t{7}); - - // unsigned integral - test_in_expression(uint8_t{7}); - test_in_expression(uint16_t{7}); - test_in_expression(uint32_t{7}); - test_in_expression(uint64_t{7}); - - // floating point - test_in_expression(float{7.7}); - test_in_expression(double{7.7}); - - // text - test_in_expression('7'); - test_in_expression("seven"); - test_in_expression(std::string("seven")); - test_in_expression(::sqlpp::string_view("seven")); - - // blob - test_in_expression(std::vector{}); - - // date - test_in_expression(::sqlpp::chrono::day_point{}); - - // timestamp - test_in_expression(::sqlpp::chrono::microsecond_point{}); - using minute_point = std::chrono::time_point; - test_in_expression(minute_point{}); - - // time_of_day - test_in_expression(std::chrono::microseconds{}); -#endif -} - diff --git a/tests/core/serialize/operator/logical_expression.cpp b/tests/core/serialize/operator/logical_expression.cpp index 4c5efa45..b59d3e2c 100644 --- a/tests/core/serialize/operator/logical_expression.cpp +++ b/tests/core/serialize/operator/logical_expression.cpp @@ -23,74 +23,66 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "../compare.h" #include -#warning: implement serialize instead of type tests here! -namespace +int main(int, char* []) { - template - using is_bool = std::is_same, sqlpp::boolean>; + const auto val = sqlpp::value(true); + const auto expr = sqlpp::value(17) > 15; - template - using is_maybe_bool = std::is_same, ::sqlpp::optional>; + // Operands are enclosed in parenheses where required + SQLPP_COMPARE(val and val, "1 AND 1"); + SQLPP_COMPARE(val and expr, "1 AND (17 > 15)"); + SQLPP_COMPARE(expr and val, "(17 > 15) AND 1"); + SQLPP_COMPARE(expr and expr, "(17 > 15) AND (17 > 15)"); + + SQLPP_COMPARE(val or val, "1 OR 1"); + SQLPP_COMPARE(val or expr, "1 OR (17 > 15)"); + SQLPP_COMPARE(expr or val, "(17 > 15) OR 1"); + SQLPP_COMPARE(expr or expr, "(17 > 15) OR (17 > 15)"); + + SQLPP_COMPARE(not val, "NOT 1"); + SQLPP_COMPARE(not expr, "NOT (17 > 15)"); + + // Chains are not nested in parentheses. + SQLPP_COMPARE(val and val and val and val and val, "1 AND 1 AND 1 AND 1 AND 1"); + SQLPP_COMPARE(val or val or val or val or val, "1 OR 1 OR 1 OR 1 OR 1"); + + // Broken chains use parentheses for the respective blocks. + SQLPP_COMPARE((val and val and val) or (val and val), "(1 AND 1 AND 1) OR (1 AND 1)"); + SQLPP_COMPARE((val or val or val) and (val or val), "(1 OR 1 OR 1) AND (1 OR 1)"); + + // NOT is not chained gracefully, but hey, don't do that anyways. + SQLPP_COMPARE(not not not val, "NOT (NOT (NOT 1))"); + + // Operands are enclosed in parenheses where required or completely dropped if inactive + SQLPP_COMPARE(val and dynamic(true, val), "1 AND 1"); + SQLPP_COMPARE(val and dynamic(true, expr), "1 AND (17 > 15)"); + SQLPP_COMPARE(expr and dynamic(true, val), "(17 > 15) AND 1"); + SQLPP_COMPARE(expr and dynamic(true, expr), "(17 > 15) AND (17 > 15)"); + + SQLPP_COMPARE(val or dynamic(true, val), "1 OR 1"); + SQLPP_COMPARE(val or dynamic(true, expr), "1 OR (17 > 15)"); + SQLPP_COMPARE(expr or dynamic(true, val), "(17 > 15) OR 1"); + SQLPP_COMPARE(expr or dynamic(true, expr), "(17 > 15) OR (17 > 15)"); + + SQLPP_COMPARE(val and dynamic(false, val), "1"); + SQLPP_COMPARE(val and dynamic(false, expr), "1"); + SQLPP_COMPARE(expr and dynamic(false, val), "17 > 15"); + SQLPP_COMPARE(expr and dynamic(false, expr), "17 > 15"); + + SQLPP_COMPARE(val or dynamic(false, val), "1"); + SQLPP_COMPARE(val or dynamic(false, expr), "1"); + SQLPP_COMPARE(expr or dynamic(false, val), "17 > 15"); + SQLPP_COMPARE(expr or dynamic(false, expr), "17 > 15"); + + // Chained partially dynamic expressions + SQLPP_COMPARE(val and dynamic(true, val) and expr, "1 AND 1 AND (17 > 15)"); + SQLPP_COMPARE(val and dynamic(false, val) and expr, "1 AND (17 > 15)"); + + SQLPP_COMPARE(val or dynamic(true, val) or expr, "1 OR 1 OR (17 > 15)"); + SQLPP_COMPARE(val or dynamic(false, val) or expr, "1 OR (17 > 15)"); + + return 0; } - -template -void test_logical_expression(Value v) -{ - auto v_not_null= sqlpp::value(v); - auto v_maybe_null= sqlpp::value(::sqlpp::make_optional(v)); - - // Combine non-nullable with non-nullable. - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - - static_assert(is_bool::value, ""); - static_assert(is_bool::value, ""); - - // Combine nullable with non-nullable. - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - - // Combine non-nullable with nullable. - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - - // Combine nullable with nullable. - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - - static_assert(is_maybe_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - - // not. - static_assert(is_bool::value, ""); - static_assert(is_maybe_bool::value, ""); - - // Logical expressions have the `as` member function. - static_assert(sqlpp::has_enabled_as::value, ""); - static_assert(sqlpp::has_enabled_as::value, ""); - - // Logical expressions do not enable comparison member functions. - static_assert(not sqlpp::has_enabled_comparison::value, ""); - static_assert(not sqlpp::has_enabled_comparison::value, ""); - - // Logical expressions have their arguments as nodes. - using L = typename std::decay::type; - using R = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); - static_assert(std::is_same, sqlpp::detail::type_vector>>::value, ""); -} - -int main() -{ - // boolean - test_logical_expression(bool{true}); -} - diff --git a/tests/core/serialize/operator/sort_order_expression.cpp b/tests/core/serialize/operator/sort_order_expression.cpp index d82d211c..14f98c39 100644 --- a/tests/core/serialize/operator/sort_order_expression.cpp +++ b/tests/core/serialize/operator/sort_order_expression.cpp @@ -23,91 +23,24 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "../compare.h" #include -#warning: implement serialize instead of type tests here! -template -void test_as_expression(Value v) +int main(int, char* []) { - using ValueType = sqlpp::value_type_of_t; - using OptValueType = ::sqlpp::optional; + const auto val = sqlpp::value(1); + const auto expr = sqlpp::value(17) + 4; - auto v_not_null= sqlpp::value(v); - auto v_maybe_null= sqlpp::value(::sqlpp::make_optional(v)); + // Operands are enclosed in parentheses where required. + SQLPP_COMPARE(val.asc(), "1 ASC"); + SQLPP_COMPARE(val.desc(), "1 DESC"); + SQLPP_COMPARE(val.order(sqlpp::sort_type::asc), "1 ASC"); + SQLPP_COMPARE(val.order(sqlpp::sort_type::desc), "1 DESC"); - // Sort order expressions have no value. - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); + SQLPP_COMPARE(expr.asc(), "(17 + 4) ASC"); + SQLPP_COMPARE(expr.desc(), "(17 + 4) DESC"); + SQLPP_COMPARE(expr.order(sqlpp::sort_type::asc), "(17 + 4) ASC"); + SQLPP_COMPARE(expr.order(sqlpp::sort_type::desc), "(17 + 4) DESC"); - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - static_assert(not sqlpp::has_value_type::value, ""); - - // Sort order expressions have no name. - static_assert(not sqlpp::has_name::value, ""); - static_assert(not sqlpp::has_name::value, ""); - static_assert(not sqlpp::has_name::value, ""); - static_assert(not sqlpp::has_name::value, ""); - - // Sort order expression do not enable the `as` member function. - static_assert(not sqlpp::has_enabled_as::value, ""); - - // Sort order expressions do not enable comparison member functions. - static_assert(not sqlpp::has_enabled_comparison::value, ""); - - // Sort order expressions have their arguments as nodes. - using L = typename std::decay::type; - static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); + return 0; } - -int main() -{ - // boolean - test_as_expression(bool{true}); - - // integral - test_as_expression(int8_t{7}); - test_as_expression(int16_t{7}); - test_as_expression(int32_t{7}); - test_as_expression(int64_t{7}); - - // unsigned integral - test_as_expression(uint8_t{7}); - test_as_expression(uint16_t{7}); - test_as_expression(uint32_t{7}); - test_as_expression(uint64_t{7}); - - // floating point - test_as_expression(float{7.7}); - test_as_expression(double{7.7}); - - // text - test_as_expression('7'); - test_as_expression("seven"); - test_as_expression(std::string("seven")); - test_as_expression(::sqlpp::string_view("seven")); - - // blob - test_as_expression(std::vector{}); - - // date - test_as_expression(::sqlpp::chrono::day_point{}); - - // timestamp - test_as_expression(::sqlpp::chrono::microsecond_point{}); - using minute_point = std::chrono::time_point; - test_as_expression(minute_point{}); - - // time_of_day - test_as_expression(std::chrono::microseconds{}); -} -