diff options
Diffstat (limited to 'odb-tests/common/const-member')
-rw-r--r-- | odb-tests/common/const-member/buildfile | 40 | ||||
-rw-r--r-- | odb-tests/common/const-member/driver.cxx | 119 | ||||
-rw-r--r-- | odb-tests/common/const-member/test.hxx | 109 | ||||
-rw-r--r-- | odb-tests/common/const-member/testscript | 33 |
4 files changed, 301 insertions, 0 deletions
diff --git a/odb-tests/common/const-member/buildfile b/odb-tests/common/const-member/buildfile new file mode 100644 index 0000000..868f7fd --- /dev/null +++ b/odb-tests/common/const-member/buildfile @@ -0,0 +1,40 @@ +# file : common/const-member/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 constm_ \ + --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/const-member/driver.cxx b/odb-tests/common/const-member/driver.cxx new file mode 100644 index 0000000..0c71dfa --- /dev/null +++ b/odb-tests/common/const-member/driver.cxx @@ -0,0 +1,119 @@ +// file : common/const-member/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test const data members. The readonly test tests that const +// members are automatically treated as read-only. +// + +#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)); + + // Const ids. + // + { + const_id o (1); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + db->load<const_id> (1, o); + t.commit (); + assert (o.id == 1); + } + } + + { + { + const_auto_id o; + transaction t (db->begin ()); + db->persist (o); + t.commit (); + assert (o.id == 1); + } + + { + transaction t (db->begin ()); + unique_ptr<const_auto_id> o (db->load<const_auto_id> (1)); + t.commit (); + assert (o->id == 1); + } + } + + // Container. + // + { + container o (1, 1); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<container> o (db->load<container> (1)); + t.commit (); + + assert (o->ccom.vec.size () == 1 && o->ccom.vec[0] == 1 && + o->ccom.cvec.size () == 1 && o->ccom.cvec[0] == 1 && + o->cvec.size () == 1 && o->cvec[0] == 1); + } + } + + // Wrapper. + // + { + wrapper o (1, "abc", 1); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<wrapper> o (db->load<wrapper> (1)); + t.commit (); + + assert (*o->str == "abc" && + o->com->str == "abc" && o->com->num == 1 && + o->com->vec.size () == 1 && o->com->vec[0] == 1 && + o->vec->size () == 1 && (*o->vec)[0] == 1); + } + } + + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/const-member/test.hxx b/odb-tests/common/const-member/test.hxx new file mode 100644 index 0000000..ab75c55 --- /dev/null +++ b/odb-tests/common/const-member/test.hxx @@ -0,0 +1,109 @@ +// file : common/const-member/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <vector> +#include <string> +#include <memory> // std::auto_ptr + +#include <odb/core.hxx> + +// Const ids. +// +#pragma db object +struct const_id +{ + const_id (unsigned long i): id (i) {} + const_id (): id (0) {} + + #pragma db id + const unsigned long id; +}; + +#pragma db object +struct const_auto_id +{ + const_auto_id (): id (0) {} + + #pragma db id auto + const unsigned long id; +}; + +// Container. +// +#pragma db value +struct container_value +{ + container_value (unsigned long x) + { + vec.push_back (x); + const_cast<std::vector<unsigned long>&> (cvec).push_back (x); + } + + container_value () {} + + std::vector<unsigned long> vec; + const std::vector<unsigned long> cvec; +}; + +#pragma db object +struct container +{ + container (unsigned long i, unsigned long x) + : id (i), ccom (x) + { + const_cast<std::vector<unsigned long>&> (cvec).push_back (x); + } + + container () {} + + #pragma db id + unsigned long id; + + const container_value ccom; + const std::vector<unsigned long> cvec; +}; + +// Wrapper. +// +#pragma db value +struct wrapped_value +{ + wrapped_value (const std::string& s, unsigned long n) + : str (s), num (n) + { + vec.push_back (n); + } + + wrapped_value () {} + + const std::string str; + unsigned long num; + std::vector<unsigned long> vec; +}; + +#pragma db object +struct wrapper +{ + wrapper (unsigned long i, const std::string& s, unsigned long n) + : id (i), + str (new std::string (s)), + com (new wrapped_value (s, n)), + vec (new std::vector<unsigned long>) + { + const_cast<std::vector<unsigned long>&> (*vec).push_back (n); + } + + wrapper () {} + + #pragma db id + unsigned long id; + + const std::unique_ptr<const std::string> str; + const std::unique_ptr<const wrapped_value> com; + const std::unique_ptr<const std::vector<unsigned long>> vec; +}; + +#endif // TEST_HXX diff --git a/odb-tests/common/const-member/testscript b/odb-tests/common/const-member/testscript new file mode 100644 index 0000000..c81d856 --- /dev/null +++ b/odb-tests/common/const-member/testscript @@ -0,0 +1,33 @@ +# file : common/const-member/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; + $* +} |