Skip to content

Commit

Permalink
fix: fix generated unit tests with repeated double (#1582)
Browse files Browse the repository at this point in the history
* chore: fix generated unit tests that fail with repeated double

* apply fix in ads templates
  • Loading branch information
parthea authored Feb 7, 2023
1 parent 862cc64 commit 4c7dd53
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
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 #}
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;
}

0 comments on commit 4c7dd53

Please sign in to comment.