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

Single query expectation with configurable behavior for database_backend. More...

#include <mock_backend_expectations.h>

Collaboration diagram for database::testing::backend_expectation:
Collaboration graph

Public Member Functions

 backend_expectation ()
 
backend_expectationfor_query (const std::string &query, backend_match_type type=backend_match_type::EXACT)
 
backend_expectationfor_pattern (const std::string &pattern)
 
backend_expectationfor_any ()
 
backend_expectationreturning (const core::database_result &result)
 
backend_expectationreturning_rows_affected (uint64_t count)
 
backend_expectationreturning_error (const std::string &error_message)
 
backend_expectationreturning_execute_success ()
 
backend_expectationtimes (int count)
 
backend_expectationat_least (int count)
 
backend_expectationat_most (int count)
 
backend_expectationonce ()
 
backend_expectationnever ()
 
bool matches (const std::string &query) const
 
bool is_satisfied () const
 
bool can_be_invoked () const
 
kcenon::common::Result< core::database_resultget_select_result ()
 
kcenon::common::Result< uint64_t > get_rows_affected ()
 
kcenon::common::VoidResult get_execute_result ()
 
bool should_error () const
 
std::string get_error_message () const
 

Private Attributes

std::string query_
 
backend_match_type match_type_
 
std::regex pattern_
 
std::optional< core::database_resultresult_
 
std::optional< uint64_t > rows_affected_
 
std::optional< std::string > error_message_
 
bool execute_success_
 
int min_invocations_
 
int max_invocations_
 
int actual_invocations_
 

Detailed Description

Single query expectation with configurable behavior for database_backend.

Unlike the legacy expectation class, this one works with Result<T> types for proper error handling.

Definition at line 37 of file mock_backend_expectations.h.

Constructor & Destructor Documentation

◆ backend_expectation()

Member Function Documentation

◆ at_least()

backend_expectation & database::testing::backend_expectation::at_least ( int count)

Definition at line 65 of file mock_backend_expectations.cpp.

65 {
66 min_invocations_ = count;
67 return *this;
68}

References min_invocations_.

Referenced by database::testing::backend_expectation_builder::any_times().

Here is the caller graph for this function:

◆ at_most()

backend_expectation & database::testing::backend_expectation::at_most ( int count)

Definition at line 70 of file mock_backend_expectations.cpp.

70 {
71 max_invocations_ = count;
72 return *this;
73}

References max_invocations_.

◆ can_be_invoked()

bool database::testing::backend_expectation::can_be_invoked ( ) const

Definition at line 100 of file mock_backend_expectations.cpp.

100 {
102}

References actual_invocations_, and max_invocations_.

◆ for_any()

backend_expectation & database::testing::backend_expectation::for_any ( )

Definition at line 33 of file mock_backend_expectations.cpp.

33 {
35 return *this;
36}

References database::testing::ANY, and match_type_.

Referenced by database::testing::mock_backend::expect_any().

Here is the caller graph for this function:

◆ for_pattern()

backend_expectation & database::testing::backend_expectation::for_pattern ( const std::string & pattern)

Definition at line 29 of file mock_backend_expectations.cpp.

29 {
31}
backend_expectation & for_query(const std::string &query, backend_match_type type=backend_match_type::EXACT)

References for_query(), and database::testing::PATTERN.

Referenced by database::testing::mock_backend::expect_pattern().

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

◆ for_query()

backend_expectation & database::testing::backend_expectation::for_query ( const std::string & query,
backend_match_type type = backend_match_type::EXACT )

Definition at line 20 of file mock_backend_expectations.cpp.

20 {
21 query_ = query;
22 match_type_ = type;
23 if (type == backend_match_type::PATTERN) {
24 pattern_ = std::regex(query);
25 }
26 return *this;
27}

References match_type_, database::testing::PATTERN, pattern_, and query_.

Referenced by database::testing::mock_backend::expect_query(), and for_pattern().

Here is the caller graph for this function:

◆ get_error_message()

std::string database::testing::backend_expectation::get_error_message ( ) const

Definition at line 132 of file mock_backend_expectations.cpp.

132 {
133 return error_message_.value_or("");
134}

References error_message_.

◆ get_execute_result()

kcenon::common::VoidResult database::testing::backend_expectation::get_execute_result ( )

Definition at line 120 of file mock_backend_expectations.cpp.

120 {
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}

References actual_invocations_, and error_message_.

◆ get_rows_affected()

kcenon::common::Result< uint64_t > database::testing::backend_expectation::get_rows_affected ( )

Definition at line 112 of file mock_backend_expectations.cpp.

112 {
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}

References actual_invocations_, error_message_, and rows_affected_.

◆ get_select_result()

kcenon::common::Result< core::database_result > database::testing::backend_expectation::get_select_result ( )

Definition at line 104 of file mock_backend_expectations.cpp.

104 {
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}
std::optional< core::database_result > result_
std::vector< database_row > database_result

References actual_invocations_, error_message_, and result_.

◆ is_satisfied()

bool database::testing::backend_expectation::is_satisfied ( ) const

Definition at line 96 of file mock_backend_expectations.cpp.

96 {
98}

References actual_invocations_, and min_invocations_.

◆ matches()

bool database::testing::backend_expectation::matches ( const std::string & query) const

Definition at line 83 of file mock_backend_expectations.cpp.

83 {
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}

References database::testing::ANY, database::testing::EXACT, match_type_, database::testing::PATTERN, pattern_, and query_.

◆ never()

backend_expectation & database::testing::backend_expectation::never ( )

Definition at line 79 of file mock_backend_expectations.cpp.

79 {
80 return times(0);
81}

References times().

Here is the call graph for this function:

◆ once()

backend_expectation & database::testing::backend_expectation::once ( )

Definition at line 75 of file mock_backend_expectations.cpp.

75 {
76 return times(1);
77}

References times().

Referenced by database::testing::backend_expectation_builder::once().

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

◆ returning()

backend_expectation & database::testing::backend_expectation::returning ( const core::database_result & result)

Definition at line 38 of file mock_backend_expectations.cpp.

38 {
39 result_ = result;
40 return *this;
41}

References result_.

Referenced by database::testing::backend_expectation_builder::will_return().

Here is the caller graph for this function:

◆ returning_error()

backend_expectation & database::testing::backend_expectation::returning_error ( const std::string & error_message)

Definition at line 48 of file mock_backend_expectations.cpp.

48 {
49 error_message_ = error_message;
50 return *this;
51}

References error_message_.

Referenced by database::testing::backend_expectation_builder::will_fail().

Here is the caller graph for this function:

◆ returning_execute_success()

backend_expectation & database::testing::backend_expectation::returning_execute_success ( )

Definition at line 53 of file mock_backend_expectations.cpp.

53 {
54 execute_success_ = true;
55 error_message_.reset();
56 return *this;
57}

References error_message_, and execute_success_.

Referenced by database::testing::backend_expectation_builder::will_succeed().

Here is the caller graph for this function:

◆ returning_rows_affected()

backend_expectation & database::testing::backend_expectation::returning_rows_affected ( uint64_t count)

Definition at line 43 of file mock_backend_expectations.cpp.

43 {
44 rows_affected_ = count;
45 return *this;
46}

References rows_affected_.

Referenced by database::testing::backend_expectation_builder::will_return_rows().

Here is the caller graph for this function:

◆ should_error()

bool database::testing::backend_expectation::should_error ( ) const

Definition at line 128 of file mock_backend_expectations.cpp.

128 {
129 return error_message_.has_value();
130}

References error_message_.

◆ times()

backend_expectation & database::testing::backend_expectation::times ( int count)

Definition at line 59 of file mock_backend_expectations.cpp.

59 {
60 min_invocations_ = count;
61 max_invocations_ = count;
62 return *this;
63}

References max_invocations_, and min_invocations_.

Referenced by never(), once(), and database::testing::backend_expectation_builder::times().

Here is the caller graph for this function:

Member Data Documentation

◆ actual_invocations_

int database::testing::backend_expectation::actual_invocations_
private

◆ error_message_

std::optional<std::string> database::testing::backend_expectation::error_message_
private

◆ execute_success_

bool database::testing::backend_expectation::execute_success_
private

Definition at line 81 of file mock_backend_expectations.h.

Referenced by returning_execute_success().

◆ match_type_

backend_match_type database::testing::backend_expectation::match_type_
private

Definition at line 75 of file mock_backend_expectations.h.

Referenced by for_any(), for_query(), and matches().

◆ max_invocations_

int database::testing::backend_expectation::max_invocations_
private

Definition at line 84 of file mock_backend_expectations.h.

Referenced by at_most(), can_be_invoked(), and times().

◆ min_invocations_

int database::testing::backend_expectation::min_invocations_
private

Definition at line 83 of file mock_backend_expectations.h.

Referenced by at_least(), is_satisfied(), and times().

◆ pattern_

std::regex database::testing::backend_expectation::pattern_
private

Definition at line 76 of file mock_backend_expectations.h.

Referenced by for_query(), and matches().

◆ query_

std::string database::testing::backend_expectation::query_
private

Definition at line 74 of file mock_backend_expectations.h.

Referenced by for_query(), and matches().

◆ result_

std::optional<core::database_result> database::testing::backend_expectation::result_
private

Definition at line 78 of file mock_backend_expectations.h.

Referenced by get_select_result(), and returning().

◆ rows_affected_

std::optional<uint64_t> database::testing::backend_expectation::rows_affected_
private

Definition at line 79 of file mock_backend_expectations.h.

Referenced by get_rows_affected(), and returning_rows_affected().


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