diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-05-31 17:35:33 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-05-31 22:21:49 +0100 |
commit | c702f1d255f8295c64b5ca6631a4f4b41b34942b (patch) | |
tree | 02739566bb57c34dfa21698da1a28d65fe4c73de | |
parent | Initial commit of prepstmt, selects, record sets (diff) | |
download | mygrate-c702f1d255f8295c64b5ca6631a4f4b41b34942b.tar.bz2 mygrate-c702f1d255f8295c64b5ca6631a4f4b41b34942b.tar.xz mygrate-c702f1d255f8295c64b5ca6631a4f4b41b34942b.zip |
Read test DB connection details from env
-rw-r--r-- | lib/helpers.h | 10 | ||||
-rw-r--r-- | test/test-mysql.cpp | 13 | ||||
-rw-r--r-- | test/test-postgresql.cpp | 9 |
3 files changed, 26 insertions, 6 deletions
diff --git a/lib/helpers.h b/lib/helpers.h index a8a630e..d2584d0 100644 --- a/lib/helpers.h +++ b/lib/helpers.h @@ -3,6 +3,7 @@ #include <concepts> #include <cstdint> +#include <cstdlib> #include <string> #include <utility> @@ -49,6 +50,15 @@ namespace MyGrate { a.size() } -> std::integral; }; + + inline const char * + getenv(const char * env, const char * dfl) + { + if (const auto e {::getenv(env)}) { + return e; + } + return dfl; + } } #endif diff --git a/test/test-mysql.cpp b/test/test-mysql.cpp index 6baf029..8710c8b 100644 --- a/test/test-mysql.cpp +++ b/test/test-mysql.cpp @@ -4,9 +4,11 @@ #include <compileTimeFormatter.h> #include <cstddef> #include <cstdint> +#include <cstdlib> #include <dbRecordSet.h> #include <dbStmt.h> #include <dbTypes.h> +#include <helpers.h> #include <input/mysqlConn.h> #include <memory> #include <stdexcept> @@ -14,13 +16,18 @@ #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 {"192.168.1.38", "repl", "repl", 3306}; + MyGrate::Input::MySQLConn {SERVER, USER, "badpass", PORT}; }()), std::runtime_error); - MyGrate::Input::MySQLConn c {"192.168.1.38", "repl", "r3pl", 3306}; + 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 = "), std::runtime_error); @@ -45,7 +52,7 @@ static_assert(SomeUpdate::paramCount == 2); BOOST_AUTO_TEST_CASE(stmt) { - MyGrate::Input::MySQLConn c {"192.168.1.38", "repl", "r3pl", 3306}; + MyGrate::Input::MySQLConn c {SERVER, USER, PASSWORD, PORT}; const auto rs {SomeShow::execute(&c)}; BOOST_REQUIRE(rs); BOOST_REQUIRE_EQUAL(rs->rows(), 1); diff --git a/test/test-postgresql.cpp b/test/test-postgresql.cpp index f388ef2..e89e031 100644 --- a/test/test-postgresql.cpp +++ b/test/test-postgresql.cpp @@ -6,6 +6,7 @@ #include <dbRecordSet.h> #include <dbStmt.h> #include <dbTypes.h> +#include <helpers.h> #include <memory> #include <output/pq/pqConn.h> #include <stdexcept> @@ -13,13 +14,15 @@ #include <type_traits> #include <variant> +const auto CONNSTR {MyGrate::getenv("MYGRATE_POSTGRESQL_CONNSTR", "user=postgres")}; + BOOST_AUTO_TEST_CASE(simple) { BOOST_CHECK_THROW(([]() { MyGrate::Output::Pq::PqConn {"nonsense"}; }()), std::runtime_error); - MyGrate::Output::Pq::PqConn c {"user=postgres"}; + MyGrate::Output::Pq::PqConn c {CONNSTR}; BOOST_CHECK_NO_THROW(c.query("SET application_name = ''")); BOOST_CHECK_NO_THROW(c.query("SET application_name = 'something'")); BOOST_CHECK_THROW(c.query("SET application_name = "), std::runtime_error); @@ -51,7 +54,7 @@ static_assert(std::is_same_v<SomeIns::Return, std::size_t>); BOOST_AUTO_TEST_CASE(stmt) { - MyGrate::Output::Pq::PqConn c {"user=postgres"}; + MyGrate::Output::Pq::PqConn c {CONNSTR}; const auto rs {SomeShow::execute(&c)}; BOOST_REQUIRE(rs); BOOST_REQUIRE_EQUAL(rs->columns(), 3); @@ -62,7 +65,7 @@ BOOST_AUTO_TEST_CASE(stmt) BOOST_AUTO_TEST_CASE(stmt_reuse) { - MyGrate::Output::Pq::PqConn c {"user=postgres"}; + MyGrate::Output::Pq::PqConn c {CONNSTR}; for (int x = 0; x < 10; x++) { const auto rs {SomeShow::execute(&c)}; BOOST_REQUIRE(rs); |