Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
mock_backend_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 <kcenon/common/patterns/result.h>
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_backend;
19
25 EXACT,
26 PATTERN,
27 ANY
28};
29
38public:
40
41 // Query matching
43 backend_expectation& for_pattern(const std::string& pattern);
45
46 // Response configuration with Result types
49 backend_expectation& returning_error(const std::string& error_message);
51
52 // Invocation limits
53 backend_expectation& times(int count);
54 backend_expectation& at_least(int count);
55 backend_expectation& at_most(int count);
58
59 // Matching and invocation
60 bool matches(const std::string& query) const;
61 bool is_satisfied() const;
62 bool can_be_invoked() const;
63
64 // Get results as Result<T> types
65 kcenon::common::Result<core::database_result> get_select_result();
66 kcenon::common::Result<uint64_t> get_rows_affected();
67 kcenon::common::VoidResult get_execute_result();
68
69 // Check if should return error
70 bool should_error() const;
71 std::string get_error_message() const;
72
73private:
74 std::string query_;
76 std::regex pattern_;
77
78 std::optional<core::database_result> result_;
79 std::optional<uint64_t> rows_affected_;
80 std::optional<std::string> error_message_;
82
86};
87
112
117class backend_exception : public std::runtime_error {
118public:
119 explicit backend_exception(const std::string& message)
120 : std::runtime_error(message) {}
121};
122
123} // namespace database::testing
Exception thrown by mock when simulating errors.
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.
Abstract interface for database backends.
std::vector< database_row > database_result
backend_match_type
How to match query strings.