summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/input/mysqlConn.cpp6
-rw-r--r--lib/input/mysqlConn.h5
-rw-r--r--test/test-mysql.cpp3
-rw-r--r--test/testdb-mysql.cpp20
-rw-r--r--test/testdb-mysql.h5
5 files changed, 29 insertions, 10 deletions
diff --git a/lib/input/mysqlConn.cpp b/lib/input/mysqlConn.cpp
index c7705f5..362f9ea 100644
--- a/lib/input/mysqlConn.cpp
+++ b/lib/input/mysqlConn.cpp
@@ -13,12 +13,12 @@
namespace MyGrate::Input {
MySQLErr::MySQLErr(const std::string & when, MYSQL * c) : std::runtime_error(when + ": " + mysql_error(c)) { }
- MySQLConn::MySQLConn(
- const char * const host, const char * const user, const char * const pass, unsigned short port) :
+ MySQLConn::MySQLConn(const char * const host, const char * const user, const char * const pass, unsigned short port,
+ const char * const db) :
st_mysql {}
{
mysql_init(this);
- if (!mysql_real_connect(this, host, user, pass, "", port, nullptr, 0)) {
+ if (!mysql_real_connect(this, host, user, pass, db, port, nullptr, 0)) {
mysql_close(this);
throw MySQLErr("Connecting", this);
}
diff --git a/lib/input/mysqlConn.h b/lib/input/mysqlConn.h
index d779fd1..dfb408f 100644
--- a/lib/input/mysqlConn.h
+++ b/lib/input/mysqlConn.h
@@ -19,7 +19,10 @@ namespace MyGrate::Input {
public:
static constexpr auto paramMode {ParamMode::QMark};
- MySQLConn(const char * const host, const char * const user, const char * const pass, unsigned short port);
+ MySQLConn(const char * const host, const char * const user, const char * const pass, unsigned short port,
+ const char * const db = "");
+ MySQLConn(const MySQLConn &) = delete;
+ MySQLConn(MySQLConn &&) = delete;
~MySQLConn();
void query(const char * const) override;
diff --git a/test/test-mysql.cpp b/test/test-mysql.cpp
index 954a121..e4551f0 100644
--- a/test/test-mysql.cpp
+++ b/test/test-mysql.cpp
@@ -65,4 +65,7 @@ BOOST_AUTO_TEST_CASE(stmt)
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_");
}
diff --git a/test/testdb-mysql.cpp b/test/testdb-mysql.cpp
index 20ba677..4b6c62e 100644
--- a/test/testdb-mysql.cpp
+++ b/test/testdb-mysql.cpp
@@ -1,4 +1,5 @@
#include "testdb-mysql.h"
+#include <compileTimeFormatter.h>
#include <cstdlib>
#include <helpers.h>
#include <input/mysqlConn.h>
@@ -9,18 +10,25 @@ namespace MyGrate {
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"))};
+ std::size_t MySQLDB::mocknum;
- MySQLDB::MySQLDB() : MySQLConn(SERVER, USER, PASSWORD, PORT)
+ MySQLDB::MySQLDB() :
+ MySQLConn(SERVER, USER, PASSWORD, PORT), mockname {scprintf<"mygrate_test_%?_%?">(getpid(), mocknum++)}
{
- query("DROP DATABASE IF EXISTS mygrate_test");
- query("CREATE DATABASE mygrate_test");
- query("USE mygrate_test");
+ query(("DROP DATABASE IF EXISTS " + mockname).c_str());
+ query(("CREATE DATABASE " + mockname).c_str());
}
MySQLDB::~MySQLDB()
{
- query("USE mysql");
- query("DROP DATABASE IF EXISTS mygrate_test");
+ query(("DROP DATABASE IF EXISTS " + mockname).c_str());
+ mockname.clear();
+ }
+
+ Input::MySQLConn
+ MySQLDB::mock() const
+ {
+ return {SERVER, USER, PASSWORD, PORT, mockname.c_str()};
}
}
}
diff --git a/test/testdb-mysql.h b/test/testdb-mysql.h
index c7cceb3..3334e24 100644
--- a/test/testdb-mysql.h
+++ b/test/testdb-mysql.h
@@ -9,6 +9,11 @@ namespace MyGrate {
public:
MySQLDB();
~MySQLDB();
+
+ Input::MySQLConn mock() const;
+
+ std::string mockname;
+ static std::size_t mocknum;
};
}
}