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 ++++++++++++++++++++++++++++++++++++++---- tracer/object/test.hxx | 22 +---- tracer/object/test.std | 94 +++++++++++++++++++--- tracer/transaction/driver.cxx | 21 +---- tracer/transaction/test.std | 6 -- 5 files changed, 257 insertions(+), 69 deletions(-) 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; + } } diff --git a/tracer/object/test.hxx b/tracer/object/test.hxx index a988411..d640e75 100644 --- a/tracer/object/test.hxx +++ b/tracer/object/test.hxx @@ -10,14 +10,14 @@ #include #pragma odb object -struct object1 +struct object { - object1 (unsigned long id) + object (unsigned long id) : id_ (id) { } - object1 () + object () { } @@ -25,20 +25,4 @@ struct object1 unsigned long id_; }; -#pragma odb object -struct object2 -{ - object2 (const std::string& id) - : id_ (id) - { - } - - object2 () - { - } - - #pragma odb id - std::string id_; -}; - #endif // TEST_HXX diff --git a/tracer/object/test.std b/tracer/object/test.std index c7e458c..aa495e7 100644 --- a/tracer/object/test.std +++ b/tracer/object/test.std @@ -1,32 +1,108 @@ + test 001 +s 1 +s 2.b + +test 002 begin transaction s 1 -insert ::object1 id 1 +insert ::object id 1 s 2 commit transaction s 3 -test 002 + +test 003 +begin transaction +s 1 +insert ::object id 0 +object already persistent +s 2 +rollback transaction + +test 004 begin transaction s 1 -insert ::object1 id 1 +insert ::object id 1 +insert ::object id 2 s 2 +delete ::object id 1 +delete ::object id 2 s 3 -delete ::object1 id 1 commit transaction s 4 -test 003 + +test 005 begin transaction s 1 -select ::object1 id 1 +delete ::object id 0 +object not persistent +s 2 +rollback transaction + +test 006 +begin transaction +s 1 +select ::object id 1 s 2 commit transaction s 3 -test 004 + +test 007 begin transaction s 1 -select ::object1 id 1 +select ::object id 0 +object not persistent s 2 +rollback transaction + +test 008 +begin transaction +s 1 +select ::object id 1 +s 2 +commit transaction +s 3 + +test 009 +begin transaction +s 1 +select ::object id 0 +object not persistent +s 2 +rollback transaction + +test 010 +begin transaction +s 1 +select ::object id 1 +s 2 +update ::object id 1 s 3 -update ::object1 id 1 commit transaction s 4 + +test 011 +begin transaction +s 1 +update ::object id 0 +object not persistent +s 2 +rollback transaction + +test 012 +begin transaction +s 1 +select ::object id 1 +select ::object id 0 +s 2 +commit transaction +s 3 + +test 013 +begin transaction +s 1 +select ::object id 1 +select ::object id 0 +s 2 +commit transaction +s 3 diff --git a/tracer/transaction/driver.cxx b/tracer/transaction/driver.cxx index 164b9a0..752eb0b 100644 --- a/tracer/transaction/driver.cxx +++ b/tracer/transaction/driver.cxx @@ -7,7 +7,6 @@ #include #include -#include #include #include #include @@ -70,27 +69,9 @@ main () } } - // Existing session via current. - // - cout << "test 006" << endl; - { - session s; - transaction t (db.begin_transaction ()); - assert (&t.session () == &s); - } - - // Existing session passed explicitly. - // - cout << "test 007" << endl; - { - session s; - transaction t (db.begin_transaction (s)); - assert (&t.session () == &s); - } - // Concrete transaction type. // - cout << "test 008" << endl; + cout << "test 006" << endl; { assert (sizeof (tracer::transaction) == sizeof (transaction)); diff --git a/tracer/transaction/test.std b/tracer/transaction/test.std index 8f962ef..8083774 100644 --- a/tracer/transaction/test.std +++ b/tracer/transaction/test.std @@ -17,9 +17,3 @@ rollback transaction test 006 begin transaction rollback transaction -test 007 -begin transaction -rollback transaction -test 008 -begin transaction -rollback transaction -- cgit v1.1