diff options
Diffstat (limited to 'odb-tests/common/wrapper')
-rw-r--r-- | odb-tests/common/wrapper/buildfile | 40 | ||||
-rw-r--r-- | odb-tests/common/wrapper/driver.cxx | 216 | ||||
-rw-r--r-- | odb-tests/common/wrapper/test.hxx | 214 | ||||
-rw-r--r-- | odb-tests/common/wrapper/testscript | 53 |
4 files changed, 523 insertions, 0 deletions
diff --git a/odb-tests/common/wrapper/buildfile b/odb-tests/common/wrapper/buildfile new file mode 100644 index 0000000..57f43f2 --- /dev/null +++ b/odb-tests/common/wrapper/buildfile @@ -0,0 +1,40 @@ +# file : common/wrapper/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 wrapper_ \ + --generate-schema + +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/wrapper/driver.cxx b/odb-tests/common/wrapper/driver.cxx new file mode 100644 index 0000000..9c352fc --- /dev/null +++ b/odb-tests/common/wrapper/driver.cxx @@ -0,0 +1,216 @@ +// file : common/wrapper/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test wrapper machinery. +// + +#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)); + + // Test 1: simple values. + // + { + using namespace test1; + + unsigned long id1, id2; + { + object1 o1; + object2 o2; + + o1.num.reset (new int (123)); + o1.nstrs.push_back (nullable_string ()); + o1.nstrs.push_back (nullable_string ("123")); + + o2.sstrs.push_back (str_sptr ()); + o2.sstrs.push_back (str_sptr (new string ("123"))); + + transaction t (db->begin ()); + id1 = db->persist (o1); + id2 = db->persist (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object1> o1 (db->load<object1> (id1)); + unique_ptr<object2> o2 (db->load<object2> (id2)); + t.commit (); + + assert (*o1->num == 123); + assert (o1->str.get () == 0); + assert (o1->nstr.null ()); + assert (o1->nstrs[0].null ()); + assert (o1->nstrs[1].get () == "123"); + + assert (!o2->sstr); + assert (!o2->sstrs[0]); + assert (*o2->sstrs[1] == "123"); + } + } + + // + // Composite values. + // + unsigned long id; + { + comp_object co; + + co.c1.reset (new comp1 ("123", 123)); + co.vc1.push_back (comp1 ("1", 1)); + co.vc1.push_back (comp1 ("2", 2)); + co.vc1.push_back (comp1 ("3", 3)); + + co.c2.reset (new comp2 ("123", 123)); + co.c2->strs.push_back ("1"); + co.c2->strs.push_back ("2"); + co.c2->strs.push_back ("3"); + + { + transaction t (db->begin ()); + id = db->persist (co); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<comp_object> o (db->load<comp_object> (id)); + t.commit (); + + assert (*o->c1 == *co.c1); + assert (o->vc1 == co.vc1); + assert (*o->c2 == *co.c2); + } + } + + // + // Containers. + // + { + cont_object co; + + co.nums.reset (new vector<int>); + co.nums->push_back (1); + co.nums->push_back (2); + co.nums->push_back (3); + + co.c.num = 123; + co.c.strs.reset (new vector<string>); + co.c.strs->push_back ("1"); + co.c.strs->push_back ("2"); + co.c.strs->push_back ("3"); + + { + transaction t (db->begin ()); + id = db->persist (co); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<cont_object> o (db->load<cont_object> (id)); + t.commit (); + + assert (*o->nums == *co.nums); + assert (o->c == co.c); + } + } + + // Test 5: composite NULL values. + // + { + using namespace test5; + + object o1, o2; + + o1.v.push_back (nullable<comp> ()); + + o2.p.reset (new comp (1, "a")); + o2.n = comp (2, "b"); + o2.v.push_back (comp (3, "c")); + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + unique_ptr<object> p1 (db->load<object> (o1.id)); + unique_ptr<object> p2 (db->load<object> (o2.id)); + t.commit (); + + assert (p1->p.get () == 0); + assert (!p1->n); + assert (!p1->v[0]); + + assert (p2->p.get () != 0 && *p2->p == *o2.p); + assert (p2->n && *p2->n == *o2.n); + assert (p2->v[0] && *p2->v[0] == *o2.v[0]); + } + + // Update. + // + { + o1.p.reset (new comp (1, "a")); + o1.n = comp (2, "b"); + o1.v[0] = comp (3, "c"); + + o2.p.reset (); + o2.n.reset (); + o2.v[0].reset (); + + transaction t (db->begin ()); + db->update (o1); + db->update (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object> p1 (db->load<object> (o1.id)); + unique_ptr<object> p2 (db->load<object> (o2.id)); + t.commit (); + + assert (p1->p.get () != 0 && *p1->p == *o1.p); + assert (p1->n && *p1->n == *o1.n); + assert (p1->v[0] && *p1->v[0] == *o1.v[0]); + + assert (p2->p.get () == 0); + assert (!p2->n); + assert (!p2->v[0]); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/wrapper/test.hxx b/odb-tests/common/wrapper/test.hxx new file mode 100644 index 0000000..91f5758 --- /dev/null +++ b/odb-tests/common/wrapper/test.hxx @@ -0,0 +1,214 @@ +// file : common/wrapper/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <memory> // std::unique_ptr +#include <vector> + +#include <odb/core.hxx> +#include <odb/nullable.hxx> + +using odb::nullable; + +// Test 1: simple values. +// +#pragma db namespace table("t1_") +namespace test1 +{ + typedef nullable<std::string> nullable_string; + + typedef std::unique_ptr<int> num_uptr; + typedef std::unique_ptr<std::string> str_uptr; + typedef std::shared_ptr<std::string> str_sptr; + + #pragma db object table("obj1") + struct object1 + { + #pragma db id auto + unsigned long id_; + + num_uptr num; + + #pragma db null + str_uptr str; + + nullable_string nstr; + std::vector<nullable_string> nstrs; + }; + + #pragma db object + struct object2 + { + #pragma db id auto + unsigned long id_; + + #pragma db null + str_sptr sstr; + + #pragma db value_null + std::vector<str_sptr> sstrs; + }; +} + +// +// Composite values. +// + +#pragma db value +struct comp1 +{ + comp1 (): num (0) {} + comp1 (const std::string& s, int n): str (s), num (n) {} + + std::string str; + int num; +}; + +inline bool +operator== (const comp1& x, const comp1& y) +{ + return x.str == y.str && x.num == y.num; +} + + +#pragma db value +struct comp2 +{ + comp2 () {} + comp2 (const std::string& s, int n): str (s), num (n) {} + + std::string str; + int num; + + std::vector<std::string> strs; +}; + +inline bool +operator== (const comp2& x, const comp2& y) +{ + return x.str == y.str && x.num == y.num && x.strs == y.strs; +} + +struct comp3; + +typedef std::unique_ptr<comp1> comp1_uptr; +typedef std::unique_ptr<comp2> comp2_uptr; +typedef std::unique_ptr<comp3> comp3_uptr; + +#pragma db object +struct comp_object +{ + #pragma db id auto + unsigned long id_; + + comp1_uptr c1; // Wrapped comp value. + std::vector<nullable<comp1> > vc1; // Container of wrapped comp values. + comp2_uptr c2; // Container inside wrapped comp value. +}; + +// This one is just a compilation test to cover more convolute cases. +// +#pragma db value +struct comp3: comp2 +{ + comp1_uptr c1; + std::vector<nullable<comp1> > vc1; +}; + +#pragma db object +struct comp_object2 +{ + #pragma db id auto + unsigned long id_; + + comp3_uptr c3; +}; + +// +// Containers. +// + +typedef std::unique_ptr<std::vector<int>> nums_uptr; +typedef std::unique_ptr<std::vector<std::string>> strs_uptr; + +#pragma db value +struct cont_comp +{ + int num; + strs_uptr strs; +}; + +inline bool +operator== (const cont_comp& x, const cont_comp& y) +{ + return x.num == y.num && *x.strs == *y.strs; +} + +#pragma db object +struct cont_object +{ + #pragma db id auto + unsigned long id_; + + nums_uptr nums; // Wrapped container. + cont_comp c; // Wrapped container in comp value. +}; + +// Test composite NULL values. +// +#pragma db namespace table("t5_") +namespace test5 +{ + #pragma db value + struct base + { + base () {} + base (int n): num (n) {} + + int num = 0; + }; + + inline bool + operator== (const base& x, const base& y) + { + return x.num == y.num; + } + + #pragma db value + struct comp: base + { + comp () {} + comp (int n, const std::string s): base (n), str (s), extra (n + 1) {} + + std::string str; + base extra; + + odb::nullable<int> always_null; + }; + + inline bool + operator== (const comp& x, const comp& y) + { + return static_cast<const base&> (x) == y && + x.str == y.str && x.extra == y.extra; + } + + #pragma db object + struct object + { + #pragma db id auto + unsigned long id; + + #pragma db null + std::unique_ptr<comp> p; + + odb::nullable<comp> n; + + std::vector< odb::nullable<comp> > v; + }; +} + +#endif // TEST_HXX diff --git a/odb-tests/common/wrapper/testscript b/odb-tests/common/wrapper/testscript new file mode 100644 index 0000000..31c3a97 --- /dev/null +++ b/odb-tests/common/wrapper/testscript @@ -0,0 +1,53 @@ +# file : common/wrapper/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; + $* +} |