test60.cxx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <iostream>
  2. #include <pqxx/nontransaction>
  3. #include <pqxx/pipeline>
  4. #include <pqxx/transaction>
  5. #include "test_helpers.hxx"
  6. using namespace pqxx;
  7. // Example program for libpqxx. Test session variable functionality.
  8. namespace
  9. {
  10. std::string GetDatestyle(connection &conn)
  11. {
  12. return conn.get_var("DATESTYLE");
  13. }
  14. std::string SetDatestyle(connection &conn, std::string style)
  15. {
  16. conn.set_session_var("DATESTYLE", style);
  17. std::string const fullname{GetDatestyle(conn)};
  18. PQXX_CHECK(
  19. not std::empty(fullname),
  20. "Setting datestyle to " + style + " makes it an empty string.");
  21. return fullname;
  22. }
  23. void CheckDatestyle(connection &conn, std::string expected)
  24. {
  25. PQXX_CHECK_EQUAL(GetDatestyle(conn), expected, "Got wrong datestyle.");
  26. }
  27. void RedoDatestyle(
  28. connection &conn, std::string const &style, std::string const &expected)
  29. {
  30. PQXX_CHECK_EQUAL(
  31. SetDatestyle(conn, style), expected, "Set wrong datestyle.");
  32. }
  33. void ActivationTest(
  34. connection &conn, std::string const &style, std::string const &expected)
  35. {
  36. RedoDatestyle(conn, style, expected);
  37. CheckDatestyle(conn, expected);
  38. }
  39. void test_060()
  40. {
  41. connection conn;
  42. PQXX_CHECK(not std::empty(GetDatestyle(conn)), "Initial datestyle not set.");
  43. std::string const ISOname{SetDatestyle(conn, "ISO")};
  44. std::string const SQLname{SetDatestyle(conn, "SQL")};
  45. PQXX_CHECK_NOT_EQUAL(ISOname, SQLname, "Same datestyle in SQL and ISO.");
  46. RedoDatestyle(conn, "SQL", SQLname);
  47. ActivationTest(conn, "ISO", ISOname);
  48. ActivationTest(conn, "SQL", SQLname);
  49. PQXX_CHECK_THROWS(
  50. conn.set_session_var("bonjour_name", std::optional<std::string>{}),
  51. pqxx::variable_set_to_null,
  52. "Setting a variable to null did not report the error correctly.");
  53. // Prove that setting an unknown variable causes an error, as expected
  54. quiet_errorhandler d{conn};
  55. PQXX_CHECK_THROWS(
  56. conn.set_session_var("NONEXISTENT_VARIABLE_I_HOPE", 1), sql_error,
  57. "Setting unknown variable failed to fail.");
  58. }
  59. PQXX_REGISTER_TEST(test_060);
  60. } // namespace