From effa072e8c567e33d4fb43475ef7a979961c948b Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Thu, 14 Nov 2024 18:29:54 +0100 Subject: [PATCH] Add more tests --- include/sqlpp11/core/aggregate_function/sum.h | 2 +- .../core/basic/parameterized_verbatim.h | 2 +- .../core/operator/arithmetic_expression.h | 2 - .../operator/arithmetic_expression.cpp | 19 +++++++ tests/core/types/CMakeLists.txt | 1 + .../types/operator/sort_order_expression.cpp | 3 -- tests/core/types/value_type/CMakeLists.txt | 32 ++++++++++++ tests/core/types/value_type/boolean.cpp | 50 +++++++++++++++++++ 8 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 tests/core/types/value_type/CMakeLists.txt create mode 100644 tests/core/types/value_type/boolean.cpp diff --git a/include/sqlpp11/core/aggregate_function/sum.h b/include/sqlpp11/core/aggregate_function/sum.h index e5c5a842..185f322e 100644 --- a/include/sqlpp11/core/aggregate_function/sum.h +++ b/include/sqlpp11/core/aggregate_function/sum.h @@ -92,7 +92,7 @@ namespace sqlpp template using check_sum_arg = - ::sqlpp::enable_if_t<(is_numeric::value or is_boolean::value) and not contains_aggregate_function::value>; + ::sqlpp::enable_if_t<(is_numeric::value) and not contains_aggregate_function::value>; template > auto sum(T t) -> sum_t diff --git a/include/sqlpp11/core/basic/parameterized_verbatim.h b/include/sqlpp11/core/basic/parameterized_verbatim.h index 1622c05d..c9ed67f2 100644 --- a/include/sqlpp11/core/basic/parameterized_verbatim.h +++ b/include/sqlpp11/core/basic/parameterized_verbatim.h @@ -78,7 +78,7 @@ namespace sqlpp auto parameterized_verbatim(std::string lhs, Expr expr, std::string rhs) -> parameterized_verbatim_t { - static_assert(is_expression_t::value, "parameterized_verbatim() requires an expression as argument"); + static_assert(has_value_type::value, "parameterized_verbatim() requires an expression as argument"); return {expr, lhs, rhs}; } diff --git a/include/sqlpp11/core/operator/arithmetic_expression.h b/include/sqlpp11/core/operator/arithmetic_expression.h index 68daebc0..c0e480f2 100644 --- a/include/sqlpp11/core/operator/arithmetic_expression.h +++ b/include/sqlpp11/core/operator/arithmetic_expression.h @@ -73,7 +73,6 @@ namespace sqlpp #warning: mysql does not offer operator||, we need to fail compilation, but maybe offer the concat function in addition template struct arithmetic_expression : public enable_as>, -#warning: need to test AS and comparison for arithmetic expressions public enable_comparison> { arithmetic_expression() = delete; @@ -93,7 +92,6 @@ namespace sqlpp template using check_arithmetic_args = ::sqlpp::enable_if_t::value and is_numeric::value>; -#warning: add boolean to numeric types // L and R are expected to be numeric value types (boolen, integral, unsigned_integral, or floating_point). template struct arithmetic_value_type diff --git a/tests/core/serialize/operator/arithmetic_expression.cpp b/tests/core/serialize/operator/arithmetic_expression.cpp index 6a5e0f9a..12241fcb 100644 --- a/tests/core/serialize/operator/arithmetic_expression.cpp +++ b/tests/core/serialize/operator/arithmetic_expression.cpp @@ -58,6 +58,7 @@ int main(int, char* []) // Same for unary expressions. SQLPP_COMPARE(-val, "-1"); + SQLPP_COMPARE(-val + val, "(-1) + 1"); SQLPP_COMPARE(-expr, "-(17 + 4)"); const auto text = sqlpp::value("a"); @@ -69,5 +70,23 @@ int main(int, char* []) SQLPP_COMPARE(text_expr + text, "('b' || 'c') || 'a'"); SQLPP_COMPARE(text_expr + text_expr, "('b' || 'c') || ('b' || 'c')"); + // Arithmetic expressions can be named with AS + SQLPP_COMPARE((val + val).as(sqlpp::alias::a), "(1 + 1) AS a"); + SQLPP_COMPARE((val - val).as(sqlpp::alias::a), "(1 - 1) AS a"); + SQLPP_COMPARE((val * val).as(sqlpp::alias::a), "(1 * 1) AS a"); + SQLPP_COMPARE((val / val).as(sqlpp::alias::a), "(1 / 1) AS a"); + SQLPP_COMPARE((val % val).as(sqlpp::alias::a), "(1 % 1) AS a"); + + // Arithmetic expressions can be compared + SQLPP_COMPARE((val + val) < 17, "(1 + 1) < 17"); + SQLPP_COMPARE((val - val) < 17, "(1 - 1) < 17"); + SQLPP_COMPARE((val * val) < 17, "(1 * 1) < 17"); + SQLPP_COMPARE((val / val) < 17, "(1 / 1) < 17"); + SQLPP_COMPARE((val % val) < 17, "(1 % 1) < 17"); + SQLPP_COMPARE(-val < 17, "(-1) < 17"); + SQLPP_COMPARE((text + text) < "z", "('a' || 'a') < 'z'"); + + + return 0; } diff --git a/tests/core/types/CMakeLists.txt b/tests/core/types/CMakeLists.txt index 9f737c4c..448bff8c 100644 --- a/tests/core/types/CMakeLists.txt +++ b/tests/core/types/CMakeLists.txt @@ -39,4 +39,5 @@ add_subdirectory(clause) add_subdirectory(detail) add_subdirectory(operator) add_subdirectory(type_traits) +add_subdirectory(value_type) diff --git a/tests/core/types/operator/sort_order_expression.cpp b/tests/core/types/operator/sort_order_expression.cpp index 60e6e258..fc39ce71 100644 --- a/tests/core/types/operator/sort_order_expression.cpp +++ b/tests/core/types/operator/sort_order_expression.cpp @@ -28,9 +28,6 @@ template void test_as_expression(Value v) { - using ValueType = sqlpp::value_type_of_t; - using OptValueType = ::sqlpp::optional; - auto v_not_null= sqlpp::value(v); auto v_maybe_null= sqlpp::value(::sqlpp::make_optional(v)); diff --git a/tests/core/types/value_type/CMakeLists.txt b/tests/core/types/value_type/CMakeLists.txt new file mode 100644 index 00000000..abea2ceb --- /dev/null +++ b/tests/core/types/value_type/CMakeLists.txt @@ -0,0 +1,32 @@ +# Copyright (c) 2024, Roland Bock +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright notice, this +# list of conditions and the following disclaimer in the documentation and/or +# other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +function(test_compile name) + set(target sqlpp11_core_types_value_type_${name}) + add_executable(${target} ${name}.cpp) + target_link_libraries(${target} PRIVATE sqlpp11::sqlpp11 sqlpp11_testing) +endfunction() + +test_compile(boolean) + diff --git a/tests/core/types/value_type/boolean.cpp b/tests/core/types/value_type/boolean.cpp new file mode 100644 index 00000000..cb243018 --- /dev/null +++ b/tests/core/types/value_type/boolean.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2024, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +template +void test_boolean() +{ + static_assert(std::is_same, sqlpp::boolean>::value, ""); + static_assert(sqlpp::is_boolean::value, ""); + static_assert(sqlpp::is_numeric::value, ""); + + static_assert(not sqlpp::is_integral::value, ""); + static_assert(not sqlpp::is_unsigned_integral::value, ""); + static_assert(not sqlpp::is_floating_point::value, ""); + static_assert(not sqlpp::is_text::value, ""); + static_assert(not sqlpp::is_blob::value, ""); + static_assert(not sqlpp::is_time_point::value, ""); + static_assert(not sqlpp::is_day_point::value, ""); + static_assert(not sqlpp::is_time_of_day::value, ""); +} + +int main() +{ + test_boolean(); + test_boolean(); +} +