File: test_helpers.hxx

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 (389 lines) | stat: -rw-r--r-- 10,308 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <map>

#include <pqxx/pqxx>


namespace pqxx
{
namespace test
{
class test_failure : public PGSTD::logic_error
{
  const PGSTD::string m_file;
  int m_line;

public:
  test_failure(
	const PGSTD::string &ffile,
	int fline,
	const PGSTD::string &desc);

  ~test_failure() throw ();

  const PGSTD::string &file() const throw () { return m_file; }
  int line() const throw () { return m_line; }
};


/// For backends that don't have generate_series(): sequence of ints
/** If the backend lacks generate_series(), prepares a temp table called
 * series" containing given range of numbers (including lowest and highest).
 *
 * Use select_series() to construct a query selecting a range of numbers.  For
 * the workaround on older backends to work, the ranges of numbers passed to
 * select_series() must be subsets of the range passed here.
 */
void prepare_series(transaction_base &t, int lowest, int highest);


/// Generate query selecting series of numbers from lowest to highest, inclusive
/** Needs to see connection object to determine whether the backend supports
 * generate_series().
 */
PGSTD::string select_series(connection_base &conn, int lowest, int highest);


/// Drop a table, if it exists.
void drop_table(transaction_base &, const PGSTD::string &table);


class base_test;
typedef PGSTD::map<PGSTD::string, base_test *> test_map;

/// Register test (if given); return test_map.
const test_map &register_test(base_test *);


/// Base class for test cases.
class base_test
{
public:
  typedef void (*testfunc)(transaction_base &);

  base_test(const PGSTD::string &tname, testfunc func);

  /// Overridable: run test case.
  virtual void run() =0;

  virtual ~base_test() =0;
  const PGSTD::string &name() const throw () { return m_name; }
private:
  PGSTD::string m_name;
protected:
  testfunc m_func;
};


/// Runner class for libpqxx tests.  Sets up a connection and transaction.
template<typename CONNECTION=connection, typename TRANSACTION=work>
class test_case : public base_test
{
public:
  // func takes connection and transaction as arguments.
  test_case(const PGSTD::string &tname, testfunc func) :
    base_test(tname, func)
  {
  }

  ~test_case() {}

  virtual void run()
  {
    CONNECTION c;
    TRANSACTION t(c, name());

    // Workaround for older backend versions that lack generate_series().
    prepare_series(t, 0, 100);

    m_func(t);
  }
};


// Register a function taking (connection_base &, transaction_base &) as a test.
#define PQXX_REGISTER_TEST(function) \
	namespace \
	{ \
	pqxx::test::test_case<> test(#function, function); \
	}

// Register a test function using given connection and transaction types.
#define PQXX_REGISTER_TEST_CT(function, connection_type, transaction_type) \
	namespace \
	{ \
	pqxx::test::test_case< connection_type, transaction_type > \
		test(#function, function); \
	}

// Register a test function using a given connection type (instead of the
// default "connection").
#define PQXX_REGISTER_TEST_C(function, connection_type) \
	PQXX_REGISTER_TEST_CT(function, connection_type, pqxx::work)

// Register a test function using a given transaction type (default is "work").
#define PQXX_REGISTER_TEST_T(function, transaction_type) \
	PQXX_REGISTER_TEST_CT(function, pqxx::connection, transaction_type)


// Register test function that takes a nullconnection and nontransaction.
#define PQXX_REGISTER_TEST_NODB(function) \
	PQXX_REGISTER_TEST_CT( \
		function, \
		pqxx::nullconnection, \
		pqxx::nontransaction)


// Unconditional test failure.
#define PQXX_CHECK_NOTREACHED(desc) \
	pqxx::test::check_notreached(__FILE__, __LINE__, (desc))
void check_notreached(const char file[], int line, PGSTD::string desc);

// Verify that a condition is met, similar to assert()
#define PQXX_CHECK(condition, desc) \
	pqxx::test::check(__FILE__, __LINE__, (condition), #condition, (desc))
void check(
	const char file[],
	int line,
	bool condition,
	const char text[], 
	PGSTD::string desc);

// Verify that variable has the expected value.
#define PQXX_CHECK_EQUAL(actual, expected, desc) \
	pqxx::test::check_equal( \
		__FILE__, \
		__LINE__, \
		(actual), \
		#actual, \
		(expected), \
		#expected, \
		(desc))
template<typename ACTUAL, typename EXPECTED>
inline void check_equal(
	const char file[],
	int line,
	ACTUAL actual,
	const char actual_text[],
	EXPECTED expected, 
	const char expected_text[],
	PGSTD::string desc)
{
  if (expected == actual) return;
  const PGSTD::string fulldesc =
	desc + " (" + actual_text + " <> " + expected_text + ": "
	"expected=" + to_string(expected) + ", "
	"actual=" + to_string(actual) + ")";
  throw test_failure(file, line, fulldesc);
}

// Verify that two values are not equal.
#define PQXX_CHECK_NOT_EQUAL(value1, value2, desc) \
	pqxx::test::check_not_equal( \
		__FILE__, \
		__LINE__, \
		(value1), \
		#value1, \
		(value2), \
		#value2, \
		(desc))
template<typename VALUE1, typename VALUE2>
inline void check_not_equal(
	const char file[],
	int line,
	VALUE1 value1,
	const char text1[],
	VALUE2 value2, 
	const char text2[],
	PGSTD::string desc)
{
  if (value1 != value2) return;
  const PGSTD::string fulldesc =
	desc + " (" + text1 + " == " + text2 + ": "
	"both are " + to_string(value2) + ")";
  throw test_failure(file, line, fulldesc);
}


// Verify that value1 is less than value2.
#define PQXX_CHECK_LESS(value1, value2, desc) \
	pqxx::test::check_less( \
		__FILE__, \
		__LINE__, \
		(value1), \
		#value1, \
		(value2), \
		#value2, \
		(desc))
template<typename VALUE1, typename VALUE2>
inline void check_less(
	const char file[],
	int line,
	VALUE1 value1,
	const char text1[],
	VALUE2 value2,
	const char text2[],
	PGSTD::string desc)
{
  if (value1 < value2) return;
  const PGSTD::string fulldesc =
	desc + " (" + text1 + " >= " + text2 + ": "
	"\"lower\"=" + to_string(value1) + ", "
	"\"upper\"=" + to_string(value2) + ")";
  throw test_failure(file, line, fulldesc);
}


struct failure_to_fail {};


namespace internal
{
/// Syntactic placeholder: require (and accept) semicolon after block.
inline void end_of_statement()
{
}
}


// Verify that "action" throws "exception_type."
#define PQXX_CHECK_THROWS(action, exception_type, desc) \
	{ \
	  try \
	  { \
	    action ; \
	    throw pqxx::test::failure_to_fail(); \
	  } \
	  catch (const pqxx::test::failure_to_fail &) \
	  { \
	    PQXX_CHECK_NOTREACHED( \
		PGSTD::string(desc) + \
		" (\"" #action "\" did not throw " #exception_type ")"); \
	  } \
	  catch (const exception_type &) {} \
	  catch (...) \
	  { \
	    PQXX_CHECK_NOTREACHED( \
		PGSTD::string(desc) + \
		" (\"" #action "\" " \
		"threw exception other than " #exception_type ")"); \
	  } \
	} \
        pqxx::test::internal::end_of_statement()

#define PQXX_CHECK_BOUNDS(value, lower, upper, desc) \
	pqxx::test::check_bounds( \
		__FILE__, \
		 __LINE__, \
		(value), \
		#value, \
		(lower), \
		#lower, \
		(upper), \
		#upper, \
		(desc))
template<typename VALUE, typename LOWER, typename UPPER>
inline void check_bounds(
	const char file[],
	int line,
	VALUE value,
	const char text[],
	LOWER lower,
	const char lower_text[],
	UPPER upper,
	const char upper_text[],
	const PGSTD::string &desc)
{
  const PGSTD::string
	range_check = PGSTD::string(lower_text) + " < " + upper_text,
	lower_check = PGSTD::string("!(") + text + " < " + lower_text + ")",
	upper_check = PGSTD::string(text) + " < " + upper_text;

  pqxx::test::check(
	file,
	line,
	lower < upper,
	range_check.c_str(),
	desc + " (acceptable range is empty; value was " + text + ")");
  pqxx::test::check(
	file,
	line,
	!(value < lower),
	lower_check.c_str(),
	desc + " (" + text + " is below lower bound " + lower_text + ")");
  pqxx::test::check(
	file,
	line,
	value < upper,
	upper_check.c_str(),
	desc + " (" + text + " is not below upper bound " + upper_text + ")");
}


// Report expected exception
void expected_exception(const PGSTD::string &);


// Represent result tuple as string
PGSTD::string list_tuple(tuple);
// Represent result as string
PGSTD::string list_result(result);
// Represent result iterator as string
PGSTD::string list_result_iterator(result::const_iterator);


// @deprecated Set up test data for legacy tests.
void create_pqxxevents(transaction_base &);
} // namespace test


// Support string conversion on result rows for debug output.
template<> struct string_traits<tuple>
{
  static const char *name() { return "pqxx::tuple"; }
  static bool has_null() { return false; }
  static bool is_null(tuple) { return false; }
  static result null(); // Not needed
  static void from_string(const char Str[], result &Obj); // Not needed
  static PGSTD::string to_string(tuple Obj)
	{ return pqxx::test::list_tuple(Obj); }
};

// Support string conversion on result objects for debug output.
template<> struct string_traits<result>
{
  static const char *name() { return "pqxx::result"; }
  static bool has_null() { return true; }
  static bool is_null(result r) { return r.empty(); }
  static result null() { return result(); }
  static void from_string(const char Str[], result &Obj); // Not needed
  static PGSTD::string to_string(result Obj)
	{ return pqxx::test::list_result(Obj); }
};

// Support string conversion on result::const_iterator for debug output.
template<> struct string_traits<result::const_iterator>
{
  typedef result::const_iterator subject_type;
  static const char *name() { return "pqxx::result::const_iterator"; }
  static bool has_null() { return false; }
  static bool is_null(subject_type) { return false; }
  static result null(); // Not needed
  static void from_string(const char Str[], subject_type &Obj); // Not needed
  static PGSTD::string to_string(subject_type Obj)
	{ return pqxx::test::list_result_iterator(Obj); }
};


// Support string conversion on vector<string> for debug output.
template<> struct string_traits<PGSTD::vector<PGSTD::string> >
{
  typedef PGSTD::vector<PGSTD::string> subject_type;
  static const char *name() { return "vector<string>"; }
  static bool has_null() { return false; }
  static bool is_null(subject_type) { return false; }
  static subject_type null(); // Not needed
  static void from_string(const char Str[], subject_type &Obj); // Not needed
  static PGSTD::string to_string(const subject_type &Obj)
				 { return separated_list("; ", Obj); }
};
} // namespace pqxx