backend tests progress

This commit is contained in:
2024-02-01 17:14:48 +01:00
parent 7c02d745c8
commit c025fb282a
23 changed files with 354 additions and 272 deletions
+1 -5
View File
@@ -234,19 +234,15 @@ sql::record mysql_connection::describe(const std::string &table)
while (reader.fetch()) {
char *end = nullptr;
// Todo: Handle error
auto index = strtoul(reader.column(0), &end, 10);
std::string name = reader.column(0);
// Todo: extract size
auto typeinfo = determine_type_info(reader.column(1));
end = nullptr;
sql::null_option null_opt{sql::null_option::NULLABLE};
if (strtoul(reader.column(2), &end, 10) == 0) {
null_opt = sql::null_option::NOT_NULL;
}
// f.default_value(res->column(4));
prototype.append({name, typeinfo.type, {typeinfo.size}, null_opt});
prototype.append({name, typeinfo.type, {typeinfo.size}, null_opt, prototype.size()});
}
return prototype;
+2 -1
View File
@@ -24,7 +24,8 @@ set(TEST_SOURCES
../../tests/ConnectionTest.cpp
../../tests/SessionRecordTest.cpp
../../tests/StatementTest.cpp
../../tests/TypeTraitsTest.cpp)
../../tests/TypeTraitsTest.cpp
../../tests/StatementCacheTest.cpp)
set(LIBRARY_TEST_TARGET mysql_tests)
+6 -3
View File
@@ -22,9 +22,12 @@ void throw_postgres_error(PGconn *db, const std::string &source)
void throw_postgres_error(PGresult *res, PGconn *db, const std::string &source, const std::string &sql)
{
if (res == nullptr ||
(PQresultStatus(res) != PGRES_COMMAND_OK &&
PQresultStatus(res) != PGRES_TUPLES_OK)) {
if (res == nullptr) {
std::stringstream msg;
msg << "postgres error (" << source << ", " << PQerrorMessage(db) << ": " << sql;
throw std::logic_error(msg.str());
} else if ((PQresultStatus(res) != PGRES_COMMAND_OK &&
PQresultStatus(res) != PGRES_TUPLES_OK)) {
std::stringstream msg;
msg << "postgres error (" << source << ", " << PQresultErrorField(res, PG_DIAG_SQLSTATE) << ") " << PQerrorMessage(db) << ": " << sql;
throw std::logic_error(msg.str());
+2 -1
View File
@@ -24,7 +24,8 @@ set(TEST_SOURCES
../../tests/ConnectionTest.cpp
../../tests/SessionRecordTest.cpp
../../tests/StatementTest.cpp
../../tests/TypeTraitsTest.cpp)
../../tests/TypeTraitsTest.cpp
../../tests/StatementCacheTest.cpp)
set(LIBRARY_TEST_TARGET postgres_tests)
+1 -1
View File
@@ -163,7 +163,7 @@ sql::record sqlite_connection::describe(const std::string& table)
null_opt = sql::null_option::NOT_NULL;
}
// f.default_value(res->column(4));
prototype.append({name, type, utils::null_attributes, null_opt});
prototype.append({name, type, utils::null_attributes, null_opt, index});
}
return std::move(prototype);
+2 -1
View File
@@ -24,7 +24,8 @@ set(TEST_SOURCES
../../tests/ConnectionTest.cpp
../../tests/SessionRecordTest.cpp
../../tests/StatementTest.cpp
../../tests/TypeTraitsTest.cpp)
../../tests/TypeTraitsTest.cpp
../../tests/StatementCacheTest.cpp)
set(LIBRARY_TEST_TARGET sqlite_tests)
+79 -78
View File
@@ -2,7 +2,7 @@
#include "matador/sql/column.hpp"
#include "matador/sql/condition.hpp"
#include "matador/sql/session.hpp"
#include "matador/sql/connection.hpp"
#include "connection.hpp"
@@ -12,8 +12,10 @@ class SessionRecordFixture
{
public:
SessionRecordFixture()
: pool(matador::test::connection::dns, 4), ses(pool)
{}
: db(matador::test::connection::dns)
{
db.open();
}
~SessionRecordFixture() {
drop_table_if_exists("flight");
drop_table_if_exists("airplane");
@@ -22,13 +24,12 @@ public:
}
protected:
matador::sql::connection_pool<matador::sql::connection> pool;
matador::sql::session ses;
matador::sql::connection db;
private:
void drop_table_if_exists(const std::string &table_name) {
if (ses.table_exists(table_name)) {
ses.drop().table(table_name).execute();
if (db.exists(table_name)) {
db.drop().table(table_name).execute();
}
}
};
@@ -37,8 +38,8 @@ using namespace matador::sql;
TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement", "[session][record]")
{
REQUIRE(!ses.table_exists("person"));
ses.create()
REQUIRE(!db.exists("person"));
db.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
@@ -46,18 +47,18 @@ TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement", "[ses
})
.execute();
REQUIRE(ses.table_exists("person"));
REQUIRE(db.exists("person"));
ses.drop()
db.drop()
.table("person")
.execute();
REQUIRE(!ses.table_exists("person"));
REQUIRE(!db.exists("person"));
}
TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement with foreign key", "[session][record]")
{
ses.create()
db.create()
.table("airplane", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("brand", 255),
@@ -65,9 +66,9 @@ TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement with fo
})
.execute();
REQUIRE(ses.table_exists("airplane"));
REQUIRE(db.exists("airplane"));
ses.create()
db.create()
.table("flight", {
make_pk_column<unsigned long>("id"),
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
@@ -75,24 +76,24 @@ TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement with fo
})
.execute();
REQUIRE(ses.table_exists("flight"));
REQUIRE(db.exists("flight"));
ses.drop()
db.drop()
.table("flight")
.execute();
REQUIRE(!ses.table_exists("flight"));
REQUIRE(!db.exists("flight"));
ses.drop()
db.drop()
.table("airplane")
.execute();
REQUIRE(!ses.table_exists("airplane"));
REQUIRE(!db.exists("airplane"));
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement", "[session][record]")
{
ses.create()
db.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
@@ -100,14 +101,14 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement", "[ses
})
.execute();
auto res = ses.insert()
auto res = db.insert()
.into("person", {"id", "name", "age"})
.values({7, "george", 45})
.execute();
REQUIRE(res == 1);
auto result = ses.select({"id", "name", "age"})
auto result = db.select({"id", "name", "age"})
.from("person")
.fetch_all();
@@ -124,14 +125,14 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement", "[ses
REQUIRE(i.at(2).template as<int>() == 45);
}
ses.drop()
db.drop()
.table("person")
.execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement with foreign key", "[session][record]")
{
ses.create()
db.create()
.table("airplane", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("brand", 255),
@@ -139,7 +140,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement with fo
})
.execute();
ses.create()
db.create()
.table("flight", {
make_pk_column<unsigned long>("id"),
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
@@ -147,31 +148,31 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement with fo
})
.execute();
auto res = ses.insert().into("airplane", {"id", "brand", "model"}).values({1, "Airbus", "A380"}).execute();
auto res = db.insert().into("airplane", {"id", "brand", "model"}).values({1, "Airbus", "A380"}).execute();
REQUIRE(res == 1);
res = ses.insert().into("airplane", {"id", "brand", "model"}).values({2, "Boeing", "707"}).execute();
res = db.insert().into("airplane", {"id", "brand", "model"}).values({2, "Boeing", "707"}).execute();
REQUIRE(res == 1);
res = ses.insert().into("airplane", {"id", "brand", "model"}).values({3, "Boeing", "747"}).execute();
res = db.insert().into("airplane", {"id", "brand", "model"}).values({3, "Boeing", "747"}).execute();
REQUIRE(res == 1);
auto count = ses.select({count_all()}).from("airplane").fetch_value<int>();
auto count = db.select({count_all()}).from("airplane").fetch_value<int>();
REQUIRE(count == 3);
res = ses.insert().into("flight", {"id", "airplane_id", "pilot_name"}).values({4, 1, "George"}).execute();
res = db.insert().into("flight", {"id", "airplane_id", "pilot_name"}).values({4, 1, "George"}).execute();
REQUIRE(res == 1);
ses.drop().table("flight").execute();
ses.drop().table("airplane").execute();
db.drop().table("flight").execute();
db.drop().table("airplane").execute();
REQUIRE(!ses.table_exists("flight"));
REQUIRE(!ses.table_exists("airplane"));
REQUIRE(!db.exists("flight"));
REQUIRE(!db.exists("airplane"));
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute update record statement", "[session][record]")
{
ses.create()
db.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
@@ -179,14 +180,14 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute update record statement", "[ses
})
.execute();
auto res = ses.insert()
auto res = db.insert()
.into("person", {"id", "name", "age"})
.values({7, "george", 45})
.execute();
REQUIRE(res == 1);
res = ses.update("person")
res = db.update("person")
.set({{"id", 7},
{"name", "jane"},
{"age", 35}})
@@ -195,7 +196,7 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute update record statement", "[ses
REQUIRE(res == 1);
auto result = ses.select({"id", "name", "age"})
auto result = db.select({"id", "name", "age"})
.from("person")
.fetch_all();
@@ -212,12 +213,12 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute update record statement", "[ses
REQUIRE(i.at(2).as<int>() == 35);
}
ses.drop().table("person").execute();
db.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement", "[session][record]")
{
ses.create()
db.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
@@ -225,16 +226,16 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement", "[session][r
})
.execute();
auto res = ses.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
auto res = db.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
REQUIRE(res == 1);
auto result = ses.select({"id", "name", "age"})
auto result = db.select({"id", "name", "age"})
.from("person")
.fetch_all();
@@ -245,22 +246,22 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement", "[session][r
}
REQUIRE(expected_names.empty());
auto rec = ses.select({"id", "name", "age"})
auto rec = db.select({"id", "name", "age"})
.from("person")
.fetch_one();
REQUIRE(rec.at(1).str() == "george");
auto name = ses.select({"name"})
auto name = db.select({"name"})
.from("person")
.fetch_value<std::string>();
REQUIRE(name == "george");
ses.drop().table("person").execute();
db.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with order by", "[session][record]")
{
ses.create()
db.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
@@ -268,16 +269,16 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with order by"
})
.execute();
auto res = ses.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
auto res = db.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
REQUIRE(res == 1);
auto result = ses.select({"id", "name", "age"})
auto result = db.select({"id", "name", "age"})
.from("person")
.order_by("name").asc()
.fetch_all();
@@ -289,12 +290,12 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with order by"
}
REQUIRE(expected_names.empty());
ses.drop().table("person").execute();
db.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with group by and order by", "[session][record]")
{
ses.create()
db.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
@@ -302,18 +303,18 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with group by
})
.execute();
auto res = ses.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
auto res = db.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({3, "michael", 13}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({3, "michael", 13}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({5, "charlie", 67}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({5, "charlie", 67}).execute();
REQUIRE(res == 1);
auto result = ses.select({alias(count("age"), "age_count"), "age"})
auto result = db.select({alias(count("age"), "age_count"), "age"})
.from("person")
.group_by("age")
.order_by("age_count").desc()
@@ -328,41 +329,41 @@ TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with group by
expected_values.pop_front();
}
ses.drop().table("person").execute();
db.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Execute delete statement", "[session][record]")
{
ses.create()
db.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
}).execute();
auto res = ses.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
auto res = db.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
REQUIRE(res == 1);
res = ses.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
res = db.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
REQUIRE(res == 1);
auto count = ses.select({count_all()}).from("person").fetch_value<int>();
auto count = db.select({count_all()}).from("person").fetch_value<int>();
REQUIRE(count == 2);
res = ses.remove()
res = db.remove()
.from("person")
.where("id"_col == 1)
.execute();
REQUIRE(res == 1);
count = ses.select({count_all()}).from("person").fetch_value<int>();
count = db.select({count_all()}).from("person").fetch_value<int>();
REQUIRE(count == 1);
ses.drop().table("person").execute();
db.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionRecordFixture, " Test quoted identifier", "[session][record]") {
ses.create()
db.create()
.table("quotes", {
make_column<std::string>("from", 255),
make_column<std::string>("to", 255)
@@ -371,26 +372,26 @@ TEST_CASE_METHOD(SessionRecordFixture, " Test quoted identifier", "[session][rec
// check table description
std::vector<std::string> columns = { "from", "to"};
std::vector<data_type_t> types = {data_type_t::type_varchar, data_type_t::type_varchar};
auto fields = ses.describe_table("quotes");
auto fields = db.describe("quotes");
for (const auto &field : fields) {
REQUIRE(field.name() == columns[field.index()]);
REQUIRE(field.type() == types[field.index()]);
}
ses.insert().into("quotes", {"from", "to"}).values({"Berlin", "London"}).execute();
db.insert().into("quotes", {"from", "to"}).values({"Berlin", "London"}).execute();
auto res = ses.select({"from", "to"}).from("quotes").fetch_one();
auto res = db.select({"from", "to"}).from("quotes").fetch_one();
REQUIRE("Berlin" == res.at("from").str());
REQUIRE("London" == res.at("to").str());
ses.update("quotes").set({{"from", "Hamburg"}, {"to", "New York"}}).where("from"_col == "Berlin").execute();
db.update("quotes").set({{"from", "Hamburg"}, {"to", "New York"}}).where("from"_col == "Berlin").execute();
res = ses.select({"from", "to"}).from("quotes").fetch_one();
res = db.select({"from", "to"}).from("quotes").fetch_one();
REQUIRE("Hamburg" == res.at("from").str());
REQUIRE("New York" == res.at("to").str());
ses.drop().table("quotes").execute();
db.drop().table("quotes").execute();
}
+39 -38
View File
@@ -13,65 +13,66 @@
using namespace matador::sql;
using namespace matador::test;
class SessionFixture
class QueryFixture
{
public:
SessionFixture()
: pool(matador::test::connection::dns, 4), ses(pool)
{}
~SessionFixture() {
QueryFixture()
: db(matador::test::connection::dns)
{
db.open();
}
~QueryFixture() {
drop_table_if_exists("flight");
drop_table_if_exists("airplane");
drop_table_if_exists("person");
}
protected:
matador::sql::connection_pool<matador::sql::connection> pool;
matador::sql::session ses;
matador::sql::connection db;
private:
void drop_table_if_exists(const std::string &table_name) {
if (ses.table_exists(table_name)) {
ses.drop().table(table_name).execute();
if (db.exists(table_name)) {
db.drop().table(table_name).execute();
}
}
};
TEST_CASE_METHOD(SessionFixture, " Create table with foreign key relation", "[session]") {
ses.create()
TEST_CASE_METHOD(QueryFixture, " Create table with foreign key relation", "[session]") {
db.create()
.table<airplane>("airplane")
.execute();
REQUIRE(ses.table_exists("airplane"));
REQUIRE(db.exists("airplane"));
ses.create()
db.create()
.table<flight>("flight")
.execute();
REQUIRE(ses.table_exists("flight"));
REQUIRE(db.exists("flight"));
ses.drop().table("flight").execute();
ses.drop().table("airplane").execute();
db.drop().table("flight").execute();
db.drop().table("airplane").execute();
REQUIRE(!ses.table_exists("flight"));
REQUIRE(!ses.table_exists("airplane"));
REQUIRE(!db.exists("flight"));
REQUIRE(!db.exists("airplane"));
}
TEST_CASE_METHOD(SessionFixture, " Execute select statement with where clause", "[session]") {
ses.create()
TEST_CASE_METHOD(QueryFixture, " Execute select statement with where clause", "[session]") {
db.create()
.table<person>("person")
.execute();
person george{7, "george", 45};
george.image.push_back(37);
auto res = ses.insert()
auto res = db.insert()
.into("person", george)
.execute();
REQUIRE(res == 1);
// fetch person as record
auto result_record = ses.select<person>()
auto result_record = db.select<person>()
.from("person")
.where("id"_col == 7)
.fetch_all();
@@ -90,7 +91,7 @@ TEST_CASE_METHOD(SessionFixture, " Execute select statement with where clause",
}
// fetch person as person
auto result_person = ses.select<person>()
auto result_person = db.select<person>()
.from("person")
.where("id"_col == 7)
.fetch_all<person>();
@@ -101,11 +102,11 @@ TEST_CASE_METHOD(SessionFixture, " Execute select statement with where clause",
REQUIRE(i.age == 45);
}
ses.drop().table("person").execute();
db.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionFixture, " Execute insert statement", "[session]") {
ses.create()
TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]") {
db.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
@@ -113,7 +114,7 @@ TEST_CASE_METHOD(SessionFixture, " Execute insert statement", "[session]") {
})
.execute();
auto res = ses.insert()
auto res = db.insert()
.into("person", {"id", "name", "color"})
.values({7, "george", "green"})
.execute();
@@ -121,7 +122,7 @@ TEST_CASE_METHOD(SessionFixture, " Execute insert statement", "[session]") {
REQUIRE(res == 1);
// fetch person as record
auto result_record = ses.select({"id", "name", "color"})
auto result_record = db.select({"id", "name", "color"})
.from("person")
.where("id"_col == 7)
.fetch_all();
@@ -139,15 +140,15 @@ TEST_CASE_METHOD(SessionFixture, " Execute insert statement", "[session]") {
REQUIRE(i.at(2).as<std::string>() == "green");
}
ses.drop().table("person").execute();
db.drop().table("person").execute();
}
TEST_CASE_METHOD(SessionFixture, " Select statement with foreign key", "[session]") {
ses.create()
TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]") {
db.create()
.table<airplane>("airplane")
.execute();
ses.create()
db.create()
.table<flight>("flight")
.execute();
@@ -158,24 +159,24 @@ TEST_CASE_METHOD(SessionFixture, " Select statement with foreign key", "[session
};
for (const auto &plane : planes) {
auto res = ses.insert().into<airplane>("airplane").values(*plane).execute();
auto res = db.insert().into<airplane>("airplane").values(*plane).execute();
REQUIRE(res == 1);
}
auto count = ses.select({count_all()}).from("airplane").fetch_value<int>();
auto count = db.select({count_all()}).from("airplane").fetch_value<int>();
REQUIRE(count == 3);
flight f4711{4, planes.at(1), "hans"};
auto res = ses.insert().into<flight>("flight").values(f4711).execute();
auto res = db.insert().into<flight>("flight").values(f4711).execute();
REQUIRE(res == 1);
auto f = *ses.select<flight>().from("flight").fetch_all<flight>().begin();
auto f = *db.select<flight>().from("flight").fetch_all<flight>().begin();
REQUIRE(f.id == 4);
REQUIRE(f.pilot_name == "hans");
REQUIRE(f.airplane.get() != nullptr);
REQUIRE(f.airplane->id == 2);
ses.drop().table("flight").execute();
ses.drop().table("airplane").execute();
db.drop().table("flight").execute();
db.drop().table("airplane").execute();
}
+32
View File
@@ -0,0 +1,32 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/connection_info.hpp"
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/session.hpp"
#include "matador/sql/statement_cache.hpp"
#include "connection.hpp"
using namespace matador;
class StatementCacheFixture
{
public:
StatementCacheFixture()
: pool(matador::test::connection::dns, 4), ses(pool)
{}
~StatementCacheFixture() = default;
protected:
matador::sql::connection_pool<matador::sql::connection> pool;
matador::sql::session ses;
};
TEST_CASE_METHOD(StatementCacheFixture, " Acquire prepared statement", "[statement cache]") {
sql::statement_cache cache;
auto conn = pool.acquire();
std::string sql = R"(SELECT * FROM person WHERE name = 'george')";
// auto &stmt = cache.acquire(sql, *conn);
}
+11 -13
View File
@@ -2,9 +2,7 @@
#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 "matador/sql/connection.hpp"
#include "connection.hpp"
@@ -17,9 +15,10 @@ class StatementTestFixture
{
public:
StatementTestFixture()
: pool(matador::test::connection::dns, 4), ses(pool)
: db(matador::test::connection::dns)
{
ses.create().table<airplane>("airplane").execute();
db.open();
db.create().table<airplane>("airplane").execute();
}
~StatementTestFixture()
@@ -28,8 +27,7 @@ public:
}
protected:
matador::sql::connection_pool<matador::sql::connection> pool;
matador::sql::session ses;
matador::sql::connection db;
std::vector<entity<airplane>> planes{
make_entity<airplane>(1, "Airbus", "A380"),
@@ -39,8 +37,8 @@ protected:
private:
void drop_table_if_exists(const std::string &table_name) {
if (ses.table_exists(table_name)) {
ses.drop().table(table_name).execute();
if (db.exists(table_name)) {
db.drop().table(table_name).execute();
}
}
@@ -49,7 +47,7 @@ private:
TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement]")
{
SECTION("Insert with prepared statement and placeholder") {
auto stmt = ses.insert()
auto stmt = db.insert()
.into<airplane>("airplane")
.values<airplane>().prepare();
@@ -59,7 +57,7 @@ TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement
stmt.reset();
}
auto result = ses.select<airplane>().from("airplane").fetch_all<airplane>();
auto result = db.select<airplane>().from("airplane").fetch_all<airplane>();
size_t index{0};
for (const auto &i: result) {
@@ -71,11 +69,11 @@ TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement
SECTION("Select with prepared statement") {
for (const auto &plane: planes) {
auto res = ses.insert().into<airplane>("airplane").values(*plane).execute();
auto res = db.insert().into<airplane>("airplane").values(*plane).execute();
REQUIRE(res == 1);
}
auto stmt = ses.select<airplane>().from("airplane").where("brand"_col == _).prepare();
auto stmt = db.select<airplane>().from("airplane").where("brand"_col == _).prepare();
stmt.bind(0, "Airbus");
+12 -12
View File
@@ -1,6 +1,6 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/session.hpp"
#include "matador/sql/connection.hpp"
#include "matador/utils/enum_mapper.hpp"
@@ -14,26 +14,26 @@ class TypeTraitsTestFixture
{
public:
TypeTraitsTestFixture()
: pool(matador::test::connection::dns, 4), ses(pool)
: db(matador::test::connection::dns)
{
ses.create()
db.open();
db.create()
.table<location>("location")
.execute();
}
~TypeTraitsTestFixture()
{
ses.drop().table("location").execute();
db.drop().table("location").execute();
}
protected:
matador::sql::connection_pool<matador::sql::connection> pool;
matador::sql::session ses;
matador::sql::connection db;
private:
void drop_table_if_exists(const std::string &table_name) {
if (ses.table_exists(table_name)) {
ses.drop().table(table_name).execute();
if (db.exists(table_name)) {
db.drop().table(table_name).execute();
}
}
};
@@ -85,10 +85,10 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
SECTION("Insert and select with direct execution") {
location loc{1, "center", {1, 2, 3}, Color::Black};
auto res = ses.insert().into<location>("location").values(loc).execute();
auto res = db.insert().into<location>("location").values(loc).execute();
REQUIRE(res == 1);
auto result = ses.select<location>().from("location").fetch_all<location>();
auto result = db.select<location>().from("location").fetch_all<location>();
for (const auto &l: result) {
REQUIRE(l.name == "center");
@@ -98,11 +98,11 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
SECTION("Insert and select with prepared statement") {
location loc{1, "center", {1, 2, 3}, Color::Black};
auto stmt = ses.insert().into<location>("location").values<location>().prepare();
auto stmt = db.insert().into<location>("location").values<location>().prepare();
auto res = stmt.bind(loc).execute();
REQUIRE(res == 1);
auto result = ses.select<location>().from("location").
auto result = db.select<location>().from("location").
template fetch_all<location>();
for (const auto &l: result) {