0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Make mysql DateTime tests a bit stricter

This commit is contained in:
Roland Bock 2021-08-14 10:27:31 +02:00
parent 25a8dfa292
commit 2c1cc8ec89

View File

@ -48,6 +48,21 @@ namespace
serialize(::sqlpp::wrap_operand_t<L>{l}, std::cerr); serialize(::sqlpp::wrap_operand_t<L>{l}, std::cerr);
std::cerr << " != "; std::cerr << " != ";
serialize(::sqlpp::wrap_operand_t<R>{r}, std::cerr); serialize(::sqlpp::wrap_operand_t<R>{r}, std::cerr);
std::cerr << "\n" ;
throw std::runtime_error("Unexpected result");
}
}
template <typename L, typename R>
auto require_close(int line, const L& l, const R& r) -> void
{
if (std::chrono::abs(l - r) > std::chrono::seconds{1})
{
std::cerr << line << ": abs(";
serialize(::sqlpp::wrap_operand_t<L>{l}, std::cerr);
std::cerr << " - ";
serialize(::sqlpp::wrap_operand_t<R>{r}, std::cerr);
std::cerr << ") > 1s\n" ;
throw std::runtime_error("Unexpected result"); throw std::runtime_error("Unexpected result");
} }
} }
@ -78,9 +93,8 @@ int main()
try try
{ {
using days_type = std::chrono::duration<int, std::ratio<60*60*24>>;
mysql::connection db(config); mysql::connection db(config);
db.execute(R"(SET time_zone = '+00:00')"); // To force MySQL's CURRENT_TIMESTAMP into the right timezone
db.execute(R"(DROP TABLE IF EXISTS tab_date_time)"); db.execute(R"(DROP TABLE IF EXISTS tab_date_time)");
db.execute(R"(CREATE TABLE tab_date_time ( db.execute(R"(CREATE TABLE tab_date_time (
col_day_point date, col_day_point date,
@ -96,14 +110,14 @@ int main()
require_equal(__LINE__, row.colDayPoint.value(), ::sqlpp::chrono::day_point{}); require_equal(__LINE__, row.colDayPoint.value(), ::sqlpp::chrono::day_point{});
require_equal(__LINE__, row.colTimePoint.is_null(), true); require_equal(__LINE__, row.colTimePoint.is_null(), true);
require_equal(__LINE__, row.colTimePoint.value(), ::sqlpp::chrono::microsecond_point{}); require_equal(__LINE__, row.colTimePoint.value(), ::sqlpp::chrono::microsecond_point{});
require_equal(__LINE__, std::chrono::time_point_cast<days_type>(row.colDateTimePoint.value()), std::chrono::time_point_cast<days_type>(std::chrono::system_clock::now())); require_close(__LINE__, row.colDateTimePoint.value(), std::chrono::system_clock::now());
} }
auto statement = db.prepare(select(tab.colDateTimePoint).from(tab).unconditionally()); auto statement = db.prepare(select(tab.colDateTimePoint).from(tab).unconditionally());
for (const auto& row : db(statement)) for (const auto& row : db(statement))
{ {
require_equal(__LINE__, row.colDateTimePoint.is_null(), false); require_equal(__LINE__, row.colDateTimePoint.is_null(), false);
require_equal(__LINE__, std::chrono::time_point_cast<days_type>(row.colDateTimePoint.value()), std::chrono::time_point_cast<days_type>(std::chrono::system_clock::now())); require_close(__LINE__, row.colDateTimePoint.value(), std::chrono::system_clock::now());
} }
db(update(tab).set(tab.colDayPoint = today, tab.colTimePoint = now).unconditionally()); db(update(tab).set(tab.colDayPoint = today, tab.colTimePoint = now).unconditionally());