Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
mock_spanner_connection.h
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_MOCKS_MOCK_SPANNER_CONNECTION_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_MOCKS_MOCK_SPANNER_CONNECTION_H
17 
18 #include "google/cloud/spanner/connection.h"
19 #include "google/cloud/spanner/query_partition.h"
20 #include "google/cloud/spanner/read_partition.h"
21 #include "google/cloud/spanner/row.h"
22 #include "google/cloud/spanner/version.h"
23 #include <gmock/gmock.h>
24 #include <vector>
25 
26 namespace google {
27 namespace cloud {
28 /// Define classes to mock the Cloud Spanner C++ client APIs.
29 namespace spanner_mocks {
30 /// An inlined versioned namespace to avoid dependency diamonds.
31 inline namespace SPANNER_CLIENT_NS {
32 
33 /**
34  * A class to mock `google::cloud::spanner::Connection`.
35  *
36  * Application developers may want to test their code with simulated responses,
37  * including errors from a `spanner::Client`. To do so, construct a
38  * `spanner::Client` with an instance of this class. Then use the Google Test
39  * framework functions to program the behavior of this mock.
40  *
41  * @see @ref spanner-mocking for an example using this class.
42  */
44  public:
45  MOCK_METHOD(spanner::RowStream, Read, (ReadParams), (override));
46  MOCK_METHOD(StatusOr<std::vector<spanner::ReadPartition>>, PartitionRead,
48  MOCK_METHOD(spanner::RowStream, ExecuteQuery, (SqlParams), (override));
49  MOCK_METHOD(StatusOr<spanner::DmlResult>, ExecuteDml, (SqlParams),
50  (override));
51  MOCK_METHOD(spanner::ProfileQueryResult, ProfileQuery, (SqlParams),
52  (override));
53  MOCK_METHOD(StatusOr<spanner::ProfileDmlResult>, ProfileDml, (SqlParams),
54  (override));
55  MOCK_METHOD(StatusOr<spanner::ExecutionPlan>, AnalyzeSql, (SqlParams),
56  (override));
57  MOCK_METHOD(StatusOr<spanner::PartitionedDmlResult>, ExecutePartitionedDml,
59  MOCK_METHOD(StatusOr<std::vector<spanner::QueryPartition>>, PartitionQuery,
61  MOCK_METHOD(StatusOr<spanner::BatchDmlResult>, ExecuteBatchDml,
63  MOCK_METHOD(StatusOr<spanner::CommitResult>, Commit, (CommitParams),
64  (override));
65  MOCK_METHOD(Status, Rollback, (RollbackParams), (override));
66 };
67 
68 /**
69  * Mock the results of a ExecuteQuery() or Read() operation.
70  *
71  * @see @ref spanner-mocking for an example using this class.
72  */
73 class MockResultSetSource : public spanner_internal::ResultSourceInterface {
74  public:
75  MOCK_METHOD(StatusOr<spanner::Row>, NextRow, (), (override));
76  MOCK_METHOD(absl::optional<google::spanner::v1::ResultSetMetadata>, Metadata,
77  (), (override));
78  MOCK_METHOD(absl::optional<google::spanner::v1::ResultSetStats>, Stats, (),
79  (const, override));
80 };
81 
82 } // namespace SPANNER_CLIENT_NS
83 } // namespace spanner_mocks
84 } // namespace cloud
85 } // namespace google
86 
87 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_MOCKS_MOCK_SPANNER_CONNECTION_H