query/backends/tests/QueryTest.cpp

182 lines
4.7 KiB
C++

#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 "models/airplane.hpp"
#include "models/flight.hpp"
#include "models/person.hpp"
using namespace matador::sql;
using namespace matador::test;
class QueryFixture
{
public:
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 db;
private:
void drop_table_if_exists(const std::string &table_name) {
if (db.exists(table_name)) {
db.drop().table(table_name).execute();
}
}
};
TEST_CASE_METHOD(QueryFixture, " Create table with foreign key relation", "[session]") {
db.create()
.table<airplane>("airplane")
.execute();
REQUIRE(db.exists("airplane"));
db.create()
.table<flight>("flight")
.execute();
REQUIRE(db.exists("flight"));
db.drop().table("flight").execute();
db.drop().table("airplane").execute();
REQUIRE(!db.exists("flight"));
REQUIRE(!db.exists("airplane"));
}
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 = db.insert()
.into("person", george)
.execute();
REQUIRE(res == 1);
// fetch person as record
auto result_record = db.select<person>()
.from("person")
.where("id"_col == 7)
.fetch_all();
for (const auto& i : result_record) {
REQUIRE(i.size() == 4);
REQUIRE(i.at(0).name() == "id");
REQUIRE(i.at(0).type() == data_type_t::type_unsigned_long);
REQUIRE(i.at(0).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).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).as<long long>() == george.age);
}
// fetch person as person
auto result_person = db.select<person>()
.from("person")
.where("id"_col == 7)
.fetch_all<person>();
for (const auto& i : result_person) {
REQUIRE(i.id == 7);
REQUIRE(i.name == "george");
REQUIRE(i.age == 45);
}
db.drop().table("person").execute();
}
TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]") {
db.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("color", 63)
})
.execute();
auto res = db.insert()
.into("person", {"id", "name", "color"})
.values({7, "george", "green"})
.execute();
REQUIRE(res == 1);
// fetch person as record
auto result_record = db.select({"id", "name", "color"})
.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_long_long);
REQUIRE(i.at(0).as<unsigned 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>() == "george");
REQUIRE(i.at(2).name() == "color");
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_varchar);
REQUIRE(i.at(2).as<std::string>() == "green");
}
db.drop().table("person").execute();
}
TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]") {
db.create()
.table<airplane>("airplane")
.execute();
db.create()
.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 = db.insert().into<airplane>("airplane").values(*plane).execute();
REQUIRE(res == 1);
}
auto count = db.select({count_all()}).from("airplane").fetch_value<int>();
REQUIRE(count == 3);
flight f4711{4, planes.at(1), "hans"};
auto res = db.insert().into<flight>("flight").values(f4711).execute();
REQUIRE(res == 1);
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);
db.drop().table("flight").execute();
db.drop().table("airplane").execute();
}