From 6b453d0f0f50dccd0333d0d6e8cd0246e86d24a9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 13 Jun 2021 01:09:01 +0100 Subject: Create and drop MySQL mock DBs as needed --- test/test-mysql.cpp | 3 +++ test/testdb-mysql.cpp | 20 ++++++++++++++------ test/testdb-mysql.h | 5 +++++ 3 files changed, 22 insertions(+), 6 deletions(-) (limited to 'test') 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().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 #include #include #include @@ -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; }; } } -- cgit v1.2.3