diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-03-28 11:22:59 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-03-28 11:22:59 +0200 |
commit | 1ba1097cd1d71b945255d9401ffa4f04a036e94f (patch) | |
tree | e58fb8541d412a9ebe62e839da2a6db9b4abbc89 /sqlite/truncation/driver.cxx | |
parent | d2c8179bfef0503647c519f44401f29d8bddd122 (diff) |
Add SQLite-specific tests
Diffstat (limited to 'sqlite/truncation/driver.cxx')
-rw-r--r-- | sqlite/truncation/driver.cxx | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/sqlite/truncation/driver.cxx b/sqlite/truncation/driver.cxx new file mode 100644 index 0000000..a48bdc2 --- /dev/null +++ b/sqlite/truncation/driver.cxx @@ -0,0 +1,162 @@ +// file : sqlite/truncation/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test insufficient buffer/truncation handling. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/sqlite/database.hxx> +#include <odb/sqlite/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + // The default pre-allocated buffer is 512 bytes long. + // + string long_str (640, 'c'); // This will get the buffer to 1024 + string longer_str (1025, 'b'); + + try + { + // Test basic operations. + // + { + auto_ptr<database> db (create_database (argc, argv)); + + // Run persist/load so that the initial bindings are established + // (version == 0). + // + { + object1 o (1); + o.str_ = "test string"; + + transaction t (db->begin ()); + db->persist (o); + db->load (1, o); + t.commit (); + } + + { + object2 o (2); + o.str_ = "test string"; + + transaction t (db->begin ()); + db->persist (o); + db->load (2, o); + t.commit (); + } + + // Store/load the long string which should trigger buffer growth. + // + { + object1 o (3); + o.str_ = long_str; + + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object2> o (db->load<object2> (3)); + assert (o->str_ == long_str); + t.commit (); + } + + // Store/load longer string. + // + { + object1 o (3); + o.str_ = longer_str; + + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object2> o (db->load<object2> (3)); + assert (o->str_ == longer_str); + t.commit (); + } + } + + // Test query. + // + { + typedef odb::query<object1> query; + typedef odb::result<object1> result; + + auto_ptr<database> db (create_database (argc, argv)); + + // Run persist/query so that the initial bindings are established + // (version == 0). + // + { + object1 o (20); + o.str_ = "test string"; + + transaction t (db->begin ()); + db->persist (o); + o.id_++; + db->persist (o); + o.id_++; + db->persist (o); + + result r (db->query<object1> (query::id == 20)); + assert (r.begin ()->id_ == 20); + t.commit (); + } + + // Test buffer growth with cached result. + // + { + object1 o; + + transaction t (db->begin ()); + + result r (db->query<object1> (query::id >= 20)); + result::iterator i (r.begin ()); + + o.id_ = i->id_; + o.str_ = long_str; + + // This forces buffer growth in the middle of result iteration. + // + db->update (o); + + ++i; + assert (i->str_ == "test string"); + + o.id_ = i->id_; + o.str_ = longer_str; + db->update (o); + + ++i; + assert (i->str_ == "test string"); + + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} |