Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,16 @@ def test_{{ method_name }}(request_type, transport: str = 'grpc'):
{% for field in method.output.fields.values() | rejectattr('message') %}
{% if not field.oneof or field.proto3_optional %}
{% if field.field_pb.type in [1, 2] %}{# Use approx eq for floats #}
{% if field.repeated %}
for index in range(len(response.{{ field.name }})):
assert math.isclose(
response.{{ field.name }}[index],
{{ field.mock_value }}[index],
rel_tol=1e-6,
)
{% else %}{# field.repeated #}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it feasible to refactor the same code?

@parthea parthea Feb 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a much larger issue as duplication exists throughout the templates. We should tackle the duplicate code as a whole separate from this bug fix. I've created https://github.com/googleapis/gapic-generator-python/issues/1592 to track the issue.

assert math.isclose(response.{{ field.name }}, {{ field.mock_value }}, rel_tol=1e-6)
{% endif %}{# field.repeated #}
{% elif field.field_pb.type == 8 %}{# Use 'is' for bools #}
assert response.{{ field.name }} is {{ field.mock_value }}
{% else %}
Expand Down Expand Up @@ -1076,7 +1085,16 @@ def test_{{ method.name|snake_case }}_rest(request_type):
{% for field in method.output.fields.values() | rejectattr('message') %}
{% if not field.oneof or field.proto3_optional %}
{% if field.field_pb.type in [1, 2] %}{# Use approx eq for floats #}
{% if field.repeated %}
for index in range(len(response.{{ field.name }})):
assert math.isclose(
response.{{ field.name }}[index],
{{ field.mock_value }}[index],
rel_tol=1e-6,
)
{% else %}{# field.repeated #}
assert math.isclose(response.{{ field.name }}, {{ field.mock_value }}, rel_tol=1e-6)
{% endif %}
{% elif field.field_pb.type == 8 %}{# Use 'is' for bools #}
assert response.{{ field.name }} is {{ field.mock_value }}
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,16 @@ def test_{{ method_name }}(request_type, transport: str = 'grpc'):
{% for field in method_output.fields.values() | rejectattr('message') %}
{% if not field.oneof or field.proto3_optional %}
{% if field.field_pb.type in [1, 2] %}{# Use approx eq for floats #}
{% if field.repeated %}
for index in range(len(response.{{ field.name }})):
assert math.isclose(
response.{{ field.name }}[index],
{{ field.mock_value }}[index],
rel_tol=1e-6,
)
{% else %}{# field.repeated #}
assert math.isclose(response.{{ field.name }}, {{ field.mock_value }}, rel_tol=1e-6)
{% endif %}{# field.repeated #}
{% elif field.field_pb.type == 8 %}{# Use 'is' for bools #}
assert response.{{ field.name }} is {{ field.mock_value }}
{% else %}
Expand Down Expand Up @@ -187,7 +196,16 @@ async def test_{{ method_name }}_async(transport: str = 'grpc_asyncio', request_
{% for field in method_output.fields.values() | rejectattr('message') %}
{% if not field.oneof or field.proto3_optional %}
{% if field.field_pb.type in [1, 2] %}{# Use approx eq for floats #}
{% if field.repeated %}
for index in range(len(response.{{ field.name }})):
assert math.isclose(
response.{{ field.name }}[index],
{{ field.mock_value }}[index],
rel_tol=1e-6,
)
{% else %}{# field.repeated #}
assert math.isclose(response.{{ field.name }}, {{ field.mock_value }}, rel_tol=1e-6)
{% endif %}{# field.repeated #}
{% elif field.field_pb.type == 8 %}{# Use 'is' for bools #}
assert response.{{ field.name }} is {{ field.mock_value }}
{% else %}
Expand Down Expand Up @@ -958,7 +976,16 @@ def test_{{ method_name }}_rest(request_type):
{% for field in method_output.fields.values() | rejectattr('message') %}
{% if not field.oneof or field.proto3_optional %}
{% if field.field_pb.type in [1, 2] %}{# Use approx eq for floats #}
{% if field.repeated %}
for index in range(len(response.{{ field.name }})):
assert math.isclose(
response.{{ field.name }}[index],
{{ field.mock_value }}[index],
rel_tol=1e-6,
)
{% else %}{# field.repeated #}
assert math.isclose(response.{{ field.name }}, {{ field.mock_value }}, rel_tol=1e-6)
{% endif %}{# field.repeated #}
{% elif field.field_pb.type == 8 %}{# Use 'is' for bools #}
assert response.{{ field.name }} is {{ field.mock_value }}
{% else %}
Expand Down
38 changes: 38 additions & 0 deletions tests/fragments/test_repeated_double.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (C) 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.fragment;

import "google/protobuf/struct.proto";
import "google/api/client.proto";

service MyServiceRepeatedDouble {
option (google.api.default_host) = "my.example.com";

rpc MyMethod(MethodRequestWithRepeatedDouble) returns (MethodResponseWithRepeatedDouble) {
option (google.api.method_signature) = "parameter,items";
}
}

message MethodRequestWithRepeatedDouble {
google.protobuf.Value parameter = 1;
repeated google.protobuf.Value items = 2;
repeated double repeated_items = 3;
}

message MethodResponseWithRepeatedDouble {
repeated double result = 1;
}