Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
value_formatter_test.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025
3// All rights reserved.
4
10#include <gtest/gtest.h>
12
13using namespace database;
14using namespace database::query;
15
16class ValueFormatterTest : public ::testing::Test {
17protected:
18 void SetUp() override {}
19 void TearDown() override {}
20};
21
22TEST_F(ValueFormatterTest, PostgreSQLStringEscaping) {
23 value_formatter fmt(database_types::postgres);
24
25 // Test single quote escaping
26 std::string input = "O'Brien";
27 std::string escaped = fmt.escape_string(input);
28 EXPECT_EQ(escaped, "O''Brien");
29
30 // Test backslash escaping
31 input = "path\\to\\file";
32 escaped = fmt.escape_string(input);
33 EXPECT_TRUE(escaped.find("\\\\") != std::string::npos);
34}
35
36TEST_F(ValueFormatterTest, SQLiteStringEscaping) {
37 value_formatter fmt(database_types::sqlite);
38
39 // Test single quote escaping
40 std::string input = "O'Brien";
41 std::string escaped = fmt.escape_string(input);
42 EXPECT_EQ(escaped, "O''Brien");
43}
44
45TEST_F(ValueFormatterTest, IdentifierQuoting) {
46 value_formatter pg_fmt(database_types::postgres);
47 EXPECT_EQ(pg_fmt.escape_identifier("table"), "\"table\"");
48
49 value_formatter sqlite_fmt(database_types::sqlite);
50 EXPECT_EQ(sqlite_fmt.escape_identifier("table"), "\"table\"");
51}
52
53TEST_F(ValueFormatterTest, BooleanLiterals) {
54 value_formatter pg_fmt(database_types::postgres);
55 EXPECT_EQ(pg_fmt.bool_literal(true), "TRUE");
56 EXPECT_EQ(pg_fmt.bool_literal(false), "FALSE");
57
58}
59
61 value_formatter fmt(database_types::postgres);
62 EXPECT_EQ(fmt.null_literal(), "NULL");
63}
64
65TEST_F(ValueFormatterTest, FormatString) {
66 value_formatter fmt(database_types::postgres);
67 std::string value = "test";
68 std::string formatted = fmt.format(value);
69 EXPECT_TRUE(formatted.find("'") != std::string::npos);
70}
71
72TEST_F(ValueFormatterTest, FormatInteger) {
73 value_formatter fmt(database_types::postgres);
74 int value = 42;
75 std::string formatted = fmt.format(value);
76 EXPECT_EQ(formatted, "42");
77}
78
79TEST_F(ValueFormatterTest, FormatDouble) {
80 value_formatter fmt(database_types::postgres);
81 double value = 3.14159;
82 std::string formatted = fmt.format(value);
83 EXPECT_TRUE(formatted.find("3.14") != std::string::npos);
84}
85
86TEST_F(ValueFormatterTest, FormatBoolean) {
87 value_formatter fmt(database_types::postgres);
88 bool value = true;
89 std::string formatted = fmt.format(value);
90 EXPECT_EQ(formatted, "TRUE");
91}
Formats database values for different backends.
std::string escape_identifier(const std::string &identifier) const
Quote and escape an identifier (table/column name)
std::string escape_string(const std::string &str) const
Escape a string value.
std::string format(const core::database_value &value) const
Format a database value.
std::string bool_literal(bool val) const
Get boolean literal.
std::string null_literal() const
Get NULL literal for this database.
TEST_F(ValueFormatterTest, PostgreSQLStringEscaping)