added assignment from value, conversion to database_type and appropriate tests
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "matador/utils/identifier.hpp"
|
||||
#include "matador/utils/default_type_traits.hpp"
|
||||
#include "matador/utils/value.hpp"
|
||||
|
||||
using namespace matador::utils;
|
||||
|
||||
@@ -279,3 +280,87 @@ TEST_CASE("identifier as() and convert() behave consistently for same integer ty
|
||||
REQUIRE(conv_u64.is_ok());
|
||||
REQUIRE(*conv_u64 == 77);
|
||||
}
|
||||
|
||||
TEST_CASE("identifier assign from value", "[utils][identifier][assign]") {
|
||||
identifier id;
|
||||
|
||||
value v1{int32_t{17}};
|
||||
const auto r1 = id.assign(v1);
|
||||
REQUIRE(r1.is_ok());
|
||||
REQUIRE(id.is_integer());
|
||||
REQUIRE(id.str() == "17");
|
||||
|
||||
value v2{std::string{"abc"}};
|
||||
const auto r2 = id.assign(v2);
|
||||
REQUIRE(r2.is_ok());
|
||||
REQUIRE(id.is_varchar());
|
||||
REQUIRE(id.str() == "abc");
|
||||
|
||||
value v3{basic_type::Double, 0};
|
||||
const auto r3 = id.assign(v3);
|
||||
REQUIRE(r3.is_error());
|
||||
}
|
||||
|
||||
TEST_CASE("identifier assign from value with exact type mapping", "[utils][identifier][assign]") {
|
||||
identifier id;
|
||||
|
||||
SECTION("integer types") {
|
||||
value v{int16_t{12}};
|
||||
const auto r = id.assign(v);
|
||||
REQUIRE(r.is_ok());
|
||||
REQUIRE(id.is_integer());
|
||||
REQUIRE(id.str() == "12");
|
||||
}
|
||||
|
||||
SECTION("string types") {
|
||||
value v{std::string{"abc"}};
|
||||
const auto r = id.assign(v);
|
||||
REQUIRE(r.is_ok());
|
||||
REQUIRE(id.is_varchar());
|
||||
REQUIRE(id.str() == "abc");
|
||||
}
|
||||
|
||||
SECTION("null") {
|
||||
value v{basic_type::Null, 0};
|
||||
const auto r = id.assign(v);
|
||||
REQUIRE(r.is_ok());
|
||||
REQUIRE(id.is_null());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("identifier::to_database_type converts internal value to database_type", "[identifier]") {
|
||||
SECTION("null identifier becomes nullptr") {
|
||||
identifier id{nullptr};
|
||||
|
||||
const auto db_value = id.to_database_type();
|
||||
|
||||
REQUIRE(std::holds_alternative<std::nullptr_t>(db_value));
|
||||
}
|
||||
|
||||
SECTION("signed integral identifier becomes same signed database type") {
|
||||
identifier id{int32_t{42}};
|
||||
|
||||
const auto db_value = id.to_database_type();
|
||||
|
||||
REQUIRE(std::holds_alternative<int32_t>(db_value));
|
||||
REQUIRE(std::get<int32_t>(db_value) == 42);
|
||||
}
|
||||
|
||||
SECTION("unsigned integral identifier becomes same unsigned database type") {
|
||||
identifier id{uint64_t{123456789ULL}};
|
||||
|
||||
const auto db_value = id.to_database_type();
|
||||
|
||||
REQUIRE(std::holds_alternative<uint64_t>(db_value));
|
||||
REQUIRE(std::get<uint64_t>(db_value) == 123456789ULL);
|
||||
}
|
||||
|
||||
SECTION("string identifier becomes std::string database type") {
|
||||
identifier id{std::string{"customer_1"}};
|
||||
|
||||
const auto db_value = id.to_database_type();
|
||||
|
||||
REQUIRE(std::holds_alternative<std::string>(db_value));
|
||||
REQUIRE(std::get<std::string>(db_value) == "customer_1");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user