From 63b2ca0dbdae190941d60a55c9cff99d4a75a0e1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 31 May 2021 13:18:17 +0100 Subject: Initial commit of prepstmt, selects, record sets This is full of holes, but all the basics are covered. --- test/test-mysql.cpp | 30 ++++++++++++++++++++++++++++++ test/test-postgresql.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) (limited to 'test') diff --git a/test/test-mysql.cpp b/test/test-mysql.cpp index 5a22567..6baf029 100644 --- a/test/test-mysql.cpp +++ b/test/test-mysql.cpp @@ -1,9 +1,18 @@ #define BOOST_TEST_MODULE MySQL #include +#include +#include +#include +#include +#include #include #include +#include #include +#include +#include +#include BOOST_AUTO_TEST_CASE(simple) { @@ -23,3 +32,24 @@ BOOST_AUTO_TEST_CASE(simple) BOOST_CHECK_NO_THROW(c.query("SET @var = ?", {1.2})); BOOST_CHECK_THROW(c.query("SET @var = ?", {MyGrate::Time {}}), std::runtime_error); } + +using SomeSelect = MyGrate::DbStmt<"SELECT * FROM foo">; +using SomeShow = MyGrate::DbStmt<"SHOW MASTER STATUS">; +using SomeUpdate = MyGrate::DbStmt<"UPDATE foo SET blah = ? WHERE bar = ?", MyGrate::ParamMode::QMark>; + +static_assert(std::is_same_v); +static_assert(std::is_same_v); +static_assert(std::is_same_v); +static_assert(SomeShow::paramCount == 0); +static_assert(SomeUpdate::paramCount == 2); + +BOOST_AUTO_TEST_CASE(stmt) +{ + MyGrate::Input::MySQLConn c {"192.168.1.38", "repl", "r3pl", 3306}; + const auto rs {SomeShow::execute(&c)}; + BOOST_REQUIRE(rs); + BOOST_REQUIRE_EQUAL(rs->rows(), 1); + BOOST_REQUIRE_EQUAL(rs->columns(), 4); + BOOST_CHECK(std::get(rs->at(0, 0)).starts_with("mariadb")); + BOOST_CHECK_GE(std::get(rs->at(0, 1)), 4); +} diff --git a/test/test-postgresql.cpp b/test/test-postgresql.cpp index 5940b38..f388ef2 100644 --- a/test/test-postgresql.cpp +++ b/test/test-postgresql.cpp @@ -1,9 +1,17 @@ #define BOOST_TEST_MODULE PostgreSQL #include +#include +#include +#include +#include #include +#include #include #include +#include +#include +#include BOOST_AUTO_TEST_CASE(simple) { @@ -26,3 +34,39 @@ BOOST_AUTO_TEST_CASE(simple) BOOST_CHECK_THROW(c.query("INSERT INTO test VALUES($1)", {MyGrate::Time {}}), std::runtime_error); c.query("DROP TABLE test"); } + +using SomeUpdate = MyGrate::DbStmt<"UPDATE foo SET blah = $2 WHERE bar = $1", MyGrate::ParamMode::DollarNum>; +using SomeUpdateRtn + = MyGrate::DbStmt<"UPDATE foo SET blah = $2 WHERE bar = $1 RETURNING x", MyGrate::ParamMode::DollarNum>; +using SomeShow = MyGrate::DbStmt<"SHOW all", MyGrate::ParamMode::DollarNum>; +using SomeIns = MyGrate::DbStmt<"INSERT INTO foo VALUES(..., $87)", MyGrate::ParamMode::DollarNum>; + +static_assert(SomeUpdate::paramCount == 2); +static_assert(std::is_same_v); +static_assert(SomeUpdateRtn::paramCount == 2); +static_assert(std::is_same_v); +static_assert(SomeShow::paramCount == 0); +static_assert(SomeIns::paramCount == 87); +static_assert(std::is_same_v); + +BOOST_AUTO_TEST_CASE(stmt) +{ + MyGrate::Output::Pq::PqConn c {"user=postgres"}; + const auto rs {SomeShow::execute(&c)}; + BOOST_REQUIRE(rs); + BOOST_REQUIRE_EQUAL(rs->columns(), 3); + BOOST_REQUIRE_GT(rs->rows(), 1); + BOOST_CHECK_EQUAL(std::get(rs->at(0, 0)), "allow_system_table_mods"); + BOOST_CHECK_EQUAL(std::get(rs->at(0, 1)), "off"); +} + +BOOST_AUTO_TEST_CASE(stmt_reuse) +{ + MyGrate::Output::Pq::PqConn c {"user=postgres"}; + for (int x = 0; x < 10; x++) { + const auto rs {SomeShow::execute(&c)}; + BOOST_REQUIRE(rs); + BOOST_CHECK_EQUAL(rs->columns(), 3); + BOOST_CHECK_GT(rs->rows(), 1); + } +} -- cgit v1.2.3