diff options
author | Constantin Michael <constantin@codesynthesis.com> | 2011-04-07 12:54:04 +0200 |
---|---|---|
committer | Constantin Michael <constantin@codesynthesis.com> | 2011-04-22 18:52:23 +0200 |
commit | 26f3245189b3f5d4a96587953ac1d33f45465f1d (patch) | |
tree | 3e5d0d7c6bad9213df99682ac415efaea8a55603 /qt/mysql | |
parent | 75f8ef2dcddea1260a31491c97a26d30a18fce6c (diff) |
Add qt/basic and qt/date-time MySQL tests
Diffstat (limited to 'qt/mysql')
-rw-r--r-- | qt/mysql/basic/driver.cxx (renamed from qt/mysql/core/driver.cxx) | 18 | ||||
-rw-r--r-- | qt/mysql/basic/makefile (renamed from qt/mysql/core/makefile) | 20 | ||||
-rw-r--r-- | qt/mysql/basic/test.hxx | 29 | ||||
-rw-r--r-- | qt/mysql/basic/test.std (renamed from qt/mysql/core/test.std) | 0 | ||||
-rw-r--r-- | qt/mysql/core/test.hxx | 40 | ||||
-rw-r--r-- | qt/mysql/date-time/driver.cxx | 153 | ||||
-rw-r--r-- | qt/mysql/date-time/makefile | 116 | ||||
-rw-r--r-- | qt/mysql/date-time/test.hxx | 55 | ||||
-rw-r--r-- | qt/mysql/date-time/test.std | 0 | ||||
-rw-r--r-- | qt/mysql/makefile | 5 | ||||
-rw-r--r-- | qt/mysql/template/Makefile.am | 1 | ||||
-rw-r--r-- | qt/mysql/template/makefile | 6 |
12 files changed, 380 insertions, 63 deletions
diff --git a/qt/mysql/core/driver.cxx b/qt/mysql/basic/driver.cxx index 3c693a0..ee1f69d 100644 --- a/qt/mysql/core/driver.cxx +++ b/qt/mysql/basic/driver.cxx @@ -1,9 +1,9 @@ -// file : qt/mysql/core/driver.cxx +// file : qt/mysql/basic/driver.cxx // author : Constantin Michael <constantin@codesynthesis.com> // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file -// Test Qt core type persistence. MySQL version. +// Test Qt basic type persistence. MySQL version. // #include <memory> // std::auto_ptr @@ -28,26 +28,26 @@ main (int argc, char* argv[]) { auto_ptr<database> db (create_database (argc, argv)); - person p1; - p1.name = "Constantin Michael"; - p1.date_of_birth.setDate (1979, 03, 07); + object o; + o.str = "Constantin Michael"; + o.blob = QByteArray ("\0x13\0xDE\0x00\0x00\0x00\0x54\0xF2\0x6A", 8); // Persist. // { transaction t (db->begin ()); - db->persist (p1); + db->persist (o); t.commit (); } - // Load. + // Load.dob // { transaction t (db->begin ()); - person* pl = db->load<person> (p1.name); + object* ol = db->load<object> (o.str); t.commit (); - assert (*pl == p1); + assert (*ol == o); } } catch (const odb::exception& e) diff --git a/qt/mysql/core/makefile b/qt/mysql/basic/makefile index 67f2e94..6d48f0c 100644 --- a/qt/mysql/core/makefile +++ b/qt/mysql/basic/makefile @@ -1,4 +1,4 @@ -# file : qt/mysql/core/makefile +# file : qt/mysql/date-time/makefile # author : Constantin Michael <constantin@codesynthesis.com> # copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC # license : GNU GPL v2; see accompanying LICENSE file @@ -26,27 +26,29 @@ $(call import,\ $(call import,\ $(scf_root)/import/libodb-qt/stub.make,\ - l: odb_qt.l,cpp-options: odb_qt.l.cpp-options) + l:odb_qt.l,cpp-options: odb_qt.l.cpp-options) $(call import,\ $(scf_root)/import/libqt/core/stub.make,\ - l: qt_core.l) + l: qt_core.l,cpp-options: qt_core.l.cpp-options) # Build. # $(driver): $(cxx_obj) $(odb_qt.l) $(common.l) $(qt_core.l) -$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -$(cxx_obj) $(cxx_od): $(common.l.cpp-options) $(odb_qt.l.cpp-options) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) $(odb_qt.l.cpp-options) \ +$(qt_core.l.cpp-options) genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) gen := $(addprefix $(out_base)/,$(genf)) $(gen): $(odb) $(gen): odb := $(odb) -$(gen) $(dist): export odb_options += --database mysql --profile qt \ +$(gen) $(dist): export odb_options += --database mysql --profile qt/basic \ --generate-schema -$(gen): cpp_options := -I$(out_base) -$(gen): $(common.l.cpp-options) $(odb_qt.l.cpp-options) +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) $(odb_qt.l.cpp-options) \ +$(qt_core.l.cpp-options) $(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) @@ -71,7 +73,7 @@ $(dist): # Test. # $(test): $(driver) $(src_base)/test.std - $(call message,sql $$1,$(dcf_root)/db-driver $$1, $(src_base)/test.sql) + $(call schema) $(call message,test $<,$< --options-file $(dcf_root)/db.options \ >$(out_base)/test.out) $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) diff --git a/qt/mysql/basic/test.hxx b/qt/mysql/basic/test.hxx new file mode 100644 index 0000000..1e1f9e6 --- /dev/null +++ b/qt/mysql/basic/test.hxx @@ -0,0 +1,29 @@ +// file : qt/mysql/basic/test.hxx +// author : Constantin Michael <constantin@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <QString> +#include <QByteArray> + +#pragma db object +struct object +{ + bool + operator== (const object& x) const + { + return + str == x.str && + blob == x.blob; + } + + #pragma db id + QString str; + + QByteArray blob; +}; + +#endif // TEST_HXX diff --git a/qt/mysql/core/test.std b/qt/mysql/basic/test.std index e69de29..e69de29 100644 --- a/qt/mysql/core/test.std +++ b/qt/mysql/basic/test.std diff --git a/qt/mysql/core/test.hxx b/qt/mysql/core/test.hxx deleted file mode 100644 index cc8e02d..0000000 --- a/qt/mysql/core/test.hxx +++ /dev/null @@ -1,40 +0,0 @@ -// file : qt/mysql/core/test.hxx -// author : Constantin Michael <constantin@codesynthesis.com> -// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST_HXX -#define TEST_HXX - -#include <QString> -#include <QDateTime> - -#pragma db object -struct time_punchcard -{ - #pragma db id auto - unsigned long id; - - QDateTime date_time; -}; - -#pragma db object -struct person -{ - bool - operator== (const person& x) const - { - return name == x.name && - date_of_birth == x.date_of_birth && - time_of_birth == x.time_of_birth; - } - - #pragma db id - QString name; - - #pragma db type("DATE NOT NULL") - QDate date_of_birth; - QTime time_of_birth; -}; - -#endif // TEST_HXX diff --git a/qt/mysql/date-time/driver.cxx b/qt/mysql/date-time/driver.cxx new file mode 100644 index 0000000..ae0643b --- /dev/null +++ b/qt/mysql/date-time/driver.cxx @@ -0,0 +1,153 @@ +// file : qt/mysql/date-time/driver.cxx +// author : Constantin Michael <constantin@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test Qt date/time type persistence. MySQL version. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <QDateTime> + +#include <odb/mysql/database.hxx> +#include <odb/mysql/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[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + // + // Check valid dates and times. + // + + object o; + + // Check persistence of null values. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> ol (db->load<object> (o.id)); + t.commit (); + + assert (ol->is_null ()); + } + + // Check persistence of valid dates and times. + // + + // Create a QDateTime containing the current date and time + // but with the milliseconds zeroed. MySQL does not currently + // support millisecond times. + // + QDateTime t (QDateTime::currentDateTime ()); + + t.setTime (QTime (t.time ().hour (), + t.time ().minute (), + t.time ().second ())); + + o.date = t.date (); + o.date_time = t; + o.timestamp = t; + o.time = t.time (); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> ol (db->load<object> (o.id)); + t.commit (); + + assert (*ol == o); + } + + // + // Check invalid dates and times. + // + + { + // Test out of range dates. + // + object or1, or2; + or1.date = QDate (999, 12, 31); + or2.date = QDate (10000, 1, 1); + + transaction t (db->begin ()); + assert (test_out_of_range_value (or1, *db)); + assert (test_out_of_range_value (or2, *db)); + t.commit (); + } + + { + // Test out of range date-times. + // + object or1, or2; + or1.date_time = QDateTime (QDate (999, 12, 31), QTime (23, 59, 59)); + or2.date_time = QDateTime (QDate (10000, 1, 1)); + + transaction t (db->begin ()); + assert (test_out_of_range_value (or1, *db)); + assert (test_out_of_range_value (or2, *db)); + t.commit (); + } + + { + // Test out of range timestamps. + // + object or1, or2; + or1.timestamp = QDateTime (QDate (1970, 1, 1)); + or2.timestamp = QDateTime (QDate (2038, 1, 19), QTime (3, 14, 8)); + + transaction t (db->begin ()); + assert (test_out_of_range_value (or1, *db)); + assert (test_out_of_range_value (or2, *db)); + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} + +bool +test_out_of_range_value (object& x, database& db) +{ + try + { + db.persist (x); + return false; + } + catch (const odb::qt::date_time::value_out_of_range&) + { + } + + return true; +} diff --git a/qt/mysql/date-time/makefile b/qt/mysql/date-time/makefile new file mode 100644 index 0000000..ca8e79c --- /dev/null +++ b/qt/mysql/date-time/makefile @@ -0,0 +1,116 @@ +# file : qt/mysql/date-time/makefile +# author : Constantin Michael <constantin@codesynthesis.com> +# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +$(call import,\ + $(scf_root)/import/libodb-qt/stub.make,\ + l: odb_qt.l,cpp-options: odb_qt.l.cpp-options) + +$(call import,\ + $(scf_root)/import/libqt/core/stub.make,\ + l: qt_core.l,cpp-options: qt_core.l.cpp-options) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) $(odb_qt.l) $(qt_core.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) $(odb_qt.l.cpp-options) \ +$(qt_core.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database mysql --profile qt/date-time \ +--generate-schema +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) $(odb_qt.l.cpp-options) \ +$(qt_core.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(subst /,-,$(subst $(src_root)/qt/mysql/,,$(src_base))) +$(dist): export extra_dist := $(data_dist) $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(odb_rules)) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/qt/mysql/date-time/test.hxx b/qt/mysql/date-time/test.hxx new file mode 100644 index 0000000..9eb328e --- /dev/null +++ b/qt/mysql/date-time/test.hxx @@ -0,0 +1,55 @@ +// file : qt/mysql/date-time/test.hxx +// author : Constantin Michael <constantin@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <vector> + +#include <QDateTime> + +#include <odb/core.hxx> + +#pragma db object +struct object +{ + bool + operator== (const object& x) const + { + return + id == x.id && + date == x.date && + date_time == x.date_time && + timestamp == x.timestamp && + time == x.time; + } + + bool + is_null () const + { + return + date.isNull () && + date_time.isNull () && + timestamp.isNull () && + time.isNull (); + } + + #pragma db id auto + unsigned long id; + + QDate date; + QDateTime date_time; + + // Specify NULL explicitly to suppress auto-initialization and + // auto-update characteristics of TIMESTAMP datatype, and to allow + // NULL values. + // + #pragma db type("TIMESTAMP NULL") + QDateTime timestamp; + + QTime time; +}; + +#endif // TEST_HXX diff --git a/qt/mysql/date-time/test.std b/qt/mysql/date-time/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/qt/mysql/date-time/test.std diff --git a/qt/mysql/makefile b/qt/mysql/makefile index 5c7fffc..c87e1ea 100644 --- a/qt/mysql/makefile +++ b/qt/mysql/makefile @@ -5,8 +5,9 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make -tests := \ -core \ +tests := \ +basic \ +date-time \ template default := $(out_base)/ diff --git a/qt/mysql/template/Makefile.am b/qt/mysql/template/Makefile.am index 1948cb1..1239fa7 100644 --- a/qt/mysql/template/Makefile.am +++ b/qt/mysql/template/Makefile.am @@ -9,6 +9,7 @@ noinst_PROGRAMS = driver driver_SOURCES = driver.cxx __path__(extra_sources) __path__(extra_headers) LDADD = $(top_builddir)/libcommon/common/libcommon.la AM_CPPFLAGS = -I'$(top_builddir)/libcommon' -I'$(top_srcdir)/libcommon' +AM_CPPFLAGS += -I'$(builddir)' -I'$(srcdir)' TESTS=$(top_builddir)/tester TESTS_ENVIRONMENT=top_builddir=$(top_builddir); export top_builddir; diff --git a/qt/mysql/template/makefile b/qt/mysql/template/makefile index fbe0460..a76f0cc 100644 --- a/qt/mysql/template/makefile +++ b/qt/mysql/template/makefile @@ -31,7 +31,7 @@ $(call import,\ #Build. # $(driver): $(cxx_obj) $(odb_qt.l) $(common.l) -$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) $(cxx_obj) $(cxx_od): $(common.l.cpp-options) $(odb_qt.l.cpp-options) genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) @@ -41,7 +41,7 @@ $(gen): $(odb) $(gen): odb := $(odb) $(gen) $(dist): export odb_options += --database mysql --profile qt \ --generate-schema -$(gen): cpp_options := -I$(out_base) +$(gen): cpp_options := -I$(src_base) $(gen): $(common.l.cpp-options) $(odb_qt.l.cpp-options) $(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) @@ -67,7 +67,7 @@ $(dist): # Test. # $(test): $(driver) $(src_base)/test.std - $(call message,sql $$1,$(dcf_root)/db-driver $$1, $(src_base)/test.sql) + $(call schema) $(call message,test $<,$< --options-file $(dcf_root)/db.options \ >$(out_base)/test.out) $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) |