File: test039.cxx

package info (click to toggle)
libpqxx 4.0.1%2Bdfsg3-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,012 kB
  • ctags: 9,469
  • sloc: sh: 11,289; cpp: 10,801; xml: 1,256; makefile: 287; ansic: 195; python: 159
file content (75 lines) | stat: -rw-r--r-- 2,163 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "test_helpers.hxx"

using namespace PGSTD;
using namespace pqxx;


// Test program for libpqxx.  Modify the database inside a NonTransaction, and
// verify that the change gets made regardless of whether the NonTransaction is
// eventually committed or aborted.  A lazy connection is used.
namespace
{
int BoringYear = 1977;


void test_039(transaction_base &T1)
{
  test::create_pqxxevents(T1);
  connection_base &C(T1.conn());
  const string Table = "pqxxevents";


  // Verify our start condition before beginning: there must not be a 1977
  // record already.
  result R( T1.exec("SELECT * FROM " + Table + " "
	              "WHERE year=" + to_string(BoringYear)) );

  PQXX_CHECK_EQUAL(
	R.size(),
	0u,
	"Already have a row for " + to_string(BoringYear) + ", cannot test.");

  // (Not needed, but verify that clear() works on empty containers)
  R.clear();
  PQXX_CHECK(R.empty(), "Result is non-empty after clear().");

  // OK.  Having laid that worry to rest, add a record for 1977.
  T1.exec("INSERT INTO " + Table + " VALUES"
            "(" +
	    to_string(BoringYear) + ","
	    "'Yawn'"
	    ")");

  // Abort T1.  Since T1 is a NonTransaction, which provides only the
  // transaction class interface without providing any form of transactional
  // integrity, this is not going to undo our work.
  T1.abort();

  // Verify that our record was added, despite the Abort()
  nontransaction T2(C, "T2");
  R = T2.exec("SELECT * FROM " + Table + " "
		"WHERE year=" + to_string(BoringYear));
  PQXX_CHECK_EQUAL(R.size(), 1u, "Unexpected result size.");

  PQXX_CHECK(R.capacity() >= R.size(), "Result's capacity is too small.");

  R.clear();
  PQXX_CHECK(R.empty(), "result::clear() is broken.");

  // Now remove our record again
  T2.exec("DELETE FROM " + Table + " "
	    "WHERE year=" + to_string(BoringYear));

  T2.commit();

  // And again, verify results
  nontransaction T3(C, "T3");

  R = T3.exec("SELECT * FROM " + Table + " "
	        "WHERE year=" + to_string(BoringYear));

  PQXX_CHECK_EQUAL(R.size(), 0u, "Record is not gone as expected.");
}
} // namespace

PQXX_REGISTER_TEST_CT(test_039, lazyconnection, nontransaction)