test39.cxx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <pqxx/nontransaction>
  2. #include "test_helpers.hxx"
  3. using namespace pqxx;
  4. // Test: nontransaction changes are committed immediately.
  5. namespace
  6. {
  7. int BoringYear{1977};
  8. void test_039()
  9. {
  10. connection conn;
  11. nontransaction tx1{conn};
  12. test::create_pqxxevents(tx1);
  13. std::string const Table{"pqxxevents"};
  14. // Verify our start condition before beginning: there must not be a 1977
  15. // record already.
  16. result R(tx1.exec(
  17. "SELECT * FROM " + Table +
  18. " "
  19. "WHERE year=" +
  20. to_string(BoringYear)));
  21. PQXX_CHECK_EQUAL(
  22. std::size(R), 0,
  23. "Already have a row for " + to_string(BoringYear) + ", cannot test.");
  24. // (Not needed, but verify that clear() works on empty containers)
  25. R.clear();
  26. PQXX_CHECK(std::empty(R), "Result is non-empty after clear().");
  27. // OK. Having laid that worry to rest, add a record for 1977.
  28. tx1.exec0(
  29. "INSERT INTO " + Table +
  30. " VALUES"
  31. "(" +
  32. to_string(BoringYear) +
  33. ","
  34. "'Yawn'"
  35. ")");
  36. // Abort tx1. Since tx1 is a nontransaction, which provides only the
  37. // transaction class interface without providing any form of transactional
  38. // integrity, this is not going to undo our work.
  39. tx1.abort();
  40. // Verify that our record was added, despite the Abort()
  41. nontransaction tx2(conn, "tx2");
  42. R = tx2.exec(
  43. "SELECT * FROM " + Table +
  44. " "
  45. "WHERE year=" +
  46. to_string(BoringYear));
  47. PQXX_CHECK_EQUAL(std::size(R), 1, "Unexpected result size.");
  48. PQXX_CHECK(R.capacity() >= std::size(R), "Result's capacity is too small.");
  49. R.clear();
  50. PQXX_CHECK(std::empty(R), "result::clear() is broken.");
  51. // Now remove our record again
  52. tx2.exec0(
  53. "DELETE FROM " + Table +
  54. " "
  55. "WHERE year=" +
  56. to_string(BoringYear));
  57. tx2.commit();
  58. // And again, verify results
  59. nontransaction tx3(conn, "tx3");
  60. R = tx3.exec(
  61. "SELECT * FROM " + Table +
  62. " "
  63. "WHERE year=" +
  64. to_string(BoringYear));
  65. PQXX_CHECK_EQUAL(std::size(R), 0, "Record is not gone as expected.");
  66. }
  67. PQXX_REGISTER_TEST(test_039);
  68. } // namespace