summaryrefslogtreecommitdiff
path: root/libmysqlpp/unittests/testmysql.cpp
blob: cb25092ef7be503ed3a9a7393f9d46a3d54358f4 (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
#define BOOST_TEST_MODULE TestMySQL
#include <boost/test/unit_test.hpp>

#include "mockDatabase.h"
#include <array>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <connection.h>
#include <cstdint>
#include <cstdio>
#include <definedDirs.h>
#include <factory.impl.h>
#include <filesystem>
#include <fstream>
#include <initializer_list>
#include <memory>
#include <modifycommand.h>
#include <my-mock.h>
#include <selectcommand.h>
#include <stdexcept>
#include <string_view>
#include <testCore.h>

namespace DB {
	class Error;
}

class StandardMockDatabase : public DB::PluginMock<MySQL::Mock> {
public:
	StandardMockDatabase() : DB::PluginMock<MySQL::Mock>("mysqlmock", {rootDir / "mysqlschema.sql"}, "options=libdbpp")
	{
	}
};

BOOST_GLOBAL_FIXTURE(StandardMockDatabase);

BOOST_FIXTURE_TEST_SUITE(Core, DB::TestCore)

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

	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());
}

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

	auto mod = rw->modify("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();
}

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

	auto select = ro->select("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;
	}
	BOOST_REQUIRE_EQUAL(1, rows);
}

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

	auto select = ro->select("SELECT * FROM test WHERE id != ?");
	select->bindParamI(0, testInt);
	select->execute();
	int rows = 0;
	while (select->fetch()) {
		assertColumnValueHelper(*select, 0, 4);
		assertColumnValueHelper(*select, 1, 123.45);
		assertColumnValueHelper(*select, 2, std::string_view("some text"));
		assertColumnValueHelper(*select, 3, true);
		assertColumnValueHelper(
				*select, 4, boost::posix_time::ptime_from_tm({3, 6, 23, 27, 3, 115, 0, 0, 0, 0, nullptr}));
		assertColumnValueHelper(*select, 5, boost::posix_time::time_duration(38, 13, 12));
		rows += 1;
	}
	BOOST_REQUIRE_EQUAL(1, rows);
}

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

	auto count = ro->select("SELECT COUNT(*) FROM bulktest");
	// Test empty
	ro->beginBulkUpload("bulktest", "");
	ro->endBulkUpload(nullptr);
	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");
	}
	std::array<char, BUFSIZ> buf {};
	for (std::streamsize r; (r = in.readsome(buf.data(), buf.size())) > 0;) {
		ro->bulkUploadData(buf.data(), static_cast<size_t>(r));
	}
	ro->endBulkUpload(nullptr);
	assertScalarValueHelper(*count, 800);
}

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

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

BOOST_AUTO_TEST_CASE(insertId)
{
	auto ro = DB::MockDatabase::openConnectionTo("mysqlmock");
	auto ins = ro->modify("INSERT INTO inserts(num) VALUES(?)");
	int64_t prevId = 0;
	for (int n : {4, 40, -4}) {
		ins->bindParamI(0, n);
		ins->execute();
		auto id = ro->insertId();
		BOOST_REQUIRE_GT(id, prevId);
		prevId = id;
	}
}

BOOST_AUTO_TEST_CASE(errors)
{
	auto ro = DB::MockDatabase::openConnectionTo("mysqlmock");
	BOOST_REQUIRE_THROW(ro->execute("nonsense"), DB::Error);
	BOOST_REQUIRE_THROW((void)DB::ConnectionFactory::createNew("mysql", "server=nohost"), DB::ConnectionError);
}

BOOST_AUTO_TEST_SUITE_END()