Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::testing::mock_backend Class Reference

Configurable mock for database_backend interface. More...

#include <mock_backend.h>

Inheritance diagram for database::testing::mock_backend:
Inheritance graph
Collaboration diagram for database::testing::mock_backend:
Collaboration graph

Public Member Functions

 mock_backend ()
 
 ~mock_backend () override=default
 
 mock_backend (const mock_backend &)=delete
 
mock_backendoperator= (const mock_backend &)=delete
 
 mock_backend (mock_backend &&other) noexcept
 
mock_backendoperator= (mock_backend &&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_resultselect_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_backendset_database_type (database_types type)
 
mock_backendset_initialize_result (bool result, const std::string &error="")
 
mock_backendset_default_select_result (const core::database_result &result)
 
mock_backendset_default_rows_affected (uint64_t rows)
 
backend_expectation_builder expect_query (const std::string &query)
 
backend_expectation_builder expect_pattern (const std::string &regex_pattern)
 
backend_expectation_builder expect_any ()
 
mock_backendsimulate_initialization_failure (const std::string &error="Mock initialization failed")
 
mock_backendsimulate_shutdown ()
 
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 ()
 
std::string get_connection_string () const
 
- Public Member Functions inherited from database::core::database_backend
virtual ~database_backend ()=default
 
virtual kcenon::common::Result< database_resultselect_prepared (const std::string &query, const std::vector< database_value > &params)
 Execute a parameterized SELECT query (prepared statement)
 
virtual kcenon::common::VoidResult execute_prepared (const std::string &query, const std::vector< database_value > &params)
 Execute a parameterized DML/DDL query (prepared statement)
 

Private Member Functions

void record_query (const std::string &query)
 
backend_expectationfind_expectation (const std::string &query)
 

Private Attributes

database_types db_type_
 
bool initialized_
 
bool init_result_
 
std::string init_error_
 
std::string connection_string_
 
core::database_result default_result_
 
uint64_t default_rows_affected_
 
std::string last_error_
 
bool in_transaction_
 
std::vector< backend_expectationexpectations_
 
std::vector< std::string > executed_queries_
 
std::mutex mutex_
 

Friends

class backend_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 > &params)
 Expand positional parameters into a SQL string (fallback)
 

Detailed Description

Configurable mock for database_backend interface.

This is the modern mock class that implements database_backend and returns Result<T> types for proper error handling.

Features:

  • Set expected queries and results with Result<T> types
  • Simulate errors with proper error messages
  • Record all executed queries for verification
  • Pattern matching for flexible expectations
  • Transaction state tracking

Example usage:

// Set up expectation
db.expect_query("SELECT * FROM users")
{{"id", int64_t(1)}, {"name", std::string("John")}}
});
// Execute query
auto result = db.select_query("SELECT * FROM users");
EXPECT_TRUE(result.is_ok());
// Verify
EXPECT_TRUE(db.verify_all_expectations());
backend_expectation_builder & will_return(const core::database_result &result)
Configurable mock for database_backend interface.
kcenon::common::Result< core::database_result > select_query(const std::string &query_string) override
Execute a SELECT query.
backend_expectation_builder expect_query(const std::string &query)
std::vector< database_row > database_result

Definition at line 48 of file mock_backend.h.

Constructor & Destructor Documentation

◆ mock_backend() [1/3]

database::testing::mock_backend::mock_backend ( )

◆ ~mock_backend()

database::testing::mock_backend::~mock_backend ( )
overridedefault

◆ mock_backend() [2/3]

database::testing::mock_backend::mock_backend ( const mock_backend & )
delete

◆ mock_backend() [3/3]

database::testing::mock_backend::mock_backend ( mock_backend && other)
noexcept

Definition at line 20 of file mock_backend.cpp.

21 : db_type_(other.db_type_)
22 , initialized_(other.initialized_)
23 , init_result_(other.init_result_)
24 , init_error_(std::move(other.init_error_))
25 , connection_string_(std::move(other.connection_string_))
26 , default_result_(std::move(other.default_result_))
27 , default_rows_affected_(other.default_rows_affected_)
28 , last_error_(std::move(other.last_error_))
29 , in_transaction_(other.in_transaction_)
30 , expectations_(std::move(other.expectations_))
31 , executed_queries_(std::move(other.executed_queries_))
32{
33 other.initialized_ = false;
34 other.in_transaction_ = false;
35}
std::vector< backend_expectation > expectations_
std::vector< std::string > executed_queries_
core::database_result default_result_

Member Function Documentation

◆ begin_transaction()

kcenon::common::VoidResult database::testing::mock_backend::begin_transaction ( )
overridevirtual

Begin a transaction.

Returns
VoidResult::ok() on success, error on failure

Implements database::core::database_backend.

Definition at line 102 of file mock_backend.cpp.

102 {
103 std::lock_guard<std::mutex> lock(mutex_);
104 if (in_transaction_) {
105 last_error_ = "Transaction already in progress";
106 return kcenon::common::VoidResult(kcenon::common::error_info{-1, last_error_});
107 }
108 in_transaction_ = true;
109 return kcenon::common::VoidResult(std::monostate{});
110}

References in_transaction_, last_error_, and mutex_.

◆ clear_expectations()

void database::testing::mock_backend::clear_expectations ( )

Definition at line 231 of file mock_backend.cpp.

231 {
232 std::lock_guard<std::mutex> lock(mutex_);
233 expectations_.clear();
234}

References expectations_, and mutex_.

◆ clear_history()

void database::testing::mock_backend::clear_history ( )

Definition at line 236 of file mock_backend.cpp.

236 {
237 std::lock_guard<std::mutex> lock(mutex_);
238 executed_queries_.clear();
239}

References executed_queries_, and mutex_.

◆ commit_transaction()

kcenon::common::VoidResult database::testing::mock_backend::commit_transaction ( )
overridevirtual

Commit the current transaction.

Returns
VoidResult::ok() on success, error on failure

Implements database::core::database_backend.

Definition at line 112 of file mock_backend.cpp.

112 {
113 std::lock_guard<std::mutex> lock(mutex_);
114 if (!in_transaction_) {
115 last_error_ = "No transaction in progress";
116 return kcenon::common::VoidResult(kcenon::common::error_info{-1, last_error_});
117 }
118 in_transaction_ = false;
119 return kcenon::common::VoidResult(std::monostate{});
120}

References in_transaction_, last_error_, and mutex_.

◆ connection_info()

std::map< std::string, std::string > database::testing::mock_backend::connection_info ( ) const
overridevirtual

Get backend-specific connection information.

Returns
Map of connection properties (for debugging/monitoring)

Example keys: "server_version", "connection_id", "protocol_version"

Implements database::core::database_backend.

Definition at line 140 of file mock_backend.cpp.

140 {
141 return {
142 {"type", "mock"},
143 {"connection_string", connection_string_},
144 {"initialized", initialized_ ? "true" : "false"}
145 };
146}

References connection_string_, and initialized_.

◆ execute_query()

kcenon::common::VoidResult database::testing::mock_backend::execute_query ( const std::string & query_string)
overridevirtual

Execute a general SQL query (DDL, DML)

Parameters
query_stringSQL statement
Returns
VoidResult::ok() on success, error on failure

Implements database::core::database_backend.

Definition at line 92 of file mock_backend.cpp.

92 {
93 std::lock_guard<std::mutex> lock(mutex_);
94 record_query(query_string);
95
96 if (auto* exp = find_expectation(query_string)) {
97 return exp->get_execute_result();
98 }
99 return kcenon::common::VoidResult(std::monostate{});
100}
backend_expectation * find_expectation(const std::string &query)
void record_query(const std::string &query)

References find_expectation(), mutex_, and record_query().

Here is the call graph for this function:

◆ expect_any()

backend_expectation_builder database::testing::mock_backend::expect_any ( )

Definition at line 181 of file mock_backend.cpp.

181 {
182 backend_expectation exp;
183 exp.for_any();
184 return backend_expectation_builder(this, std::move(exp));
185}

References backend_expectation_builder, and database::testing::backend_expectation::for_any().

Referenced by database::testing::mock_backend_builder::failing_database().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expect_pattern()

backend_expectation_builder database::testing::mock_backend::expect_pattern ( const std::string & regex_pattern)

Definition at line 175 of file mock_backend.cpp.

175 {
176 backend_expectation exp;
177 exp.for_pattern(regex_pattern);
178 return backend_expectation_builder(this, std::move(exp));
179}

References backend_expectation_builder, and database::testing::backend_expectation::for_pattern().

Referenced by database::testing::mock_backend_builder::with_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expect_query()

backend_expectation_builder database::testing::mock_backend::expect_query ( const std::string & query)

Definition at line 169 of file mock_backend.cpp.

169 {
170 backend_expectation exp;
171 exp.for_query(query, backend_match_type::EXACT);
172 return backend_expectation_builder(this, std::move(exp));
173}

References backend_expectation_builder, database::testing::EXACT, and database::testing::backend_expectation::for_query().

Here is the call graph for this function:

◆ find_expectation()

backend_expectation * database::testing::mock_backend::find_expectation ( const std::string & query)
private

Definition at line 249 of file mock_backend.cpp.

249 {
250 for (auto& exp : expectations_) {
251 if (exp.matches(query) && exp.can_be_invoked()) {
252 return &exp;
253 }
254 }
255 return nullptr;
256}

References expectations_.

Referenced by execute_query(), and select_query().

Here is the caller graph for this function:

◆ get_connection_string()

std::string database::testing::mock_backend::get_connection_string ( ) const

Definition at line 241 of file mock_backend.cpp.

241 {
242 return connection_string_;
243}

References connection_string_.

◆ get_executed_queries()

std::vector< std::string > database::testing::mock_backend::get_executed_queries ( ) const

Definition at line 204 of file mock_backend.cpp.

204 {
205 std::lock_guard<std::mutex> lock(mutex_);
206 return executed_queries_;
207}

References executed_queries_, and mutex_.

◆ get_query_count() [1/2]

size_t database::testing::mock_backend::get_query_count ( ) const

Definition at line 209 of file mock_backend.cpp.

209 {
210 std::lock_guard<std::mutex> lock(mutex_);
211 return executed_queries_.size();
212}

References executed_queries_, and mutex_.

Referenced by TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ get_query_count() [2/2]

size_t database::testing::mock_backend::get_query_count ( const std::string & pattern) const

Definition at line 214 of file mock_backend.cpp.

214 {
215 std::lock_guard<std::mutex> lock(mutex_);
216 std::regex re(pattern);
217 return std::count_if(executed_queries_.begin(), executed_queries_.end(),
218 [&re](const std::string& q) { return std::regex_search(q, re); });
219}

References executed_queries_, and mutex_.

◆ in_transaction()

bool database::testing::mock_backend::in_transaction ( ) const
overridevirtual

Check if backend is currently in a transaction.

Returns
true if transaction is active

Implements database::core::database_backend.

Definition at line 132 of file mock_backend.cpp.

132 {
133 return in_transaction_;
134}

References in_transaction_.

◆ initialize()

kcenon::common::VoidResult database::testing::mock_backend::initialize ( const core::connection_config & config)
overridevirtual

Initialize the database backend.

Parameters
configConnection configuration
Returns
VoidResult::ok() on success, error on failure

This method should:

  • Establish database connection
  • Validate connection parameters
  • Set up connection pooling if applicable
  • Initialize backend-specific resources

Implements database::core::database_backend.

Definition at line 60 of file mock_backend.cpp.

60 {
61 std::lock_guard<std::mutex> lock(mutex_);
62 connection_string_ = config.host + ":" + std::to_string(config.port) + "/" + config.database;
63 if (init_result_) {
64 initialized_ = true;
65 return kcenon::common::VoidResult(std::monostate{});
66 }
67 last_error_ = init_error_.empty() ? "Mock initialization failed" : init_error_;
68 return kcenon::common::VoidResult(kcenon::common::error_info{-1, last_error_});
69}

References connection_string_, database::core::connection_config::database, database::core::connection_config::host, init_error_, init_result_, initialized_, last_error_, mutex_, and database::core::connection_config::port.

Referenced by TEST_F().

Here is the caller graph for this function:

◆ is_initialized()

bool database::testing::mock_backend::is_initialized ( ) const
overridevirtual

Check if backend is initialized and ready.

Returns
true if initialized and can accept queries

Implements database::core::database_backend.

Definition at line 78 of file mock_backend.cpp.

78 {
79 return initialized_;
80}

References initialized_.

Referenced by TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ last_error()

std::string database::testing::mock_backend::last_error ( ) const
overridevirtual

Get last error message from backend.

Returns
Error message, or empty string if no error

Implements database::core::database_backend.

Definition at line 136 of file mock_backend.cpp.

136 {
137 return last_error_;
138}

References last_error_.

◆ operator=() [1/2]

mock_backend & database::testing::mock_backend::operator= ( const mock_backend & )
delete

◆ operator=() [2/2]

mock_backend & database::testing::mock_backend::operator= ( mock_backend && other)
noexcept

Definition at line 37 of file mock_backend.cpp.

37 {
38 if (this != &other) {
39 db_type_ = other.db_type_;
40 initialized_ = other.initialized_;
41 init_result_ = other.init_result_;
42 init_error_ = std::move(other.init_error_);
43 connection_string_ = std::move(other.connection_string_);
44 default_result_ = std::move(other.default_result_);
45 default_rows_affected_ = other.default_rows_affected_;
46 last_error_ = std::move(other.last_error_);
47 in_transaction_ = other.in_transaction_;
48 expectations_ = std::move(other.expectations_);
49 executed_queries_ = std::move(other.executed_queries_);
50 other.initialized_ = false;
51 other.in_transaction_ = false;
52 }
53 return *this;
54}

◆ record_query()

void database::testing::mock_backend::record_query ( const std::string & query)
private

Definition at line 245 of file mock_backend.cpp.

245 {
246 executed_queries_.push_back(query);
247}

References executed_queries_.

Referenced by execute_query(), and select_query().

Here is the caller graph for this function:

◆ reset()

void database::testing::mock_backend::reset ( )

Definition at line 221 of file mock_backend.cpp.

221 {
222 std::lock_guard<std::mutex> lock(mutex_);
223 expectations_.clear();
224 executed_queries_.clear();
225 initialized_ = false;
226 in_transaction_ = false;
227 connection_string_.clear();
228 last_error_.clear();
229}

References connection_string_, executed_queries_, expectations_, in_transaction_, initialized_, last_error_, and mutex_.

◆ rollback_transaction()

kcenon::common::VoidResult database::testing::mock_backend::rollback_transaction ( )
overridevirtual

Rollback the current transaction.

Returns
VoidResult::ok() on success, error on failure

Implements database::core::database_backend.

Definition at line 122 of file mock_backend.cpp.

122 {
123 std::lock_guard<std::mutex> lock(mutex_);
124 if (!in_transaction_) {
125 last_error_ = "No transaction in progress";
126 return kcenon::common::VoidResult(kcenon::common::error_info{-1, last_error_});
127 }
128 in_transaction_ = false;
129 return kcenon::common::VoidResult(std::monostate{});
130}

References in_transaction_, last_error_, and mutex_.

◆ select_query()

kcenon::common::Result< core::database_result > database::testing::mock_backend::select_query ( const std::string & query_string)
overridevirtual

Execute a SELECT query.

Parameters
query_stringSQL SELECT statement
Returns
Query results as rows, or error

Implements database::core::database_backend.

Definition at line 82 of file mock_backend.cpp.

82 {
83 std::lock_guard<std::mutex> lock(mutex_);
84 record_query(query_string);
85
86 if (auto* exp = find_expectation(query_string)) {
87 return exp->get_select_result();
88 }
89 return kcenon::common::Result<core::database_result>::ok(default_result_);
90}

References default_result_, find_expectation(), mutex_, and record_query().

Here is the call graph for this function:

◆ set_database_type()

mock_backend & database::testing::mock_backend::set_database_type ( database_types type)

Definition at line 148 of file mock_backend.cpp.

148 {
149 db_type_ = type;
150 return *this;
151}
database_types type() const override
Get the database type of this backend.

References db_type_, and type().

Referenced by BackendContractTest::SetUp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_default_rows_affected()

mock_backend & database::testing::mock_backend::set_default_rows_affected ( uint64_t rows)

Definition at line 164 of file mock_backend.cpp.

164 {
166 return *this;
167}

References default_rows_affected_.

◆ set_default_select_result()

mock_backend & database::testing::mock_backend::set_default_select_result ( const core::database_result & result)

Definition at line 159 of file mock_backend.cpp.

159 {
160 default_result_ = result;
161 return *this;
162}

References default_result_.

◆ set_initialize_result()

mock_backend & database::testing::mock_backend::set_initialize_result ( bool result,
const std::string & error = "" )

Definition at line 153 of file mock_backend.cpp.

153 {
154 init_result_ = result;
156 return *this;
157}
VoidResult error(const std::string &msg, int code=-1)

References init_error_, and init_result_.

◆ shutdown()

kcenon::common::VoidResult database::testing::mock_backend::shutdown ( )
overridevirtual

Shutdown the database backend gracefully.

Returns
VoidResult::ok() on success, error on failure

This method should:

  • Close active connections
  • Release connection pool resources
  • Flush pending transactions
  • Clean up backend-specific resources

Implements database::core::database_backend.

Definition at line 71 of file mock_backend.cpp.

71 {
72 std::lock_guard<std::mutex> lock(mutex_);
73 initialized_ = false;
74 in_transaction_ = false;
75 return kcenon::common::VoidResult(std::monostate{});
76}

References in_transaction_, initialized_, and mutex_.

◆ simulate_initialization_failure()

mock_backend & database::testing::mock_backend::simulate_initialization_failure ( const std::string & error = "Mock initialization failed")

Definition at line 187 of file mock_backend.cpp.

187 {
188 init_result_ = false;
190 return *this;
191}

References init_error_, and init_result_.

◆ simulate_shutdown()

mock_backend & database::testing::mock_backend::simulate_shutdown ( )

Definition at line 193 of file mock_backend.cpp.

193 {
194 initialized_ = false;
195 return *this;
196}

References initialized_.

◆ type()

database_types database::testing::mock_backend::type ( ) const
overridevirtual

Get the database type of this backend.

Returns
Database type identifier

Implements database::core::database_backend.

Definition at line 56 of file mock_backend.cpp.

56 {
57 return db_type_;
58}

References db_type_.

Referenced by set_database_type(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ verify_all_expectations()

bool database::testing::mock_backend::verify_all_expectations ( ) const

Definition at line 198 of file mock_backend.cpp.

198 {
199 std::lock_guard<std::mutex> lock(mutex_);
200 return std::all_of(expectations_.begin(), expectations_.end(),
201 [](const backend_expectation& exp) { return exp.is_satisfied(); });
202}

References expectations_, and mutex_.

Friends And Related Symbol Documentation

◆ backend_expectation_builder

friend class backend_expectation_builder
friend

Definition at line 106 of file mock_backend.h.

Referenced by expect_any(), expect_pattern(), and expect_query().

Member Data Documentation

◆ connection_string_

std::string database::testing::mock_backend::connection_string_
private

Definition at line 112 of file mock_backend.h.

Referenced by connection_info(), get_connection_string(), initialize(), and reset().

◆ db_type_

database_types database::testing::mock_backend::db_type_
private

Definition at line 108 of file mock_backend.h.

Referenced by set_database_type(), and type().

◆ default_result_

core::database_result database::testing::mock_backend::default_result_
private

Definition at line 113 of file mock_backend.h.

Referenced by select_query(), and set_default_select_result().

◆ default_rows_affected_

uint64_t database::testing::mock_backend::default_rows_affected_
private

Definition at line 114 of file mock_backend.h.

Referenced by set_default_rows_affected().

◆ executed_queries_

std::vector<std::string> database::testing::mock_backend::executed_queries_
private

◆ expectations_

◆ in_transaction_

bool database::testing::mock_backend::in_transaction_
private

◆ init_error_

std::string database::testing::mock_backend::init_error_
private

◆ init_result_

bool database::testing::mock_backend::init_result_
private

◆ initialized_

bool database::testing::mock_backend::initialized_
private

◆ last_error_

std::string database::testing::mock_backend::last_error_
private

◆ mutex_


The documentation for this class was generated from the following files: