query/test/StatementTest.cpp

119 lines
3.2 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include "matador/sql/column.hpp"
#include "matador/sql/condition.hpp"
#include "matador/sql/connection_info.hpp"
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/session.hpp"
#include "models/airplane.hpp"
using namespace matador::sql;
using namespace matador::test;
struct Postgres
{
// constexpr static const char *dns{"postgres://test:test123@127.0.0.1:15432/test"};
constexpr static const char *dns{"postgres://test:test123@127.0.0.1:5432/matador_test"};
};
struct Sqlite
{
constexpr static const char *dns{"sqlite://sqlite.db"};
};
template<class Type>
class StatementTestFixture
{
public:
StatementTestFixture()
: pool_(Type::dns, 4), session_(pool_)
{
auto res = session_.create()
.table<airplane>("airplane")
.execute();
REQUIRE(res.first == 0);
REQUIRE(res.second == R"(CREATE TABLE "airplane" ("id" BIGINT, "brand" VARCHAR(255), "model" VARCHAR(255), CONSTRAINT PK_airplane PRIMARY KEY (id)))");
}
~StatementTestFixture()
{
session_.drop().table("airplane").execute();
}
matador::sql::session &session()
{ return session_; }
std::vector<entity<airplane>> &planes()
{ return planes_; }
private:
matador::sql::connection_pool<matador::sql::connection> pool_;
matador::sql::session session_;
std::vector<entity<airplane>> planes_{
make_entity<airplane>(1, "Airbus", "A380"),
make_entity<airplane>(2, "Boeing", "707"),
make_entity<airplane>(3, "Boeing", "747")
};
};
TEMPLATE_TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]", Sqlite, Postgres)
{
auto &s = StatementTestFixture<TestType>::session();
auto &planes = StatementTestFixture<TestType>::planes();
SECTION("Insert with prepared statement and placeholder") {
auto stmt = s.insert()
.template into<airplane>("airplane")
.template values<airplane>().prepare();
for (const auto &plane: planes) {
auto res = stmt.bind(*plane).execute();
REQUIRE(res == 1);
stmt.reset();
}
auto result = s.template select<airplane>().from("airplane").template fetch_all<airplane>();
size_t index{0};
for (const auto &i: result) {
REQUIRE(i.id == planes[index]->id);
REQUIRE(i.brand == planes[index]->brand);
REQUIRE(i.model == planes[index++]->model);
}
}
SECTION("Select with prepared statement") {
for (const auto &plane: planes) {
auto res = s.insert().template into<airplane>("airplane").values(*plane).execute();
REQUIRE(res.first == 1);
}
auto stmt = s.template select<airplane>().from("airplane").where("brand"_col == _).prepare();
stmt.bind(0, "Airbus");
auto result = stmt.template fetch<airplane>();
for (const auto &i: result) {
REQUIRE(i.id == planes[0]->id);
REQUIRE(i.brand == planes[0]->brand);
REQUIRE(i.model == planes[0]->model);
}
stmt.reset();
stmt.bind(0, "Boeing");
result = stmt.template fetch<airplane>();
size_t index{1};
for (const auto &i: result) {
REQUIRE(i.id == planes[index]->id);
REQUIRE(i.brand == planes[index]->brand);
REQUIRE(i.model == planes[index++]->model);
}
}
}