diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-03-28 09:54:19 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-03-28 09:54:19 +0200 |
commit | e61fb61f6406db4d1b3b33eb72007ba22efb44bd (patch) | |
tree | 0dff61043ebda523eff7451b8f210d395ef4b9f0 | |
parent | 3de8e4843bc85f9fc6e63c1a4fea6b57ff0351b6 (diff) |
Recode tests to work with implementations lacking result::size()
-rw-r--r-- | common/composite/driver.cxx | 6 | ||||
-rw-r--r-- | common/const/driver.cxx | 12 | ||||
-rw-r--r-- | common/ctor/driver.cxx | 15 | ||||
-rw-r--r-- | common/inverse/driver.cxx | 9 | ||||
-rw-r--r-- | common/query/driver.cxx | 6 | ||||
-rw-r--r-- | common/relationship/driver.cxx | 3 | ||||
-rw-r--r-- | libcommon/common/common.cxx | 2 | ||||
-rw-r--r-- | libcommon/common/common.hxx | 21 |
8 files changed, 52 insertions, 22 deletions
diff --git a/common/composite/driver.cxx b/common/composite/driver.cxx index 51e6a26..e1dfef8 100644 --- a/common/composite/driver.cxx +++ b/common/composite/driver.cxx @@ -92,8 +92,9 @@ main (int argc, char* argv[]) result r (db->query<person> (query::name::first == "Joe")); - assert (r.size () == 1); + assert (!r.empty ()); assert (*r.begin () == p); + assert (size (r) == 1); t.commit (); } @@ -105,8 +106,9 @@ main (int argc, char* argv[]) result r (db->query<person> (query::name::flags::alias)); - assert (r.size () == 1); + assert (!r.empty ()); assert (*r.begin () == p); + assert (size (r) == 1); t.commit (); } diff --git a/common/const/driver.cxx b/common/const/driver.cxx index 10e3103..17c2edc 100644 --- a/common/const/driver.cxx +++ b/common/const/driver.cxx @@ -122,8 +122,7 @@ main (int argc, char* argv[]) { transaction t (db->begin ()); result1 r1 (db->query<const obj1> (query1::id < 3)); - - assert (r1.size () == 2); + size_t n1 (0); for (result1::iterator i (r1.begin ()); i != r1.end (); ++i) { @@ -134,11 +133,13 @@ main (int argc, char* argv[]) i.load (o); assert (p->id == o.id); delete p; + n1++; } - result2 r2 (db->query<const obj2> (query2::id < 3)); + assert (n1 == 2); - assert (r2.size () == 2); + result2 r2 (db->query<const obj2> (query2::id < 3)); + size_t n2 (0); for (result2::iterator i (r2.begin ()); i != r2.end (); ++i) { @@ -148,8 +149,11 @@ main (int argc, char* argv[]) obj2 o (0); i.load (o); assert (p->id == o.id); + n2++; } + assert (n2 == 2); + t.commit (); } diff --git a/common/ctor/driver.cxx b/common/ctor/driver.cxx index 23d2af1..a83724c 100644 --- a/common/ctor/driver.cxx +++ b/common/ctor/driver.cxx @@ -58,16 +58,15 @@ main (int argc, char* argv[]) result r (db->query<person> (query::age < 30)); - assert (r.size () == 1); + assert (!r.empty ()); - for (result::iterator i (r.begin ()); i != r.end (); ++i) - { - i.load (p); + result::iterator i (r.begin ()); + i.load (p); + assert (p.first_ == "Jane"); + assert (p.last_ == "Doe"); + assert (p.age_ == 29); - assert (p.first_ == "Jane"); - assert (p.last_ == "Doe"); - assert (p.age_ == 29); - } + assert (size (r) == 1); t.commit (); } diff --git a/common/inverse/driver.cxx b/common/inverse/driver.cxx index 1f018e7..c07b751 100644 --- a/common/inverse/driver.cxx +++ b/common/inverse/driver.cxx @@ -173,9 +173,10 @@ main (int argc, char* argv[]) transaction t (db->begin ()); result r (db->query<obj2> (query::o1::id == "obj1 1")); - assert (r.size () == 1); + assert (!r.empty ()); assert (r.begin ()->id == o2->id); assert (r.begin ()->o1->id == o1_1->id); + assert (size (r) == 1); t.commit (); } @@ -190,15 +191,17 @@ main (int argc, char* argv[]) transaction t (db->begin ()); result r (db->query<obj3> (query::o1::id == "obj1 1")); - - assert (r.size () == 2); + size_t n (0); for (result::iterator i (r.begin ()); i != r.end (); ++i) { assert (i->id == o3_1->id || i->id == o3_2->id); assert (i->o1->id == o1_1->id); + n++; } + assert (n == 2); + t.commit (); } diff --git a/common/query/driver.cxx b/common/query/driver.cxx index 9d948cc..2f97f2e 100644 --- a/common/query/driver.cxx +++ b/common/query/driver.cxx @@ -392,10 +392,10 @@ main (int argc, char* argv[]) transaction t (db->begin ()); result r (db->query<person> (query::last_name == "Doe")); - assert (r.size () == 2); - result::iterator i (r.begin ()); - i++; + assert (i != r.end ()); + ++i; + assert (i != r.end ()); auto_ptr<person> joe (db->load<person> (3)); assert (i->last_name_ == "Doe"); diff --git a/common/relationship/driver.cxx b/common/relationship/driver.cxx index cad17a0..abbae29 100644 --- a/common/relationship/driver.cxx +++ b/common/relationship/driver.cxx @@ -105,8 +105,9 @@ main (int argc, char* argv[]) transaction t (db->begin ()); result r (db->query<aggr> (query::o1::str == "obj1")); - assert (r.size () == 1); + assert (!r.empty ()); assert (r.begin ()->o1->id == a.o1->id); + assert (size (r) == 1); t.commit (); } diff --git a/libcommon/common/common.cxx b/libcommon/common/common.cxx index 4ad169c..1ed65e3 100644 --- a/libcommon/common/common.cxx +++ b/libcommon/common/common.cxx @@ -8,6 +8,8 @@ #include <common/config.hxx> +#include <odb/database.hxx> + #if defined(DATABASE_MYSQL) # include <odb/mysql/database.hxx> # include <odb/mysql/connection-factory.hxx> diff --git a/libcommon/common/common.hxx b/libcommon/common/common.hxx index 1862aae..23e9d80 100644 --- a/libcommon/common/common.hxx +++ b/libcommon/common/common.hxx @@ -9,11 +9,30 @@ #include <memory> // std::auto_ptr #include <cstddef> // std::size_t -#include <odb/database.hxx> +#include <odb/forward.hxx> // odb::database +#include <odb/result.hxx> +#include <common/config.hxx> #include <common/export.hxx> LIBCOMMON_EXPORT std::auto_ptr<odb::database> create_database (int& argc, char* argv[], std::size_t max_connections = 0); +// This function returns an accurate result only if the result iterator +// hasn't been advanced. +// +template <typename T> +std::size_t +size (odb::result<T>& r) +{ + std::size_t n (0); +#if defined(DATABASE_SQLITE) + for (typename odb::result<T>::iterator i (r.begin ()); i != r.end (); ++i) + n++; +#else + n = r.size (); +#endif + return n; +} + #endif // LIBCOMMON_COMMON_COMMON_HXX |