diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2024-01-25 20:32:06 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2024-01-25 20:32:06 +0300 |
commit | 0d49ea1fe08cf1eab41a00149393a291c65a59d7 (patch) | |
tree | 0391eb09309ca95282e200516937e64d89f3e1bb /odb-tests/common/relationship/basics | |
parent | fc3fb39c90ab7fe5fccbe3f3bc0eb2645157bb96 (diff) |
Turn odb-tests repository into package for muti-package repositoryodb-tests
Diffstat (limited to 'odb-tests/common/relationship/basics')
-rw-r--r-- | odb-tests/common/relationship/basics/buildfile | 41 | ||||
-rw-r--r-- | odb-tests/common/relationship/basics/driver.cxx | 150 | ||||
-rw-r--r-- | odb-tests/common/relationship/basics/test.hxx | 260 | ||||
-rw-r--r-- | odb-tests/common/relationship/basics/testscript | 33 |
4 files changed, 484 insertions, 0 deletions
diff --git a/odb-tests/common/relationship/basics/buildfile b/odb-tests/common/relationship/basics/buildfile new file mode 100644 index 0000000..d7bbb7e --- /dev/null +++ b/odb-tests/common/relationship/basics/buildfile @@ -0,0 +1,41 @@ +# file : common/relationship/basics/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_basics_ \ + --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/relationship/basics/driver.cxx b/odb-tests/common/relationship/basics/driver.cxx new file mode 100644 index 0000000..e27c127 --- /dev/null +++ b/odb-tests/common/relationship/basics/driver.cxx @@ -0,0 +1,150 @@ +// file : common/relationship/basics/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test object relationships. +// + +#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)); + + aggr a ("aggr"); + a.o1 = new obj1 ("o1", "obj1"); + a.o2.reset (new obj2 ("obj2")); + + a.v2.push_back (obj2_ptr (new obj2 ("v1 obj2 1"))); + a.v2.push_back (0); + a.v2.push_back (obj2_ptr (new obj2 ("v1 obj2 2"))); + + a.o3.reset (new obj3 ("obj3")); + + a.c.num = 123; + a.c.o3.reset (new obj3 ("c")); + + a.cv.push_back (comp (234, obj3_ptr (new obj3 ("cv 0")))); + a.cv.push_back (comp (235, obj3_ptr ())); + a.cv.push_back (comp (236, obj3_ptr (new obj3 ("cv 2")))); + + a.v1.push_back (new obj1 ("v1 0", "v1 0")); + a.v1.push_back (0); + a.v1.push_back (new obj1 ("v1 2", "v1 2")); + + // Set cannot contain NULL pointers. + // + a.s1.insert (new obj1 ("s1 0", "s1 0")); + a.s1.insert (new obj1 ("s1 2", "s1 2")); + + a.m1[0] = new obj1 ("m1 0", "m1 0"); + a.m1[1] = 0; + a.m1[2] = new obj1 ("m1 2", "m1 2"); + + // persist + // + { + transaction t (db->begin ()); + db->persist (a.o1); + db->persist (a.o2); + + for (obj2_vec::iterator i (a.v2.begin ()); i != a.v2.end (); ++i) + if (*i) + db->persist (*i); + + db->persist (a.o3); + + db->persist (a.c.o3); + + for (comp_vec::iterator i (a.cv.begin ()); i != a.cv.end (); ++i) + if (i->o3) + db->persist (i->o3); + + for (obj1_vec::iterator i (a.v1.begin ()); i != a.v1.end (); ++i) + if (*i) + db->persist (*i); + + for (obj1_set::iterator i (a.s1.begin ()); i != a.s1.end (); ++i) + if (*i) + db->persist (*i); + + for (obj1_map::iterator i (a.m1.begin ()); i != a.m1.end (); ++i) + if (i->second) + db->persist (i->second); + + db->persist (a); + t.commit (); + } + + // load & compare + // + { + transaction t (db->begin ()); + unique_ptr<aggr> a1 (db->load<aggr> (a.id)); + t.commit (); + + assert (*a1 == a); + } + + // query + // + typedef odb::query<aggr> query; + typedef odb::result<aggr> result; + + { + transaction t (db->begin ()); + + result r (db->query<aggr> (query::o1->str == "obj1")); + assert (!r.empty ()); + assert (r.begin ()->o1->id == a.o1->id); + assert (size (r) == 1); + + t.commit (); + } + + // Test NULL pointer. + // + delete a.o1; + a.o1 = 0; + a.o2.reset (); + a.o3.reset (); + + { + transaction t (db->begin ()); + db->update (a); + t.commit (); + } + + // load & compare + // + { + transaction t (db->begin ()); + unique_ptr<aggr> a1 (db->load<aggr> (a.id)); + t.commit (); + + assert (*a1 == a); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/relationship/basics/test.hxx b/odb-tests/common/relationship/basics/test.hxx new file mode 100644 index 0000000..8a2742e --- /dev/null +++ b/odb-tests/common/relationship/basics/test.hxx @@ -0,0 +1,260 @@ +// file : common/relationship/basics/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <set> +#include <map> +#include <vector> +#include <string> +#include <memory> + +#include <odb/core.hxx> + +// Raw pointer. +// +#pragma db object pointer(obj1*) +struct obj1 +{ + obj1 () {} + obj1 (const std::string& i, const std::string& s): id (i), str (s) {} + + #pragma db id + std::string id; + std::string str; +}; + +inline bool +operator== (const obj1& x, const obj1& y) +{ + return x.id == y.id && x.str == y.str; +} + +// vector +// +typedef std::vector<obj1*> obj1_vec; + +inline bool +operator== (const obj1_vec& x, const obj1_vec& y) +{ + if (x.size () != y.size ()) + return false; + + for (obj1_vec::size_type i (0); i < x.size (); ++i) + if (!(x[i] ? (y[i] && *x[i] == *y[i]) : !y[i])) + return false; + + return true; +} + +// set +// +struct obj1_cmp +{ + bool + operator() (obj1* x, obj1* y) const + { + return (!x || !y) ? x < y : x->id < y->id; + } +}; + +typedef std::set<obj1*, obj1_cmp> obj1_set; + +inline bool +operator== (const obj1_set& x, const obj1_set& y) +{ + if (x.size () != y.size ()) + return false; + + for (obj1_set::const_iterator i (x.begin ()); i != x.end (); ++i) + { + obj1_set::const_iterator j (y.find (*i)); + + if (j == y.end ()) + return false; + + obj1* x (*i); + obj1* y (*j); + + if (!(x ? (y && *x == *y) : !y)) + return false; + } + + return true; +} + +// map +// +typedef std::map<int, obj1*> obj1_map; + +inline bool +operator== (const obj1_map& x, const obj1_map& y) +{ + if (x.size () != y.size ()) + return false; + + for (obj1_map::const_iterator i (x.begin ()); i != x.end (); ++i) + { + obj1_map::const_iterator j (y.find (i->first)); + + if (j == y.end ()) + return false; + + obj1* x (i->second); + obj1* y (j->second); + + if (!(x ? (y && *x == *y) : !y)) + return false; + } + + return true; +} + +// auto_ptr/unique_ptr +// +struct obj2; + +typedef std::unique_ptr<obj2> obj2_ptr; + +#pragma db object pointer(obj2_ptr) +struct obj2 +{ + obj2 () {} + obj2 (const std::string& s): str (s) {} + + #pragma db id auto + unsigned long id; + + std::string str; +}; + +inline bool +operator== (const obj2& x, const obj2& y) +{ + return x.id == y.id && x.str == y.str; +} + +typedef std::vector<obj2_ptr> obj2_vec; + +inline bool +operator== (const obj2_vec& x, const obj2_vec& y) +{ + if (x.size () != y.size ()) + return false; + + for (obj2_vec::size_type i (0); i < x.size (); ++i) + if (!(x[i] ? (y[i] && *x[i] == *y[i]) : !y[i])) + return false; + + return true; +} + +// shared_ptr +// +struct obj3; + +typedef std::shared_ptr<obj3> obj3_ptr; + +#pragma db object pointer(obj3_ptr) +struct obj3 +{ + obj3 () {} + obj3 (const std::string& s): str (s) {} + + #pragma db id auto + unsigned long id; + + std::string str; +}; + +inline bool +operator== (const obj3& x, const obj3& y) +{ + return x.id == y.id && x.str == y.str; +} + +// composite +// +#pragma db value +struct comp +{ + comp () {} + comp (int n, obj3_ptr o): num (n), o3 (o) {} + + int num; + obj3_ptr o3; +}; + +inline bool +operator== (const comp& x, const comp& y) +{ + return x.num == y.num && + (x.o3 ? (y.o3 && *x.o3 == *y.o3) : !y.o3); +} + +typedef std::vector<comp> comp_vec; + +// +// +#pragma db object +struct aggr +{ + aggr (): o1 (0) {} + aggr (const std::string& s): o1 (0), str (s) {} + + ~aggr () + { + delete o1; + + for (obj1_vec::iterator i (v1.begin ()); i != v1.end (); ++i) + delete *i; + + for (obj1_set::iterator i (s1.begin ()); i != s1.end (); ++i) + delete *i; + + for (obj1_map::iterator i (m1.begin ()); i != m1.end (); ++i) + delete i->second; + } + + #pragma db id auto + unsigned long id; + + obj1* o1; + + obj2_ptr o2; // std::auto_ptr or std::unique_ptr + obj2_vec v2; + + obj3_ptr o3; + comp c; + comp_vec cv; + + obj1_vec v1; + obj1_set s1; + obj1_map m1; + + std::string str; + +private: + aggr (const aggr&); + aggr& operator= (const aggr&); +}; + +inline bool +operator== (const aggr& x, const aggr& y) +{ + return + x.id == y.id && + (x.o1 ? (y.o1 && *x.o1 == *y.o1) : !y.o1) && + (x.o2.get () ? (y.o2.get () && *x.o2 == *y.o2) : !y.o2.get ()) && + x.v2 == y.v2 && + (x.o3.get () ? (y.o3.get () && *x.o3 == *y.o3) : !y.o3.get ()) && + x.c == y.c && + x.cv == y.cv && + x.v1 == y.v1 && + x.s1 == y.s1 && + x.m1 == y.m1 && + x.str == y.str; +} + +#endif // TEST_HXX diff --git a/odb-tests/common/relationship/basics/testscript b/odb-tests/common/relationship/basics/testscript new file mode 100644 index 0000000..b498bfa --- /dev/null +++ b/odb-tests/common/relationship/basics/testscript @@ -0,0 +1,33 @@ +# file : common/relationship/basics/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; + $* +} |