test_stateless_cursor.cxx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <pqxx/cursor>
  2. #include <pqxx/transaction>
  3. #include "../test_helpers.hxx"
  4. namespace
  5. {
  6. void test_stateless_cursor()
  7. {
  8. pqxx::connection conn;
  9. pqxx::work tx{conn};
  10. pqxx::stateless_cursor<
  11. pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
  12. empty(tx, "SELECT generate_series(0, -1)", "empty", false);
  13. auto rows{empty.retrieve(0, 0)};
  14. PQXX_CHECK_EQUAL(std::empty(rows), true, "Empty result not empty");
  15. rows = empty.retrieve(0, 1);
  16. PQXX_CHECK_EQUAL(std::size(rows), 0, "Empty result returned rows");
  17. PQXX_CHECK_EQUAL(empty.size(), 0, "Empty cursor not empty");
  18. PQXX_CHECK_THROWS(
  19. empty.retrieve(1, 0), std::out_of_range, "Empty cursor tries to retrieve");
  20. pqxx::stateless_cursor<
  21. pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
  22. stateless(tx, "SELECT generate_series(0, 9)", "stateless", false);
  23. PQXX_CHECK_EQUAL(stateless.size(), 10, "stateless_cursor::size() mismatch");
  24. // Retrieve nothing.
  25. rows = stateless.retrieve(1, 1);
  26. PQXX_CHECK_EQUAL(std::size(rows), 0, "1-to-1 retrieval not empty");
  27. // Retrieve two rows.
  28. rows = stateless.retrieve(1, 3);
  29. PQXX_CHECK_EQUAL(std::size(rows), 2, "Retrieved wrong number of rows");
  30. PQXX_CHECK_EQUAL(rows[0][0].as<int>(), 1, "Data/position mismatch");
  31. PQXX_CHECK_EQUAL(rows[1][0].as<int>(), 2, "Data/position mismatch");
  32. // Retrieve same rows in reverse.
  33. rows = stateless.retrieve(2, 0);
  34. PQXX_CHECK_EQUAL(std::size(rows), 2, "Retrieved wrong number of rows");
  35. PQXX_CHECK_EQUAL(rows[0][0].as<int>(), 2, "Data/position mismatch");
  36. PQXX_CHECK_EQUAL(rows[1][0].as<int>(), 1, "Data/position mismatch");
  37. // Retrieve beyond end.
  38. rows = stateless.retrieve(9, 13);
  39. PQXX_CHECK_EQUAL(std::size(rows), 1, "Row count wrong at end");
  40. PQXX_CHECK_EQUAL(rows[0][0].as<int>(), 9, "Data/pos mismatch at end");
  41. // Retrieve beyond beginning.
  42. rows = stateless.retrieve(0, -4);
  43. PQXX_CHECK_EQUAL(std::size(rows), 1, "Row count wrong at beginning");
  44. PQXX_CHECK_EQUAL(rows[0][0].as<int>(), 0, "Data/pos mismatch at beginning");
  45. // Retrieve entire result set backwards.
  46. rows = stateless.retrieve(10, -15);
  47. PQXX_CHECK_EQUAL(
  48. std::size(rows), 10, "Reverse complete retrieval is broken");
  49. PQXX_CHECK_EQUAL(rows[0][0].as<int>(), 9, "Data mismatch");
  50. PQXX_CHECK_EQUAL(rows[9][0].as<int>(), 0, "Data mismatch");
  51. // Normal usage pattern: step through result set, 4 rows at a time.
  52. rows = stateless.retrieve(0, 4);
  53. PQXX_CHECK_EQUAL(std::size(rows), 4, "Wrong batch size");
  54. PQXX_CHECK_EQUAL(rows[0][0].as<int>(), 0, "Batch in wrong place");
  55. PQXX_CHECK_EQUAL(rows[3][0].as<int>(), 3, "Batch in wrong place");
  56. rows = stateless.retrieve(4, 8);
  57. PQXX_CHECK_EQUAL(std::size(rows), 4, "Wrong batch size");
  58. PQXX_CHECK_EQUAL(rows[0][0].as<int>(), 4, "Batch in wrong place");
  59. PQXX_CHECK_EQUAL(rows[3][0].as<int>(), 7, "Batch in wrong place");
  60. rows = stateless.retrieve(8, 12);
  61. PQXX_CHECK_EQUAL(std::size(rows), 2, "Wrong batch size");
  62. PQXX_CHECK_EQUAL(rows[0][0].as<int>(), 8, "Batch in wrong place");
  63. PQXX_CHECK_EQUAL(rows[1][0].as<int>(), 9, "Batch in wrong place");
  64. }
  65. PQXX_REGISTER_TEST(test_stateless_cursor);
  66. } // namespace