Added tests for schemata (#76)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-10-24 08:14:34 +02:00
committed by GitHub
parent 91e993ffdd
commit e6b468af9b
5 changed files with 85 additions and 0 deletions

View File

@@ -833,6 +833,9 @@ std::string table_or_query_to_sql(
return _table_or_query.visit([](const auto& _t) -> std::string {
using Type = std::remove_cvref_t<decltype(_t)>;
if constexpr (std::is_same_v<Type, dynamic::Table>) {
if (_t.schema) {
return wrap_in_quotes(*_t.schema) + "." + wrap_in_quotes(_t.name);
}
return wrap_in_quotes(_t.name);
} else {
return "(" + select_from_to_sql(*_t) + ")";

View File

@@ -716,6 +716,9 @@ std::string table_or_query_to_sql(
return _table_or_query.visit([](const auto& _t) -> std::string {
using Type = std::remove_cvref_t<decltype(_t)>;
if constexpr (std::is_same_v<Type, dynamic::Table>) {
if (_t.schema) {
return wrap_in_quotes(*_t.schema) + "." + wrap_in_quotes(_t.name);
}
return wrap_in_quotes(_t.name);
} else {
return "(" + select_from_to_sql(*_t) + ")";

View File

@@ -0,0 +1,26 @@
#include <gtest/gtest.h>
#include <sqlgen.hpp>
#include <sqlgen/mysql.hpp>
namespace test_to_select_from_with_schema_dry {
struct TestTable {
constexpr static const char* tablename = "test_table";
constexpr static const char* schema = "my_schema";
std::string field1;
int32_t field2;
sqlgen::PrimaryKey<uint32_t> id;
std::optional<std::string> nullable;
};
TEST(mysql, test_to_select_from_with_schema_dry) {
const auto query = sqlgen::read<std::vector<TestTable>>;
const auto expected =
R"(SELECT `field1`, `field2`, `id`, `nullable` FROM `my_schema`.`test_table`)";
EXPECT_EQ(sqlgen::mysql::to_sql(query), expected);
}
} // namespace test_to_select_from_with_schema_dry

View File

@@ -0,0 +1,26 @@
#include <gtest/gtest.h>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
namespace test_to_select_from_with_schema_dry {
struct TestTable {
constexpr static const char* tablename = "test_table";
constexpr static const char* schema = "my_schema";
std::string field1;
int32_t field2;
sqlgen::PrimaryKey<uint32_t> id;
std::optional<std::string> nullable;
};
TEST(postgres, test_to_select_from_with_schema_dry) {
const auto query = sqlgen::read<std::vector<TestTable>>;
const auto expected =
R"(SELECT "field1", "field2", "id", "nullable" FROM "my_schema"."test_table")";
EXPECT_EQ(sqlgen::postgres::to_sql(query), expected);
}
} // namespace test_to_select_from_with_schema_dry

View File

@@ -0,0 +1,27 @@
#include <gtest/gtest.h>
#include <sqlgen.hpp>
#include <sqlgen/sqlite.hpp>
namespace test_to_select_from_with_schema {
struct TestTable {
constexpr static const char* tablename = "test_table";
constexpr static const char* schema = "my_schema";
std::string field1;
int32_t field2;
sqlgen::PrimaryKey<uint32_t> id;
std::optional<std::string> nullable;
};
TEST(sqlite, test_to_select_from_with_schema) {
const auto select_from_stmt =
sqlgen::transpilation::read_to_select_from<TestTable>();
const auto conn = sqlgen::sqlite::connect().value();
const auto expected =
R"(SELECT "field1", "field2", "id", "nullable" FROM "my_schema"."test_table")";
EXPECT_EQ(conn->to_sql(select_from_stmt), expected);
}
} // namespace test_to_select_from_with_schema