Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
mock_backend_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
6#include "mock_backend.h"
7
8namespace database::testing {
9
10// backend_expectation implementation
12 : match_type_(backend_match_type::ANY)
13 , execute_success_(true)
14 , min_invocations_(0)
15 , max_invocations_(std::numeric_limits<int>::max())
16 , actual_invocations_(0)
17{
18}
19
21 query_ = query;
22 match_type_ = type;
23 if (type == backend_match_type::PATTERN) {
24 pattern_ = std::regex(query);
25 }
26 return *this;
27}
28
31}
32
37
39 result_ = result;
40 return *this;
41}
42
44 rows_affected_ = count;
45 return *this;
46}
47
49 error_message_ = error_message;
50 return *this;
51}
52
58
60 min_invocations_ = count;
61 max_invocations_ = count;
62 return *this;
63}
64
66 min_invocations_ = count;
67 return *this;
68}
69
71 max_invocations_ = count;
72 return *this;
73}
74
78
82
83bool backend_expectation::matches(const std::string& query) const {
84 switch (match_type_) {
86 return query == query_;
88 return std::regex_search(query, pattern_);
90 return true;
91 default:
92 return false;
93 }
94}
95
99
103
104kcenon::common::Result<core::database_result> backend_expectation::get_select_result() {
106 if (error_message_) {
107 return kcenon::common::Result<core::database_result>(kcenon::common::error_info{-1, *error_message_});
108 }
109 return kcenon::common::Result<core::database_result>::ok(result_.value_or(core::database_result{}));
110}
111
112kcenon::common::Result<uint64_t> backend_expectation::get_rows_affected() {
114 if (error_message_) {
115 return kcenon::common::Result<uint64_t>(kcenon::common::error_info{-1, *error_message_});
116 }
117 return kcenon::common::Result<uint64_t>::ok(rows_affected_.value_or(0));
118}
119
120kcenon::common::VoidResult backend_expectation::get_execute_result() {
122 if (error_message_) {
123 return kcenon::common::VoidResult(kcenon::common::error_info{-1, *error_message_});
124 }
125 return kcenon::common::VoidResult(std::monostate{});
126}
127
129 return error_message_.has_value();
130}
131
133 return error_message_.value_or("");
134}
135
136// backend_expectation_builder implementation
141
143 exp_.returning(result);
144 db_->expectations_.push_back(exp_);
145 pushed_ = true;
146 return *this;
147}
148
151 db_->expectations_.push_back(exp_);
152 pushed_ = true;
153 return *this;
154}
155
157 exp_.returning_error(error_message);
158 db_->expectations_.push_back(exp_);
159 pushed_ = true;
160 return *this;
161}
162
169
171 exp_.times(count);
172 if (pushed_) {
173 db_->expectations_.back().times(count);
174 }
175 return *this;
176}
177
179 exp_.once();
180 if (pushed_) {
181 db_->expectations_.back().once();
182 }
183 return *this;
184}
185
187 exp_.at_least(0);
188 if (pushed_) {
189 db_->expectations_.back().at_least(0);
190 }
191 return *this;
192}
193
194} // namespace database::testing
Fluent builder for backend expectations.
backend_expectation_builder & will_fail(const std::string &error_message)
backend_expectation_builder & will_return_rows(uint64_t count)
backend_expectation_builder & will_return(const core::database_result &result)
backend_expectation_builder(mock_backend *db, backend_expectation exp)
Single query expectation with configurable behavior for database_backend.
kcenon::common::Result< uint64_t > get_rows_affected()
backend_expectation & for_query(const std::string &query, backend_match_type type=backend_match_type::EXACT)
backend_expectation & returning(const core::database_result &result)
std::optional< core::database_result > result_
kcenon::common::Result< core::database_result > get_select_result()
backend_expectation & for_pattern(const std::string &pattern)
backend_expectation & returning_rows_affected(uint64_t count)
bool matches(const std::string &query) const
backend_expectation & returning_error(const std::string &error_message)
Configurable mock for database_backend interface.
std::vector< backend_expectation > expectations_
std::vector< database_row > database_result
backend_match_type
How to match query strings.