test71.cxx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <iostream>
  2. #include <map>
  3. #include <pqxx/pipeline>
  4. #include <pqxx/transaction>
  5. #include "test_helpers.hxx"
  6. using namespace pqxx;
  7. // Test program for libpqxx. Issue queries through a pipeline, and retrieve
  8. // results both in-order and out-of-order.
  9. namespace
  10. {
  11. using Exp = std::map<pipeline::query_id, int>;
  12. template<typename PAIR> void checkresult(pipeline &P, PAIR c)
  13. {
  14. result const r{P.retrieve(c.first)};
  15. int const val{r.at(0).at(0).as(int(0))};
  16. PQXX_CHECK_EQUAL(val, c.second, "Wrong result from pipeline.");
  17. }
  18. void test_071()
  19. {
  20. connection conn;
  21. work tx{conn};
  22. pipeline P(tx);
  23. // Keep expected result for every query we issue
  24. Exp values;
  25. // Insert queries returning various numbers.
  26. for (int i{1}; i < 10; ++i) values[P.insert("SELECT " + to_string(i))] = i;
  27. // Retrieve results in query_id order, and compare to expected values
  28. for (auto &c : values) checkresult(P, c);
  29. PQXX_CHECK(std::empty(P), "Pipeline was not empty retrieving all results.");
  30. values.clear();
  31. // Insert more queries returning various numbers
  32. P.retain(20);
  33. for (int i{100}; i > 90; --i) values[P.insert("SELECT " + to_string(i))] = i;
  34. P.resume();
  35. // Retrieve results in reverse order
  36. for (auto c{std::rbegin(values)}; c != std::rend(values); ++c)
  37. checkresult(P, *c);
  38. values.clear();
  39. P.retain(10);
  40. for (int i{1010}; i > 1000; --i)
  41. values[P.insert("SELECT " + to_string(i))] = i;
  42. for (auto &c : values)
  43. {
  44. if (P.is_finished(c.first))
  45. std::cout << "Query #" << c.first << " completed despite retain()"
  46. << std::endl;
  47. }
  48. // See that all results are retrieved by complete()
  49. P.complete();
  50. for (auto &c : values)
  51. PQXX_CHECK(P.is_finished(c.first), "Query not finished after complete().");
  52. }
  53. } // namespace
  54. PQXX_REGISTER_TEST(test_071);