diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-08-19 11:25:20 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-08-19 11:25:20 +0200 |
commit | b59214c08549043cb5812c025159c49d6f9efe01 (patch) | |
tree | 3d586457497b96391c292af33ffcfe9a25a33701 /common/auto/driver.cxx | |
parent | bc95a07e6ecd388b4ef485f218aa1211d36e0e53 (diff) |
Test automatic id assignment
Diffstat (limited to 'common/auto/driver.cxx')
-rw-r--r-- | common/auto/driver.cxx | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/common/auto/driver.cxx b/common/auto/driver.cxx new file mode 100644 index 0000000..423254e --- /dev/null +++ b/common/auto/driver.cxx @@ -0,0 +1,69 @@ +// file : common/auto/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test automatic id assignment. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + unsigned long id1, id2, id3; + { + object o1 ("one"); + object o2 ("two"); + object o3 ("three"); + + transaction t (db->begin_transaction ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + + id1 = o1.id_; + id2 = o2.id_; + id3 = o3.id_; + + assert (id1 != id2); + assert (id1 != id3); + assert (id2 != id3); + } + + { + transaction t (db->begin_transaction ()); + auto_ptr<object> o1 (db->load<object> (id1)); + auto_ptr<object> o2 (db->load<object> (id2)); + auto_ptr<object> o3 (db->load<object> (id3)); + t.commit (); + + assert (o1->id_ == id1 && o1->str_ == "one"); + assert (o2->id_ == id2 && o2->str_ == "two"); + assert (o3->id_ == id3 && o3->str_ == "three"); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} |