78 lines
2.1 KiB
C++
78 lines
2.1 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include "ColorEnumTraits.hpp"
|
|
|
|
#include "matador/sql/connection.hpp"
|
|
|
|
#include "matador/query/generator.hpp"
|
|
#include "matador/query/query.hpp"
|
|
|
|
#include "QueryFixture.hpp"
|
|
|
|
#include "models/location.hpp"
|
|
|
|
using namespace matador::query;
|
|
using namespace matador::sql;
|
|
using namespace matador::test;
|
|
|
|
class TypeTraitsTestFixture : public QueryFixture
|
|
{
|
|
public:
|
|
TypeTraitsTestFixture() {
|
|
REQUIRE(db.open());
|
|
const auto res = query::create()
|
|
.table<location>("location", repo)
|
|
.execute(db);
|
|
tables_to_drop.emplace("location");
|
|
}
|
|
};
|
|
|
|
TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with type traits", "[typetraits]") {
|
|
REQUIRE(repo.attach<location>("location"));
|
|
SECTION("Insert and select with direct execution") {
|
|
location loc{1, "center", {1, 2, 3}, Color::Black};
|
|
|
|
auto res = query::insert()
|
|
.into("location", generator::columns<location>(repo))
|
|
.values(loc)
|
|
.execute(db);
|
|
REQUIRE(res.is_ok());
|
|
REQUIRE(*res == 1);
|
|
|
|
auto result = query::select(generator::columns<location>(repo))
|
|
.from("location")
|
|
.fetch_one<location>(db);
|
|
|
|
REQUIRE(result.is_ok());
|
|
REQUIRE((*result)->name == "center");
|
|
REQUIRE((*result)->color == Color::Black);
|
|
REQUIRE((*result)->coord.x == 1);
|
|
REQUIRE((*result)->coord.y == 2);
|
|
REQUIRE((*result)->coord.z == 3);
|
|
}
|
|
|
|
SECTION("Insert and select with prepared statement") {
|
|
location loc{1, "center", {1, 2, 3}, Color::Black};
|
|
|
|
auto stmt = query::insert()
|
|
.into("location", generator::columns<location>(repo))
|
|
.values(generator::placeholders<location>())
|
|
.prepare(db);
|
|
REQUIRE(stmt);
|
|
auto res = stmt->bind(loc)
|
|
.execute();
|
|
REQUIRE(res.is_ok());
|
|
REQUIRE(*res == 1);
|
|
|
|
auto result = query::select(generator::columns<location>(repo))
|
|
.from("location")
|
|
.fetch_one<location>(db);
|
|
|
|
REQUIRE(result.is_ok());
|
|
REQUIRE((*result)->name == "center");
|
|
REQUIRE((*result)->color == Color::Black);
|
|
REQUIRE((*result)->coord.x == 1);
|
|
REQUIRE((*result)->coord.y == 2);
|
|
REQUIRE((*result)->coord.z == 3);
|
|
}
|
|
} |