Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
mock_expectations.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
5#include "mock_expectations.h"
6#include "mock_database.h"
7#include <cstdint>
8
9namespace database::testing {
10
11// expectation implementation
13 : match_type_(match_type::ANY)
14 , min_invocations_(0)
15 , max_invocations_(std::numeric_limits<int>::max())
16 , actual_invocations_(0)
17{
18}
19
20expectation& expectation::for_query(const std::string& query, match_type type) {
21 query_ = query;
22 match_type_ = type;
23 if (type == match_type::PATTERN) {
24 pattern_ = std::regex(query);
25 }
26 return *this;
27}
28
29expectation& expectation::for_pattern(const std::string& pattern) {
30 return for_query(pattern, match_type::PATTERN);
31}
32
37
39 result_ = result;
40 return *this;
41}
42
44 rows_affected_ = count;
45 return *this;
46}
47
48expectation& expectation::throwing(const std::string& error_message) {
49 error_message_ = error_message;
50 return *this;
51}
52
54 execute_result_ = result;
55 return *this;
56}
57
59 min_invocations_ = count;
60 max_invocations_ = count;
61 return *this;
62}
63
65 min_invocations_ = count;
66 return *this;
67}
68
70 max_invocations_ = count;
71 return *this;
72}
73
75 return times(1);
76}
77
79 return times(0);
80}
81
82bool expectation::matches(const std::string& query) const {
83 switch (match_type_) {
85 return query == query_;
87 return std::regex_search(query, pattern_);
88 case match_type::ANY:
89 return true;
90 default:
91 return false;
92 }
93}
94
98
102
110
113 if (error_message_) {
115 }
116 return rows_affected_.value_or(0);
117}
118
121 if (error_message_) {
123 }
124 return execute_result_.value_or(true);
125}
126
128 return error_message_.has_value();
129}
130
132 return error_message_.value_or("");
133}
134
135// expectation_builder implementation
137 : db_(db), exp_(std::move(exp))
138{
139}
140
142 exp_.returning(result);
143 db_->expectations_.push_back(exp_);
144 return *this;
145}
146
149 db_->expectations_.push_back(exp_);
150 return *this;
151}
152
153expectation_builder& expectation_builder::will_fail(const std::string& error_message) {
154 exp_.throwing(error_message);
155 db_->expectations_.push_back(exp_);
156 return *this;
157}
158
164
166 exp_.times(count);
167 return *this;
168}
169
171 exp_.once();
172 return *this;
173}
174
179
180} // namespace database::testing
Exception thrown by mock when simulating errors.
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.
std::vector< expectation > expectations_
std::vector< database_row > database_result
match_type
How to match query strings.
@ PATTERN
Regex pattern match.
@ EXACT
Exact string match.