|
Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
|
Configurable mock for database_backend interface. More...
#include <mock_database.h>


Public Member Functions | |
| mock_database () | |
| ~mock_database () override=default | |
| mock_database (const mock_database &)=delete | |
| mock_database & | operator= (const mock_database &)=delete |
| mock_database (mock_database &&other) noexcept | |
| mock_database & | operator= (mock_database &&other) noexcept |
| database_types | type () const override |
| Get the database type of this backend. | |
| kcenon::common::VoidResult | initialize (const core::connection_config &config) override |
| Initialize the database backend. | |
| kcenon::common::VoidResult | shutdown () override |
| Shutdown the database backend gracefully. | |
| bool | is_initialized () const override |
| Check if backend is initialized and ready. | |
| kcenon::common::Result< core::database_result > | select_query (const std::string &query_string) override |
| Execute a SELECT query. | |
| kcenon::common::VoidResult | execute_query (const std::string &query_string) override |
| Execute a general SQL query (DDL, DML) | |
| kcenon::common::VoidResult | begin_transaction () override |
| Begin a transaction. | |
| kcenon::common::VoidResult | commit_transaction () override |
| Commit the current transaction. | |
| kcenon::common::VoidResult | rollback_transaction () override |
| Rollback the current transaction. | |
| bool | in_transaction () const override |
| Check if backend is currently in a transaction. | |
| std::string | last_error () const override |
| Get last error message from backend. | |
| std::map< std::string, std::string > | connection_info () const override |
| Get backend-specific connection information. | |
| mock_database & | set_database_type (database_types type) |
| mock_database & | set_connect_result (bool result) |
| mock_database & | set_default_select_result (const core::database_result &result) |
| mock_database & | set_default_rows_affected (uint64_t rows) |
| expectation_builder | expect_query (const std::string &query) |
| expectation_builder | expect_pattern (const std::string ®ex_pattern) |
| expectation_builder | expect_any () |
| mock_database & | simulate_connection_failure () |
| mock_database & | simulate_disconnect () |
| bool | verify_all_expectations () const |
| std::vector< std::string > | get_executed_queries () const |
| size_t | get_query_count () const |
| size_t | get_query_count (const std::string &pattern) const |
| void | reset () |
| void | clear_expectations () |
| void | clear_history () |
| bool | is_connected () const |
| std::string | get_connection_string () const |
Public Member Functions inherited from database::core::database_backend | |
| virtual | ~database_backend ()=default |
| virtual kcenon::common::Result< database_result > | select_prepared (const std::string &query, const std::vector< database_value > ¶ms) |
| Execute a parameterized SELECT query (prepared statement) | |
| virtual kcenon::common::VoidResult | execute_prepared (const std::string &query, const std::vector< database_value > ¶ms) |
| Execute a parameterized DML/DDL query (prepared statement) | |
Private Member Functions | |
| void | record_query (const std::string &query) |
| expectation * | find_expectation (const std::string &query) |
Private Attributes | |
| database_types | db_type_ |
| bool | initialized_ |
| bool | connect_result_ |
| bool | in_transaction_ |
| std::string | connection_string_ |
| core::database_result | default_result_ |
| uint64_t | default_rows_affected_ |
| std::string | last_error_ |
| std::vector< expectation > | expectations_ |
| std::vector< std::string > | executed_queries_ |
| std::mutex | mutex_ |
Friends | |
| class | expectation_builder |
Additional Inherited Members | |
Static Protected Member Functions inherited from database::core::database_backend | |
| static std::string | expand_params (const std::string &query, const std::vector< database_value > ¶ms) |
| Expand positional parameters into a SQL string (fallback) | |
Configurable mock for database_backend interface.
Features:
Example usage:
Definition at line 41 of file mock_database.h.
| database::testing::mock_database::mock_database | ( | ) |
Definition at line 11 of file mock_database.cpp.
|
overridedefault |
|
delete |
|
noexcept |
Definition at line 20 of file mock_database.cpp.
|
overridevirtual |
Begin a transaction.
Implements database::core::database_backend.
Definition at line 111 of file mock_database.cpp.
References in_transaction_, and mutex_.
| void database::testing::mock_database::clear_expectations | ( | ) |
Definition at line 235 of file mock_database.cpp.
References expectations_, and mutex_.
| void database::testing::mock_database::clear_history | ( | ) |
Definition at line 240 of file mock_database.cpp.
References executed_queries_, and mutex_.
|
overridevirtual |
Commit the current transaction.
Implements database::core::database_backend.
Definition at line 120 of file mock_database.cpp.
References in_transaction_, and mutex_.
|
overridevirtual |
Get backend-specific connection information.
Example keys: "server_version", "connection_id", "protocol_version"
Implements database::core::database_backend.
Definition at line 146 of file mock_database.cpp.
References connection_string_, in_transaction_, and initialized_.
|
overridevirtual |
Execute a general SQL query (DDL, DML)
| query_string | SQL statement |
Implements database::core::database_backend.
Definition at line 94 of file mock_database.cpp.
References find_expectation(), last_error_, mutex_, and record_query().

| expectation_builder database::testing::mock_database::expect_any | ( | ) |
Definition at line 186 of file mock_database.cpp.
References expectation_builder, and database::testing::expectation::for_any().
Referenced by database::testing::mock_database_builder::failing_database().


| expectation_builder database::testing::mock_database::expect_pattern | ( | const std::string & | regex_pattern | ) |
Definition at line 180 of file mock_database.cpp.
References expectation_builder, and database::testing::expectation::for_pattern().
Referenced by database::testing::mock_database_builder::with_data().


| expectation_builder database::testing::mock_database::expect_query | ( | const std::string & | query | ) |
Definition at line 174 of file mock_database.cpp.
References database::testing::EXACT, expectation_builder, and database::testing::expectation::for_query().

|
private |
Definition at line 257 of file mock_database.cpp.
References expectations_.
Referenced by execute_query(), and select_query().

| std::string database::testing::mock_database::get_connection_string | ( | ) | const |
Definition at line 249 of file mock_database.cpp.
References connection_string_.
| std::vector< std::string > database::testing::mock_database::get_executed_queries | ( | ) | const |
Definition at line 208 of file mock_database.cpp.
References executed_queries_, and mutex_.
| size_t database::testing::mock_database::get_query_count | ( | ) | const |
Definition at line 213 of file mock_database.cpp.
References executed_queries_, and mutex_.
| size_t database::testing::mock_database::get_query_count | ( | const std::string & | pattern | ) | const |
Definition at line 218 of file mock_database.cpp.
References executed_queries_, and mutex_.
|
overridevirtual |
Check if backend is currently in a transaction.
Implements database::core::database_backend.
Definition at line 138 of file mock_database.cpp.
References in_transaction_.
|
overridevirtual |
Initialize the database backend.
| config | Connection configuration |
This method should:
Implements database::core::database_backend.
Definition at line 58 of file mock_database.cpp.
References connect_result_, connection_string_, database::core::connection_config::database, database::core::connection_config::host, initialized_, last_error_, mutex_, and database::core::connection_config::port.
Referenced by database::testing::scoped_test_database::scoped_test_database().

| bool database::testing::mock_database::is_connected | ( | ) | const |
Definition at line 245 of file mock_database.cpp.
References initialized_.
|
overridevirtual |
Check if backend is initialized and ready.
Implements database::core::database_backend.
Definition at line 76 of file mock_database.cpp.
References initialized_.
|
overridevirtual |
Get last error message from backend.
Implements database::core::database_backend.
Definition at line 142 of file mock_database.cpp.
References last_error_.
|
delete |
|
noexcept |
Definition at line 36 of file mock_database.cpp.
|
private |
Definition at line 253 of file mock_database.cpp.
References executed_queries_.
Referenced by execute_query(), and select_query().

| void database::testing::mock_database::reset | ( | ) |
Definition at line 225 of file mock_database.cpp.
References connection_string_, executed_queries_, expectations_, in_transaction_, initialized_, last_error_, and mutex_.
Referenced by database::testing::scoped_test_database::~scoped_test_database().

|
overridevirtual |
Rollback the current transaction.
Implements database::core::database_backend.
Definition at line 129 of file mock_database.cpp.
References in_transaction_, and mutex_.
|
overridevirtual |
Execute a SELECT query.
| query_string | SQL SELECT statement |
Implements database::core::database_backend.
Definition at line 80 of file mock_database.cpp.
References default_result_, find_expectation(), last_error_, mutex_, and record_query().

| mock_database & database::testing::mock_database::set_connect_result | ( | bool | result | ) |
Definition at line 159 of file mock_database.cpp.
References connect_result_.
| mock_database & database::testing::mock_database::set_database_type | ( | database_types | type | ) |
Definition at line 154 of file mock_database.cpp.
References db_type_, and type().

| mock_database & database::testing::mock_database::set_default_rows_affected | ( | uint64_t | rows | ) |
Definition at line 169 of file mock_database.cpp.
References default_rows_affected_.
| mock_database & database::testing::mock_database::set_default_select_result | ( | const core::database_result & | result | ) |
Definition at line 164 of file mock_database.cpp.
References default_result_.
|
overridevirtual |
Shutdown the database backend gracefully.
This method should:
Implements database::core::database_backend.
Definition at line 69 of file mock_database.cpp.
References in_transaction_, initialized_, and mutex_.
Referenced by database::testing::scoped_test_database::~scoped_test_database().

| mock_database & database::testing::mock_database::simulate_connection_failure | ( | ) |
Definition at line 192 of file mock_database.cpp.
References connect_result_.
| mock_database & database::testing::mock_database::simulate_disconnect | ( | ) |
Definition at line 197 of file mock_database.cpp.
References initialized_.
|
overridevirtual |
Get the database type of this backend.
Implements database::core::database_backend.
Definition at line 54 of file mock_database.cpp.
References db_type_.
Referenced by set_database_type().

| bool database::testing::mock_database::verify_all_expectations | ( | ) | const |
Definition at line 202 of file mock_database.cpp.
References expectations_, and mutex_.
|
friend |
Definition at line 97 of file mock_database.h.
Referenced by expect_any(), expect_pattern(), and expect_query().
|
private |
Definition at line 101 of file mock_database.h.
Referenced by initialize(), set_connect_result(), and simulate_connection_failure().
|
private |
Definition at line 103 of file mock_database.h.
Referenced by connection_info(), get_connection_string(), initialize(), and reset().
|
private |
Definition at line 99 of file mock_database.h.
Referenced by set_database_type(), and type().
|
private |
Definition at line 104 of file mock_database.h.
Referenced by select_query(), and set_default_select_result().
|
private |
Definition at line 105 of file mock_database.h.
Referenced by set_default_rows_affected().
|
private |
Definition at line 109 of file mock_database.h.
Referenced by clear_history(), get_executed_queries(), get_query_count(), get_query_count(), record_query(), and reset().
|
private |
Definition at line 108 of file mock_database.h.
Referenced by clear_expectations(), find_expectation(), reset(), verify_all_expectations(), database::testing::expectation_builder::will_fail(), database::testing::expectation_builder::will_return(), database::testing::expectation_builder::will_return_rows(), and database::testing::expectation_builder::will_succeed().
|
private |
Definition at line 102 of file mock_database.h.
Referenced by begin_transaction(), commit_transaction(), connection_info(), in_transaction(), reset(), rollback_transaction(), and shutdown().
|
private |
Definition at line 100 of file mock_database.h.
Referenced by connection_info(), initialize(), is_connected(), is_initialized(), reset(), shutdown(), and simulate_disconnect().
|
private |
Definition at line 106 of file mock_database.h.
Referenced by execute_query(), initialize(), last_error(), reset(), and select_query().
|
mutableprivate |
Definition at line 110 of file mock_database.h.
Referenced by begin_transaction(), clear_expectations(), clear_history(), commit_transaction(), execute_query(), get_executed_queries(), get_query_count(), get_query_count(), initialize(), reset(), rollback_transaction(), select_query(), shutdown(), and verify_all_expectations().