test_row.cxx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <pqxx/transaction>
  2. #include "../test_helpers.hxx"
  3. namespace
  4. {
  5. void test_row()
  6. {
  7. pqxx::connection conn;
  8. pqxx::work tx{conn};
  9. pqxx::result rows{tx.exec("SELECT 1, 2, 3")};
  10. pqxx::row r{rows[0]};
  11. PQXX_CHECK_EQUAL(std::size(r), 3, "Unexpected row size.");
  12. PQXX_CHECK_EQUAL(r.at(0).as<int>(), 1, "Wrong value at index 0.");
  13. PQXX_CHECK(std::begin(r) != std::end(r), "Broken row iteration.");
  14. PQXX_CHECK(std::begin(r) < std::end(r), "Row begin does not precede end.");
  15. PQXX_CHECK(std::cbegin(r) == std::begin(r), "Wrong cbegin.");
  16. PQXX_CHECK(std::cend(r) == std::end(r), "Wrong cend.");
  17. PQXX_CHECK(std::rbegin(r) != std::rend(r), "Broken reverse row iteration.");
  18. PQXX_CHECK(std::crbegin(r) == std::rbegin(r), "Wrong crbegin.");
  19. PQXX_CHECK(std::crend(r) == std::rend(r), "Wrong crend.");
  20. PQXX_CHECK_EQUAL(r.front().as<int>(), 1, "Wrong row front().");
  21. PQXX_CHECK_EQUAL(r.back().as<int>(), 3, "Wrong row back().");
  22. }
  23. void test_row_iterator()
  24. {
  25. pqxx::connection conn;
  26. pqxx::work tx{conn};
  27. pqxx::result rows{tx.exec("SELECT 1, 2, 3")};
  28. auto i{std::begin(rows[0])};
  29. PQXX_CHECK_EQUAL(i->as<int>(), 1, "Row iterator is wrong.");
  30. auto i2{i};
  31. PQXX_CHECK_EQUAL(i2->as<int>(), 1, "Row iterator copy is wrong.");
  32. i2++;
  33. PQXX_CHECK_EQUAL(i2->as<int>(), 2, "Row iterator increment is wrong.");
  34. pqxx::row::const_iterator i3;
  35. i3 = i2;
  36. PQXX_CHECK_EQUAL(i3->as<int>(), 2, "Row iterator assignment is wrong.");
  37. auto r{std::rbegin(rows[0])};
  38. PQXX_CHECK_EQUAL(r->as<int>(), 3, "Row reverse iterator is wrong.");
  39. auto r2{r};
  40. PQXX_CHECK_EQUAL(r2->as<int>(), 3, "Row reverse iterator copy is wrong.");
  41. r2++;
  42. PQXX_CHECK_EQUAL(
  43. r2->as<int>(), 2, "Row reverse iterator increment is wrong.");
  44. pqxx::row::const_reverse_iterator r3;
  45. r3 = r2;
  46. PQXX_CHECK_EQUAL(
  47. i3->as<int>(), 2, "Row reverse iterator assignment is wrong.");
  48. }
  49. void test_row_as()
  50. {
  51. using pqxx::operator"" _zv;
  52. pqxx::connection conn;
  53. pqxx::work tx{conn};
  54. pqxx::row const r{tx.exec1("SELECT 1, 2, 3")};
  55. auto [one, two, three]{r.as<int, float, pqxx::zview>()};
  56. static_assert(std::is_same_v<decltype(one), int>);
  57. static_assert(std::is_same_v<decltype(two), float>);
  58. static_assert(std::is_same_v<decltype(three), pqxx::zview>);
  59. PQXX_CHECK_EQUAL(one, 1, "row::as() did not produce the right int.");
  60. PQXX_CHECK_GREATER(two, 1.9, "row::as() did not produce the right float.");
  61. PQXX_CHECK_LESS(two, 2.1, "row::as() did not produce the right float.");
  62. PQXX_CHECK_EQUAL(
  63. three, "3"_zv, "row::as() did not produce the right zview.");
  64. PQXX_CHECK_EQUAL(
  65. std::get<0>(tx.exec1("SELECT 999").as<int>()), 999,
  66. "Unary tuple did not extract right.");
  67. }
  68. PQXX_REGISTER_TEST(test_row);
  69. PQXX_REGISTER_TEST(test_row_iterator);
  70. PQXX_REGISTER_TEST(test_row_as);
  71. } // namespace