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

Fixed a bunch of tests

This commit is contained in:
Roland Bock 2024-08-17 19:32:52 +02:00
parent b95a23b161
commit f04a597bf5
10 changed files with 74 additions and 148 deletions

View File

@ -220,28 +220,21 @@ namespace sqlpp
} }
template <typename Context> template <typename Context>
auto to_sql_string(Context& context, const ::sqlpp::chrono::day_point& t) -> std::string auto to_sql_string(Context& , const ::sqlpp::chrono::day_point& t) -> std::string
{ {
const auto ymd = ::date::year_month_day{t}; return date::format("DATE '%Y-%m-%d'", t);
context << "DATE '" << ymd << "'";
return context;
} }
template <typename Context> template <typename Context>
auto to_sql_string(Context& context, const std::chrono::microseconds& t) -> std::string auto to_sql_string(Context&, const std::chrono::microseconds& t) -> std::string
{ {
context << '\'' << ::date::make_time(t) << '\''; return date::format("'%H:%M:%S'", t);
return context;
} }
template <typename Period, typename Context> template <typename Period, typename Context>
auto to_sql_string(Context& context, const std::chrono::time_point<std::chrono::system_clock, Period>& t) -> std::string auto to_sql_string(Context&, const std::chrono::time_point<std::chrono::system_clock, Period>& t) -> std::string
{ {
const auto dp = ::sqlpp::chrono::floor<::date::days>(t); return date::format("TIMESTAMP '%Y-%m-%dT%H:%M:%S'", t);
const auto time = ::date::make_time(t - dp);
const auto ymd = ::date::year_month_day{dp};
context << "TIMESTAMP '" << ymd << ' ' << time << "'";
return context;
} }
template <typename Context> template <typename Context>

View File

@ -1,4 +1,4 @@
# Copyright (c) 2016-2016, Roland Bock # Copyright (c) 2016, Roland Bock
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without modification, # Redistribution and use in source and binary forms, with or without modification,
@ -26,10 +26,8 @@ function(test_compile name)
set(target sqlpp11_${name}) set(target sqlpp11_${name})
add_executable(${target} ${name}.cpp) add_executable(${target} ${name}.cpp)
target_link_libraries(${target} PRIVATE sqlpp11::sqlpp11 sqlpp11_testing) target_link_libraries(${target} PRIVATE sqlpp11::sqlpp11 sqlpp11_testing)
# conditionally bump to a higher C++ standard to test compatibility
endfunction() endfunction()
test_compile(case_when)
test_compile(dynamic) test_compile(dynamic)
test_compile(result_row) test_compile(result_row)
test_compile(select_as) test_compile(select_as)

View File

@ -59,8 +59,8 @@ void test_avg(Value v)
static_assert(sqlpp::has_name<decltype(avg(v_not_null))>::value, ""); static_assert(sqlpp::has_name<decltype(avg(v_not_null))>::value, "");
static_assert(sqlpp::has_name<decltype(avg(sqlpp::distinct, v_not_null))>::value, ""); static_assert(sqlpp::has_name<decltype(avg(sqlpp::distinct, v_not_null))>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(avg(v_not_null))>::name == sqlpp::string_view("avg"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(avg(v_not_null))>, sqlpp::alias::_avg_t::_sqlpp_name_tag>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(avg(sqlpp::distinct, v_not_null))>::name == sqlpp::string_view("avg"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(avg(sqlpp::distinct, v_not_null))>, sqlpp::alias::_avg_t::_sqlpp_name_tag>::value, "");
// avg enables OVER. // avg enables OVER.
static_assert(sqlpp::has_enabled_over<decltype(avg(v_not_null))>::value, ""); static_assert(sqlpp::has_enabled_over<decltype(avg(v_not_null))>::value, "");

View File

@ -57,8 +57,8 @@ void test_count(Value v)
static_assert(sqlpp::has_name<decltype(count(v_not_null))>::value, ""); static_assert(sqlpp::has_name<decltype(count(v_not_null))>::value, "");
static_assert(sqlpp::has_name<decltype(count(sqlpp::distinct, v_not_null))>::value, ""); static_assert(sqlpp::has_name<decltype(count(sqlpp::distinct, v_not_null))>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(count(v_not_null))>::name == sqlpp::string_view("count"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(count(v_not_null))>, sqlpp::alias::_count_t::_sqlpp_name_tag>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(sqlpp::distinct, count(v_not_null))>::name == sqlpp::string_view("count"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(count(sqlpp::distinct, v_not_null))>, sqlpp::alias::_count_t::_sqlpp_name_tag>::value, "");
// count enables comparison member functions. // count enables comparison member functions.
static_assert(sqlpp::has_enabled_comparison<decltype(count(v_not_null))>::value, ""); static_assert(sqlpp::has_enabled_comparison<decltype(count(v_not_null))>::value, "");

View File

@ -59,8 +59,8 @@ void test_max(Value v)
static_assert(sqlpp::has_name<decltype(max(v_not_null))>::value, ""); static_assert(sqlpp::has_name<decltype(max(v_not_null))>::value, "");
static_assert(sqlpp::has_name<decltype(max(sqlpp::distinct, v_not_null))>::value, ""); static_assert(sqlpp::has_name<decltype(max(sqlpp::distinct, v_not_null))>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(max(v_not_null))>::name == sqlpp::string_view("max"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(max(v_not_null))>, sqlpp::alias::_max_t::_sqlpp_name_tag>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(sqlpp::distinct, max(v_not_null))>::name == sqlpp::string_view("max"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(max(sqlpp::distinct, v_not_null))>, sqlpp::alias::_max_t::_sqlpp_name_tag>::value, "");
// max enables comparison member functions. // max enables comparison member functions.
static_assert(sqlpp::has_enabled_comparison<decltype(max(v_not_null))>::value, ""); static_assert(sqlpp::has_enabled_comparison<decltype(max(v_not_null))>::value, "");

View File

@ -59,8 +59,8 @@ void test_min(Value v)
static_assert(sqlpp::has_name<decltype(min(v_not_null))>::value, ""); static_assert(sqlpp::has_name<decltype(min(v_not_null))>::value, "");
static_assert(sqlpp::has_name<decltype(min(sqlpp::distinct, v_not_null))>::value, ""); static_assert(sqlpp::has_name<decltype(min(sqlpp::distinct, v_not_null))>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(min(v_not_null))>::name == sqlpp::string_view("min"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(min(v_not_null))>, sqlpp::alias::_min_t::_sqlpp_name_tag>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(sqlpp::distinct, min(v_not_null))>::name == sqlpp::string_view("min"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(min(sqlpp::distinct, v_not_null))>, sqlpp::alias::_min_t::_sqlpp_name_tag>::value, "");
// min enables comparison member functions. // min enables comparison member functions.
static_assert(sqlpp::has_enabled_comparison<decltype(min(v_not_null))>::value, ""); static_assert(sqlpp::has_enabled_comparison<decltype(min(v_not_null))>::value, "");

View File

@ -63,9 +63,9 @@ void test_aggregate_functions(Value v)
static_assert(sqlpp::has_name<decltype(max(v_not_null).over())>::value, ""); static_assert(sqlpp::has_name<decltype(max(v_not_null).over())>::value, "");
static_assert(sqlpp::has_name<decltype(min(v_not_null).over())>::value, ""); static_assert(sqlpp::has_name<decltype(min(v_not_null).over())>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(count(v_not_null).over())>::name == sqlpp::string_view("count"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(count(v_not_null).over())>, sqlpp::alias::_count_t::_sqlpp_name_tag>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(max(v_not_null).over())>::name == sqlpp::string_view("max"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(min(v_not_null).over())>, sqlpp::alias::_min_t::_sqlpp_name_tag>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(min(v_not_null).over())>::name == sqlpp::string_view("min"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(max(sqlpp::distinct, v_not_null).over())>, sqlpp::alias::_max_t::_sqlpp_name_tag>::value, "");
// Aggregate functions enable comparison member functions. // Aggregate functions enable comparison member functions.
static_assert(sqlpp::has_enabled_comparison<decltype(count(v_not_null).over())>::value, ""); static_assert(sqlpp::has_enabled_comparison<decltype(count(v_not_null).over())>::value, "");
@ -110,8 +110,9 @@ void test_numeric_aggregate_functions(Value v)
static_assert(sqlpp::has_name<decltype(sum(v_not_null).over())>::value, ""); static_assert(sqlpp::has_name<decltype(sum(v_not_null).over())>::value, "");
static_assert(sqlpp::has_name<decltype(avg(v_not_null).over())>::value, ""); static_assert(sqlpp::has_name<decltype(avg(v_not_null).over())>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(sum(v_not_null).over())>::name == sqlpp::string_view("sum"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(sum(v_not_null).over())>, sqlpp::alias::_sum_t::_sqlpp_name_tag>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(avg(v_not_null).over())>::name == sqlpp::string_view("avg"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(avg(v_not_null).over())>, sqlpp::alias::_avg_t::_sqlpp_name_tag>::value, "");
static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(avg(sqlpp::distinct, v_not_null).over())>, sqlpp::alias::_avg_t::_sqlpp_name_tag>::value, "");
// Aggregate functions enable OVER. // Aggregate functions enable OVER.
static_assert(not sqlpp::has_enabled_over<decltype(sum(v_not_null).over())>::value, ""); static_assert(not sqlpp::has_enabled_over<decltype(sum(v_not_null).over())>::value, "");

View File

@ -60,8 +60,9 @@ void test_sum(Value v)
static_assert(sqlpp::has_name<decltype(sum(v_not_null))>::value, ""); static_assert(sqlpp::has_name<decltype(sum(v_not_null))>::value, "");
static_assert(sqlpp::has_name<decltype(sum(sqlpp::distinct, v_not_null))>::value, ""); static_assert(sqlpp::has_name<decltype(sum(sqlpp::distinct, v_not_null))>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(sum(v_not_null))>::name == sqlpp::string_view("sum"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(sum(v_not_null))>, sqlpp::alias::_sum_t::_sqlpp_name_tag>::value, "");
static_assert(sqlpp::name_tag_of_t<decltype(sum(sqlpp::distinct, v_not_null))>::name == sqlpp::string_view("sum"), ""); static_assert(std::is_same<sqlpp::name_tag_of_t<decltype(sum(sqlpp::distinct, v_not_null))>, sqlpp::alias::_sum_t::_sqlpp_name_tag>::value, "");
// sum enables OVER. // sum enables OVER.
static_assert(sqlpp::has_enabled_over<decltype(sum(v_not_null))>::value, ""); static_assert(sqlpp::has_enabled_over<decltype(sum(v_not_null))>::value, "");

View File

@ -1,112 +0,0 @@
/*
* 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>
namespace
{
template <typename T, typename V>
using is_same_type = std::is_same<sqlpp::value_type_of_t<T>, V>;
SQLPP_ALIAS_PROVIDER(r_not_null);
SQLPP_ALIAS_PROVIDER(r_maybe_null);
}
template <typename Value>
void test_case_when(Value v)
{
using ValueType = sqlpp::value_type_of_t<Value>;
using OptValueType = sqlpp::value_type_of_t<::sqlpp::optional<Value>>;
// Selectable values.
auto v_not_null = sqlpp::value(v);
const auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v));
// No value types for incomplete clauses
static_assert(is_same_type<decltype(sqlpp::case_when(true)), sqlpp::no_value_t>::value, "");
static_assert(is_same_type<decltype(sqlpp::case_when(true).then(v_not_null)), sqlpp::no_value_t>::value, "");
// The value type is optional if either of the of the values is optional
static_assert(is_same_type<decltype(sqlpp::case_when(true).then(v_not_null).else_(v_not_null)), ValueType>::value,
"");
static_assert(
is_same_type<decltype(sqlpp::case_when(true).then(v_not_null).else_(v_maybe_null)), OptValueType>::value, "");
static_assert(
is_same_type<decltype(sqlpp::case_when(true).then(v_maybe_null).else_(v_not_null)), OptValueType>::value, "");
static_assert(
is_same_type<decltype(sqlpp::case_when(true).then(v_maybe_null).else_(v_maybe_null)), OptValueType>::value, "");
// The value type is always optional if the condition is optional
const auto opt_bool = ::sqlpp::make_optional(true);
static_assert(
is_same_type<decltype(sqlpp::case_when(opt_bool).then(v_not_null).else_(v_maybe_null)), OptValueType>::value, "");
#warning: test can be aliased
#warning: test has comparison operators
#warning: test nodes
}
int main()
{
// boolean
test_case_when(bool{true});
// integral
test_case_when(int8_t{7});
test_case_when(int16_t{7});
test_case_when(int32_t{7});
test_case_when(int64_t{7});
// unsigned integral
test_case_when(uint8_t{7});
test_case_when(uint16_t{7});
test_case_when(uint32_t{7});
test_case_when(uint64_t{7});
// floating point
test_case_when(float{7.7});
test_case_when(double{7.7});
// text
test_case_when('7');
test_case_when("seven");
test_case_when(std::string("seven"));
test_case_when(::sqlpp::string_view("seven"));
// blob
test_case_when(std::vector<uint8_t>{});
// date
test_case_when(::sqlpp::chrono::day_point{});
// timestamp
test_case_when(::sqlpp::chrono::microsecond_point{});
using minute_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>;
test_case_when(minute_point{});
// time_of_day
test_case_when(std::chrono::microseconds{});
}

View File

@ -47,12 +47,13 @@ void test_select_columns()
auto v = sqlpp::value("text"); auto v = sqlpp::value("text");
auto col_int = test::TabFoo{}.id; auto col_int = test::TabFoo{}.id;
auto col_txt = test::TabFoo{}.textNnD; auto col_txt = test::TabFoo{}.textNnD;
auto col_bool = test::TabFoo{}.boolN;
using unknown = sqlpp::detail::type_vector<>; using unknown = sqlpp::detail::type_vector<>;
using knownInt = sqlpp::detail::type_vector<decltype(col_int)>; using knownInt = sqlpp::detail::type_vector<decltype(col_int)>;
using knownTxt = sqlpp::detail::type_vector<decltype(col_txt)>; using knownTxt = sqlpp::detail::type_vector<decltype(col_txt)>;
// Single column // Single column.
{ {
using T = clause_of_t<decltype(select_columns(col_int))>; using T = clause_of_t<decltype(select_columns(col_int))>;
static_assert(std::is_same<sqlpp::name_tag_of_t<T>, test::TabFoo_::Id::_sqlpp_name_tag>::value, ""); static_assert(std::is_same<sqlpp::name_tag_of_t<T>, test::TabFoo_::Id::_sqlpp_name_tag>::value, "");
@ -63,7 +64,7 @@ void test_select_columns()
static_assert(not sqlpp::has_correct_aggregates<knownTxt, T>::value, ""); static_assert(not sqlpp::has_correct_aggregates<knownTxt, T>::value, "");
} }
// Single dynamic column // Single dynamic column.
{ {
using T = clause_of_t<decltype(select_columns(dynamic(true, col_int)))>; using T = clause_of_t<decltype(select_columns(dynamic(true, col_int)))>;
static_assert(std::is_same<sqlpp::name_tag_of_t<T>, test::TabFoo_::Id::_sqlpp_name_tag>::value, ""); static_assert(std::is_same<sqlpp::name_tag_of_t<T>, test::TabFoo_::Id::_sqlpp_name_tag>::value, "");
@ -74,7 +75,29 @@ void test_select_columns()
static_assert(not sqlpp::has_correct_aggregates<knownTxt, T>::value, ""); static_assert(not sqlpp::has_correct_aggregates<knownTxt, T>::value, "");
} }
// Single declared group by column // Single aggregate function.
{
using T = clause_of_t<decltype(select_columns(avg(col_int)))>;
static_assert(std::is_same<sqlpp::name_tag_of_t<T>, sqlpp::alias::_avg_t::_sqlpp_name_tag>::value, "");
static_assert(std::is_same<sqlpp::value_type_of_t<T>, sqlpp::optional<sqlpp::floating_point>>::value, "");
static_assert(sqlpp::is_result_clause<T>::value, "");
static_assert(sqlpp::has_correct_aggregates<unknown, T>::value, "");
static_assert(sqlpp::has_correct_aggregates<knownInt, T>::value, "");
static_assert(sqlpp::has_correct_aggregates<knownTxt, T>::value, "");
}
// Single dynamic aggregate function.
{
using T = clause_of_t<decltype(select_columns(dynamic(true, avg(col_int))))>;
static_assert(std::is_same<sqlpp::name_tag_of_t<T>, sqlpp::alias::_avg_t::_sqlpp_name_tag>::value, "");
static_assert(std::is_same<sqlpp::value_type_of_t<T>, sqlpp::optional<sqlpp::floating_point>>::value, "");
static_assert(sqlpp::is_result_clause<T>::value, "");
static_assert(sqlpp::has_correct_aggregates<unknown, T>::value, "");
static_assert(sqlpp::has_correct_aggregates<knownInt, T>::value, "");
static_assert(sqlpp::has_correct_aggregates<knownTxt, T>::value, "");
}
// Single declared group by column.
{ {
using T = clause_of_t<decltype(select_columns(declare_group_by_column(v).as(cheese)))>; using T = clause_of_t<decltype(select_columns(declare_group_by_column(v).as(cheese)))>;
static_assert(std::is_same<sqlpp::name_tag_of_t<T>, cheese_t::_sqlpp_name_tag>::value, ""); static_assert(std::is_same<sqlpp::name_tag_of_t<T>, cheese_t::_sqlpp_name_tag>::value, "");
@ -85,7 +108,7 @@ void test_select_columns()
static_assert(sqlpp::has_correct_aggregates<knownTxt, T>::value, ""); static_assert(sqlpp::has_correct_aggregates<knownTxt, T>::value, "");
} }
// Single dynamic declared group by column // Single dynamic declared group by column.
{ {
using T = clause_of_t<decltype(select_columns(dynamic(true, declare_group_by_column(v)).as(cheese)))>; using T = clause_of_t<decltype(select_columns(dynamic(true, declare_group_by_column(v)).as(cheese)))>;
static_assert(std::is_same<sqlpp::name_tag_of_t<T>, cheese_t::_sqlpp_name_tag>::value, ""); static_assert(std::is_same<sqlpp::name_tag_of_t<T>, cheese_t::_sqlpp_name_tag>::value, "");
@ -96,7 +119,29 @@ void test_select_columns()
static_assert(sqlpp::has_correct_aggregates<knownTxt, T>::value, ""); static_assert(sqlpp::has_correct_aggregates<knownTxt, T>::value, "");
} }
#warning: add actual tests here // Mixed columns.
// The columns in group_by determine if aggregates are correct or not.
{
using T = clause_of_t<decltype(select_columns(col_int, col_txt, col_bool))>;
static_assert(not sqlpp::has_name<T>::value, "");
static_assert(not sqlpp::has_value_type<T>::value, "");
static_assert(sqlpp::is_result_clause<T>::value, "");
static_assert(sqlpp::has_correct_aggregates<unknown, T>::value, "");
static_assert(not sqlpp::has_correct_aggregates<knownInt, T>::value, ""); // col_int is a known aggregate here.
static_assert(not sqlpp::has_correct_aggregates<knownTxt, T>::value, "");
}
// Mixed columns.
// The columns in group_by determine if aggregates are correct or not.
{
using T = clause_of_t<decltype(select_columns(col_int, max(col_txt), declare_group_by_column(v).as(cheese)))>;
static_assert(not sqlpp::has_name<T>::value, "");
static_assert(not sqlpp::has_value_type<T>::value, "");
static_assert(sqlpp::is_result_clause<T>::value, "");
static_assert(not sqlpp::has_correct_aggregates<unknown, T>::value, "");
static_assert(sqlpp::has_correct_aggregates<knownInt, T>::value, ""); // col_int is a known aggregate here.
static_assert(not sqlpp::has_correct_aggregates<knownTxt, T>::value, "");
}
} }