summaryrefslogtreecommitdiff
path: root/odb-tests/sqlite/auto
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/sqlite/auto')
-rw-r--r--odb-tests/sqlite/auto/buildfile32
-rw-r--r--odb-tests/sqlite/auto/driver.cxx76
-rw-r--r--odb-tests/sqlite/auto/test.hxx32
-rw-r--r--odb-tests/sqlite/auto/testscript9
4 files changed, 149 insertions, 0 deletions
diff --git a/odb-tests/sqlite/auto/buildfile b/odb-tests/sqlite/auto/buildfile
new file mode 100644
index 0000000..2c70b7c
--- /dev/null
+++ b/odb-tests/sqlite/auto/buildfile
@@ -0,0 +1,32 @@
+# file : sqlite/auto/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+assert ($sqlite && !$multi || $build.meta_operation == 'dist') \
+"sqlite should be configured via config.odb_tests.database variable as a single database"
+
+import libodb = libodb%lib{odb}
+
+import libs = libodb-sqlite%lib{odb-sqlite}
+import libs += lib{common}
+
+exe{driver}: {hxx cxx}{* -*-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}
+
+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 sqlitex_auto_ \
+ --generate-schema \
+ --default-database common \
+ --generate-query \
+ --sqlite-lax-auto-id
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
diff --git a/odb-tests/sqlite/auto/driver.cxx b/odb-tests/sqlite/auto/driver.cxx
new file mode 100644
index 0000000..bcd42c1
--- /dev/null
+++ b/odb-tests/sqlite/auto/driver.cxx
@@ -0,0 +1,76 @@
+// file : sqlite/auto/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test manual/automatic id assignment.
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#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;
+namespace sqlite = odb::sqlite;
+using namespace sqlite;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_specific_database<database> (argc, argv));
+
+ // object
+ //
+ {
+ unsigned long id1, id2, id3;
+ {
+ object o1 (1, "one");
+ object o2 ("two");
+ object o3 (3, "three");
+
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (o2);
+ db->persist (o3);
+ t.commit ();
+
+ id1 = *o1.id_;
+ id2 = *o2.id_;
+ id3 = *o3.id_;
+
+ assert (id1 == 1);
+ assert (id3 == 3);
+
+ assert (id2 != id1);
+ assert (id2 != id3);
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> o1 (db->load<object> (id1));
+ unique_ptr<object> o2 (db->load<object> (id2));
+ unique_ptr<object> o3 (db->load<object> (id3));
+ t.commit ();
+
+ assert (*o1->id_ == id1 && o1->str_ == "one");
+ assert (*o2->id_ == id2 && o2->str_ == "two");
+ assert (*o3->id_ == id3 && o3->str_ == "three");
+ }
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/sqlite/auto/test.hxx b/odb-tests/sqlite/auto/test.hxx
new file mode 100644
index 0000000..9905182
--- /dev/null
+++ b/odb-tests/sqlite/auto/test.hxx
@@ -0,0 +1,32 @@
+// file : sqlite/auto/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <odb/core.hxx>
+#include <odb/nullable.hxx>
+
+#include <string>
+
+#include <odb/core.hxx>
+
+#pragma db object
+struct object
+{
+ explicit
+ object (const std::string& str): str_ (str) {}
+ object (unsigned long id, const std::string& str): id_ (id), str_ (str) {}
+
+ #pragma db auto id
+ odb::nullable<unsigned long> id_;
+
+ std::string str_;
+
+private:
+ object () {}
+
+ friend class odb::access;
+};
+
+#endif // TEST_HXX
diff --git a/odb-tests/sqlite/auto/testscript b/odb-tests/sqlite/auto/testscript
new file mode 100644
index 0000000..a5a7da5
--- /dev/null
+++ b/odb-tests/sqlite/auto/testscript
@@ -0,0 +1,9 @@
+# file : sqlite/auto/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+.include ../../sqlite.testscript
+
+: basics
+:
+$*