Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 56be5a2

Browse files
authored
feat: add datatype integration test for struct (#1102)
Fixes #1096
1 parent 4df2f3f commit 56be5a2

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

google/cloud/spanner/integration_tests/data_types_integration_test.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,49 @@ TEST_F(DataTypeIntegrationTest, WriteReadArrayDate) {
295295
EXPECT_THAT(*result, UnorderedElementsAreArray(data));
296296
}
297297

298+
// This test differs a lot from the other tests since Spanner STRUCT types may
299+
// not be used as column types, and they may not be returned as top-level
300+
// objects in a select statement. See
301+
// https://cloud.google.com/spanner/docs/data-types#struct-type for more info.
302+
//
303+
// So the approach taken here instead is to use a STRUCT (i.e., a std::tuple<>
304+
// in the C++ world), as a bound SQL query parameter to insert some data into
305+
// the table. This, in a way, tests the "write" path.
306+
//
307+
// To test the "read" path, we create a query that returns an array of struct,
308+
// that we then compare to the original data.
309+
TEST_F(DataTypeIntegrationTest, InsertAndQueryWithStruct) {
310+
using StructType =
311+
std::tuple<std::pair<std::string, std::string>,
312+
std::pair<std::string, std::vector<std::int64_t>>>;
313+
auto data = StructType{{"StringValue", "xx"}, {"ArrayInt64Value", {1, 2, 3}}};
314+
315+
auto& client = *client_;
316+
auto commit_result = client.Commit(
317+
[&data, &client](Transaction const& txn) -> StatusOr<Mutations> {
318+
auto dml_result = client.ExecuteDml(
319+
txn,
320+
SqlStatement(
321+
"INSERT INTO DataTypes (Id, StringValue, ArrayInt64Value)"
322+
"VALUES(@id, @struct.StringValue, @struct.ArrayInt64Value)",
323+
{{"id", Value("id-1")}, {"struct", Value(data)}}));
324+
if (!dml_result) return dml_result.status();
325+
return Mutations{};
326+
});
327+
ASSERT_STATUS_OK(commit_result);
328+
329+
auto rows = client_->ExecuteQuery(
330+
SqlStatement("SELECT ARRAY(SELECT STRUCT(StringValue, ArrayInt64Value)) "
331+
"FROM DataTypes"));
332+
using RowType = std::tuple<std::vector<StructType>>;
333+
auto row = GetSingularRow(StreamOf<RowType>(rows));
334+
ASSERT_STATUS_OK(row);
335+
336+
auto const& v = std::get<0>(*row);
337+
EXPECT_EQ(1, v.size());
338+
EXPECT_EQ(data, v[0]);
339+
}
340+
298341
} // namespace
299342
} // namespace SPANNER_CLIENT_NS
300343
} // namespace spanner

0 commit comments

Comments
 (0)