From cbaa5532f056d0ad20300abbba253b065982187a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 8 Nov 2011 17:06:00 +0200 Subject: Add common/transaction test (port from tracer) --- common/transaction/driver.cxx | 117 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 common/transaction/driver.cxx (limited to 'common/transaction/driver.cxx') diff --git a/common/transaction/driver.cxx b/common/transaction/driver.cxx new file mode 100644 index 0000000..d868782 --- /dev/null +++ b/common/transaction/driver.cxx @@ -0,0 +1,117 @@ +// file : common/transaction/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test transaction operations. +// + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +using namespace std; +using namespace odb::core; + +struct transaction_tracer: odb::tracer +{ + virtual void + execute (connection&, const char* s) + { + string str (s); + + if (str == "BEGIN") + cout << "begin transaction" << endl; + else if (str == "COMMIT") + cout << "commit transaction" << endl; + else if (str == "ROLLBACK") + cout << "rollback transaction" << endl; + } +}; + +int +main (int argc, char* argv[]) +{ + transaction_tracer tracer; + auto_ptr db (create_database (argc, argv, false)); + db->tracer (tracer); + + assert (!transaction::has_current ()); + + // Current and db accessors. + // + cout << "test 001" << endl; + { + transaction t (db->begin ()); + assert (&t.database () == db.get ()); + assert (transaction::has_current ()); + assert (&transaction::current () == &t); + + transaction::reset_current (); + assert (!transaction::has_current ()); + + transaction t2 (db->begin (), false); + assert (!transaction::has_current ()); + + transaction::current (t2); + assert (&transaction::current () == &t2); + } + + // Commit. + // + cout << "test 002" << endl; + { + transaction t (db->begin ()); + t.commit (); + } + + // Rollback. + // + cout << "test 003" << endl; + { + transaction t (db->begin ()); + t.rollback (); + } + + // Auto rollback. + // + cout << "test 004" << endl; + { + transaction t (db->begin ()); + } + + // Nested transaction. + // + cout << "test 005" << endl; + { + transaction t (db->begin ()); + + try + { + transaction n (db->begin ()); + } + catch (const already_in_transaction&) + { + cout << "already_in_transaction" << endl; + } + } + + // Concrete transaction type. + // + cout << "test 006" << endl; + { + assert (sizeof (odb_db::transaction) == sizeof (transaction)); + + odb_db::transaction t (static_cast (*db).begin ()); + odb_db::transaction& r (odb_db::transaction::current ()); + assert (&t == &r); + } +} -- cgit v1.1