summaryrefslogtreecommitdiff
path: root/odb-tests/common/query/one
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/common/query/one')
-rw-r--r--odb-tests/common/query/one/buildfile42
-rw-r--r--odb-tests/common/query/one/driver.cxx205
-rw-r--r--odb-tests/common/query/one/test.hxx26
-rw-r--r--odb-tests/common/query/one/testscript53
4 files changed, 326 insertions, 0 deletions
diff --git a/odb-tests/common/query/one/buildfile b/odb-tests/common/query/one/buildfile
new file mode 100644
index 0000000..76a36b0
--- /dev/null
+++ b/odb-tests/common/query/one/buildfile
@@ -0,0 +1,42 @@
+# file : common/query/one/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+import libodb = libodb%lib{odb}
+
+libs =
+
+for db: $databases
+ import libs += libodb-$db%lib{odb-$db}
+
+import libs += lib{common}
+
+exe{driver}: {hxx cxx}{* -*-odb -*-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}: $libodb
+
+<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta}
+
+for db: $databases
+{
+ exe{driver}: {hxx ixx cxx}{test-odb-$db}: include = $multi
+ <{hxx ixx cxx}{test-odb-$db}>: 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 = --table-prefix t_query_one_ \
+ --generate-schema \
+ --generate-query \
+ --generate-prepared
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
+
+# Testscript's run-time prerequisites.
+#
+exe{driver}: ../../../alias{database-client}: include = adhoc
diff --git a/odb-tests/common/query/one/driver.cxx b/odb-tests/common/query/one/driver.cxx
new file mode 100644
index 0000000..4c3dcdc
--- /dev/null
+++ b/odb-tests/common/query/one/driver.cxx
@@ -0,0 +1,205 @@
+// file : common/query/one/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test query one support.
+//
+// We assume that other tests in common/query/ exercise a variety of
+// different kinds of queries. Here we are concerned with what is
+// specific to query_one() and query_value().
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <libcommon/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+#undef NDEBUG
+#include <cassert>
+
+using namespace std;
+using namespace odb::core;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv));
+ odb::database_id db_id (db->id ());
+
+ transaction t (db->begin ());
+
+ // query_one()
+ //
+ {
+ unique_ptr<object> o (db->query_one<object> ());
+ assert (o.get () == 0);
+ }
+
+ {
+ object o (4);
+ assert (!db->query_one<object> (o) && o.id_ == 4 && o.str_.empty ());
+ }
+
+ /*
+ {
+ object o (db->query_value<object> ());
+ assert (false);
+ }
+ */
+
+ object o (1);
+ o.str_ = "value 1";
+ db->persist (o);
+
+ {
+ unique_ptr<object> o (db->query_one<object> ());
+ assert (o.get () != 0 && o->str_ == "value 1");
+ }
+
+ {
+ object o;
+ assert (db->query_one<object> (o) && o.str_ == "value 1");
+ }
+
+ {
+ object o (db->query_value<object> ());
+ assert (o.str_ == "value 1");
+ }
+
+ // query_one(const char*)
+ //
+ const char* q1_c (db_id == odb::id_oracle ? "\"id\" = 1" : "id = 1");
+ const char* q0_c (db_id == odb::id_oracle ? "\"id\" = 2" : "id = 2");
+
+ {
+ unique_ptr<object> o (db->query_one<object> (q1_c));
+ assert (o.get () != 0 && o->str_ == "value 1");
+ }
+
+ {
+ unique_ptr<object> o (db->query_one<object> (q0_c));
+ assert (o.get () == 0);
+ }
+
+ {
+ object o;
+ assert (db->query_one<object> (q1_c, o) && o.str_ == "value 1");
+ }
+
+ {
+ object o (4);
+ assert (!db->query_one<object> (q0_c, o) &&
+ o.id_ == 4 && o.str_.empty ());
+ }
+
+ {
+ object o (db->query_value<object> (q1_c));
+ assert (o.str_ == "value 1");
+ }
+
+ // query_one(std::string)
+ //
+ string q1_s (q1_c);
+ string q0_s (q0_c);
+
+ {
+ unique_ptr<object> o (db->query_one<object> (q1_s));
+ assert (o.get () != 0 && o->str_ == "value 1");
+ }
+
+ {
+ unique_ptr<object> o (db->query_one<object> (q0_s));
+ assert (o.get () == 0);
+ }
+
+ {
+ object o;
+ assert (db->query_one<object> (q1_s, o) && o.str_ == "value 1");
+ }
+
+ {
+ object o (4);
+ assert (!db->query_one<object> (q0_s, o) &&
+ o.id_ == 4 && o.str_.empty ());
+ }
+
+ {
+ object o (db->query_value<object> (q1_s));
+ assert (o.str_ == "value 1");
+ }
+
+ // query_one(odb::query)
+ //
+ typedef odb::query<object> query;
+
+ query q1 (query::id == 1);
+ query q0 (query::id == 2);
+
+ {
+ unique_ptr<object> o (db->query_one<object> (q1));
+ assert (o.get () != 0 && o->str_ == "value 1");
+ }
+
+ {
+ unique_ptr<object> o (db->query_one<object> (q0));
+ assert (o.get () == 0);
+ }
+
+ {
+ object o;
+ assert (db->query_one<object> (q1, o) && o.str_ == "value 1");
+ }
+
+ {
+ object o (4);
+ assert (!db->query_one<object> (q0, o) && o.id_ == 4 && o.str_.empty ());
+ }
+
+ {
+ object o (db->query_value<object> (q1));
+ assert (o.str_ == "value 1");
+ }
+
+ // Assertion on more than one element.
+ //
+ {
+ object o (2);
+ o.str_ = "value 2";
+ db->persist (o);
+ }
+
+ /*
+ {
+ unique_ptr<object> o (db->query_one<object> ());
+ assert (false);
+ }
+ */
+
+ /*
+ {
+ object o;
+ db->query_one<object> (o);
+ assert (false);
+ }
+ */
+
+ /*
+ {
+ object o (db->query_value<object> ());
+ assert (false);
+ }
+ */
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/common/query/one/test.hxx b/odb-tests/common/query/one/test.hxx
new file mode 100644
index 0000000..3008063
--- /dev/null
+++ b/odb-tests/common/query/one/test.hxx
@@ -0,0 +1,26 @@
+// file : common/query/one/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <string>
+
+#pragma db object
+struct object
+{
+ object (unsigned long id)
+ : id_ (id)
+ {
+ }
+
+ object ()
+ {
+ }
+
+ #pragma db id
+ unsigned long id_;
+ std::string str_;
+};
+
+#endif // TEST_HXX
diff --git a/odb-tests/common/query/one/testscript b/odb-tests/common/query/one/testscript
new file mode 100644
index 0000000..94ebcde
--- /dev/null
+++ b/odb-tests/common/query/one/testscript
@@ -0,0 +1,53 @@
+# file : common/query/one/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../../database-options.testscript
+
+: mysql
+:
+if $mysql
+{
+ .include ../../../mysql.testscript
+
+ $create_schema;
+ $*
+}
+
+: sqlite
+:
+if $sqlite
+{
+ .include ../../../sqlite.testscript
+
+ $*
+}
+
+: pgsql
+:
+if $pgsql
+{
+ .include ../../../pgsql.testscript
+
+ $create_schema;
+ $*
+}
+
+: oracle
+:
+if $oracle
+{
+ .include ../../../oracle.testscript
+
+ $create_schema;
+ $*
+}
+
+: mssql
+:
+if $mssql
+{
+ .include ../../../mssql.testscript
+
+ $create_schema;
+ $*
+}