test_column.cxx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <pqxx/transaction>
  2. #include "../test_helpers.hxx"
  3. namespace
  4. {
  5. void test_table_column()
  6. {
  7. pqxx::connection conn;
  8. pqxx::work tx{conn};
  9. tx.exec0("CREATE TEMP TABLE pqxxfoo (x varchar, y integer, z integer)");
  10. tx.exec0("INSERT INTO pqxxfoo VALUES ('xx', 1, 2)");
  11. auto R{tx.exec("SELECT z,y,x FROM pqxxfoo")};
  12. auto X{tx.exec("SELECT x,y,z,99 FROM pqxxfoo")};
  13. pqxx::row::size_type x{R.table_column(2)}, y{R.table_column(1)},
  14. z{R.table_column(static_cast<int>(0))};
  15. PQXX_CHECK_EQUAL(x, 0, "Wrong column number.");
  16. PQXX_CHECK_EQUAL(y, 1, "Wrong column number.");
  17. PQXX_CHECK_EQUAL(z, 2, "Wrong column number.");
  18. x = R.table_column("x");
  19. y = R.table_column("y");
  20. z = R.table_column("z");
  21. PQXX_CHECK_EQUAL(x, 0, "Wrong number for named column.");
  22. PQXX_CHECK_EQUAL(y, 1, "Wrong number for named column.");
  23. PQXX_CHECK_EQUAL(z, 2, "Wrong number for named column.");
  24. pqxx::row::size_type xx{X[0].table_column(static_cast<int>(0))},
  25. yx{X[0].table_column(pqxx::row::size_type(1))}, zx{X[0].table_column("z")};
  26. PQXX_CHECK_EQUAL(xx, 0, "Bad result from table_column(int).");
  27. PQXX_CHECK_EQUAL(yx, 1, "Bad result from table_column(size_type).");
  28. PQXX_CHECK_EQUAL(zx, 2, "Bad result from table_column(string).");
  29. for (pqxx::row::size_type i{0}; i < std::size(R[0]); ++i)
  30. PQXX_CHECK_EQUAL(
  31. R[0][i].table_column(), R.table_column(i),
  32. "Bad result from column_table().");
  33. int col;
  34. PQXX_CHECK_THROWS_EXCEPTION(
  35. col = R.table_column(3), "table_column() with invalid index didn't fail.");
  36. pqxx::ignore_unused(col);
  37. PQXX_CHECK_THROWS_EXCEPTION(
  38. col = R.table_column("nonexistent"),
  39. "table_column() with invalid column name didn't fail.");
  40. pqxx::ignore_unused(col);
  41. PQXX_CHECK_THROWS_EXCEPTION(
  42. col = X.table_column(3), "table_column() on non-table didn't fail.");
  43. pqxx::ignore_unused(col);
  44. }
  45. } // namespace
  46. PQXX_REGISTER_TEST(test_table_column);