0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 12:51:13 +08:00

More tests

This commit is contained in:
Roland Bock 2024-08-03 08:31:16 +02:00
parent 42d1b61279
commit a7ee6e5d3b
8 changed files with 230 additions and 12 deletions

View File

@ -26,19 +26,23 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/core/operator/as_expression.h>
#include <sqlpp11/core/operator/enable_as.h>
#include <sqlpp11/core/operator/enable_comparison.h>
#include <sqlpp11/core/type_traits.h>
namespace sqlpp
{
template <typename T>
struct value_t
struct value_t:
public enable_as<value_t<T>>,
public enable_comparison<value_t<T>>
{
template <typename alias_provider>
as_expression<value_t, alias_provider> as(const alias_provider& /*unused*/) const
{
return {*this};
}
value_t(T t): _value(std::move(t)) {}
value_t(const value_t&) = default;
value_t(value_t&&) = default;
value_t& operator=(const value_t&) = default;
value_t& operator=(value_t&&) = default;
~value_t() = default;
T _value;
};

View File

@ -34,6 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace sqlpp
{
// To be used as CRTP base for expressions that should offer the comparison member functions.
// This also enables sort order member functions
template <typename Expr>
class enable_comparison
{

View File

@ -27,6 +27,7 @@
*/
#include <sqlpp11/core/type_traits.h>
#include <sqlpp11/core/operator/sort_order_expression.h>
#include <sqlpp11/core/operator/enable_as.h>
#include <sqlpp11/core/serialize.h>
@ -95,4 +96,10 @@ namespace sqlpp
{
return {condition, std::move(t)};
}
template <typename Expr, typename = check_dynamic_args<Expr>>
auto dynamic(bool condition, sort_order_expression<Expr> t) -> dynamic_t<sort_order_expression<Expr>>
{
return {condition, std::move(t)};
}
} // namespace sqlpp

View File

@ -178,6 +178,7 @@ namespace sqlpp
return {static_cast<const derived_statement_t<Policies>&>(*this), on_conflict_data_t<no_data_t>{no_data_t{}}};
}
#warning: Allow for more than one column, see #586
template <typename ConflictTarget>
auto on_conflict(ConflictTarget column) const -> _new_statement_t<consistent_t, on_conflict_t<ConflictTarget>>
{

View File

@ -42,6 +42,8 @@ test_compile(between_expression)
test_compile(bit_expression)
test_compile(case_expression)
test_compile(comparison_expression)
test_compile(exists_expression)
test_compile(in_expression)
test_compile(logical_expression)
test_compile(sort_order_expression)

View File

@ -23,13 +23,8 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "MockDb.h"
#include "Sample.h"
#include <sqlpp11/sqlpp11.h>
SQLPP_ALIAS_PROVIDER(r_not_null);
SQLPP_ALIAS_PROVIDER(r_maybe_null);
SQLPP_ALIAS_PROVIDER(cheese);
template <typename T, typename ValueType>

View File

@ -0,0 +1,96 @@
/*
* 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>
SQLPP_ALIAS_PROVIDER(r_not_null);
SQLPP_ALIAS_PROVIDER(r_maybe_null);
template <typename Value>
void test_exists(Value v)
{
// 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);
// EXISTS expression can be used in basic comparison expressions, which use remove_exists_t to look inside.
static_assert(std::is_same<sqlpp::value_type_of_t<decltype(exists(select(v_not_null)))>, sqlpp::boolean>::value, "");
static_assert(std::is_same<sqlpp::value_type_of_t<decltype(exists(select(v_maybe_null)))>, sqlpp::boolean>::value, "");
// EXISTS expressions enable `as` member function.
static_assert(sqlpp::has_enabled_as<decltype(exists(select(v_not_null)))>::value, "");
// EXISTS expressions do not enable comparison member functions.
static_assert(not sqlpp::has_enabled_comparison<decltype(exists(select(v_not_null)))>::value, "");
// EXISTS expressions have the SELECT as node.
using S = decltype(select(v_not_null));
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(exists(select(v_not_null)))>, sqlpp::detail::type_vector<S>>::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.
}
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<uint8_t>{});
// date
test_exists(::sqlpp::chrono::day_point{});
// timestamp
test_exists(::sqlpp::chrono::microsecond_point{});
using minute_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>;
test_exists(minute_point{});
// time_of_day
test_exists(std::chrono::microseconds{});
}

View File

@ -0,0 +1,112 @@
/*
* 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 Value>
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_maybe_null= sqlpp::value(::sqlpp::make_optional(v));
// Sort order expressions have no value.
static_assert(not sqlpp::has_value_type<decltype(v_not_null.asc())>::value, "");
static_assert(not sqlpp::has_value_type<decltype(v_not_null.desc())>::value, "");
static_assert(not sqlpp::has_value_type<decltype(v_not_null.order(sqlpp::sort_type::asc))>::value, "");
static_assert(not sqlpp::has_value_type<decltype(v_maybe_null.asc())>::value, "");
static_assert(not sqlpp::has_value_type<decltype(v_maybe_null.desc())>::value, "");
static_assert(not sqlpp::has_value_type<decltype(v_maybe_null.order(sqlpp::sort_type::asc))>::value, "");
static_assert(not sqlpp::has_value_type<decltype(dynamic(true, v_not_null.asc()))>::value, "");
static_assert(not sqlpp::has_value_type<decltype(dynamic(true, v_not_null.desc()))>::value, "");
static_assert(not sqlpp::has_value_type<decltype(dynamic(true, v_not_null.order(sqlpp::sort_type::asc)))>::value, "");
static_assert(not sqlpp::has_value_type<decltype(dynamic(true, v_maybe_null.asc()))>::value, "");
static_assert(not sqlpp::has_value_type<decltype(dynamic(true, v_maybe_null.desc()))>::value, "");
static_assert(not sqlpp::has_value_type<decltype(dynamic(true, v_maybe_null.order(sqlpp::sort_type::asc)))>::value, "");
// Sort order expressions have no name.
static_assert(not sqlpp::has_name<decltype(v_not_null.asc())>::value, "");
static_assert(not sqlpp::has_name<decltype(v_maybe_null.asc())>::value, "");
static_assert(not sqlpp::has_name<decltype(dynamic(true, v_not_null.asc()))>::value, "");
static_assert(not sqlpp::has_name<decltype(dynamic(true, v_maybe_null.asc()))>::value, "");
// Sort order expression do not enable the `as` member function.
static_assert(not sqlpp::has_enabled_as<decltype(v_not_null.asc())>::value, "");
// Sort order expressions do not enable comparison member functions.
static_assert(not sqlpp::has_enabled_comparison<decltype(v_not_null.asc())>::value, "");
// Sort order expressions have their arguments as nodes.
using L = typename std::decay<decltype(v_not_null)>::type;
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(v_not_null.asc())>, sqlpp::detail::type_vector<L>>::value, "");
}
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<uint8_t>{});
// date
test_as_expression(::sqlpp::chrono::day_point{});
// timestamp
test_as_expression(::sqlpp::chrono::microsecond_point{});
using minute_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>;
test_as_expression(minute_point{});
// time_of_day
test_as_expression(std::chrono::microseconds{});
}