diff options
Diffstat (limited to 'odb-tests/common/const-object')
-rw-r--r-- | odb-tests/common/const-object/buildfile | 41 | ||||
-rw-r--r-- | odb-tests/common/const-object/driver.cxx | 216 | ||||
-rw-r--r-- | odb-tests/common/const-object/test.hxx | 51 | ||||
-rw-r--r-- | odb-tests/common/const-object/testscript | 33 |
4 files changed, 341 insertions, 0 deletions
diff --git a/odb-tests/common/const-object/buildfile b/odb-tests/common/const-object/buildfile new file mode 100644 index 0000000..853c831 --- /dev/null +++ b/odb-tests/common/const-object/buildfile @@ -0,0 +1,41 @@ +# file : common/const-object/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 consto_ \ + --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/const-object/driver.cxx b/odb-tests/common/const-object/driver.cxx new file mode 100644 index 0000000..7ef48ee --- /dev/null +++ b/odb-tests/common/const-object/driver.cxx @@ -0,0 +1,216 @@ +// file : common/const-object/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test database operations with const objects. +// + +#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)); + + aggr a (1); + aggr ca_ (2); // o1 and o2 are NULL + const aggr& ca (ca_); + + obj1* o1 (new obj1 (1)); + obj1* co1_ (new obj1 (2)); + const obj1* co1 (co1_); + a.o1 = co1; + + unique_ptr<obj2> o2 (new obj2 (1)); + obj2* co2_ (new obj2 (2)); + a.o2.reset (co2_); + unique_ptr<const obj2>& co2 (a.o2); + + // persist via references + // + { + transaction t (db->begin ()); + db->persist (*o1); + db->persist (*co1); + db->persist (*o2); + db->persist (*co2); + db->persist (a); + db->persist (ca); + t.commit (); + } + + // persist via pointers + // + o1->id += 2; + co1_->id += 2; + o2->id += 2; + co2_->id += 2; + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (co1); + db->persist (o2); + db->persist (co2); + t.commit (); + } + + // load & compare + // + { + transaction t (db->begin ()); + + unique_ptr<aggr> a (db->load<aggr> (1)); + unique_ptr<const aggr> ca (db->load<aggr> (2)); + + t.commit (); + + assert (a->o1->id == 2); + assert (a->o2->id == 2); + + assert (ca->o1 == 0); + assert (ca->o2.get () == 0); + } + + // update via references + // + { + transaction t (db->begin ()); + db->update (*o1); + db->update (*co1); + db->update (*o2); + db->update (*co2); + db->update (a); + db->update (ca); + t.commit (); + } + + // update via pointers + // + { + transaction t (db->begin ()); + db->update (o1); + db->update (co1); + db->update (o2); + db->update (co2); + t.commit (); + } + + // query + // + typedef odb::query<obj1> query1; + typedef odb::query<obj2> query2; + + typedef odb::result<const obj1> result1; + typedef odb::result<const obj2> result2; + + { + transaction t (db->begin ()); + result1 r1 (db->query<obj1> (query1::id < 3)); + // odb::result<obj1> ur (r1); // error + size_t n1 (0); + + for (result1::iterator i (r1.begin ()); i != r1.end (); ++i) + { + // i->f (); // error + i->cf (); + // obj1* p (i.load ()); // error + const obj1* p (i.load ()); + obj1 o (0); + i.load (o); + assert (p->id == o.id); + delete p; + n1++; + } + + assert (n1 == 2); + + result2 r2 (db->query<obj2> (query2::id < 3)); + size_t n2 (0); + + for (result2::iterator i (r2.begin ()); i != r2.end (); ++i) + { + // i->f (); // error + i->cf (); + //unique_ptr<obj2> p (i.load ()); // error + unique_ptr<const obj2> p (i.load ()); + obj2 o (0); + i.load (o); + assert (p->id == o.id); + n2++; + } + + assert (n2 == 2); + + t.commit (); + } + + // erase via references + // + { + transaction t (db->begin ()); + db->erase (*o1); + db->erase (*co1); + db->erase (*o2); + db->erase (*co2); + db->erase (a); + db->erase (ca); + t.commit (); + } + + // erase via pointers + // + o1->id -= 2; + co1_->id -= 2; + o2->id -= 2; + co2_->id -= 2; + + { + transaction t (db->begin ()); + db->erase (o1); + db->erase (co1); + db->erase (o2); + db->erase (co2); + t.commit (); + } + + // Test session and const/non-const object handling + // + { + session s; + transaction t (db->begin ()); + + obj1 o1 (1); + const obj1& co1 (o1); + db->persist (co1); + + assert (db->load<obj1> (1) == &o1); + + t.commit (); + } + + delete o1; + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/const-object/test.hxx b/odb-tests/common/const-object/test.hxx new file mode 100644 index 0000000..4e66231 --- /dev/null +++ b/odb-tests/common/const-object/test.hxx @@ -0,0 +1,51 @@ +// file : common/const-object/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <memory> +#include <odb/core.hxx> + +#pragma db object pointer (obj1*) session +struct obj1 +{ + obj1 () {} + obj1 (int i): id (i) {} + + #pragma db id + int id; + + void f () {} + void cf () const {} +}; + +#pragma db object pointer (std::unique_ptr<obj2>) +struct obj2 +{ + obj2 () {} + obj2 (int i): id (i) {} + + #pragma db id + int id; + + void f () {} + void cf () const {} +}; + +#pragma db object +struct aggr +{ + aggr (int i): id (i), o1 (0) {} + aggr (): o1 (0) {} + ~aggr () {delete o1;} + + #pragma db id + int id; + + const obj1* o1; + + std::unique_ptr<const obj2> o2; +}; + +#endif // TEST_HXX diff --git a/odb-tests/common/const-object/testscript b/odb-tests/common/const-object/testscript new file mode 100644 index 0000000..3885e96 --- /dev/null +++ b/odb-tests/common/const-object/testscript @@ -0,0 +1,33 @@ +# file : common/const-object/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; + $* +} |