0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

Add more tests

This commit is contained in:
Roland Bock 2024-11-14 18:29:54 +01:00
parent 7cdcf0f172
commit effa072e8c
8 changed files with 104 additions and 7 deletions

View File

@ -92,7 +92,7 @@ namespace sqlpp
template <typename T> template <typename T>
using check_sum_arg = using check_sum_arg =
::sqlpp::enable_if_t<(is_numeric<T>::value or is_boolean<T>::value) and not contains_aggregate_function<T>::value>; ::sqlpp::enable_if_t<(is_numeric<T>::value) and not contains_aggregate_function<T>::value>;
template <typename T, typename = check_sum_arg<T>> template <typename T, typename = check_sum_arg<T>>
auto sum(T t) -> sum_t<noop, T> auto sum(T t) -> sum_t<noop, T>

View File

@ -78,7 +78,7 @@ namespace sqlpp
auto parameterized_verbatim(std::string lhs, Expr expr, std::string rhs) auto parameterized_verbatim(std::string lhs, Expr expr, std::string rhs)
-> parameterized_verbatim_t<ValueType, Expr> -> parameterized_verbatim_t<ValueType, Expr>
{ {
static_assert(is_expression_t<Expr>::value, "parameterized_verbatim() requires an expression as argument"); static_assert(has_value_type<Expr>::value, "parameterized_verbatim() requires an expression as argument");
return {expr, lhs, rhs}; return {expr, lhs, rhs};
} }

View File

@ -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 #warning: mysql does not offer operator||, we need to fail compilation, but maybe offer the concat function in addition
template <typename L, typename Operator, typename R> template <typename L, typename Operator, typename R>
struct arithmetic_expression : public enable_as<arithmetic_expression<L, Operator, R>>, struct arithmetic_expression : public enable_as<arithmetic_expression<L, Operator, R>>,
#warning: need to test AS and comparison for arithmetic expressions
public enable_comparison<arithmetic_expression<L, Operator, R>> public enable_comparison<arithmetic_expression<L, Operator, R>>
{ {
arithmetic_expression() = delete; arithmetic_expression() = delete;
@ -93,7 +92,6 @@ namespace sqlpp
template <typename L, typename R> template <typename L, typename R>
using check_arithmetic_args = ::sqlpp::enable_if_t<is_numeric<L>::value and is_numeric<R>::value>; using check_arithmetic_args = ::sqlpp::enable_if_t<is_numeric<L>::value and is_numeric<R>::value>;
#warning: add boolean to numeric types
// L and R are expected to be numeric value types (boolen, integral, unsigned_integral, or floating_point). // L and R are expected to be numeric value types (boolen, integral, unsigned_integral, or floating_point).
template <typename Operator, typename L, typename R> template <typename Operator, typename L, typename R>
struct arithmetic_value_type struct arithmetic_value_type

View File

@ -58,6 +58,7 @@ int main(int, char* [])
// Same for unary expressions. // Same for unary expressions.
SQLPP_COMPARE(-val, "-1"); SQLPP_COMPARE(-val, "-1");
SQLPP_COMPARE(-val + val, "(-1) + 1");
SQLPP_COMPARE(-expr, "-(17 + 4)"); SQLPP_COMPARE(-expr, "-(17 + 4)");
const auto text = sqlpp::value("a"); 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, "('b' || 'c') || 'a'");
SQLPP_COMPARE(text_expr + text_expr, "('b' || 'c') || ('b' || 'c')"); 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; return 0;
} }

View File

@ -39,4 +39,5 @@ add_subdirectory(clause)
add_subdirectory(detail) add_subdirectory(detail)
add_subdirectory(operator) add_subdirectory(operator)
add_subdirectory(type_traits) add_subdirectory(type_traits)
add_subdirectory(value_type)

View File

@ -28,9 +28,6 @@
template<typename Value> template<typename Value>
void test_as_expression(Value v) void test_as_expression(Value v)
{ {
using ValueType = sqlpp::value_type_of_t<Value>;
using OptValueType = ::sqlpp::optional<ValueType>;
auto v_not_null= sqlpp::value(v); auto v_not_null= sqlpp::value(v);
auto v_maybe_null= sqlpp::value(::sqlpp::make_optional(v)); auto v_maybe_null= sqlpp::value(::sqlpp::make_optional(v));

View File

@ -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)

View File

@ -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 <sqlpp11/sqlpp11.h>
template <typename T>
void test_boolean()
{
static_assert(std::is_same<sqlpp::value_type_of_t<T>, sqlpp::boolean>::value, "");
static_assert(sqlpp::is_boolean<T>::value, "");
static_assert(sqlpp::is_numeric<T>::value, "");
static_assert(not sqlpp::is_integral<T>::value, "");
static_assert(not sqlpp::is_unsigned_integral<T>::value, "");
static_assert(not sqlpp::is_floating_point<T>::value, "");
static_assert(not sqlpp::is_text<T>::value, "");
static_assert(not sqlpp::is_blob<T>::value, "");
static_assert(not sqlpp::is_time_point<T>::value, "");
static_assert(not sqlpp::is_day_point<T>::value, "");
static_assert(not sqlpp::is_time_of_day<T>::value, "");
}
int main()
{
test_boolean<bool>();
test_boolean<sqlpp::boolean>();
}