From afc6505d75dbc391452f41081af457045d5e7526 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 20 Jul 2010 11:08:04 +0200 Subject: Get rid of the session mechanism for now Test low-level API instead. --- tracer/object/driver.cxx | 183 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 168 insertions(+), 15 deletions(-) (limited to 'tracer/object/driver.cxx') diff --git a/tracer/object/driver.cxx b/tracer/object/driver.cxx index 8680b0a..8e9e437 100644 --- a/tracer/object/driver.cxx +++ b/tracer/object/driver.cxx @@ -3,9 +3,11 @@ // copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file +#include #include #include +#include #include #include @@ -15,18 +17,33 @@ using namespace std; using namespace odb; -using odb::shared_ptr; - int main () { tracer::database db; - // transient -> persistent in transaction + // database operation out of transaction + // + cout << "\ntest 001" << endl; + { + object o1 (1); + try + { + cout << "s 1" << endl; + db.persist (o1); + cout << "s 2.a" << endl; + } + catch (const not_in_transaction&) + { + cout << "s 2.b" << endl; + } + } + + // transient -> persistent // - cout << "test 001" << endl; + cout << "\ntest 002" << endl; { - shared_ptr o1 (new (shared) object1 (1)); + object o1 (1); transaction t (db.begin_transaction ()); cout << "s 1" << endl; db.persist (o1); @@ -35,44 +52,180 @@ main () cout << "s 3" << endl; } - // persistent -> transient in transaction + // transient -> persistent (already exist) // - cout << "test 002" << endl; + cout << "\ntest 003" << endl; { - shared_ptr o1 (new (shared) object1 (1)); + object o1 (0); + transaction t (db.begin_transaction ()); + cout << "s 1" << endl; + try + { + db.persist (o1); + } + catch (const object_already_persistent&) + { + cout << "object already persistent" << endl; + } + cout << "s 2" << endl; + } + + // persistent -> transient + // + cout << "\ntest 004" << endl; + { + object o1 (1); + object o2 (2); transaction t (db.begin_transaction ()); cout << "s 1" << endl; db.persist (o1); + db.persist (o2); cout << "s 2" << endl; db.erase (o1); + db.erase (2); cout << "s 3" << endl; t.commit (); cout << "s 4" << endl; } - // load in transaction + // persistent -> transient (not exist) + // + cout << "\ntest 005" << endl; + { + object o1 (0); + transaction t (db.begin_transaction ()); + cout << "s 1" << endl; + try + { + db.erase (o1); + } + catch (const object_not_persistent&) + { + cout << "object not persistent" << endl; + } + cout << "s 2" << endl; + } + + // load new object // - cout << "test 003" << endl; + cout << "\ntest 006" << endl; { transaction t (db.begin_transaction ()); cout << "s 1" << endl; - shared_ptr o1 (db.load (1)); + auto_ptr o1 (db.load (1)); cout << "s 2" << endl; t.commit (); cout << "s 3" << endl; } - // persistent/clean -> persistent/dirty in transaction + // load new object (not exist) + // + cout << "\ntest 007" << endl; + { + transaction t (db.begin_transaction ()); + cout << "s 1" << endl; + try + { + auto_ptr o1 (db.load (0)); + } + catch (const object_not_persistent&) + { + cout << "object not persistent" << endl; + } + cout << "s 2" << endl; + } + + // load into existing object // - cout << "test 004" << endl; + cout << "\ntest 008" << endl; { + object o1; transaction t (db.begin_transaction ()); cout << "s 1" << endl; - shared_ptr o1 (db.load (1)); + db.load (1, o1); cout << "s 2" << endl; - db.modified (o1); + t.commit (); + cout << "s 3" << endl; + } + + // load into existing object (not exist) + // + cout << "\ntest 009" << endl; + { + object o1; + transaction t (db.begin_transaction ()); + cout << "s 1" << endl; + try + { + db.load (0, o1); + } + catch (const object_not_persistent&) + { + cout << "object not persistent" << endl; + } + cout << "s 2" << endl; + } + + // store + // + cout << "\ntest 010" << endl; + { + transaction t (db.begin_transaction ()); + cout << "s 1" << endl; + auto_ptr o1 (db.load (1)); + cout << "s 2" << endl; + db.store (*o1); cout << "s 3" << endl; t.commit (); cout << "s 4" << endl; } + + // store (not exist) + // + cout << "\ntest 011" << endl; + { + object o1 (0); + transaction t (db.begin_transaction ()); + cout << "s 1" << endl; + try + { + db.store (o1); + } + catch (const object_not_persistent&) + { + cout << "object not persistent" << endl; + } + cout << "s 2" << endl; + } + + // find new object + // + cout << "\ntest 012" << endl; + { + transaction t (db.begin_transaction ()); + cout << "s 1" << endl; + auto_ptr o1 (db.find (1)); + assert (o1.get () != 0); + auto_ptr o2 (db.find (0)); + assert (o2.get () == 0); + cout << "s 2" << endl; + t.commit (); + cout << "s 3" << endl; + } + + // load into existing object + // + cout << "\ntest 013" << endl; + { + object o1; + transaction t (db.begin_transaction ()); + cout << "s 1" << endl; + bool r (db.find (1, o1)); + assert (r); + r = db.find (0, o1); + assert (!r); + cout << "s 2" << endl; + t.commit (); + cout << "s 3" << endl; + } } -- cgit v1.1