Skip to content

Commit

Permalink
feat(spanner): add integration tests for proto columns (#13756)
Browse files Browse the repository at this point in the history
  • Loading branch information
devbww authored Mar 9, 2024
1 parent 44c6ad9 commit f66a62f
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
#include "google/cloud/spanner/database.h"
#include "google/cloud/spanner/mutations.h"
#include "google/cloud/spanner/testing/database_integration_test.h"
#include "google/cloud/spanner/testing/singer.pb.h"
#include "google/cloud/spanner/timestamp.h"
#include "google/cloud/testing_util/status_matchers.h"
#include "absl/time/time.h"
#include <gmock/gmock.h>
#include <cstdint>
#include <string>
#include <vector>

namespace google {
Expand All @@ -43,6 +46,16 @@ absl::Time MakeTime(std::time_t sec, int nanos) {
return absl::FromTimeT(sec) + absl::Nanoseconds(nanos);
}

testing::SingerInfo MakeSinger(std::int64_t singer_id, std::string birth_date,
std::string nationality, testing::Genre genre) {
testing::SingerInfo singer;
singer.set_singer_id(singer_id);
singer.set_birth_date(std::move(birth_date));
singer.set_nationality(std::move(nationality));
singer.set_genre(genre);
return singer;
}

// A helper function used in the test fixtures below. This function writes the
// given data to the DataTypes table, then it reads all the data back and
// returns it to the caller.
Expand Down Expand Up @@ -292,6 +305,30 @@ TEST_F(PgDataTypeIntegrationTest, WriteReadNumeric) {
EXPECT_THAT(result, IsOkAndHolds(UnorderedElementsAreArray(data)));
}

TEST_F(DataTypeIntegrationTest, WriteReadProtoEnum) {
if (UsingEmulator()) GTEST_SKIP() << "emulator does not support PROTO";

std::vector<ProtoEnum<testing::Genre>> const data = {
testing::Genre::POP,
testing::Genre::JAZZ,
testing::Genre::FOLK,
testing::Genre::ROCK,
};
auto result = WriteReadData(*client_, data, "SingerGenre");
EXPECT_THAT(result, IsOkAndHolds(UnorderedElementsAreArray(data)));
}

TEST_F(DataTypeIntegrationTest, WriteReadProtoMessage) {
if (UsingEmulator()) GTEST_SKIP() << "emulator does not support PROTO";

std::vector<ProtoMessage<testing::SingerInfo>> const data = {
MakeSinger(1, "1817-05-25", "French", testing::Genre::FOLK),
MakeSinger(2123139547, "1942-06-18", "British", testing::Genre::POP),
};
auto result = WriteReadData(*client_, data, "SingerInfo");
EXPECT_THAT(result, IsOkAndHolds(UnorderedElementsAreArray(data)));
}

TEST_F(DataTypeIntegrationTest, WriteReadArrayBool) {
std::vector<std::vector<bool>> const data = {
std::vector<bool>{},
Expand Down Expand Up @@ -434,6 +471,36 @@ TEST_F(PgDataTypeIntegrationTest, WriteReadArrayNumeric) {
EXPECT_THAT(result, IsOkAndHolds(UnorderedElementsAreArray(data)));
}

TEST_F(DataTypeIntegrationTest, WriteReadArrayProtoEnum) {
if (UsingEmulator()) GTEST_SKIP() << "emulator does not support PROTO";

std::vector<std::vector<ProtoEnum<testing::Genre>>> const data = {
std::vector<ProtoEnum<testing::Genre>>{},
std::vector<ProtoEnum<testing::Genre>>{
testing::Genre::POP,
testing::Genre::JAZZ,
testing::Genre::FOLK,
testing::Genre::ROCK,
},
};
auto result = WriteReadData(*client_, data, "ArraySingerGenre");
EXPECT_THAT(result, IsOkAndHolds(UnorderedElementsAreArray(data)));
}

TEST_F(DataTypeIntegrationTest, WriteReadArrayProtoMessage) {
if (UsingEmulator()) GTEST_SKIP() << "emulator does not support PROTO";

std::vector<std::vector<ProtoMessage<testing::SingerInfo>>> const data = {
std::vector<ProtoMessage<testing::SingerInfo>>{},
std::vector<ProtoMessage<testing::SingerInfo>>{
MakeSinger(1, "1817-05-25", "French", testing::Genre::FOLK),
MakeSinger(2123139547, "1942-06-18", "British", testing::Genre::POP),
},
};
auto result = WriteReadData(*client_, data, "ArraySingerInfo");
EXPECT_THAT(result, IsOkAndHolds(UnorderedElementsAreArray(data)));
}

TEST_F(DataTypeIntegrationTest, JsonIndexAndPrimaryKey) {
spanner_admin::DatabaseAdminClient admin_client(
spanner_admin::MakeDatabaseAdminConnection());
Expand Down Expand Up @@ -573,6 +640,25 @@ TEST_F(PgDataTypeIntegrationTest, NumericPrimaryKey) {
HasSubstr("part of the primary key"))));
}

TEST_F(DataTypeIntegrationTest, InsertAndQueryWithProtoEnumKey) {
if (UsingEmulator()) GTEST_SKIP() << "emulator does not support PROTO";

auto& client = *client_;
auto const key = testing::Genre::POP;

auto commit_result = client.Commit(
Mutations{InsertOrUpdateMutationBuilder("ProtoEnumKey", {"Key"})
.EmplaceRow(key)
.Build()});
ASSERT_STATUS_OK(commit_result);

auto rows = client.Read("ProtoEnumKey", KeySet::All(), {"Key"});
using RowType = std::tuple<ProtoEnum<testing::Genre>>;
auto row = GetSingularRow(StreamOf<RowType>(rows));
ASSERT_STATUS_OK(row);
EXPECT_EQ(std::get<0>(*std::move(row)), key);
}

TEST_F(DataTypeIntegrationTest, DmlReturning) {
if (UsingEmulator()) GTEST_SKIP() << "emulator does not support THEN RETURN";

Expand Down
41 changes: 41 additions & 0 deletions google/cloud/spanner/testing/database_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "google/cloud/spanner/testing/cleanup_stale_databases.h"
#include "google/cloud/spanner/testing/pick_random_instance.h"
#include "google/cloud/spanner/testing/random_database_name.h"
#include "google/cloud/spanner/testing/singer.pb.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/status_matchers.h"
#include <chrono>
Expand Down Expand Up @@ -96,12 +97,52 @@ void DatabaseIntegrationTest::SetUpTestSuite() {
ArrayNumericValue ARRAY<NUMERIC>
) PRIMARY KEY (Id)
)sql");
if (!emulator_) { // proto columns
google::protobuf::FileDescriptorSet fds;
google::cloud::spanner::testing::SingerInfo::default_instance()
.GetMetadata()
.descriptor->file()
->CopyTo(fds.add_file());
fds.SerializeToString(request.mutable_proto_descriptors());
request.add_extra_statements(R"sql(
CREATE PROTO BUNDLE (
google.cloud.spanner.testing.SingerInfo,
google.cloud.spanner.testing.Genre,
)
)sql");
request.add_extra_statements(R"sql(
ALTER TABLE DataTypes
ADD COLUMN SingerInfo google.cloud.spanner.testing.SingerInfo
)sql");
request.add_extra_statements(R"sql(
ALTER TABLE DataTypes
ADD COLUMN SingerGenre google.cloud.spanner.testing.Genre
)sql");
request.add_extra_statements(R"sql(
ALTER TABLE DataTypes
ADD COLUMN ArraySingerInfo
ARRAY<google.cloud.spanner.testing.SingerInfo>
)sql");
request.add_extra_statements(R"sql(
ALTER TABLE DataTypes
ADD COLUMN ArraySingerGenre
ARRAY<google.cloud.spanner.testing.Genre>
)sql");
}
// Verify that NUMERIC can be used as a table key.
request.add_extra_statements(R"sql(
CREATE TABLE NumericKey (
Key NUMERIC NOT NULL
) PRIMARY KEY (Key)
)sql");
if (!emulator_) { // proto columns
// Verify that ProtoEnum<T> can be used as a table key.
request.add_extra_statements(R"sql(
CREATE TABLE ProtoEnumKey (
Key google.cloud.spanner.testing.Genre NOT NULL
) PRIMARY KEY (Key)
)sql");
}
auto database_future = admin_client.CreateDatabase(request);

int i = 0;
Expand Down

0 comments on commit f66a62f

Please sign in to comment.