From 24cc85beab49be38416c47e13e20c2461e863cbb Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 13 Jun 2021 01:43:09 +0100 Subject: Create and drop PostgreSQL mock DBs as needed --- test/Jamfile.jam | 2 +- test/test-postgresql.cpp | 9 +++++++++ test/testdb-postgresql.cpp | 30 ++++++++++++++++++++++++++++++ test/testdb-postgresql.h | 21 +++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 test/testdb-postgresql.cpp create mode 100644 test/testdb-postgresql.h (limited to 'test') diff --git a/test/Jamfile.jam b/test/Jamfile.jam index 38bee41..691a68f 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -16,4 +16,4 @@ run test-bitset.cpp ; run test-streams.cpp ; run test-misc.cpp ; run test-mysql.cpp : : : testdb ; -run test-postgresql.cpp ; +run test-postgresql.cpp : : : testdb ; diff --git a/test/test-postgresql.cpp b/test/test-postgresql.cpp index eaf6d58..38feb3c 100644 --- a/test/test-postgresql.cpp +++ b/test/test-postgresql.cpp @@ -1,6 +1,7 @@ #define BOOST_TEST_MODULE PostgreSQL #include +#include "testdb-postgresql.h" #include #include #include @@ -73,3 +74,11 @@ BOOST_AUTO_TEST_CASE(stmt_reuse) BOOST_CHECK_GT(rs->rows(), 1); } } + +BOOST_AUTO_TEST_CASE(mock) +{ + MyGrate::Testing::PqConnDB db; + auto mdb = db.mock(); + auto rs = MyGrate::DbStmt<"SELECT CURRENT_DATABASE()">::execute(&mdb); + BOOST_CHECK_EQUAL(rs->at(0, 0).get().substr(0, 13), "mygrate_test_"); +} diff --git a/test/testdb-postgresql.cpp b/test/testdb-postgresql.cpp new file mode 100644 index 0000000..55f163f --- /dev/null +++ b/test/testdb-postgresql.cpp @@ -0,0 +1,30 @@ +#include "testdb-postgresql.h" +#include +#include +#include +#include + +namespace MyGrate { + namespace Testing { + const auto CONNSTR {MyGrate::getenv("MYGRATE_POSTGRESQL_CONNSTR", "user=postgres")}; + std::size_t PqConnDB::mocknum; + + PqConnDB::PqConnDB() : PqConn(CONNSTR), mockname {scprintf<"mygrate_test_%?_%?">(getpid(), mocknum++)} + { + query(("DROP DATABASE IF EXISTS " + mockname).c_str()); + query(("CREATE DATABASE " + mockname).c_str()); + } + + PqConnDB::~PqConnDB() + { + query(("DROP DATABASE IF EXISTS " + mockname).c_str()); + mockname.clear(); + } + + Output::Pq::PqConn + PqConnDB::mock() const + { + return PqConn {scprintf<"%? dbname=%?">(CONNSTR, mockname).c_str()}; + } + } +} diff --git a/test/testdb-postgresql.h b/test/testdb-postgresql.h new file mode 100644 index 0000000..fc73d2a --- /dev/null +++ b/test/testdb-postgresql.h @@ -0,0 +1,21 @@ +#ifndef MYGRATE_TESTING_POSTGRESQL_H +#define MYGRATE_TESTING_POSTGRESQL_H + +#include + +namespace MyGrate { + namespace Testing { + class PqConnDB : public Output::Pq::PqConn { + public: + PqConnDB(); + ~PqConnDB(); + + Output::Pq::PqConn mock() const; + + std::string mockname; + static std::size_t mocknum; + }; + } +} + +#endif -- cgit v1.2.3