diff options
Diffstat (limited to 'odb-tests/qt/sqlite/date-time')
-rw-r--r-- | odb-tests/qt/sqlite/date-time/driver.cxx | 140 | ||||
-rw-r--r-- | odb-tests/qt/sqlite/date-time/test.hxx | 60 |
2 files changed, 200 insertions, 0 deletions
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..adf7889 --- /dev/null +++ b/odb-tests/qt/sqlite/date-time/driver.cxx @@ -0,0 +1,140 @@ +// 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::auto_ptr +#include <cassert> +#include <iostream> + +#include <QtCore/QDateTime> +#include <QtCore/QCoreApplication> + +#include <odb/sqlite/database.hxx> +#include <odb/sqlite/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +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 + { + auto_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 ()); + auto_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 ()); + auto_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 |