From 0d49ea1fe08cf1eab41a00149393a291c65a59d7 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 25 Jan 2024 20:32:06 +0300 Subject: Turn odb-tests repository into package for muti-package repository --- odb-tests/pgsql/truncation/driver.cxx | 163 ++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 odb-tests/pgsql/truncation/driver.cxx (limited to 'odb-tests/pgsql/truncation/driver.cxx') diff --git a/odb-tests/pgsql/truncation/driver.cxx b/odb-tests/pgsql/truncation/driver.cxx new file mode 100644 index 0000000..337a26e --- /dev/null +++ b/odb-tests/pgsql/truncation/driver.cxx @@ -0,0 +1,163 @@ +// file : pgsql/truncation/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test insufficient buffer/truncation handling. +// + +#include // std::unique_ptr +#include + +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include + +using namespace std; +namespace pgsql = odb::pgsql; +using namespace pgsql; + +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. + // + { + unique_ptr db (create_specific_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 ()); + unique_ptr o (db->load (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 ()); + unique_ptr o (db->load (3)); + assert (o->str_ == longer_str); + t.commit (); + } + } + + // Test query. + // + { + typedef pgsql::query query; + typedef odb::result result; + + unique_ptr db (create_specific_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 (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 (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; + } +} -- cgit v1.1