backend tests progress
This commit is contained in:
parent
47848ff674
commit
e171147915
|
|
@ -22,7 +22,9 @@ set(TEST_SOURCES
|
|||
../../tests/QueryTest.cpp
|
||||
../../tests/SessionTest.cpp
|
||||
../../tests/ConnectionTest.cpp
|
||||
../../tests/SessionRecordTest.cpp)
|
||||
../../tests/SessionRecordTest.cpp
|
||||
../../tests/StatementTest.cpp
|
||||
../../tests/TypeTraitsTest.cpp)
|
||||
|
||||
set(LIBRARY_TEST_TARGET mysql_tests)
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,9 @@ set(TEST_SOURCES
|
|||
../../tests/QueryTest.cpp
|
||||
../../tests/SessionTest.cpp
|
||||
../../tests/ConnectionTest.cpp
|
||||
../../tests/SessionRecordTest.cpp)
|
||||
../../tests/SessionRecordTest.cpp
|
||||
../../tests/StatementTest.cpp
|
||||
../../tests/TypeTraitsTest.cpp)
|
||||
|
||||
set(LIBRARY_TEST_TARGET postgres_tests)
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,9 @@ set(TEST_SOURCES
|
|||
../../tests/QueryTest.cpp
|
||||
../../tests/SessionTest.cpp
|
||||
../../tests/ConnectionTest.cpp
|
||||
../../tests/SessionRecordTest.cpp)
|
||||
../../tests/SessionRecordTest.cpp
|
||||
../../tests/StatementTest.cpp
|
||||
../../tests/TypeTraitsTest.cpp)
|
||||
|
||||
set(LIBRARY_TEST_TARGET sqlite_tests)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,361 @@
|
|||
//
|
||||
// Created by sascha on 28.01.24.
|
||||
//
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/condition.hpp"
|
||||
#include "matador/sql/session.hpp"
|
||||
|
||||
#include "connection.hpp"
|
||||
|
||||
#include <list>
|
||||
|
||||
class SessionRecordFixture
|
||||
{
|
||||
public:
|
||||
SessionRecordFixture()
|
||||
: pool(matador::test::connection::dns, 4), ses(pool)
|
||||
{}
|
||||
~SessionRecordFixture() {
|
||||
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;
|
||||
|
||||
private:
|
||||
void drop_table_if_exists(const std::string &table_name) {
|
||||
if (ses.table_exists(table_name)) {
|
||||
ses.drop().table(table_name).execute();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement", "[session record]")
|
||||
{
|
||||
REQUIRE(!ses.table_exists("person"));
|
||||
ses.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
})
|
||||
.execute();
|
||||
|
||||
REQUIRE(ses.table_exists("person"));
|
||||
|
||||
ses.drop()
|
||||
.table("person")
|
||||
.execute();
|
||||
|
||||
REQUIRE(!ses.table_exists("person"));
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionRecordFixture, " Create and drop table statement with foreign key", "[session record]")
|
||||
{
|
||||
ses.create()
|
||||
.table("airplane", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("brand", 255),
|
||||
make_column<std::string>("model", 255),
|
||||
})
|
||||
.execute();
|
||||
|
||||
REQUIRE(ses.table_exists("airplane"));
|
||||
|
||||
ses.create()
|
||||
.table("flight", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
|
||||
make_column<std::string>("pilot_name", 255),
|
||||
})
|
||||
.execute();
|
||||
|
||||
REQUIRE(ses.table_exists("flight"));
|
||||
|
||||
ses.drop()
|
||||
.table("flight")
|
||||
.execute();
|
||||
|
||||
REQUIRE(!ses.table_exists("flight"));
|
||||
|
||||
ses.drop()
|
||||
.table("airplane")
|
||||
.execute();
|
||||
|
||||
REQUIRE(!ses.table_exists("airplane"));
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement", "[session record]")
|
||||
{
|
||||
ses.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({7, "george", 45})
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = ses.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_all();
|
||||
|
||||
for (const auto &i: result) {
|
||||
REQUIRE(i.size() == 3);
|
||||
REQUIRE(i.at(0).name() == "id");
|
||||
// REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
||||
REQUIRE(i.at(0).template as<long long>() == 7);
|
||||
REQUIRE(i.at(1).name() == "name");
|
||||
// REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||
REQUIRE(i.at(1).template as<std::string>() == "george");
|
||||
REQUIRE(i.at(2).name() == "age");
|
||||
// REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
|
||||
REQUIRE(i.at(2).template as<int>() == 45);
|
||||
}
|
||||
|
||||
ses.drop()
|
||||
.table("person")
|
||||
.execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionRecordFixture, " Execute insert record statement with foreign key", "[session record]")
|
||||
{
|
||||
ses.create()
|
||||
.table("airplane", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("brand", 255),
|
||||
make_column<std::string>("model", 255),
|
||||
})
|
||||
.execute();
|
||||
|
||||
ses.create()
|
||||
.table("flight", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
|
||||
make_column<std::string>("pilot_name", 255),
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = ses.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();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
res = ses.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>();
|
||||
REQUIRE(count == 3);
|
||||
|
||||
res = ses.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();
|
||||
|
||||
REQUIRE(!ses.table_exists("flight"));
|
||||
REQUIRE(!ses.table_exists("airplane"));
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionRecordFixture, " Execute update record statement", "[session record]")
|
||||
{
|
||||
ses.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({7, "george", 45})
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
res = ses.update("person")
|
||||
.set({{"id", 7},
|
||||
{"name", "jane"},
|
||||
{"age", 35}})
|
||||
.where("id"_col == 7)
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = ses.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_all();
|
||||
|
||||
for (const auto &i: result) {
|
||||
REQUIRE(i.size() == 3);
|
||||
REQUIRE(i.at(0).name() == "id");
|
||||
REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
||||
REQUIRE(i.at(0).as<long long>() == 7);
|
||||
REQUIRE(i.at(1).name() == "name");
|
||||
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||
REQUIRE(i.at(1).as<std::string>() == "jane");
|
||||
REQUIRE(i.at(2).name() == "age");
|
||||
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
|
||||
REQUIRE(i.at(2).as<int>() == 35);
|
||||
}
|
||||
|
||||
ses.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement", "[session record]")
|
||||
{
|
||||
ses.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();
|
||||
REQUIRE(res == 1);
|
||||
res = ses.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();
|
||||
REQUIRE(res == 1);
|
||||
res = ses.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = ses.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_all();
|
||||
|
||||
std::list<std::string> expected_names{"george", "jane", "michael", "bob"};
|
||||
for (const auto &p: result) {
|
||||
REQUIRE(p.at(1).str() == expected_names.front());
|
||||
expected_names.pop_front();
|
||||
}
|
||||
REQUIRE(expected_names.empty());
|
||||
|
||||
auto rec = ses.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_one();
|
||||
REQUIRE(rec.at(1).str() == "george");
|
||||
|
||||
auto name = ses.select({"name"})
|
||||
.from("person")
|
||||
.fetch_value<std::string>();
|
||||
REQUIRE(name == "george");
|
||||
|
||||
ses.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with order by", "[session record]")
|
||||
{
|
||||
ses.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();
|
||||
REQUIRE(res == 1);
|
||||
res = ses.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();
|
||||
REQUIRE(res == 1);
|
||||
res = ses.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = ses.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.order_by("name").asc()
|
||||
.fetch_all();
|
||||
|
||||
std::list<std::string> expected_names{"bob", "george", "jane", "michael"};
|
||||
for (const auto &p: result) {
|
||||
REQUIRE(p.at(1).str() == expected_names.front());
|
||||
expected_names.pop_front();
|
||||
}
|
||||
REQUIRE(expected_names.empty());
|
||||
|
||||
ses.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionRecordFixture, " Execute select statement with group by and order by", "[session record]")
|
||||
{
|
||||
ses.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();
|
||||
REQUIRE(res == 1);
|
||||
res = ses.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();
|
||||
REQUIRE(res == 1);
|
||||
res = ses.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();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = ses.select({alias(count("age"), "age_count"), "age"})
|
||||
.from("person")
|
||||
.group_by("age")
|
||||
.order_by("age_count").desc()
|
||||
.fetch_all();
|
||||
|
||||
std::list<std::pair<int, int>> expected_values{{2, 45},
|
||||
{2, 13},
|
||||
{1, 67}};
|
||||
for (const auto &r: result) {
|
||||
REQUIRE(r.at(0).as<int>() == expected_values.front().first);
|
||||
REQUIRE(r.at(1).as<int>() == expected_values.front().second);
|
||||
expected_values.pop_front();
|
||||
}
|
||||
|
||||
ses.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionRecordFixture, " Execute delete statement", "[session record]")
|
||||
{
|
||||
ses.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();
|
||||
REQUIRE(res == 1);
|
||||
res = ses.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>();
|
||||
REQUIRE(count == 2);
|
||||
|
||||
res = ses.remove()
|
||||
.from("person")
|
||||
.where("id"_col == 1)
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
count = ses.select({count_all()}).from("person").fetch_value<int>();
|
||||
REQUIRE(count == 1);
|
||||
|
||||
ses.drop().table("person").execute();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/catch_template_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/condition.hpp"
|
||||
|
|
@ -7,56 +6,52 @@
|
|||
#include "matador/sql/connection_pool.hpp"
|
||||
#include "matador/sql/session.hpp"
|
||||
|
||||
#include "Databases.hpp"
|
||||
#include "connection.hpp"
|
||||
|
||||
#include "models/airplane.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::test;
|
||||
|
||||
template<class Type>
|
||||
class StatementTestFixture
|
||||
{
|
||||
public:
|
||||
StatementTestFixture()
|
||||
: pool_(Type::dns, 4), session_(pool_)
|
||||
: pool(matador::test::connection::dns, 4), ses(pool)
|
||||
{
|
||||
session_.create()
|
||||
.table<airplane>("airplane")
|
||||
.execute();
|
||||
ses.create().table<airplane>("airplane").execute();
|
||||
}
|
||||
|
||||
~StatementTestFixture()
|
||||
{
|
||||
session_.drop().table("airplane").execute();
|
||||
drop_table_if_exists("airplane");
|
||||
}
|
||||
|
||||
matador::sql::session &session()
|
||||
{ return session_; }
|
||||
protected:
|
||||
matador::sql::connection_pool<matador::sql::connection> pool;
|
||||
matador::sql::session ses;
|
||||
|
||||
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_{
|
||||
std::vector<entity<airplane>> planes{
|
||||
make_entity<airplane>(1, "Airbus", "A380"),
|
||||
make_entity<airplane>(2, "Boeing", "707"),
|
||||
make_entity<airplane>(3, "Boeing", "747")
|
||||
};
|
||||
|
||||
private:
|
||||
void drop_table_if_exists(const std::string &table_name) {
|
||||
if (ses.table_exists(table_name)) {
|
||||
ses.drop().table(table_name).execute();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
TEMPLATE_TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]", Sqlite, Postgres, MySql)
|
||||
TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement]")
|
||||
{
|
||||
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();
|
||||
auto stmt = ses.insert()
|
||||
.into<airplane>("airplane")
|
||||
.values<airplane>().prepare();
|
||||
|
||||
for (const auto &plane: planes) {
|
||||
auto res = stmt.bind(*plane).execute();
|
||||
|
|
@ -64,7 +59,7 @@ TEMPLATE_TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[s
|
|||
stmt.reset();
|
||||
}
|
||||
|
||||
auto result = s.template select<airplane>().from("airplane").template fetch_all<airplane>();
|
||||
auto result = ses.select<airplane>().from("airplane").fetch_all<airplane>();
|
||||
|
||||
size_t index{0};
|
||||
for (const auto &i: result) {
|
||||
|
|
@ -76,15 +71,15 @@ TEMPLATE_TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[s
|
|||
|
||||
SECTION("Select with prepared statement") {
|
||||
for (const auto &plane: planes) {
|
||||
auto res = s.insert().template into<airplane>("airplane").values(*plane).execute();
|
||||
auto res = ses.insert().into<airplane>("airplane").values(*plane).execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto stmt = s.template select<airplane>().from("airplane").where("brand"_col == _).prepare();
|
||||
auto stmt = ses.select<airplane>().from("airplane").where("brand"_col == _).prepare();
|
||||
|
||||
stmt.bind(0, "Airbus");
|
||||
|
||||
auto result = stmt.template fetch<airplane>();
|
||||
auto result = stmt.fetch<airplane>();
|
||||
|
||||
for (const auto &i: result) {
|
||||
REQUIRE(i.id == planes[0]->id);
|
||||
|
|
@ -96,7 +91,7 @@ TEMPLATE_TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[s
|
|||
|
||||
stmt.bind(0, "Boeing");
|
||||
|
||||
result = stmt.template fetch<airplane>();
|
||||
result = stmt.fetch<airplane>();
|
||||
|
||||
size_t index{1};
|
||||
for (const auto &i: result) {
|
||||
|
|
@ -1,44 +1,41 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/catch_template_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/connection_pool.hpp"
|
||||
#include "matador/sql/session.hpp"
|
||||
|
||||
#include "matador/utils/enum_mapper.hpp"
|
||||
|
||||
#include "Databases.hpp"
|
||||
#include "connection.hpp"
|
||||
|
||||
#include "models/location.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::test;
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::test;
|
||||
|
||||
template<class Type>
|
||||
class TypeTraitsTestFixture
|
||||
{
|
||||
public:
|
||||
TypeTraitsTestFixture()
|
||||
: pool_(Type::dns, 4), session_(pool_)
|
||||
: pool(matador::test::connection::dns, 4), ses(pool)
|
||||
{
|
||||
session_.create()
|
||||
ses.create()
|
||||
.table<location>("location")
|
||||
.execute();
|
||||
}
|
||||
|
||||
~TypeTraitsTestFixture()
|
||||
{
|
||||
session_.drop().table("location").execute();
|
||||
ses.drop().table("location").execute();
|
||||
}
|
||||
|
||||
matador::sql::session &session()
|
||||
{ return session_; }
|
||||
protected:
|
||||
matador::sql::connection_pool<matador::sql::connection> pool;
|
||||
matador::sql::session ses;
|
||||
|
||||
private:
|
||||
matador::sql::connection_pool<matador::sql::connection> pool_;
|
||||
matador::sql::session session_;
|
||||
void drop_table_if_exists(const std::string &table_name) {
|
||||
if (ses.table_exists(table_name)) {
|
||||
ses.drop().table(table_name).execute();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static const matador::utils::enum_mapper<Color> color_enum({
|
||||
|
|
@ -83,18 +80,15 @@ struct data_type_traits<Color, void>
|
|||
}
|
||||
|
||||
|
||||
TEMPLATE_TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with type traits", "[typetraits]", Sqlite, Postgres, MySql)
|
||||
TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with type traits", "[typetraits]")
|
||||
{
|
||||
auto &s = TypeTraitsTestFixture<TestType>::session();
|
||||
|
||||
SECTION("Insert and select with direct execution") {
|
||||
location loc{1, "center", {1, 2, 3}, Color::Black};
|
||||
|
||||
auto res = s.insert().template into<location>("location").values(loc).execute();
|
||||
auto res = ses.insert().into<location>("location").values(loc).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = s.template select<location>().from("location").
|
||||
template fetch_all<location>();
|
||||
auto result = ses.select<location>().from("location").fetch_all<location>();
|
||||
|
||||
for (const auto &l: result) {
|
||||
REQUIRE(l.name == "center");
|
||||
|
|
@ -104,11 +98,11 @@ TEMPLATE_TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes
|
|||
SECTION("Insert and select with prepared statement") {
|
||||
location loc{1, "center", {1, 2, 3}, Color::Black};
|
||||
|
||||
auto stmt = s.insert().template into<location>("location").template values<location>().prepare();
|
||||
auto stmt = ses.insert().into<location>("location").values<location>().prepare();
|
||||
auto res = stmt.bind(loc).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = s.template select<location>().from("location").
|
||||
auto result = ses.select<location>().from("location").
|
||||
template fetch_all<location>();
|
||||
|
||||
for (const auto &l: result) {
|
||||
|
|
@ -9,11 +9,9 @@ FetchContent_Declare(
|
|||
FetchContent_MakeAvailable(Catch2)
|
||||
|
||||
add_executable(tests QueryBuilderTest.cpp
|
||||
SessionTest.cpp
|
||||
RecordTest.cpp
|
||||
ConnectionPoolTest.cpp
|
||||
BackendProviderTest.cpp
|
||||
ConnectionTest.cpp
|
||||
models/product.hpp
|
||||
ColumnGeneratorTest.cpp
|
||||
ColumnNameGeneratorTest.cpp
|
||||
|
|
@ -25,13 +23,9 @@ add_executable(tests QueryBuilderTest.cpp
|
|||
models/person.hpp
|
||||
AnyTypeToVisitorTest.cpp
|
||||
ColumnTest.cpp
|
||||
SessionRecordTest.cpp
|
||||
StatementCacheTest.cpp
|
||||
StatementTest.cpp
|
||||
models/coordinate.hpp
|
||||
models/location.hpp
|
||||
TypeTraitsTest.cpp
|
||||
Databases.hpp
|
||||
models/optional.hpp
|
||||
ConvertTest.cpp
|
||||
DummyConnection.hpp
|
||||
|
|
|
|||
|
|
@ -1,30 +0,0 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/catch_template_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
#include "Databases.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
template<class Type>
|
||||
class ConnectionTestFixture
|
||||
{
|
||||
public:
|
||||
ConnectionTestFixture() = default;
|
||||
~ConnectionTestFixture() = default;
|
||||
|
||||
std::string dns() { return Type::dns; }
|
||||
};
|
||||
|
||||
TEMPLATE_TEST_CASE_METHOD(ConnectionTestFixture, "Create connection", "[connection]", Sqlite, Postgres, MySql) {
|
||||
|
||||
connection c(ConnectionTestFixture<TestType>::dns());
|
||||
REQUIRE(!c.is_open());
|
||||
|
||||
c.open();
|
||||
REQUIRE(c.is_open());
|
||||
|
||||
c.close();
|
||||
REQUIRE(!c.is_open());
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
#ifndef QUERY_DATABASES_HPP
|
||||
#define QUERY_DATABASES_HPP
|
||||
|
||||
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"};
|
||||
};
|
||||
|
||||
struct MySql
|
||||
{
|
||||
constexpr static const char *dns{"mysql://test:test123!@127.0.0.1:3306/testdb"};
|
||||
// constexpr static const char *dns{"mysql://test:test123!@127.0.0.1:3306/matador_test"};
|
||||
};
|
||||
|
||||
#endif //QUERY_DATABASES_HPP
|
||||
|
|
@ -1,378 +0,0 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/generators/catch_generators.hpp>
|
||||
#include <catch2/catch_template_test_macros.hpp>
|
||||
|
||||
#include <matador/sql/condition.hpp>
|
||||
#include <matador/sql/session.hpp>
|
||||
|
||||
#include "Databases.hpp"
|
||||
|
||||
#include <list>
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
template<class Type>
|
||||
class SessionRecordTestFixture
|
||||
{
|
||||
public:
|
||||
SessionRecordTestFixture()
|
||||
: pool_(Type::dns, 4), session_(pool_)
|
||||
{}
|
||||
|
||||
matador::sql::session &session()
|
||||
{ return session_; }
|
||||
|
||||
private:
|
||||
matador::sql::connection_pool<matador::sql::connection> pool_;
|
||||
matador::sql::session session_;
|
||||
};
|
||||
|
||||
TEMPLATE_TEST_CASE_METHOD(SessionRecordTestFixture, "Create and drop table statement", "[session record]", Sqlite, Postgres, MySql)
|
||||
{
|
||||
auto &s = SessionRecordTestFixture<TestType>::session();
|
||||
|
||||
REQUIRE(!s.table_exists("person"));
|
||||
s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
})
|
||||
.execute();
|
||||
|
||||
REQUIRE(s.table_exists("person"));
|
||||
|
||||
s.drop()
|
||||
.table("person")
|
||||
.execute();
|
||||
|
||||
REQUIRE(!s.table_exists("person"));
|
||||
}
|
||||
|
||||
TEMPLATE_TEST_CASE_METHOD(SessionRecordTestFixture, "Create and drop table statement with foreign key", "[session record]", Sqlite, Postgres, MySql)
|
||||
{
|
||||
auto &s = SessionRecordTestFixture<TestType>::session();
|
||||
|
||||
s.create()
|
||||
.table("airplane", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("brand", 255),
|
||||
make_column<std::string>("model", 255),
|
||||
})
|
||||
.execute();
|
||||
|
||||
REQUIRE(s.table_exists("airplane"));
|
||||
|
||||
s.create()
|
||||
.table("flight", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
|
||||
make_column<std::string>("pilot_name", 255),
|
||||
})
|
||||
.execute();
|
||||
|
||||
REQUIRE(s.table_exists("flight"));
|
||||
|
||||
s.drop()
|
||||
.table("flight")
|
||||
.execute();
|
||||
|
||||
REQUIRE(!s.table_exists("flight"));
|
||||
|
||||
s.drop()
|
||||
.table("airplane")
|
||||
.execute();
|
||||
|
||||
REQUIRE(!s.table_exists("airplane"));
|
||||
}
|
||||
|
||||
TEMPLATE_TEST_CASE_METHOD(SessionRecordTestFixture, "Execute insert record statement", "[session record]", MySql)
|
||||
{
|
||||
auto &s = SessionRecordTestFixture<TestType>::session();
|
||||
|
||||
s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = s.insert()
|
||||
.into("person", {"id", "name", "age"})
|
||||
.values({7, "george", 45})
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = s.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_all();
|
||||
|
||||
for (const auto &i: result) {
|
||||
REQUIRE(i.size() == 3);
|
||||
REQUIRE(i.at(0).name() == "id");
|
||||
// REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
||||
REQUIRE(i.at(0).template as<long long>() == 7);
|
||||
REQUIRE(i.at(1).name() == "name");
|
||||
// REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||
REQUIRE(i.at(1).template as<std::string>() == "george");
|
||||
REQUIRE(i.at(2).name() == "age");
|
||||
// REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
|
||||
REQUIRE(i.at(2).template as<int>() == 45);
|
||||
}
|
||||
|
||||
s.drop()
|
||||
.table("person")
|
||||
.execute();
|
||||
}
|
||||
|
||||
TEST_CASE("Execute insert record statement with foreign key", "[session record]")
|
||||
{
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
s.create()
|
||||
.table("airplane", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("brand", 255),
|
||||
make_column<std::string>("model", 255),
|
||||
})
|
||||
.execute();
|
||||
|
||||
s.create()
|
||||
.table("flight", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
|
||||
make_column<std::string>("pilot_name", 255),
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = s.insert().into("airplane", {"id", "brand", "model"}).values({1, "Airbus", "A380"}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
res = s.insert().into("airplane", {"id", "brand", "model"}).values({2, "Boeing", "707"}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
res = s.insert().into("airplane", {"id", "brand", "model"}).values({3, "Boeing", "747"}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto count = s.select({count_all()}).from("airplane").fetch_value<int>();
|
||||
REQUIRE(count == 3);
|
||||
|
||||
res = s.insert().into("flight", {"id", "airplane_id", "pilot_name"}).values({4, 1, "George"}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
s.drop().table("flight").execute();
|
||||
s.drop().table("airplane").execute();
|
||||
|
||||
REQUIRE(!s.table_exists("flight"));
|
||||
REQUIRE(!s.table_exists("airplane"));
|
||||
}
|
||||
|
||||
TEST_CASE("Execute update record statement", "[session record]")
|
||||
{
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = s.insert()
|
||||
.into("person", {"id", "name", "age"})
|
||||
.values({7, "george", 45})
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
res = s.update("person")
|
||||
.set({{"id", 7},
|
||||
{"name", "jane"},
|
||||
{"age", 35}})
|
||||
.where("id"_col == 7)
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = s.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_all();
|
||||
|
||||
for (const auto &i: result) {
|
||||
REQUIRE(i.size() == 3);
|
||||
REQUIRE(i.at(0).name() == "id");
|
||||
REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
||||
REQUIRE(i.at(0).as<long long>() == 7);
|
||||
REQUIRE(i.at(1).name() == "name");
|
||||
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||
REQUIRE(i.at(1).as<std::string>() == "jane");
|
||||
REQUIRE(i.at(2).name() == "age");
|
||||
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
|
||||
REQUIRE(i.at(2).as<int>() == 35);
|
||||
}
|
||||
|
||||
s.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE("Execute select statement", "[session record]")
|
||||
{
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = s.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = s.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_all();
|
||||
|
||||
std::list<std::string> expected_names{"george", "jane", "michael", "bob"};
|
||||
for (const auto &p: result) {
|
||||
REQUIRE(p.at(1).str() == expected_names.front());
|
||||
expected_names.pop_front();
|
||||
}
|
||||
REQUIRE(expected_names.empty());
|
||||
|
||||
auto rec = s.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_one();
|
||||
REQUIRE(rec.at(1).str() == "george");
|
||||
|
||||
auto name = s.select({"name"})
|
||||
.from("person")
|
||||
.fetch_value<std::string>();
|
||||
REQUIRE(name == "george");
|
||||
|
||||
s.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE("Execute select statement with order by", "[session record]")
|
||||
{
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = s.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({2, "jane", 32}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({3, "michael", 67}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = s.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.order_by("name").asc()
|
||||
.fetch_all();
|
||||
|
||||
std::list<std::string> expected_names{"bob", "george", "jane", "michael"};
|
||||
for (const auto &p: result) {
|
||||
REQUIRE(p.at(1).str() == expected_names.front());
|
||||
expected_names.pop_front();
|
||||
}
|
||||
REQUIRE(expected_names.empty());
|
||||
|
||||
s.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE("Execute select statement with group by and order by", "[session record]")
|
||||
{
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = s.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({3, "michael", 13}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({4, "bob", 13}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({5, "charlie", 67}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = s.select({alias(count("age"), "age_count"), "age"})
|
||||
.from("person")
|
||||
.group_by("age")
|
||||
.order_by("age_count").desc()
|
||||
.fetch_all();
|
||||
|
||||
std::list<std::pair<int, int>> expected_values{{2, 45},
|
||||
{2, 13},
|
||||
{1, 67}};
|
||||
for (const auto &r: result) {
|
||||
REQUIRE(r.at(0).as<int>() == expected_values.front().first);
|
||||
REQUIRE(r.at(1).as<int>() == expected_values.front().second);
|
||||
expected_values.pop_front();
|
||||
}
|
||||
|
||||
s.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE("Execute delete statement", "[session record]")
|
||||
{
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
}).execute();
|
||||
|
||||
auto res = s.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto count = s.select({count_all()}).from("person").fetch_value<int>();
|
||||
REQUIRE(count == 2);
|
||||
|
||||
res = s.remove()
|
||||
.from("person")
|
||||
.where("id"_col == 1)
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
count = s.select({count_all()}).from("person").fetch_value<int>();
|
||||
REQUIRE(count == 1);
|
||||
|
||||
s.drop().table("person").execute();
|
||||
}
|
||||
|
|
@ -1,164 +0,0 @@
|
|||
#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/session.hpp>
|
||||
|
||||
#include "Databases.hpp"
|
||||
|
||||
#include "models/product.hpp"
|
||||
#include "models/airplane.hpp"
|
||||
#include "models/flight.hpp"
|
||||
#include "models/person.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::test;
|
||||
|
||||
template<class Type>
|
||||
class SessionTestFixture
|
||||
{
|
||||
public:
|
||||
SessionTestFixture()
|
||||
: pool_(Type::dns, 4), session_(pool_)
|
||||
{}
|
||||
~SessionTestFixture() = default;
|
||||
|
||||
matador::sql::session &session()
|
||||
{ return session_; }
|
||||
|
||||
private:
|
||||
matador::sql::connection_pool<matador::sql::connection> pool_;
|
||||
matador::sql::session session_;
|
||||
};
|
||||
|
||||
TEMPLATE_TEST_CASE_METHOD(SessionTestFixture, "Create table with foreign key relation", "[session]", Sqlite, Postgres) {
|
||||
auto &s = SessionTestFixture<TestType>::session();
|
||||
|
||||
s.create()
|
||||
.template table<airplane>("airplane")
|
||||
.execute();
|
||||
|
||||
REQUIRE(s.table_exists("airplane"));
|
||||
|
||||
s.create()
|
||||
.template table<flight>("flight")
|
||||
.execute();
|
||||
|
||||
REQUIRE(s.table_exists("flight"));
|
||||
|
||||
s.drop().table("flight").execute();
|
||||
s.drop().table("airplane").execute();
|
||||
|
||||
REQUIRE(!s.table_exists("flight"));
|
||||
REQUIRE(!s.table_exists("airplane"));
|
||||
}
|
||||
|
||||
TEMPLATE_TEST_CASE_METHOD(SessionTestFixture, "Execute select statement with where clause", "[session]", Postgres) {
|
||||
auto &s = SessionTestFixture<TestType>::session();
|
||||
|
||||
s.create()
|
||||
.template table<person>("person")
|
||||
.execute();
|
||||
|
||||
person george{7, "george", 45};
|
||||
|
||||
auto res = s.insert()
|
||||
.into("person", george)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
// fetch person as record
|
||||
auto result_record = s.template select<person>()
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all();
|
||||
|
||||
for (const auto& i : result_record) {
|
||||
REQUIRE(i.size() == 3);
|
||||
REQUIRE(i.at(0).name() == "id");
|
||||
REQUIRE(i.at(0).type() == data_type_t::type_unsigned_long);
|
||||
REQUIRE(i.at(0).template as<long long>() == george.id);
|
||||
REQUIRE(i.at(1).name() == "name");
|
||||
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||
REQUIRE(i.at(1).template as<std::string>() == george.name);
|
||||
REQUIRE(i.at(2).name() == "age");
|
||||
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_unsigned_int);
|
||||
REQUIRE(i.at(2).template as<long long>() == george.age);
|
||||
}
|
||||
|
||||
// fetch person as person
|
||||
auto result_person = s.template select<person>()
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.template fetch_all<person>();
|
||||
|
||||
for (const auto& i : result_person) {
|
||||
REQUIRE(i.id == 7);
|
||||
REQUIRE(i.name == "george");
|
||||
REQUIRE(i.age == 45);
|
||||
}
|
||||
|
||||
s.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEMPLATE_TEST_CASE_METHOD(SessionTestFixture, "Execute insert statement", "[session]", Sqlite, Postgres) {
|
||||
auto &s = SessionTestFixture<TestType>::session();
|
||||
|
||||
s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<std::string>("color", 63)
|
||||
})
|
||||
.execute();
|
||||
|
||||
auto res = s.insert()
|
||||
.into("person", {"id", "name", "color"})
|
||||
.values({7, "george", "green"})
|
||||
.execute();
|
||||
|
||||
REQUIRE(res == 1);
|
||||
|
||||
s.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEMPLATE_TEST_CASE_METHOD(SessionTestFixture, "Select statement with foreign key", "[session]", Sqlite, Postgres) {
|
||||
auto &s = SessionTestFixture<TestType>::session();
|
||||
|
||||
s.create()
|
||||
.template table<airplane>("airplane")
|
||||
.execute();
|
||||
|
||||
s.create()
|
||||
.template table<flight>("flight")
|
||||
.execute();
|
||||
|
||||
std::vector<entity<airplane>> planes {
|
||||
make_entity<airplane>(1, "Airbus", "A380"),
|
||||
make_entity<airplane>(2, "Boeing", "707"),
|
||||
make_entity<airplane>(3, "Boeing", "747")
|
||||
};
|
||||
|
||||
for (const auto &plane : planes) {
|
||||
auto res = s.insert().template into<airplane>("airplane").values(*plane).execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto count = s.select({count_all()}).from("airplane").template fetch_value<int>();
|
||||
REQUIRE(count == 3);
|
||||
|
||||
flight f4711{4, planes.at(1), "hans"};
|
||||
|
||||
auto res = s.insert().template into<flight>("flight").values(f4711).execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto f = *s.template select<flight>().from("flight").template fetch_all<flight>().begin();
|
||||
REQUIRE(f.id == 4);
|
||||
REQUIRE(f.pilot_name == "hans");
|
||||
REQUIRE(f.airplane.get() != nullptr);
|
||||
REQUIRE(f.airplane->id == 2);
|
||||
|
||||
s.drop().table("flight").execute();
|
||||
s.drop().table("airplane").execute();
|
||||
}
|
||||
|
|
@ -16,7 +16,7 @@ TEST_CASE("Extract values object", "[value extractor]") {
|
|||
p.quantity_per_unit = "pcs";
|
||||
p.category = make_entity<matador::test::category>();
|
||||
p.category->id = 7;
|
||||
p.supplier = make_entity<matador::test::supplier>();;
|
||||
p.supplier = make_entity<matador::test::supplier>();
|
||||
p.supplier->id = 13;
|
||||
p.product_name = "candle";
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue