summaryrefslogtreecommitdiff
path: root/test/test-mysql.cpp
blob: 4ad9bb5244a2a1199a92e3fb4d6b11131213fe56 (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
#define BOOST_TEST_MODULE MySQL
#include <boost/test/unit_test.hpp>

#include "testdb-mysql.h"
#include <cstdint>
#include <cstdlib>
#include <dbConn.h>
#include <dbRecordSet.h>
#include <dbStmt.h>
#include <dbTypes.h>
#include <fixedString.h>
#include <helpers.h>
#include <input/mysqlConn.h>
#include <memory>
#include <stdexcept>
#include <string_view>
#include <type_traits>
#include <variant>

BOOST_AUTO_TEST_CASE(simple)
{
	using namespace MyGrate::Testing;
	BOOST_CHECK_THROW(([]() {
		MyGrate::Input::MySQLConn {MySQLDB::SERVER, MySQLDB::USER, "badpass", MySQLDB::PORT};
	}()),
			MyGrate::Input::MySQLErr);
	MyGrate::Input::MySQLConn c {MySQLDB::SERVER, MySQLDB::USER, MySQLDB::PASSWORD, MySQLDB::PORT};
	BOOST_CHECK_NO_THROW(c.query("SET @var = ''"));
	BOOST_CHECK_NO_THROW(c.query("SET @var = 'something'"));
	BOOST_CHECK_THROW(c.query("SET @var = "), MyGrate::Input::MySQLErr);
	BOOST_CHECK_THROW(c.query("SET @var = ?", {}), std::logic_error);
	BOOST_CHECK_NO_THROW(c.query("SET @var = ''", {}));
	BOOST_CHECK_NO_THROW(c.query("SET @var = ?", {1}));
	BOOST_CHECK_NO_THROW(c.query("SET @var = ?", {"string_view"}));
	BOOST_CHECK_NO_THROW(c.query("SET @var = ?", {nullptr}));
	BOOST_CHECK_NO_THROW(c.query("SET @var = ?", {1.2}));
	BOOST_CHECK_THROW(c.query("SET @var = ?", {MyGrate::Time {}}), std::logic_error);
}

using SomeSelect = MyGrate::DbStmt<"SELECT * FROM foo">;
using SomeShow = MyGrate::DbStmt<"SHOW VARIABLES LIKE 'version'">;
using SomeUpdate = MyGrate::DbStmt<"UPDATE foo SET blah = ? WHERE bar = ?">;

static_assert(std::is_same_v<SomeSelect::Return, MyGrate::RecordSetPtr>);
static_assert(std::is_same_v<SomeShow::Return, MyGrate::RecordSetPtr>);
static_assert(std::is_same_v<SomeUpdate::Return, std::size_t>);
static_assert(SomeShow::paramCount(MyGrate::ParamMode::QMark) == 0);
static_assert(SomeUpdate::paramCount(MyGrate::ParamMode::QMark) == 2);

BOOST_AUTO_TEST_CASE(stmt)
{
	using namespace MyGrate::Testing;
	MyGrate::Input::MySQLConn c {MySQLDB::SERVER, MySQLDB::USER, MySQLDB::PASSWORD, MySQLDB::PORT};
	const auto rs {SomeShow::execute(&c)};
	BOOST_REQUIRE(rs);
	BOOST_REQUIRE_EQUAL(rs->rows(), 1);
	BOOST_REQUIRE_EQUAL(rs->columns(), 2);
	const std::string_view versionString {rs->at(0, 1)};
	BOOST_CHECK(versionString.find("MariaDB") != std::string_view::npos);
}

BOOST_AUTO_TEST_CASE(mock)
{
	MyGrate::Testing::MySQLDB db;
	auto mdb = db.mock();
	auto rs = MyGrate::DbStmt<"SELECT DATABASE()">::execute(&mdb);
	BOOST_CHECK_EQUAL(rs->at(0, 0).get<std::string_view>().substr(0, 13), "mygrate_test_");
}