diff options
Diffstat (limited to 'odb-tests/qt/sqlite/date-time')
-rw-r--r-- | odb-tests/qt/sqlite/date-time/buildfile | 39 | ||||
-rw-r--r-- | odb-tests/qt/sqlite/date-time/driver.cxx | 142 | ||||
-rw-r--r-- | odb-tests/qt/sqlite/date-time/test.hxx | 60 | ||||
-rw-r--r-- | odb-tests/qt/sqlite/date-time/testscript | 9 |
4 files changed, 250 insertions, 0 deletions
diff --git a/odb-tests/qt/sqlite/date-time/buildfile b/odb-tests/qt/sqlite/date-time/buildfile new file mode 100644 index 0000000..98ad822 --- /dev/null +++ b/odb-tests/qt/sqlite/date-time/buildfile @@ -0,0 +1,39 @@ +# file : qt/sqlite/date-time/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($qt) \ + "Qt version should be configured for this test via config.odb_tests.qt variable" + + assert ($sqlite) "sqlite should be configured for this test" + assert (!$multi) "multi-database mode is not supported by this test" +} + +import meta_libs = libodb%lib{odb} +import meta_libs += libodb-qt%lib{odb-qt} +import meta_libs += "libQt$(qt_ver)Core"%lib{"Qt$(qt_ver)Core"} + +import libs = libodb-sqlite%lib{odb-sqlite} +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -test-odb} {hxx ixx cxx}{test-odb} testscript + +# Introduce the metadata library target to make sure the libodb library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $meta_libs + +<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta} + +exe{driver}: libue{test-meta} $libs + +# Specify the ODB custom options to be used by the odb_compile ad hoc rule +# (see build/root.build for details). +# +odb_options = --std ($qt_ver == 5 ? c++11 : c++17) \ + --table-prefix qt_sqlite_dt_ \ + --profile qt/date-time \ + --generate-schema + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/qt/sqlite/date-time/driver.cxx b/odb-tests/qt/sqlite/date-time/driver.cxx new file mode 100644 index 0000000..2c8d1ea --- /dev/null +++ b/odb-tests/qt/sqlite/date-time/driver.cxx @@ -0,0 +1,142 @@ +// file : qt/sqlite/date-time/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test Qt date/time type persistence. SQLite version. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <QtCore/QDateTime> +#include <QtCore/QCoreApplication> + +#include <odb/sqlite/database.hxx> +#include <odb/sqlite/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +using namespace odb::core; + +bool +test_out_of_range_value (object&, database&); + +int +main (int argc, char* argv[]) +{ + QCoreApplication app (argc, argv); + + try + { + unique_ptr<database> db (create_database (argc, argv)); + + // Check persistence of null values. + // + object o1; + { + transaction t (db->begin ()); + db->persist (o1); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object> ol1 (db->load<object> (o1.id)); + t.commit (); + + assert (ol1->is_null ()); + } + + QDateTime ct (QDateTime::currentDateTime ()); + QDateTime ct_no_ms = QDateTime (QDate (ct.date ().year (), + ct.date ().month (), + ct.date ().day ()), + QTime (ct.time ().hour (), + ct.time ().minute (), + ct.time ().second ())); + + // Check persistence of valid dates and times. + // + object o2; + { + o2.date = ct.date (); + o2.unix_day = ct.date (); + o2.time = ct.time (); + + // QTime stored as SQLite INTEGER has second resolution. + // The millsecond part is zeroed to avoid false negatives. + // + o2.seconds = ct_no_ms.time (); + + o2.date_time = ct; + + // UNIX time has second reolution. The millsecond part is + // zeroed to avoid false negatives. + // + o2.unix_time = ct_no_ms; + + transaction t (db->begin ()); + db->persist (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object> ol2 (db->load<object> (o2.id)); + t.commit (); + + assert (*ol2 == o2); + } + + // Test out of range values for QDateTime traits UNIX time + // implementation. + // + { + object o; + o.unix_time = QDateTime (QDate (1969, 12, 31), + QTime (23, 59, 59), + Qt::UTC); + + assert (test_out_of_range_value (o, *db)); + } + + // Test out of range values for QDate traits UNIX time + // implementation. + // + { + object o; + o.unix_day = QDate (QDate (1969, 12, 31)); + + assert (test_out_of_range_value (o, *db)); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} + +bool +test_out_of_range_value (object& x, database& db) +{ + try + { + transaction t (db.begin ()); + db.persist (x); + t.rollback (); + + return false; + } + catch (const odb::qt::date_time::value_out_of_range&) + { + } + + return true; +} diff --git a/odb-tests/qt/sqlite/date-time/test.hxx b/odb-tests/qt/sqlite/date-time/test.hxx new file mode 100644 index 0000000..9795846 --- /dev/null +++ b/odb-tests/qt/sqlite/date-time/test.hxx @@ -0,0 +1,60 @@ +// file : qt/sqlite/date-time/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <vector> + +#include <QtCore/QDateTime> + +#include <odb/core.hxx> + +#pragma db object +struct object +{ + bool + operator== (const object& x) const + { + return + id == x.id && + date == x.date && + unix_day == x.unix_day && + time == x.time && + seconds == x.seconds && + date_time == x.date_time && + unix_time == x.unix_time; + } + + bool + is_null () const + { + return + date.isNull () && + unix_day.isNull () && + time.isNull () && + seconds.isNull () && + date_time.isNull () && + unix_time.isNull (); + } + + #pragma db id auto + unsigned long id; + + QDate date; + + #pragma db type ("INTEGER") + QDate unix_day; + + QTime time; + + #pragma db type ("INTEGER") + QTime seconds; + + QDateTime date_time; + + #pragma db type ("INTEGER") + QDateTime unix_time; +}; + +#endif // TEST_HXX diff --git a/odb-tests/qt/sqlite/date-time/testscript b/odb-tests/qt/sqlite/date-time/testscript new file mode 100644 index 0000000..90bf150 --- /dev/null +++ b/odb-tests/qt/sqlite/date-time/testscript @@ -0,0 +1,9 @@ +# file : qt/sqlite/basic/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../../database-options.testscript +.include ../../../sqlite.testscript + +: basics +: +$* |