Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
test_configuration.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
17
18#include <cassert>
19#include <chrono>
20#include <iostream>
21#include <string>
22
23using namespace database::integrated;
24
29{
30 std::cout << "Testing default configuration values...\n";
31
32 unified_db_config config;
33
34 // Database defaults
35 assert(config.database.type == backend_type::postgres);
36 assert(config.database.connection_string == "host=localhost port=5432 dbname=postgres");
37 assert(config.database.enable_ssl == false);
38 assert(config.database.enable_prepared_statements == true);
39
40 // Connection pool defaults
41 assert(config.connection_pool.pool_name == "default_pool");
42 assert(config.connection_pool.min_connections == 2);
43 assert(config.connection_pool.max_connections == 10);
44 assert(config.connection_pool.connection_timeout == std::chrono::seconds(30));
45 assert(config.connection_pool.idle_timeout == std::chrono::seconds(300));
46 assert(config.connection_pool.enable_health_checks == true);
47 assert(config.connection_pool.enable_priority_queue == false);
48
49 // Thread pool defaults
50 assert(config.thread.pool_name == "db_thread_pool");
51 assert(config.thread.thread_count == 0); // Auto-detect
52 assert(config.thread.max_queue_size == 1000);
53 assert(config.thread.enable_priority_scheduling == false);
54 assert(config.thread.pool_type == thread_pool_type::standard);
55
56 // Logger defaults
57 assert(config.logger.enable_query_logging == false);
58 assert(config.logger.enable_connection_logging == true);
59 assert(config.logger.log_slow_queries == true);
60 assert(config.logger.slow_query_threshold == std::chrono::milliseconds(1000));
61 assert(config.logger.min_log_level == db_log_level::info);
62 assert(config.logger.enable_file_logging == false);
63
64 // Monitoring defaults
65 assert(config.monitoring.enable_metrics == true);
66 assert(config.monitoring.enable_profiling == false);
67 assert(config.monitoring.enable_health_checks == true);
69 assert(config.monitoring.enable_prometheus_export == false);
70 assert(config.monitoring.prometheus_port == 9090);
71
72 // Integration flags
73 assert(config.enable_common_system_integration == true);
74 assert(config.enable_thread_system_integration == true);
75 assert(config.enable_monitoring_system_integration == true);
76
77 std::cout << " ✓ All default values correct\n";
78}
79
84{
85 std::cout << "Testing builder pattern...\n";
86
87 // Test method chaining
88 auto config = unified_db_config{}
89 .set_backend(backend_type::postgres, "host=192.168.1.100 dbname=myapp")
90 .set_credentials("admin", "secret123")
91 .set_pool_size(5, 25)
92 .set_pool_name("myapp_pool")
93 .set_log_level(db_log_level::debug)
95 .enable_slow_query_logging(true, std::chrono::milliseconds(500))
96 .enable_file_logging(true, "/var/log/myapp")
98 .enable_prometheus(true, 9091, "/db_metrics")
101 .enable_ssl(true, "/etc/ssl/cert.pem", "/etc/ssl/key.pem")
102 .set_timeouts(std::chrono::seconds(60), std::chrono::seconds(600));
103
104 // Verify builder results
105 assert(config.database.type == backend_type::postgres);
106 assert(config.database.connection_string == "host=192.168.1.100 dbname=myapp");
107 assert(config.database.username == "admin");
108 assert(config.database.password == "secret123");
109 assert(config.database.enable_ssl == true);
110 assert(config.database.ssl_cert_path == "/etc/ssl/cert.pem");
111 assert(config.database.ssl_key_path == "/etc/ssl/key.pem");
112
113 assert(config.connection_pool.min_connections == 5);
114 assert(config.connection_pool.max_connections == 25);
115 assert(config.connection_pool.pool_name == "myapp_pool");
116 assert(config.connection_pool.connection_timeout == std::chrono::seconds(60));
117 assert(config.connection_pool.idle_timeout == std::chrono::seconds(600));
118 assert(config.connection_pool.enable_priority_queue == true);
119
120 assert(config.thread.thread_count == 8);
121 assert(config.thread.enable_priority_scheduling == true);
122 assert(config.thread.pool_type == thread_pool_type::typed);
123
124 assert(config.logger.min_log_level == db_log_level::debug);
125 assert(config.logger.enable_query_logging == true);
126 assert(config.logger.log_slow_queries == true);
127 assert(config.logger.slow_query_threshold == std::chrono::milliseconds(500));
128 assert(config.logger.enable_file_logging == true);
129 assert(config.logger.log_directory == "/var/log/myapp");
130
131 assert(config.monitoring.enable_metrics == true);
132 assert(config.monitoring.enable_health_checks == true);
133 assert(config.monitoring.enable_prometheus_export == true);
134 assert(config.monitoring.prometheus_port == 9091);
135 assert(config.monitoring.prometheus_endpoint == "/db_metrics");
136
137 std::cout << " ✓ Builder pattern works correctly\n";
138 std::cout << " ✓ Method chaining works correctly\n";
139}
140
145{
146 std::cout << "Testing enum types...\n";
147
148 // Test log levels
149 db_log_level levels[] = {
150 db_log_level::trace, db_log_level::debug, db_log_level::info, db_log_level::warning,
151 db_log_level::error, db_log_level::critical, db_log_level::fatal,
152 };
153
154 // Ensure all enum values are unique
155 for (size_t i = 0; i < 7; ++i)
156 {
157 for (size_t j = i + 1; j < 7; ++j)
158 {
159 assert(levels[i] != levels[j]);
160 }
161 }
162
163 // Test backend types
164 backend_type backends[] = {
165 backend_type::postgres,
166 backend_type::sqlite,
167 backend_type::mongodb,
168 backend_type::redis,
169 };
170
171 for (size_t i = 0; i < 4; ++i)
172 {
173 for (size_t j = i + 1; j < 4; ++j)
174 {
175 assert(backends[i] != backends[j]);
176 }
177 }
178
179 // Test thread pool types
180 assert(thread_pool_type::standard != thread_pool_type::typed);
181
182 std::cout << " ✓ All enum types are distinct\n";
183}
184
189{
190 std::cout << "Testing struct copy/move semantics...\n";
191
192 // Create original config
193 auto original = unified_db_config{}.set_backend(backend_type::postgres, "test_db").set_pool_size(3, 15);
194
195 // Test copy constructor
196 unified_db_config copied = original;
197 assert(copied.database.type == backend_type::postgres);
198 assert(copied.database.connection_string == "test_db");
199 assert(copied.connection_pool.min_connections == 3);
200 assert(copied.connection_pool.max_connections == 15);
201
202 // Test copy assignment
203 unified_db_config assigned;
204 assigned = original;
205 assert(assigned.database.type == backend_type::postgres);
206 assert(assigned.database.connection_string == "test_db");
207
208 // Test move constructor
209 unified_db_config moved = std::move(original);
210 assert(moved.database.type == backend_type::postgres);
211 assert(moved.database.connection_string == "test_db");
212
213 // Test move assignment
214 unified_db_config move_assigned;
215 move_assigned = std::move(copied);
216 assert(move_assigned.database.type == backend_type::postgres);
217
218 std::cout << " ✓ Copy semantics work correctly\n";
219 std::cout << " ✓ Move semantics work correctly\n";
220}
221
226{
227 std::cout << "Testing zero-config usage...\n";
228
229 // Should be able to create config with zero configuration
230 unified_db_config config;
231
232 // Verify it has sensible defaults
233 assert(config.database.type == backend_type::postgres);
234 assert(!config.database.connection_string.empty());
235 assert(config.connection_pool.min_connections > 0);
237 assert(config.monitoring.enable_metrics == true);
238 assert(config.logger.enable_connection_logging == true);
239
240 std::cout << " ✓ Zero-config creates usable configuration\n";
241}
242
246int main()
247{
248 std::cout << "=== Running Integrated Database Configuration Tests ===\n\n";
249
250 try
251 {
253 std::cout << std::endl;
254
256 std::cout << std::endl;
257
259 std::cout << std::endl;
260
262 std::cout << std::endl;
263
265 std::cout << std::endl;
266
267 std::cout << "=== All tests passed! ✓ ===\n";
268 return 0;
269 }
270 catch (const std::exception& e)
271 {
272 std::cerr << "Test failed with exception: " << e.what() << "\n";
273 return 1;
274 }
275 catch (...)
276 {
277 std::cerr << "Test failed with unknown exception\n";
278 return 1;
279 }
280}
Unified configuration for integrated database system.
db_log_level
Database logging level enumeration.
backend_type
Database backend type enumeration.
bool enable_prepared_statements
Enable prepared statement caching.
bool enable_ssl
Enable SSL/TLS for database connections.
std::string connection_string
Connection string (format depends on backend)
backend_type type
Database backend type.
bool enable_query_logging
Log all SQL queries executed.
bool enable_file_logging
Enable logging to file (in addition to console)
std::chrono::milliseconds slow_query_threshold
Threshold for considering a query "slow".
bool enable_connection_logging
Log connection pool events (acquire, release, etc.)
bool log_slow_queries
Automatically detect and log slow queries.
db_log_level min_log_level
Minimum log level to output.
bool enable_prometheus_export
Enable Prometheus metrics export.
std::uint16_t prometheus_port
Port for Prometheus metrics server.
bool enable_profiling
Enable performance profiling.
bool enable_metrics
Enable metrics collection.
bool enable_health_checks
Enable health check endpoints.
double connection_usage_warning_threshold
Warn when connection pool usage exceeds this percentage (0.0-1.0)
thread_pool_type pool_type
Thread pool implementation type.
std::string pool_name
Name for this thread pool (for logging and monitoring)
bool enable_priority_scheduling
Enable priority-based task scheduling.
std::size_t max_queue_size
Maximum queued tasks (0 = unlimited)
std::size_t thread_count
Number of worker threads (0 = auto-detect from hardware)
std::string pool_name
Unique name for this pool (for logging and monitoring)
std::size_t min_connections
Minimum number of connections to maintain.
std::chrono::seconds connection_timeout
Timeout for acquiring a connection from the pool.
std::chrono::seconds idle_timeout
Time before an idle connection is closed.
bool enable_health_checks
Enable periodic health checks on connections.
std::size_t max_connections
Maximum number of connections allowed.
bool enable_priority_queue
Enable priority-based connection acquisition.
unified_db_config & enable_slow_query_logging(bool enable=true, std::chrono::milliseconds threshold=std::chrono::milliseconds(1000))
Configure slow query detection.
unified_db_config & set_timeouts(std::chrono::seconds acquisition, std::chrono::seconds idle)
Set connection timeouts.
unified_db_config & set_pool_size(std::size_t min, std::size_t max)
Configure connection pool size.
unified_db_config & set_backend(backend_type type, const std::string &connection_str)
Set database backend type and connection string.
db_logger_config logger
Logger configuration.
unified_db_config & set_credentials(const std::string &user, const std::string &pass)
Set database credentials.
unified_db_config & enable_ssl(bool enable=true, const std::string &cert_path="", const std::string &key_path="")
Enable SSL/TLS for database connections.
unified_db_config & enable_monitoring(bool enable=true)
Enable/disable monitoring.
unified_db_config & set_thread_count(std::size_t count)
Set thread pool size.
unified_db_config & enable_prometheus(bool enable=true, std::uint16_t port=9090, const std::string &endpoint="/metrics")
Configure Prometheus metrics export.
pool_config connection_pool
Connection pool configuration.
unified_db_config & set_pool_name(const std::string &name)
Set connection pool name.
unified_db_config & set_log_level(db_log_level level)
Set minimum logging level.
unified_db_config & enable_priority_scheduling(bool enable=true)
Enable priority-based scheduling.
bool enable_thread_system_integration
Enable thread_system integration (typed thread pools)
db_monitoring_config monitoring
Monitoring configuration.
bool enable_monitoring_system_integration
Enable monitoring_system integration.
unified_db_config & enable_file_logging(bool enable=true, const std::string &directory="./logs")
Enable file logging.
unified_db_config & enable_query_logging(bool enable=true)
Enable/disable query logging.
bool enable_common_system_integration
Enable common_system integration (Result pattern, ILogger, LOG_* macros)
db_thread_config thread
Thread pool configuration.
database_config database
Database connection configuration.
void test_enum_types()
Test enum types.
void test_zero_config()
Test zero-config scenario.
void test_struct_semantics()
Test struct copy and move semantics.
void test_builder_pattern()
Test builder pattern methods.
int main()
Main test runner.
void test_default_values()
Test default configuration values.