1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#define BOOST_TEST_MODULE PostgreSQL
#include <boost/test/unit_test.hpp>
#include "testdb-postgresql.h"
#include <cstddef>
#include <dbConn.h>
#include <dbRecordSet.h>
#include <dbStmt.h>
#include <dbTypes.h>
#include <fixedString.h>
#include <helpers.h>
#include <memory>
#include <output/pq/pqConn.h>
#include <stdexcept>
#include <string_view>
#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"};
}()),
MyGrate::Output::Pq::PqErr);
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 = "), MyGrate::Output::Pq::PqErr);
// BOOST_CHECK_THROW(c.query("SET application_name = $1", {}), std::logic_error);
BOOST_CHECK_NO_THROW(c.query("SET application_name = 'something'", {}));
}
using SomeUpdate = MyGrate::DbStmt<"UPDATE foo SET blah = $2 WHERE bar = $1">;
using SomeUpdateRtn = MyGrate::DbStmt<"UPDATE foo SET blah = $2 WHERE bar = $1 RETURNING x">;
using SomeShow = MyGrate::DbStmt<"SHOW all">;
using SomeIns = MyGrate::DbStmt<"INSERT INTO foo VALUES(..., $87)">;
static_assert(SomeUpdate::paramCount(MyGrate::ParamMode::DollarNum) == 2);
static_assert(std::is_same_v<SomeUpdate::Return, std::size_t>);
static_assert(SomeUpdateRtn::paramCount(MyGrate::ParamMode::DollarNum) == 2);
static_assert(std::is_same_v<SomeUpdateRtn::Return, MyGrate::RecordSetPtr>);
static_assert(SomeShow::paramCount(MyGrate::ParamMode::DollarNum) == 0);
static_assert(SomeIns::paramCount(MyGrate::ParamMode::DollarNum) == 87);
static_assert(std::is_same_v<SomeIns::Return, std::size_t>);
BOOST_AUTO_TEST_CASE(stmt)
{
MyGrate::Output::Pq::PqConn c {CONNSTR};
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<std::string_view>(rs->at(0, 0)), "allow_system_table_mods");
BOOST_CHECK_EQUAL(std::get<std::string_view>(rs->at(0, 1)), "off");
}
BOOST_AUTO_TEST_CASE(stmt_reuse)
{
MyGrate::Output::Pq::PqConn c {CONNSTR};
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);
}
}
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<std::string_view>().substr(0, 13), "mygrate_test_");
mdb.query("CREATE TABLE test(c text)");
BOOST_CHECK_NO_THROW(mdb.query("INSERT INTO test VALUES($1)", {1}));
BOOST_CHECK_NO_THROW(mdb.query("INSERT INTO test VALUES($1)", {"string_view"}));
BOOST_CHECK_NO_THROW(mdb.query("INSERT INTO test VALUES($1)", {nullptr}));
BOOST_CHECK_NO_THROW(mdb.query("INSERT INTO test VALUES($1)", {1.2}));
BOOST_CHECK_THROW(mdb.query("INSERT INTO test VALUES($1)", {timespec {}}), std::logic_error);
auto rscount = MyGrate::DbStmt<"SELECT COUNT(*) FROM test">::execute(&mdb);
BOOST_CHECK_EQUAL(rscount->at(0, 0).operator unsigned int(), 4);
}
BOOST_AUTO_TEST_CASE(mock_schema)
{
MyGrate::Testing::PqConnDB db {ROOT "/db/schema.sql"};
auto mdb = db.mock();
auto rs = MyGrate::DbStmt<"SELECT COUNT(*) FROM mygrate.source">::execute(&mdb);
BOOST_CHECK_EQUAL(rs->at(0, 0).operator unsigned int(), 0);
}
|