summaryrefslogtreecommitdiff
path: root/libpqpp/unittests/testpq.cpp
blob: 56ab20fe66bebff03fd7119cb207523f7286fb7a (plain)
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#define BOOST_TEST_MODULE TestPQ
#include <boost/test/unit_test.hpp>

#include <definedDirs.h>
#include <modifycommand.h>
#include <selectcommand.h>
#include <column.h>
#include <pq-mock.h>
#include <testCore.h>
#include <fstream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <pq-error.h>
#include <pq-connection.h>
#include <pq-command.h>
#include <selectcommandUtil.impl.h>
#include <fileUtils.h>

class StandardMockDatabase : public PQ::Mock {
	public:
		StandardMockDatabase() : PQ::Mock("user=postgres dbname=postgres", "PQmock", {
				rootDir / "pqschema.sql" })
		{
		}
};

BOOST_GLOBAL_FIXTURE( StandardMockDatabase );

BOOST_FIXTURE_TEST_SUITE( Core, DB::TestCore );

BOOST_AUTO_TEST_CASE( transactions )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");

	BOOST_REQUIRE_EQUAL(false, ro->inTx());
	ro->beginTx();
	BOOST_REQUIRE_EQUAL(true, ro->inTx());
	ro->rollbackTx();
	BOOST_REQUIRE_EQUAL(false, ro->inTx());

	ro->beginTx();
	BOOST_REQUIRE_EQUAL(true, ro->inTx());
	ro->commitTx();
	BOOST_REQUIRE_EQUAL(false, ro->inTx());

	delete ro;
}

BOOST_AUTO_TEST_CASE( bindAndSend )
{
	auto rw = DB::MockDatabase::openConnectionTo("PQmock");

	auto mod = rw->newModifyCommand("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)");
	mod->bindParamI(0, testInt);
	mod->bindParamF(1, testDouble);
	mod->bindParamS(2, testString);
	mod->bindParamB(3, testBool);
	mod->bindParamT(4, testDateTime);
	mod->bindParamT(5, testInterval);
	mod->execute();
	mod->bindParamI(0, (unsigned int)(testInt + 10));
	mod->bindParamF(1, (float)(testDouble + 10));
	mod->bindParamS(2, testString + " something");
	mod->bindParamB(3, true);
	mod->bindParamT(4, testDateTime);
	mod->bindParamT(5, testInterval);
	mod->execute();
	mod->bindNull(0);
	mod->bindParamI(1, (long long unsigned int)(testDouble + 10));
	mod->bindParamI(2, (long long int)testInt);
	mod->bindParamB(3, true);
	mod->bindNull(4);
	mod->bindNull(5);
	mod->execute();
	mod->bindNull(0);
	mod->bindParamI(1, (long unsigned int)(testDouble + 10));
	mod->bindParamI(2, (long int)testInt);
	mod->bindParamB(3, true);
	mod->bindParamS(4, "2016-01-01T12:34:56");
	mod->bindNull(5);
	mod->execute();
	delete mod;
	mod = rw->newModifyCommand("DELETE FROM test WHERE string = '?'");
	BOOST_REQUIRE_THROW(mod->execute(false), DB::NoRowsAffected);
	BOOST_REQUIRE_EQUAL(0, mod->execute(true));
	delete mod;
	delete rw;
}

BOOST_AUTO_TEST_CASE( bindAndSelect )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");

	auto select = ro->newSelectCommand("SELECT * FROM test WHERE id = ?");
	select->bindParamI(0, testInt);
	select->execute();
	int rows = 0;
	while (select->fetch()) {
		assertColumnValueHelper(*select, 0, testInt);
		assertColumnValueHelper(*select, 1, testDouble);
		assertColumnValueHelper(*select, 2, testString);
		assertColumnValueHelper(*select, 3, testBool);
		assertColumnValueHelper(*select, 4, testDateTime);
		assertColumnValueHelper(*select, 5, testInterval);
		rows += 1;
	}
	delete select;
	BOOST_REQUIRE_EQUAL(1, rows);
	delete ro;
}

BOOST_AUTO_TEST_CASE( selectInTx )
{
	auto db = DB::MockDatabase::openConnectionTo("PQmock");

	// Loop to ensure we can create the same statement several times
	for (int x = 0; x < 2; x++) {
		auto select = db->select("SELECT * FROM test");
		// Loop to ensure we can use the same command several times
		for (int y = 0; y < 2; y++) {
			while (select->fetch()) { }
		}
	}
	db->finish();

	db->beginTx();
	auto select = db->newSelectCommand("SELECT * FROM test");
	while (select->fetch()) { }
	delete select;
	db->commitTx();
	db->finish();

	delete db;
}

BOOST_AUTO_TEST_CASE( bindAndSelectOther )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");

	auto select = ro->newSelectCommand("SELECT * FROM test WHERE id != ? AND id != ?");
	select->bindParamI(0, testInt);
	select->bindParamI(1, testInt + 10);
	select->execute();
	int rows = 0;
	while (select->fetch()) {
		assertColumnValueHelper(*select, 0, 4);
		assertColumnValueHelper(*select, 1, 123.45);
		assertColumnValueHelper(*select, 2, std::string("some text with a ; in it and a ' too"));
		assertColumnValueHelper(*select, 3, true);
		assertColumnValueHelper(*select, 4, boost::posix_time::ptime_from_tm({ 3, 6, 23, 27, 3, 115, 0, 0, 0, 0, 0}));
		assertColumnValueHelper(*select, 5, boost::posix_time::time_duration(38, 13, 12));
		rows += 1;
	}
	delete select;
	BOOST_REQUIRE_EQUAL(1, rows);
	delete ro;
}

BOOST_AUTO_TEST_CASE( testP2MockScriptDir )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");

	auto select = ro->newSelectCommand("SELECT path FROM test2");
	select->execute();
	while (select->fetch()) {
		std::string path;
		(*select)[0] >> path;
		BOOST_REQUIRE(boost::filesystem::exists(path));
	}
	delete select;
	delete ro;
}

BOOST_AUTO_TEST_CASE( bulkload )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");

	auto count = ro->newSelectCommand("SELECT COUNT(*) FROM bulktest");
	// Test empty
	ro->beginBulkUpload("bulktest", "");
	ro->endBulkUpload(NULL);
	assertScalarValueHelper(*count, 0);
	// Test sample file
	ro->beginBulkUpload("bulktest", "");
	std::ifstream in((rootDir / "bulk.sample").string());
	if (!in.good()) throw std::runtime_error("Couldn't open bulk.sample");
	char buf[BUFSIZ];
	for (std::streamsize r; (r = in.readsome(buf, sizeof(buf))) > 0; ) {
		ro->bulkUploadData(buf, r);
	}
	ro->endBulkUpload(NULL);
	assertScalarValueHelper(*count, 800);

	delete count;
	delete ro;
}

BOOST_AUTO_TEST_CASE( nofetch )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");
	auto count = ro->newSelectCommand("SELECT * FROM bulktest");
	count->execute();
	delete count;
	delete ro;
}

BOOST_AUTO_TEST_CASE( bigIterate )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");

	auto count = ro->newSelectCommand("SELECT * FROM bulktest");
	unsigned int rows = 0;
	while (count->fetch()) {
		rows += 1;
	}
	BOOST_REQUIRE_EQUAL(800, rows);

	delete count;
	delete ro;
}

BOOST_AUTO_TEST_CASE( insertId )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");
	auto ins = ro->newModifyCommand("INSERT INTO idtest(foo) VALUES(1)");
	for (int x = 1; x < 4; x++) {
		ins->execute();
		BOOST_REQUIRE_EQUAL(x, ro->insertId());
	}
	delete ins;
	delete ro;
}

BOOST_AUTO_TEST_CASE( reconnect )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");
	auto rok = DB::MockDatabase::openConnectionTo("PQmock");
	auto pqconn = dynamic_cast<PQ::Connection *>(ro);
	int pid1 = PQbackendPID(pqconn->conn);
	BOOST_REQUIRE(pid1);
	ro->ping();
	ro->modify("TRUNCATE TABLE test")->execute();
	auto kil = rok->newModifyCommand("SELECT pg_terminate_backend(?)");
	kil->bindParamI(0, pid1);
	kil->execute();
	delete kil;
	usleep(5000);
	ro->ping();
	int pid2 = PQbackendPID(pqconn->conn);
	BOOST_REQUIRE(pid2);
	BOOST_REQUIRE(pid1 != pid2);
	ro->modify("TRUNCATE TABLE test")->execute();
	delete ro;
	delete rok;
}

BOOST_AUTO_TEST_CASE( reconnectInTx )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");
	auto rok = DB::MockDatabase::openConnectionTo("PQmock");
	auto pqconn = dynamic_cast<PQ::Connection *>(ro);
	int pid1 = PQbackendPID(pqconn->conn);
	BOOST_REQUIRE(pid1);
	ro->ping();
	ro->beginTx();
	auto kil = rok->newModifyCommand("SELECT pg_terminate_backend(?)");
	kil->bindParamI(0, pid1);
	kil->execute();
	delete kil;
	usleep(5000);
	BOOST_REQUIRE_THROW(ro->ping(), DB::ConnectionError);
	delete ro;
	delete rok;
}

BOOST_AUTO_TEST_CASE( statementReuse )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");
	auto pqconn = dynamic_cast<PQ::Connection *>(ro);
	BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 0);
	ro->modify("DELETE FROM test")->execute();
	BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 1);
	for (int y = 0; y < 4; y += 1) {
		auto m1 = ro->modify("INSERT INTO test(id) VALUES(?)");
		BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), y == 0 ? 1 : 2);
		for (int x = 0; x < 4; x += 1) {
			m1->bindParamI(0, x);
			m1->execute();
		}
	}
	BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 2);
	auto select = ro->newSelectCommand("SELECT COUNT(id), SUM(id) FROM test");
	while (select->fetch()) {
		assertColumnValueHelper(*select, 0, 16);
		assertColumnValueHelper(*select, 1, 24);
	}
	delete select;
	delete ro;
}

BOOST_AUTO_TEST_CASE( bulkSelect )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");
	PQ::CommandOptions co(0, 35, false);
	auto sel = ro->newSelectCommand("SELECT * FROM test WHERE id > ?", &co);
	sel->bindParamI(0, 1);
	int totalInt = 0, count = 0;
	sel->forEachRow<int64_t>([&totalInt, &count](auto i) {
			totalInt += i;
			count += 1;
		});
	delete sel;
	BOOST_REQUIRE_EQUAL(20, totalInt);
	BOOST_REQUIRE_EQUAL(8, count);
	delete ro;
}

BOOST_AUTO_TEST_CASE( selectWithSmallPages )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");
	PQ::CommandOptions co(0, 1, true);
	auto sel = ro->newSelectCommand("SELECT * FROM test WHERE id > ?", &co);
	sel->bindParamI(0, 1);
	int totalInt = 0, count = 0;
	sel->forEachRow<int64_t>([&totalInt, &count](auto i) {
			totalInt += i;
			count += 1;
		});
	delete sel;
	BOOST_REQUIRE_EQUAL(20, totalInt);
	BOOST_REQUIRE_EQUAL(8, count);
	delete ro;
}

BOOST_AUTO_TEST_CASE( dateoid )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");
	PQ::CommandOptions co(0, 1, false);
	auto sel = ro->select("SELECT '2017-01-08'::date", &co);
	for (const auto & r : sel->as<boost::posix_time::ptime>()) {
		BOOST_REQUIRE_EQUAL(boost::posix_time::ptime(boost::gregorian::date(2017, 1, 8)), r.value<0>());
	}
	delete ro;
}

BOOST_AUTO_TEST_CASE( insertReturning )
{
	auto ro = DB::MockDatabase::openConnectionTo("PQmock");
	PQ::CommandOptions co(0, 35, false);
	auto sel = ro->newSelectCommand("INSERT INTO test(id, fl) VALUES(1, 3) RETURNING id + fl", &co);
	int totalInt = 0, count = 0;
	sel->forEachRow<int64_t>([&totalInt, &count](auto i) {
			totalInt += i;
			count += 1;
		});
	delete sel;
	BOOST_REQUIRE_EQUAL(4, totalInt);
	BOOST_REQUIRE_EQUAL(1, count);
	delete ro;
}

BOOST_AUTO_TEST_CASE( closeOnError )
{
	auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock"));
	BOOST_REQUIRE_THROW({
			ro->select("SELECT * FROM test")->forEachRow<>([&ro](){
					ro->execute("nonsense");
				});
			}, DB::Error);
	BOOST_REQUIRE_THROW({
			ro->select("SELECT * FROM test")->forEachRow<>([&ro](){
				ro->select("SELECT * FROM test")->forEachRow<>([&ro](){
						ro->execute("nonsense");
					});
				});
			}, DB::Error);
	ro->beginTx();
	BOOST_REQUIRE_THROW({
			ro->select("SELECT * FROM test")->forEachRow<>([&ro](){
					ro->execute("nonsense");
				});
			}, DB::Error);
	BOOST_REQUIRE_THROW({
			ro->select("SELECT * FROM test")->forEachRow<>([&ro](){
				ro->select("SELECT * FROM test")->forEachRow<>([&ro](){
						ro->execute("nonsense");
					});
				});
			}, DB::Error);
	ro->commitTx();
}

BOOST_AUTO_TEST_CASE( blobs )
{
	auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock"));
	std::vector<char> buf(29);
	memcpy(&buf[0], "This is some binary text data", 29);
	auto ins = ro->modify("INSERT INTO blobtest(data) VALUES(?)");
	DB::Blob blob(buf);
	ins->bindParamBLOB(0, blob);
	ins->execute();

	ro->execute("UPDATE blobtest SET md5 = md5(data)");

	auto sel = ro->select("SELECT data, md5, length(data) FROM blobtest");
	for (const auto & r : sel->as<DB::Blob, std::string, int64_t>()) {
		// Assert the DB understood the insert
		BOOST_REQUIRE_EQUAL(r.value<2>(), buf.size());
		BOOST_REQUIRE_EQUAL(r.value<1>(), "37c7c3737f93e8d17e845deff8fa74d2");
		// Assert the fetch of the data is correct
		BOOST_REQUIRE_EQUAL(r.value<0>().len, buf.size());
		std::string str((const char *)r.value<0>().data, r.value<0>().len);
		BOOST_REQUIRE_EQUAL(str, "This is some binary text data");
		BOOST_REQUIRE_EQUAL(r.value<0>(), blob);
	}
}

BOOST_AUTO_TEST_CASE( fetchAsBinary )
{
	auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock"));
	std::vector<char> buf(29);
	memcpy(&buf[0], "This is some binary text data", 29);
	DB::Blob blob(buf);
	PQ::CommandOptions opts(0);
	opts.fetchBinary = true;
	opts.useCursor = false;
	auto sel = ro->select("SELECT data, md5, length(data) FROM blobtest", &opts);
	for (const auto & r : sel->as<DB::Blob, boost::optional<std::string>, int64_t>()) {
		// Assert the DB understood the insert
		BOOST_REQUIRE_EQUAL(r.value<2>(), buf.size());
		BOOST_REQUIRE(r.value<1>());
		BOOST_REQUIRE_EQUAL(*r.value<1>(), "37c7c3737f93e8d17e845deff8fa74d2");
		// Assert the fetch of the data is correct
		BOOST_REQUIRE_EQUAL(r.value<0>(), blob);
	}
	*opts.hash += 1;
	sel = ro->select("SELECT CAST(length(data) AS BIGINT) big, CAST(length(data) AS SMALLINT) small FROM blobtest", &opts);
	for (const auto & r : sel->as<int64_t, int64_t>()) {
		BOOST_REQUIRE_EQUAL(r.value<0>(), buf.size());
		BOOST_REQUIRE_EQUAL(r.value<1>(), buf.size());
	}
	*opts.hash += 1;
	sel = ro->select("SELECT true a, false b", &opts);
	for (const auto & r : sel->as<bool, bool>()) {
		BOOST_REQUIRE_EQUAL(r.value<0>(), true);
		BOOST_REQUIRE_EQUAL(r.value<1>(), false);
	}
	*opts.hash += 1;
	sel = ro->select("SELECT xmlelement(name xml)", &opts);
	for (const auto & r : sel->as<std::string>()) {
		BOOST_REQUIRE_EQUAL(r.value<0>(), "<xml/>");
	}
	*opts.hash += 1;
	sel = ro->select("SELECT NULL, now()", &opts);
	for (const auto & r : sel->as<boost::optional<int64_t>, boost::posix_time::ptime>()) {
		BOOST_REQUIRE(!r.value<0>());
		BOOST_REQUIRE_THROW(r.value<1>(), DB::ColumnTypeNotSupported);
	}
}

BOOST_AUTO_TEST_CASE( largeBlob )
{
	auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock"));
	ro->execute("TRUNCATE TABLE blobtest");
	AdHoc::FileUtils::MemMap f("/proc/self/exe");
	DB::Blob blob(f.data, f.getStat().st_size);
	BOOST_REQUIRE(blob.len > 140000); // Just assert the mapped file is actually "large"
	auto ins = ro->modify("INSERT INTO blobtest(data) VALUES(?)");
	ins->bindParamBLOB(0, blob);
	ins->execute();

	PQ::CommandOptions opts(0);
	opts.fetchBinary = true;
	opts.useCursor = false;
	auto sel = ro->select("SELECT data, length(data) FROM blobtest", &opts);
	for (const auto & r : sel->as<DB::Blob, int64_t>()) {
		BOOST_REQUIRE_EQUAL(r.value<1>(), f.getStat().st_size);
		BOOST_REQUIRE_EQUAL(r.value<0>(), blob);
	}
}

BOOST_AUTO_TEST_SUITE_END();

BOOST_AUTO_TEST_CASE( connfail )
{
	BOOST_REQUIRE_THROW(DB::ConnectionFactory::createNew("postgresql", "host=localhost user=no"), DB::ConnectionError);
	try {
		DB::ConnectionFactory::createNew("postgresql", "host=localhost user=no");
	}
	catch (const DB::ConnectionError & e) {
		BOOST_REQUIRE(std::string(e.what()).find("\"no\""));
	}
}

BOOST_AUTO_TEST_CASE( ssl )
{
	auto conn = DB::ConnectionFactory::createNew("postgresql", "host=randomdan.homeip.net user=gentoo dbname=postgres sslmode=require");
	BOOST_REQUIRE(conn);
	auto pqconn = dynamic_cast<PQ::Connection *>(conn);
	BOOST_REQUIRE(pqconn);
	BOOST_REQUIRE(PQgetssl(pqconn->conn));
	delete conn;
}