In this document we describe how to write unit tests that mock google::cloud::spanner::Client
using Google Mock. This document assumes the reader is familiar with the Google Test and Google Mock frameworks and with the Cloud Spanner C++ Client.
Mocking a successful <tt>ExecuteQuery</tt>
First include the headers for the Cloud Spanner Client, the mocking class, and the Google Mock framework.
#include "absl/memory/memory.h"
#include <google/protobuf/text_format.h>
#include <gmock/gmock.h>
The example uses a number of aliases to save typing and improve readability:
using ::testing::Return;
namespace spanner = ::google::cloud::spanner;
Contains all the Cloud Spanner C++ client types and functions.
Create a mocking object for google::cloud::spanner::Connection
:
auto conn = std::make_shared<google::cloud::spanner_mocks::MockConnection>();
We will setup this mock in a second, but first let's look at how it is used to create a google::cloud::spanner::Client
object:
Performs database client operations on Spanner.
Once the client is created you can make calls on the client as usual:
auto rows = client.ExecuteQuery(
Represents a potentially parameterized SQL statement.
And then verify the results meet your expectations:
int count = 0;
using RowType = std::tuple<std::int64_t, std::string>;
for (auto const& row : spanner::StreamOf<RowType>(rows)) {
ASSERT_TRUE(row);
auto expected_id = ++count;
EXPECT_EQ(expected_id, std::get<0>(*row));
EXPECT_EQ("Hello World", std::get<1>(*row));
}
All of this depends on creating a google::cloud::spanner::RowStream
that simulates the stream of results you want. To do so, you need to mock a source that streams google::cloud::spanner::Row
s:
auto source =
absl::make_unique<google::cloud::spanner_mocks::MockResultSetSource>();
The source must define the names and types of the columns returned by the query:
auto constexpr kText = R"pb(
row_type: {
fields: {
name: "Id",
type: { code: INT64 }
}
fields: {
name: "Greeting",
type: { code: STRING }
}
})pb";
google::spanner::v1::ResultSetMetadata metadata;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(kText, &metadata));
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
And then setup the mock to return the results. Note that the results are returned one value at a time, even if a row contains multiple values.
EXPECT_CALL(*source, NextRow())
.WillOnce(Return(
.WillOnce(Return(
The Value class represents a type-safe, nullable Spanner value.
Row MakeTestRow(std::vector< std::pair< std::string, Value >> pairs)
Creates a Row with the specified column names and values.
Note that the last value in the stream is indicated by an absl::optional without a value:
A Row is a sequence of columns each with a name and an associated Value.
Once the source
has been created and its behavior mocked, you mock the behavior for ExecuteQuery
:
EXPECT_CALL(*conn, ExecuteQuery)
.WillOnce([&source](spanner::Connection::SqlParams const&)
});
Represents the stream of Rows returned from spanner::Client::Read() or spanner::Client::ExecuteQuery(...
Full Listing
Finally we present the full code for this example:
#include "absl/memory/memory.h"
#include <google/protobuf/text_format.h>
#include <gmock/gmock.h>
namespace {
using ::testing::Return;
namespace spanner = ::google::cloud::spanner;
TEST(MockSpannerClient, SuccessfulExecuteQuery) {
auto source =
absl::make_unique<google::cloud::spanner_mocks::MockResultSetSource>();
auto constexpr kText = R"pb(
row_type: {
fields: {
name: "Id",
type: { code: INT64 }
}
fields: {
name: "Greeting",
type: { code: STRING }
}
})pb";
google::spanner::v1::ResultSetMetadata metadata;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(kText, &metadata));
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
EXPECT_CALL(*source, NextRow())
.WillOnce(Return(
.WillOnce(Return(
auto conn = std::make_shared<google::cloud::spanner_mocks::MockConnection>();
EXPECT_CALL(*conn, ExecuteQuery)
.WillOnce([&source](spanner::Connection::SqlParams const&)
});
auto rows = client.ExecuteQuery(
int count = 0;
using RowType = std::tuple<std::int64_t, std::string>;
for (auto const& row : spanner::StreamOf<RowType>(rows)) {
ASSERT_TRUE(row);
auto expected_id = ++count;
EXPECT_EQ(expected_id, std::get<0>(*row));
EXPECT_EQ("Hello World", std::get<1>(*row));
}
}
}