test_cursor.cxx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <pqxx/cursor>
  2. #include <pqxx/transaction>
  3. #include "../test_helpers.hxx"
  4. namespace
  5. {
  6. void test_stateless_cursor_provides_random_access(pqxx::connection &conn)
  7. {
  8. pqxx::work tx{conn};
  9. pqxx::stateless_cursor<
  10. pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
  11. c{tx, "SELECT * FROM generate_series(0, 3)", "count", false};
  12. auto r{c.retrieve(1, 2)};
  13. PQXX_CHECK_EQUAL(std::size(r), 1, "Wrong number of rows from retrieve().");
  14. PQXX_CHECK_EQUAL(r[0][0].as<int>(), 1, "Cursor retrieved wrong data.");
  15. r = c.retrieve(3, 10);
  16. PQXX_CHECK_EQUAL(std::size(r), 1, "Expected 1 row retrieving past end.");
  17. PQXX_CHECK_EQUAL(r[0][0].as<int>(), 3, "Wrong data retrieved at end.");
  18. r = c.retrieve(0, 1);
  19. PQXX_CHECK_EQUAL(std::size(r), 1, "Wrong number of rows back at beginning.");
  20. PQXX_CHECK_EQUAL(r[0][0].as<int>(), 0, "Wrong data back at beginning.");
  21. }
  22. void test_stateless_cursor_ignores_trailing_semicolon(pqxx::connection &conn)
  23. {
  24. pqxx::work tx{conn};
  25. pqxx::stateless_cursor<
  26. pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
  27. c{tx, "SELECT * FROM generate_series(0, 3) ;; ; \n \t ", "count", false};
  28. auto r{c.retrieve(1, 2)};
  29. PQXX_CHECK_EQUAL(std::size(r), 1, "Trailing semicolon confused retrieve().");
  30. }
  31. void test_cursor()
  32. {
  33. pqxx::connection conn;
  34. test_stateless_cursor_provides_random_access(conn);
  35. test_stateless_cursor_ignores_trailing_semicolon(conn);
  36. }
  37. PQXX_REGISTER_TEST(test_cursor);
  38. } // namespace