test_transaction_base.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <pqxx/stream_to>
  2. #include <pqxx/transaction>
  3. #include "../test_helpers.hxx"
  4. namespace
  5. {
  6. void test_exec0(pqxx::transaction_base &trans)
  7. {
  8. pqxx::result E{trans.exec0("SELECT * FROM pg_tables WHERE 0 = 1")};
  9. PQXX_CHECK(std::empty(E), "Nonempty result from exec0.");
  10. PQXX_CHECK_THROWS(
  11. trans.exec0("SELECT 99"), pqxx::unexpected_rows,
  12. "Nonempty exec0 result did not throw unexpected_rows.");
  13. }
  14. void test_exec1(pqxx::transaction_base &trans)
  15. {
  16. pqxx::row R{trans.exec1("SELECT 99")};
  17. PQXX_CHECK_EQUAL(std::size(R), 1, "Wrong size result from exec1.");
  18. PQXX_CHECK_EQUAL(R.front().as<int>(), 99, "Wrong result from exec1.");
  19. PQXX_CHECK_THROWS(
  20. trans.exec1("SELECT * FROM pg_tables WHERE 0 = 1"), pqxx::unexpected_rows,
  21. "Empty exec1 result did not throw unexpected_rows.");
  22. PQXX_CHECK_THROWS(
  23. trans.exec1("SELECT * FROM generate_series(1, 2)"), pqxx::unexpected_rows,
  24. "Two-row exec1 result did not throw unexpected_rows.");
  25. }
  26. void test_exec_n(pqxx::transaction_base &trans)
  27. {
  28. pqxx::result R{trans.exec_n(3, "SELECT * FROM generate_series(1, 3)")};
  29. PQXX_CHECK_EQUAL(std::size(R), 3, "Wrong result size from exec_n.");
  30. PQXX_CHECK_THROWS(
  31. trans.exec_n(2, "SELECT * FROM generate_series(1, 3)"),
  32. pqxx::unexpected_rows,
  33. "exec_n did not throw unexpected_rows for an undersized result.");
  34. PQXX_CHECK_THROWS(
  35. trans.exec_n(4, "SELECT * FROM generate_series(1, 3)"),
  36. pqxx::unexpected_rows,
  37. "exec_n did not throw unexpected_rows for an oversized result.");
  38. }
  39. void test_query_value(pqxx::connection &conn)
  40. {
  41. pqxx::work tx{conn};
  42. PQXX_CHECK_EQUAL(
  43. tx.query_value<int>("SELECT 84 / 2"), 42,
  44. "Got wrong value from query_value.");
  45. PQXX_CHECK_THROWS(
  46. tx.query_value<int>("SAVEPOINT dummy"), pqxx::unexpected_rows,
  47. "Got field when none expected.");
  48. PQXX_CHECK_THROWS(
  49. tx.query_value<int>("SELECT generate_series(1, 2)"), pqxx::unexpected_rows,
  50. "Failed to fail for multiple rows.");
  51. PQXX_CHECK_THROWS(
  52. tx.query_value<int>("SELECT 1, 2"), pqxx::usage_error,
  53. "No error for too many fields.");
  54. PQXX_CHECK_THROWS(
  55. tx.query_value<int>("SELECT 3.141"), pqxx::conversion_error,
  56. "Got int field from float string.");
  57. }
  58. void test_transaction_base()
  59. {
  60. pqxx::connection conn;
  61. {
  62. pqxx::work tx{conn};
  63. test_exec_n(tx);
  64. test_exec0(tx);
  65. test_exec1(tx);
  66. }
  67. test_query_value(conn);
  68. }
  69. void test_transaction_for_each()
  70. {
  71. constexpr auto query{
  72. "SELECT i, concat('x', (2*i)::text) "
  73. "FROM generate_series(1, 3) AS i "
  74. "ORDER BY i"};
  75. pqxx::connection conn;
  76. pqxx::work tx{conn};
  77. std::string ints;
  78. std::string strings;
  79. tx.for_each(query, [&ints, &strings](int i, std::string const &s) {
  80. ints += pqxx::to_string(i) + " ";
  81. strings += s + " ";
  82. });
  83. PQXX_CHECK_EQUAL(ints, "1 2 3 ", "Unexpected int sequence.");
  84. PQXX_CHECK_EQUAL(strings, "x2 x4 x6 ", "Unexpected string sequence.");
  85. }
  86. PQXX_REGISTER_TEST(test_transaction_base);
  87. PQXX_REGISTER_TEST(test_transaction_for_each);
  88. } // namespace