diff options
Diffstat (limited to 'odb-tests/common/no-id')
-rw-r--r-- | odb-tests/common/no-id/buildfile | 41 | ||||
-rw-r--r-- | odb-tests/common/no-id/driver.cxx | 102 | ||||
-rw-r--r-- | odb-tests/common/no-id/test.hxx | 21 | ||||
-rw-r--r-- | odb-tests/common/no-id/testscript | 53 |
4 files changed, 217 insertions, 0 deletions
diff --git a/odb-tests/common/no-id/buildfile b/odb-tests/common/no-id/buildfile new file mode 100644 index 0000000..1a64401 --- /dev/null +++ b/odb-tests/common/no-id/buildfile @@ -0,0 +1,41 @@ +# file : common/no-id/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 no_id_ \ + --generate-schema \ + --generate-query + +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/no-id/driver.cxx b/odb-tests/common/no-id/driver.cxx new file mode 100644 index 0000000..eee69a5 --- /dev/null +++ b/odb-tests/common/no-id/driver.cxx @@ -0,0 +1,102 @@ +// file : common/no-id/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test persistent classes without id. +// + +#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)); + + object o1 (1, "aaa"); + object o2 (2, "bbb"); + object o3 (3, "ccc"); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o2); // Ok, since there is no id. + db->persist (o3); + t.commit (); + } + + // Compile errors. + // + { + //db->load<object> (1); + //db->find<object> (1); + //db->update (o1); + //db->erase<object> (1); + } + + typedef odb::query<object> query; + typedef odb::result<object> result; + + // Query. + // + { + transaction t (db->begin ()); + + { + result r (db->query<object> ()); + assert (size (r) == 4); + } + + { + result r (db->query<object> (query::str == "aaa")); + result::iterator i (r.begin ()); + assert (i != r.end ()); + assert (i->num == 1 && i->str == "aaa"); + object o; + i.load (o); + //i.id (); // Compile-time error. + assert (o.num == 1 && o.str == "aaa"); + assert (++i == r.end ()); + } + + { + result r (db->query<object> (query::num < 3)); + assert (size (r) == 3); + } + + t.commit (); + } + + // Erase (query). + // + { + transaction t (db->begin ()); + assert (db->erase_query<object> (query::num == 2) == 2); + assert (db->erase_query<object> () == 2); + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/no-id/test.hxx b/odb-tests/common/no-id/test.hxx new file mode 100644 index 0000000..c5b5c65 --- /dev/null +++ b/odb-tests/common/no-id/test.hxx @@ -0,0 +1,21 @@ +// file : common/no-id/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> + +#include <odb/core.hxx> + +#pragma db object no_id +struct object +{ + object () {} + object (unsigned long n, const std::string& s): num (n), str (s) {} + + unsigned long num; + std::string str; +}; + +#endif // TEST_HXX diff --git a/odb-tests/common/no-id/testscript b/odb-tests/common/no-id/testscript new file mode 100644 index 0000000..96b8926 --- /dev/null +++ b/odb-tests/common/no-id/testscript @@ -0,0 +1,53 @@ +# file : common/no-id/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; + $* +} |