Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
mock_expectations.h
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
5#pragma once
6
8#include <cstdint>
9#include <string>
10#include <functional>
11#include <regex>
12#include <optional>
13#include <stdexcept>
14
15namespace database::testing {
16
17// Forward declaration
18class mock_database;
19
24enum class match_type {
25 EXACT,
26 PATTERN,
27 ANY
28};
29
35public:
37
38 // Query matching
39 expectation& for_query(const std::string& query, match_type type = match_type::EXACT);
40 expectation& for_pattern(const std::string& pattern);
42
43 // Response configuration
45 expectation& returning_rows_affected(uint64_t count);
46 expectation& throwing(const std::string& error_message);
48
49 // Invocation limits
50 expectation& times(int count);
51 expectation& at_least(int count);
52 expectation& at_most(int count);
55
56 // Matching and invocation
57 bool matches(const std::string& query) const;
58 bool is_satisfied() const;
59 bool can_be_invoked() const;
60
61 // Get results
63 uint64_t get_rows_affected();
64 bool get_execute_result();
65
66 // Check if should throw
67 bool should_throw() const;
68 std::string get_error_message() const;
69
70private:
71 std::string query_;
73 std::regex pattern_;
74
75 std::optional<core::database_result> result_;
76 std::optional<uint64_t> rows_affected_;
77 std::optional<bool> execute_result_;
78 std::optional<std::string> error_message_;
79
83};
84
90public:
92
93 // Response configuration
96 expectation_builder& will_fail(const std::string& error_message);
98
99 // Invocation limits
100 expectation_builder& times(int count);
103
104private:
107};
108
113class database_exception : public std::runtime_error {
114public:
115 explicit database_exception(const std::string& message)
116 : std::runtime_error(message) {}
117};
118
119} // namespace database::testing
Exception thrown by mock when simulating errors.
database_exception(const std::string &message)
Fluent builder for expectations.
expectation_builder & will_return(const core::database_result &result)
expectation_builder & times(int count)
expectation_builder & will_fail(const std::string &error_message)
expectation_builder(mock_database *db, expectation exp)
expectation_builder & will_return_rows(uint64_t count)
Single query expectation with configurable behavior.
expectation & throwing(const std::string &error_message)
expectation & returning(const core::database_result &result)
std::optional< uint64_t > rows_affected_
core::database_result get_result()
expectation & returning_rows_affected(uint64_t count)
expectation & at_least(int count)
expectation & at_most(int count)
std::optional< std::string > error_message_
bool matches(const std::string &query) const
expectation & for_pattern(const std::string &pattern)
expectation & returning_execute_result(bool result)
expectation & for_query(const std::string &query, match_type type=match_type::EXACT)
std::optional< bool > execute_result_
std::optional< core::database_result > result_
expectation & times(int count)
Configurable mock for database_backend interface.
Abstract interface for database backends.
std::vector< database_row > database_result
match_type
How to match query strings.
@ EXACT
Exact string match.