summaryrefslogtreecommitdiff
path: root/odb-tests/common/session/cache
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/common/session/cache')
-rw-r--r--odb-tests/common/session/cache/buildfile41
-rw-r--r--odb-tests/common/session/cache/driver.cxx83
-rw-r--r--odb-tests/common/session/cache/test.hxx50
-rw-r--r--odb-tests/common/session/cache/testscript33
4 files changed, 207 insertions, 0 deletions
diff --git a/odb-tests/common/session/cache/buildfile b/odb-tests/common/session/cache/buildfile
new file mode 100644
index 0000000..6d5b0bc
--- /dev/null
+++ b/odb-tests/common/session/cache/buildfile
@@ -0,0 +1,41 @@
+# file : common/session/cache/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 session_cache_ \
+ --generate-schema \
+ --generate-session
+
+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/session/cache/driver.cxx b/odb-tests/common/session/cache/driver.cxx
new file mode 100644
index 0000000..4b4ea12
--- /dev/null
+++ b/odb-tests/common/session/cache/driver.cxx
@@ -0,0 +1,83 @@
+// file : common/session/cache/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test session object cache.
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/session.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));
+
+ // Test the session_required exception.
+ //
+ {
+ using namespace test1;
+
+ shared_ptr<obj1> o1a (new obj1 (1));
+ shared_ptr<obj1> o1b (new obj1 (2));
+ shared_ptr<obj2> o2 (new obj2 (1));
+
+ o1a->o2 = o2;
+ o1b->o2 = o2;
+
+ o2->o1.push_back (o1a);
+ o2->o1.push_back (o1b);
+
+ {
+ transaction t (db->begin ());
+ db->persist (o1a);
+ db->persist (o1b);
+ db->persist (o2);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+
+ try
+ {
+ shared_ptr<obj1> o1 (db->load<obj1> (1));
+ assert (false);
+ }
+ catch (const session_required&)
+ {
+ }
+
+ t.commit ();
+ }
+
+ {
+ session s;
+ transaction t (db->begin ());
+ shared_ptr<obj1> o1 (db->load<obj1> (1));
+ t.commit ();
+ }
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/common/session/cache/test.hxx b/odb-tests/common/session/cache/test.hxx
new file mode 100644
index 0000000..d2b1b2b
--- /dev/null
+++ b/odb-tests/common/session/cache/test.hxx
@@ -0,0 +1,50 @@
+// file : common/session/cache/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <memory>
+#include <vector>
+
+#include <odb/core.hxx>
+
+// Test the session_required exception.
+//
+#pragma db namespace table("t1_")
+namespace test1
+{
+ using std::shared_ptr;
+ using std::weak_ptr;
+
+ #pragma db namespace(test1) pointer(shared_ptr)
+
+ struct obj2;
+
+ #pragma db object
+ struct obj1
+ {
+ obj1 () {}
+ obj1 (unsigned long id): id_ (id) {}
+
+ #pragma db id
+ unsigned long id_;
+
+ shared_ptr<obj2> o2;
+ };
+
+ #pragma db object
+ struct obj2
+ {
+ obj2 () {}
+ obj2 (unsigned long id): id_ (id) {}
+
+ #pragma db id
+ unsigned long id_;
+
+ #pragma db inverse (o2)
+ std::vector< weak_ptr<obj1> > o1;
+ };
+}
+
+#endif // TEST_HXX
diff --git a/odb-tests/common/session/cache/testscript b/odb-tests/common/session/cache/testscript
new file mode 100644
index 0000000..6d013eb
--- /dev/null
+++ b/odb-tests/common/session/cache/testscript
@@ -0,0 +1,33 @@
+# file : common/session/cache/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;
+ $*
+}