test72.cxx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <iostream>
  2. #include <pqxx/pipeline>
  3. #include <pqxx/transaction>
  4. #include "test_helpers.hxx"
  5. using namespace pqxx;
  6. // Test program for libpqxx. Test error handling for pipeline.
  7. namespace
  8. {
  9. void test_072()
  10. {
  11. connection conn;
  12. work tx{conn};
  13. pipeline P{tx};
  14. // Ensure all queries are issued at once to make the test more interesting
  15. P.retain();
  16. // The middle query should fail; the surrounding two should succeed
  17. auto const id_1{P.insert("SELECT 1")};
  18. auto const id_f{P.insert("SELECT * FROM pg_nonexist")};
  19. auto const id_2{P.insert("SELECT 2")};
  20. // See that we can process the queries without stumbling over the error
  21. P.complete();
  22. // We should be able to get the first result, which preceeds the error
  23. auto const res_1{P.retrieve(id_1).at(0).at(0).as<int>()};
  24. PQXX_CHECK_EQUAL(res_1, 1, "Got wrong result from pipeline.");
  25. // We should *not* get a result for the query behind the error
  26. {
  27. quiet_errorhandler d{conn};
  28. PQXX_CHECK_THROWS(
  29. P.retrieve(id_2).at(0).at(0).as<int>(), std::runtime_error,
  30. "Pipeline wrongly resumed after SQL error.");
  31. }
  32. // Now see that we get an exception when we touch the failed result
  33. {
  34. quiet_errorhandler d{conn};
  35. PQXX_CHECK_THROWS(
  36. P.retrieve(id_f), sql_error, "Pipeline failed to register SQL error.");
  37. }
  38. }
  39. } // namespace
  40. PQXX_REGISTER_TEST(test_072);