test61.cxx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <iostream>
  2. #include <pqxx/transaction>
  3. #include "test_helpers.hxx"
  4. using namespace pqxx;
  5. // Example program for libpqxx. Test local variable functionality.
  6. namespace
  7. {
  8. std::string GetDatestyle(transaction_base &T)
  9. {
  10. return T.conn().get_var("DATESTYLE");
  11. }
  12. std::string SetDatestyle(transaction_base &T, std::string style)
  13. {
  14. T.conn().set_session_var("DATESTYLE", style);
  15. std::string const fullname{GetDatestyle(T)};
  16. PQXX_CHECK(
  17. not std::empty(fullname),
  18. "Setting datestyle to " + style + " makes it an empty string.");
  19. return fullname;
  20. }
  21. void RedoDatestyle(
  22. transaction_base &T, std::string const &style, std::string const &expected)
  23. {
  24. PQXX_CHECK_EQUAL(SetDatestyle(T, style), expected, "Set wrong datestyle.");
  25. }
  26. void test_061()
  27. {
  28. connection conn;
  29. work tx{conn};
  30. PQXX_CHECK(not std::empty(GetDatestyle(tx)), "Initial datestyle not set.");
  31. std::string const ISOname{SetDatestyle(tx, "ISO")};
  32. std::string const SQLname{SetDatestyle(tx, "SQL")};
  33. PQXX_CHECK_NOT_EQUAL(ISOname, SQLname, "Same datestyle in SQL and ISO.");
  34. RedoDatestyle(tx, "SQL", SQLname);
  35. // Prove that setting an unknown variable causes an error, as expected
  36. quiet_errorhandler d(tx.conn());
  37. PQXX_CHECK_THROWS(
  38. conn.set_session_var("NONEXISTENT_VARIABLE_I_HOPE", 1), sql_error,
  39. "Setting unknown variable failed to fail.");
  40. }
  41. PQXX_REGISTER_TEST(test_061);
  42. } // namespace