diff options
Diffstat (limited to 'odb-tests/common/relationship/on-delete')
-rw-r--r-- | odb-tests/common/relationship/on-delete/buildfile | 43 | ||||
-rw-r--r-- | odb-tests/common/relationship/on-delete/driver.cxx | 82 | ||||
-rw-r--r-- | odb-tests/common/relationship/on-delete/test.hxx | 58 | ||||
-rw-r--r-- | odb-tests/common/relationship/on-delete/testscript | 33 |
4 files changed, 216 insertions, 0 deletions
diff --git a/odb-tests/common/relationship/on-delete/buildfile b/odb-tests/common/relationship/on-delete/buildfile new file mode 100644 index 0000000..965a2eb --- /dev/null +++ b/odb-tests/common/relationship/on-delete/buildfile @@ -0,0 +1,43 @@ +# file : common/relationship/on-delete/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_rel_on_d_ \ + --generate-schema \ + --fkeys-deferrable-mode mysql:not_deferrable \ + --fkeys-deferrable-mode mssql:not_deferrable + + +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/relationship/on-delete/driver.cxx b/odb-tests/common/relationship/on-delete/driver.cxx new file mode 100644 index 0000000..eec57cf --- /dev/null +++ b/odb-tests/common/relationship/on-delete/driver.cxx @@ -0,0 +1,82 @@ +// file : common/relationship/on-delete/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test ON DELETE functionality. +// + +#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 o; + + cascade c; + c.p = &o; + + cascade_cont cc; + cc.p.push_back (&o); + + set_null n; + n.p = &o; + + set_null_cont nc; + nc.p.push_back (&o); + + { + transaction t (db->begin ()); + db->persist (o); + db->persist (c); + db->persist (cc); + db->persist (n); + db->persist (nc); + t.commit (); + } + + { + transaction t (db->begin ()); + db->erase (o); + t.commit (); + } + + { + transaction t (db->begin ()); + assert (db->find<cascade> (c.id) == 0); + + unique_ptr<cascade_cont> pcc (db->load<cascade_cont> (cc.id)); + assert (pcc->p.empty ()); + + unique_ptr<set_null> pn (db->load<set_null> (n.id)); + assert (pn->p == 0); + + unique_ptr<set_null_cont> pnc (db->load<set_null_cont> (nc.id)); + assert (pnc->p.size () == 1 && pnc->p[0] == 0); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/relationship/on-delete/test.hxx b/odb-tests/common/relationship/on-delete/test.hxx new file mode 100644 index 0000000..841acd9 --- /dev/null +++ b/odb-tests/common/relationship/on-delete/test.hxx @@ -0,0 +1,58 @@ +// file : common/relationship/on-delete/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <vector> + +#include <odb/core.hxx> + +#pragma db object +struct object +{ + #pragma db id auto + unsigned long id; +}; + +#pragma db object +struct cascade +{ + #pragma db id auto + unsigned long id; + + #pragma db on_delete(cascade) + object* p; +}; + +#pragma db object +struct cascade_cont +{ + #pragma db id auto + unsigned long id; + + #pragma db on_delete(cascade) + std::vector<object*> p; +}; + +#pragma db object +struct set_null +{ + #pragma db id auto + unsigned long id; + + #pragma db on_delete(set_null) + object* p; +}; + +#pragma db object +struct set_null_cont +{ + #pragma db id auto + unsigned long id; + + #pragma db on_delete(set_null) + std::vector<object*> p; +}; + +#endif // TEST_HXX diff --git a/odb-tests/common/relationship/on-delete/testscript b/odb-tests/common/relationship/on-delete/testscript new file mode 100644 index 0000000..8288ebc --- /dev/null +++ b/odb-tests/common/relationship/on-delete/testscript @@ -0,0 +1,33 @@ +# file : common/relationship/on-delete/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; + $* +} |