From 86f71bd8525302519d1ec22ab135e662c43e8af3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 9 Sep 2011 12:20:26 +0200 Subject: Add test for queries involving object relationships --- common/relationship-query/driver.cxx | 175 +++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 common/relationship-query/driver.cxx (limited to 'common/relationship-query/driver.cxx') diff --git a/common/relationship-query/driver.cxx b/common/relationship-query/driver.cxx new file mode 100644 index 0000000..57a6215 --- /dev/null +++ b/common/relationship-query/driver.cxx @@ -0,0 +1,175 @@ +// file : common/relationship-query/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test relationship queries. +// + +#include // std::auto_ptr +#include +#include + +#include +#include +#include + +#include // HAVE_TR1_MEMORY +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + +#ifdef HAVE_TR1_MEMORY + + using std::tr1::shared_ptr; + + // + // + { + shared_ptr ca (new country ("CA", "Canada")); + shared_ptr za (new country ("ZA", "South Africa")); + shared_ptr us (new country ("US", "United States")); + shared_ptr se (new country ("SE", "Sweden")); + + shared_ptr st (new employer ("Simple Tech, Inc")); + shared_ptr ct (new employer ("Complex Tech, Inc")); + + // person + // + shared_ptr p1 ( + new person (1, "John", "Doe", 30, ca, true, za)); + + shared_ptr p2 ( + new person (2, "Jane", "Doe", 29, za, false, us)); + p2->husband = p1; + + shared_ptr p3 ( + new person (3, "Joe", "Dirt", 31, us, true, us)); + + shared_ptr p4 ( + new person (4, "Johan", "Johansen", 32, se, false, ca)); + + // employee + // + shared_ptr e1 ( + new employee (1, "John", "Doe", 30, ca, true, za, st)); + + shared_ptr e2 ( + new employee (2, "Jane", "Doe", 29, za, false, us, ct)); + e2->husband = p1; + + shared_ptr e3 ( + new employee (3, "Joe", "Dirt", 31, us, true, us, st)); + + shared_ptr e4 ( + new employee (4, "Johan", "Johansen", 32, se, false, ca, ct)); + + transaction t (db->begin ()); + db->persist (ca); + db->persist (za); + db->persist (us); + db->persist (se); + + db->persist (st); + db->persist (ct); + + db->persist (p1); + db->persist (p2); + db->persist (p3); + db->persist (p4); + + db->persist (e1); + db->persist (e2); + db->persist (e3); + db->persist (e4); + t.commit (); + } + + typedef odb::query p_query; + typedef odb::result p_result; + + typedef odb::query e_query; + typedef odb::result e_result; + + // Make sure we have an independent JOIN for each relationship. + // + { + session s; + transaction t (db->begin ()); + + p_result pr (db->query ( + p_query::residence::location::code == "ZA")); + assert (size (pr) == 1); + + e_result er (db->query ( + e_query::residence::location::code == "ZA")); + assert (size (er) == 1); + + t.commit (); + } + + // Test Self-JOIN. + // + { + session s; + transaction t (db->begin ()); + + p_result pr (db->query (p_query::husband::last_name == "Doe")); + assert (size (pr) == 1); + + e_result er (db->query (e_query::husband::last_name == "Doe")); + assert (size (er) == 1); + + t.commit (); + } + + // Test query conditions from both base and derived. + // + { + session s; + transaction t (db->begin ()); + + e_result r ( + db->query ( + e_query::employed_by::name == "Simple Tech, Inc" && + e_query::nationality::code == "US")); + + assert (size (r) == 1); + + t.commit (); + } + + // Test second-level pointers. + // + { + session s; + transaction t (db->begin ()); + + p_result r ( + db->query ( + p_query::husband::residence::location == "CA")); + + assert (size (r) == 1); + + t.commit (); + } + +#endif + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} -- cgit v1.1