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

Single query expectation with configurable behavior. More...

#include <mock_expectations.h>

Collaboration diagram for database::testing::expectation:
Collaboration graph

Public Member Functions

 expectation ()
 
expectationfor_query (const std::string &query, match_type type=match_type::EXACT)
 
expectationfor_pattern (const std::string &pattern)
 
expectationfor_any ()
 
expectationreturning (const core::database_result &result)
 
expectationreturning_rows_affected (uint64_t count)
 
expectationthrowing (const std::string &error_message)
 
expectationreturning_execute_result (bool result)
 
expectationtimes (int count)
 
expectationat_least (int count)
 
expectationat_most (int count)
 
expectationonce ()
 
expectationnever ()
 
bool matches (const std::string &query) const
 
bool is_satisfied () const
 
bool can_be_invoked () const
 
core::database_result get_result ()
 
uint64_t get_rows_affected ()
 
bool get_execute_result ()
 
bool should_throw () const
 
std::string get_error_message () const
 

Private Attributes

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

Detailed Description

Single query expectation with configurable behavior.

Definition at line 34 of file mock_expectations.h.

Constructor & Destructor Documentation

◆ expectation()

database::testing::expectation::expectation ( )

Member Function Documentation

◆ at_least()

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

Definition at line 64 of file mock_expectations.cpp.

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

References min_invocations_.

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

Here is the caller graph for this function:

◆ at_most()

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

Definition at line 69 of file mock_expectations.cpp.

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

References max_invocations_.

◆ can_be_invoked()

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

Definition at line 99 of file mock_expectations.cpp.

99 {
101}

References actual_invocations_, and max_invocations_.

◆ for_any()

expectation & database::testing::expectation::for_any ( )

Definition at line 33 of file mock_expectations.cpp.

33 {
35 return *this;
36}

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

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

Here is the caller graph for this function:

◆ for_pattern()

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

Definition at line 29 of file mock_expectations.cpp.

29 {
30 return for_query(pattern, match_type::PATTERN);
31}
expectation & for_query(const std::string &query, match_type type=match_type::EXACT)
@ PATTERN
Regex pattern match.

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

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

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

◆ for_query()

expectation & database::testing::expectation::for_query ( const std::string & query,
match_type type = match_type::EXACT )

Definition at line 20 of file mock_expectations.cpp.

20 {
21 query_ = query;
22 match_type_ = type;
23 if (type == 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_database::expect_query(), and for_pattern().

Here is the caller graph for this function:

◆ get_error_message()

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

Definition at line 131 of file mock_expectations.cpp.

131 {
132 return error_message_.value_or("");
133}
std::optional< std::string > error_message_

References error_message_.

◆ get_execute_result()

bool database::testing::expectation::get_execute_result ( )

Definition at line 119 of file mock_expectations.cpp.

119 {
121 if (error_message_) {
122 throw database_exception(*error_message_);
123 }
124 return execute_result_.value_or(true);
125}
std::optional< bool > execute_result_

References actual_invocations_, error_message_, and execute_result_.

◆ get_result()

core::database_result database::testing::expectation::get_result ( )

Definition at line 103 of file mock_expectations.cpp.

103 {
105 if (error_message_) {
106 throw database_exception(*error_message_);
107 }
108 return result_.value_or(core::database_result{});
109}
std::optional< core::database_result > result_
std::vector< database_row > database_result

References actual_invocations_, error_message_, and result_.

◆ get_rows_affected()

uint64_t database::testing::expectation::get_rows_affected ( )

Definition at line 111 of file mock_expectations.cpp.

111 {
113 if (error_message_) {
114 throw database_exception(*error_message_);
115 }
116 return rows_affected_.value_or(0);
117}
std::optional< uint64_t > rows_affected_

References actual_invocations_, error_message_, and rows_affected_.

◆ is_satisfied()

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

Definition at line 95 of file mock_expectations.cpp.

95 {
97}

References actual_invocations_, and min_invocations_.

◆ matches()

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

Definition at line 82 of file mock_expectations.cpp.

82 {
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}
@ EXACT
Exact string match.

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

◆ never()

expectation & database::testing::expectation::never ( )

Definition at line 78 of file mock_expectations.cpp.

78 {
79 return times(0);
80}
expectation & times(int count)

References times().

Here is the call graph for this function:

◆ once()

expectation & database::testing::expectation::once ( )

Definition at line 74 of file mock_expectations.cpp.

74 {
75 return times(1);
76}

References times().

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

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

◆ returning()

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

Definition at line 38 of file mock_expectations.cpp.

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

References result_.

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

Here is the caller graph for this function:

◆ returning_execute_result()

expectation & database::testing::expectation::returning_execute_result ( bool result)

Definition at line 53 of file mock_expectations.cpp.

53 {
54 execute_result_ = result;
55 return *this;
56}

References execute_result_.

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

Here is the caller graph for this function:

◆ returning_rows_affected()

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

Definition at line 43 of file mock_expectations.cpp.

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

References rows_affected_.

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

Here is the caller graph for this function:

◆ should_throw()

bool database::testing::expectation::should_throw ( ) const

Definition at line 127 of file mock_expectations.cpp.

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

References error_message_.

◆ throwing()

expectation & database::testing::expectation::throwing ( const std::string & error_message)

Definition at line 48 of file mock_expectations.cpp.

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

References error_message_.

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

Here is the caller graph for this function:

◆ times()

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

Definition at line 58 of file mock_expectations.cpp.

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

References max_invocations_, and min_invocations_.

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

Here is the caller graph for this function:

Member Data Documentation

◆ actual_invocations_

int database::testing::expectation::actual_invocations_
private

◆ error_message_

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

◆ execute_result_

std::optional<bool> database::testing::expectation::execute_result_
private

Definition at line 77 of file mock_expectations.h.

Referenced by get_execute_result(), and returning_execute_result().

◆ match_type_

match_type database::testing::expectation::match_type_
private

Definition at line 72 of file mock_expectations.h.

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

◆ max_invocations_

int database::testing::expectation::max_invocations_
private

Definition at line 81 of file mock_expectations.h.

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

◆ min_invocations_

int database::testing::expectation::min_invocations_
private

Definition at line 80 of file mock_expectations.h.

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

◆ pattern_

std::regex database::testing::expectation::pattern_
private

Definition at line 73 of file mock_expectations.h.

Referenced by for_query(), and matches().

◆ query_

std::string database::testing::expectation::query_
private

Definition at line 71 of file mock_expectations.h.

Referenced by for_query(), and matches().

◆ result_

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

Definition at line 75 of file mock_expectations.h.

Referenced by get_result(), and returning().

◆ rows_affected_

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

Definition at line 76 of file mock_expectations.h.

Referenced by get_rows_affected(), and returning_rows_affected().


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