diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test-e2e.cpp | 107 |
1 files changed, 68 insertions, 39 deletions
diff --git a/test/test-e2e.cpp b/test/test-e2e.cpp index 31f3b95..4dd40d7 100644 --- a/test/test-e2e.cpp +++ b/test/test-e2e.cpp @@ -42,32 +42,79 @@ private: semaphore ops {0}; }; -BOOST_AUTO_TEST_CASE(e2e, *boost::unit_test::timeout(15)) -{ - const char * const target_schema {"testout"}; - using namespace MyGrate::Testing; +using namespace MyGrate::Testing; +class MockSetup { +public: + static constexpr const char * const target_schema {"testout"}; + MockSetup() : pq {ROOT "/db/schema.sql"}, pqm {pq.mock()}, mym {my.mock()} { } + + TestUpdateDatabase & + getUpdateDatabase() + { + BOOST_REQUIRE(!out); + auto & o = out.emplace(pqm.connstr.c_str(), + MyGrate::Output::Pq::UpdateDatabase::createNew(&pqm, MySQLDB::SERVER, MySQLDB::USER, MySQLDB::PASSWORD, + MySQLDB::PORT, my.mockname.c_str(), 100, target_schema) + .source); + BOOST_CHECK_EQUAL(o.source, 1); + return o; + } + + const MyGrate::EventSourceBasePtr & + getSource() + { + BOOST_REQUIRE(out); + if (!src) { + src = out->getSource(); + BOOST_REQUIRE(src); + } + return src; + } + + void + run() + { + BOOST_REQUIRE(out); + BOOST_REQUIRE(!repl); + repl.emplace(&MyGrate::EventSourceBase::readEvents, getSource().get(), std::ref(*out)); + } + + void + stopAfter(unsigned int events) + { + BOOST_REQUIRE(out); + BOOST_REQUIRE(src); + BOOST_REQUIRE(repl); + out->waitFor(events); + src->stopEvents(); + repl->join(); + repl.reset(); + } + MySQLDB my; - PqConnDB pq {ROOT "/db/schema.sql"}; + PqConnDB pq; + MyGrate::Output::Pq::PqConn pqm; + MyGrate::Input::MySQLConn mym; - auto pqm = pq.mock(); - auto mym = my.mock(); + std::optional<TestUpdateDatabase> out; + MyGrate::EventSourceBasePtr src; + std::optional<std::thread> repl; +}; + +BOOST_FIXTURE_TEST_SUITE(setup, MockSetup); +BOOST_AUTO_TEST_CASE(e2e, *boost::unit_test::timeout(15)) +{ MyGrate::sql::createTestTable::execute(&mym); MyGrate::sql::fillTestTable::execute(&mym); - TestUpdateDatabase out {pqm.connstr.c_str(), - MyGrate::Output::Pq::UpdateDatabase::createNew(&pqm, MySQLDB::SERVER, MySQLDB::USER, MySQLDB::PASSWORD, - MySQLDB::PORT, my.mockname.c_str(), 100, target_schema) - .source}; - BOOST_CHECK_EQUAL(out.source, 1); - auto src = out.getSource(); - BOOST_REQUIRE(src); + TestUpdateDatabase & out {getUpdateDatabase()}; out.addTable(&mym, "session"); BOOST_CHECK_EQUAL(MyGrate::sql::selectTestTable::execute(&pqm)->rows(), 1); - std::thread repl {&MyGrate::EventSourceBase::readEvents, src.get(), std::ref(out)}; + run(); auto upd = mym.prepare("UPDATE session SET session_id = ? WHERE id = ?", 2); upd->execute({"food", 1}); @@ -79,35 +126,18 @@ BOOST_AUTO_TEST_CASE(e2e, *boost::unit_test::timeout(15)) ins->execute({"hashyhash", "testuser", "groupadm", "10.10.0.1", 2433}); mym.query("flush logs"); - out.waitFor(4); - - src->stopEvents(); - repl.join(); + stopAfter(4); } BOOST_AUTO_TEST_CASE(txns, *boost::unit_test::timeout(15)) { - const char * const target_schema {"testout"}; - using namespace MyGrate::Testing; - MySQLDB my; - PqConnDB pq {ROOT "/db/schema.sql"}; - - auto pqm = pq.mock(); - auto mym = my.mock(); - MyGrate::sql::simpleCreate::execute(&mym); - TestUpdateDatabase out {pqm.connstr.c_str(), - MyGrate::Output::Pq::UpdateDatabase::createNew(&pqm, MySQLDB::SERVER, MySQLDB::USER, MySQLDB::PASSWORD, - MySQLDB::PORT, my.mockname.c_str(), 100, target_schema) - .source}; - BOOST_CHECK_EQUAL(out.source, 1); - auto src = out.getSource(); - BOOST_REQUIRE(src); + TestUpdateDatabase & out {getUpdateDatabase()}; out.addTable(&mym, "test"); - std::thread repl {&MyGrate::EventSourceBase::readEvents, src.get(), std::ref(out)}; + run(); MyGrate::Tx {&mym}([&]() { for (unsigned int x = 0; x < 10; x += 1) { MyGrate::sql::simpleInsert::execute(&mym, MyGrate::scprintf<"some string %?">(x)); @@ -115,10 +145,7 @@ BOOST_AUTO_TEST_CASE(txns, *boost::unit_test::timeout(15)) MyGrate::sql::simpleUpdateAll::execute(&mym, "Same"); MyGrate::sql::simpleDeleteSome::execute(&mym, 5); }); - out.waitFor(14); - - src->stopEvents(); - repl.join(); + stopAfter(14); auto recs = MyGrate::sql::simpleSelectAll::execute(&pqm); BOOST_REQUIRE_EQUAL(recs->rows(), 6); @@ -127,3 +154,5 @@ BOOST_AUTO_TEST_CASE(txns, *boost::unit_test::timeout(15)) BOOST_CHECK_EQUAL(recs->at(1, 1).get<std::string_view>(), "Same"); BOOST_CHECK_EQUAL(recs->at(3, 1).get<std::string_view>(), "Same"); } + +BOOST_AUTO_TEST_SUITE_END(); |