summaryrefslogtreecommitdiff
path: root/test/test-mysql.cpp
blob: e4551f0313adb973e36d7e118ca4a7ec1bd2f183 (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
#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>

const auto SERVER {MyGrate::getenv("MYGRATE_MYSQL_SERVER", "localhost")};
const auto USER {MyGrate::getenv("MYGRATE_MYSQL_USER", getenv("LOGNAME"))};
const auto PASSWORD {getenv("MYGRATE_MYSQL_PASSWORD")};
const auto PORT {(unsigned short)std::atoi(MyGrate::getenv("MYGRATE_MYSQL_PORT", "3306"))};

BOOST_AUTO_TEST_CASE(simple)
{
	BOOST_CHECK_THROW(([]() {
		MyGrate::Input::MySQLConn {SERVER, USER, "badpass", PORT};
	}()),
			MyGrate::Input::MySQLErr);
	MyGrate::Input::MySQLConn c {SERVER, USER, PASSWORD, 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 MASTER STATUS">;
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)
{
	MyGrate::Input::MySQLConn c {SERVER, USER, PASSWORD, PORT};
	const auto rs {SomeShow::execute(&c)};
	BOOST_REQUIRE(rs);
	BOOST_REQUIRE_EQUAL(rs->rows(), 1);
	BOOST_REQUIRE_EQUAL(rs->columns(), 4);
	BOOST_CHECK(std::get<std::string_view>(rs->at(0, 0)).starts_with("mariadb"));
	BOOST_CHECK_GE(std::get<int64_t>(rs->at(0, 1)), 4);
}

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_");
}