test20.cxx 2.3 KB

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