|
| 1 | +// Copyright 2025 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 | +// https://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 | +#include "google/cloud/bigtable/bound_query.h" |
| 16 | +#include "google/cloud/bigtable/prepared_query.h" |
| 17 | +#include "google/cloud/bigtable/sql_statement.h" |
| 18 | +#include "google/cloud/bigtable/value.h" |
| 19 | +#include "google/cloud/testing_util/status_matchers.h" |
| 20 | +#include <algorithm> |
| 21 | + |
| 22 | +namespace google { |
| 23 | +namespace cloud { |
| 24 | +namespace bigtable { |
| 25 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 26 | +namespace { |
| 27 | +using ::google::bigtable::v2::PrepareQueryResponse; |
| 28 | + |
| 29 | +TEST(BoundQuery, FromPreparedQuery) { |
| 30 | + CompletionQueue cq; |
| 31 | + Project p("dummy-project"); |
| 32 | + InstanceResource instance(p, "dummy-instance"); |
| 33 | + std::string statement_contents( |
| 34 | + "SELECT * FROM my_table WHERE col1 = @val1 and col2 = @val2;"); |
| 35 | + SqlStatement sql_statement(statement_contents); |
| 36 | + PrepareQueryResponse response; |
| 37 | + std::unordered_map<std::string, Value> parameters = {{"val1", Value(true)}, |
| 38 | + {"val2", Value(2.0)}}; |
| 39 | + |
| 40 | + // The following variables are only meant to confirm the metadata is correctly |
| 41 | + // passed down to the BoundQuery. |
| 42 | + auto metadata = std::make_unique<google::bigtable::v2::ResultSetMetadata>(); |
| 43 | + auto schema = std::make_unique<google::bigtable::v2::ProtoSchema>(); |
| 44 | + auto column = google::bigtable::v2::ColumnMetadata(); |
| 45 | + *column.mutable_name() = "col1"; |
| 46 | + schema->mutable_columns()->Add(std::move(column)); |
| 47 | + metadata->set_allocated_proto_schema(schema.release()); |
| 48 | + response.set_allocated_metadata(metadata.release()); |
| 49 | + |
| 50 | + PreparedQuery pq(cq, instance, sql_statement, response); |
| 51 | + auto bq = pq.BindParameters(parameters); |
| 52 | + EXPECT_EQ(instance.FullName(), bq.instance().FullName()); |
| 53 | + EXPECT_STATUS_OK(bq.prepared_query()); |
| 54 | + EXPECT_EQ(statement_contents, bq.prepared_query().value()); |
| 55 | + EXPECT_EQ(parameters, bq.parameters()); |
| 56 | + EXPECT_STATUS_OK(bq.metadata()); |
| 57 | + EXPECT_TRUE(bq.metadata().value().has_proto_schema()); |
| 58 | + EXPECT_EQ(1, bq.metadata().value().proto_schema().columns_size()); |
| 59 | + EXPECT_EQ("col1", bq.metadata().value().proto_schema().columns()[0].name()); |
| 60 | +} |
| 61 | + |
| 62 | +TEST(BoundQuery, ToRequestProto) { |
| 63 | + CompletionQueue cq; |
| 64 | + Project p("dummy-project"); |
| 65 | + InstanceResource instance(p, "dummy-instance"); |
| 66 | + std::string statement_contents( |
| 67 | + "SELECT * FROM my_table WHERE col1 = @val1 and col2 = @val2;"); |
| 68 | + SqlStatement sql_statement(statement_contents); |
| 69 | + PrepareQueryResponse response; |
| 70 | + std::unordered_map<std::string, Value> parameters = {{"val1", Value(true)}, |
| 71 | + {"val2", Value(2.0)}}; |
| 72 | + |
| 73 | + PreparedQuery pq(cq, instance, sql_statement, response); |
| 74 | + auto bq = pq.BindParameters(parameters); |
| 75 | + google::bigtable::v2::ExecuteQueryRequest proto = bq.ToRequestProto(); |
| 76 | + EXPECT_EQ(instance.FullName(), proto.instance_name()); |
| 77 | + EXPECT_EQ(statement_contents, proto.prepared_query()); |
| 78 | + |
| 79 | + // Test param contents. |
| 80 | + EXPECT_EQ(parameters.size(), proto.mutable_params()->size()); |
| 81 | + |
| 82 | + // The first parameter is a boolean. |
| 83 | + EXPECT_TRUE(proto.params().contains("val1")); |
| 84 | + auto val1 = proto.params().find("val1")->second; |
| 85 | + EXPECT_TRUE(val1.has_bool_value()); |
| 86 | + EXPECT_EQ(true, val1.bool_value()); |
| 87 | + |
| 88 | + // The second parameter is a double. |
| 89 | + EXPECT_TRUE(proto.params().contains("val2")); |
| 90 | + auto val2 = proto.params().find("val2")->second; |
| 91 | + EXPECT_TRUE(val2.has_float_value()); |
| 92 | + EXPECT_EQ(2.0, val2.float_value()); |
| 93 | +} |
| 94 | +} // namespace |
| 95 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 96 | +} // namespace bigtable |
| 97 | +} // namespace cloud |
| 98 | +} // namespace google |
0 commit comments