Skip to content

Commit 6c2c7a6

Browse files
authored
chore: refactor request type tests (#17026)
This PR depends on #16944. Please review #16944 first. This PR addresses the feedback in #16944 (comment)
1 parent 794c15a commit 6c2c7a6

13 files changed

Lines changed: 1962 additions & 1823 deletions

File tree

packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
{% macro grpc_required_tests(method, service, api, full_extended_lro=False) %}
2+
{%- set method_settings = api.all_method_settings.get(method.meta.address.proto) -%}
3+
{%- set auto_populated_fields = method_settings.auto_populated_fields if method_settings else [] -%}
4+
{%- set auto_populated_field_sample_value = "explicit value for autopopulate-able field" -%}
25
{% with method_name = method.client_method_name|snake_case + "_unary" if method.extended_lro and not full_extended_lro else method.client_method_name|snake_case, method_output = method.extended_lro.operation_type if method.extended_lro and not full_extended_lro else method.output %}
36
@pytest.mark.parametrize("request_type", [
4-
{{ method.input.ident }},
5-
dict,
7+
{{ method.input.ident }}({
8+
{% for auto_populated_field in auto_populated_fields %}
9+
"{{ auto_populated_field }}": "{{ auto_populated_field_sample_value }}",
10+
{% endfor %}
11+
}),
12+
{
13+
{% for auto_populated_field in auto_populated_fields %}
14+
"{{ auto_populated_field }}": "{{ auto_populated_field_sample_value }}",
15+
{% endfor %}
16+
},
617
])
718
def test_{{ method_name }}(request_type, transport: str = 'grpc'):
8-
{% with auto_populated_field_sample_value = "explicit value for autopopulate-able field" %}
919
client = {{ service.client_name }}(
1020
credentials=ga_credentials.AnonymousCredentials(),
1121
transport=transport,
1222
)
1323

1424
# Everything is optional in proto3 as far as the runtime is concerned,
1525
# and we are mocking out the actual API, so just send an empty request.
16-
request = request_type()
17-
{# Set UUID4 fields so that they are not automatically populated. #}
18-
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
19-
{% if method_settings is not none %}
20-
{% for auto_populated_field in method_settings.auto_populated_fields %}
21-
if isinstance(request, dict):
22-
request['{{ auto_populated_field }}'] = "{{ auto_populated_field_sample_value }}"
23-
else:
24-
request.{{ auto_populated_field }} = "{{ auto_populated_field_sample_value }}"
25-
{% endfor %}
26-
{% endif %}{# if method_settings is not none #}
27-
{% endwith %}{# method_settings #}
26+
request = request_type
2827
{% if method.client_streaming %}
2928
requests = [request]
3029
{% endif %}
@@ -71,13 +70,9 @@ def test_{{ method_name }}(request_type, transport: str = 'grpc'):
7170
assert next(args[0]) == request
7271
{% else %}
7372
request = {{ method.input.ident }}()
74-
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
75-
{% if method_settings is not none %}
76-
{% for auto_populated_field in method_settings.auto_populated_fields %}
73+
{% for auto_populated_field in auto_populated_fields %}
7774
request.{{ auto_populated_field }} = "{{ auto_populated_field_sample_value }}"
7875
{% endfor %}
79-
{% endif %}{# if method_settings is not none #}
80-
{% endwith %}{# method_settings #}
8176
assert args[0] == request
8277
{% endif %}
8378

@@ -118,10 +113,9 @@ def test_{{ method_name }}(request_type, transport: str = 'grpc'):
118113
{% endif %}{# end oneof/optional #}
119114
{% endfor %}
120115
{% endif %}
121-
{% endwith %}{# auto_populated_field_sample_value #}
116+
{%- if not method.client_streaming %}
122117

123118

124-
{% if not method.client_streaming %}
125119
def test_{{ method_name }}_non_empty_request_with_auto_populated_field():
126120
# This test is a coverage failsafe to make sure that UUID4 fields are
127121
# automatically populated, according to AIP-4235, with non-empty requests.
@@ -265,27 +259,27 @@ async def test_{{ method_name }}_async_use_cached_wrapped_rpc(transport: str = "
265259
assert mock_rpc.call_count == 2
266260

267261
@pytest.mark.asyncio
268-
async def test_{{ method_name }}_async(transport: str = 'grpc_asyncio', request_type={{ method.input.ident }}):
269-
{% with auto_populated_field_sample_value = "explicit value for autopopulate-able field" %}
262+
@pytest.mark.parametrize("request_type", [
263+
{{ method.input.ident }}({
264+
{%- for auto_populated_field in auto_populated_fields %}
265+
"{{ auto_populated_field }}": "{{ auto_populated_field_sample_value }}",
266+
{%- endfor %}
267+
}),
268+
{
269+
{%- for auto_populated_field in auto_populated_fields %}
270+
"{{ auto_populated_field }}": "{{ auto_populated_field_sample_value }}",
271+
{%- endfor %}
272+
},
273+
])
274+
async def test_{{ method_name }}_async(request_type, transport: str = 'grpc_asyncio'):
270275
client = {{ service.async_client_name }}(
271276
credentials=async_anonymous_credentials(),
272277
transport=transport,
273278
)
274279

275280
# Everything is optional in proto3 as far as the runtime is concerned,
276281
# and we are mocking out the actual API, so just send an empty request.
277-
request = request_type()
278-
{# Set UUID4 fields so that they are not automatically populated. #}
279-
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
280-
{% if method_settings is not none %}
281-
{% for auto_populated_field in method_settings.auto_populated_fields %}
282-
if isinstance(request, dict):
283-
request['{{ auto_populated_field }}'] = "{{ auto_populated_field_sample_value }}"
284-
else:
285-
request.{{ auto_populated_field }} = "{{ auto_populated_field_sample_value }}"
286-
{% endfor %}
287-
{% endif %}{# if method_settings is not none #}
288-
{% endwith %}{# method_settings #}
282+
request = request_type
289283
{% if method.client_streaming %}
290284
requests = [request]
291285
{% endif %}
@@ -335,13 +329,9 @@ async def test_{{ method_name }}_async(transport: str = 'grpc_asyncio', request_
335329
assert next(args[0]) == request
336330
{% else %}
337331
request = {{ method.input.ident }}()
338-
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
339-
{% if method_settings is not none %}
340-
{% for auto_populated_field in method_settings.auto_populated_fields %}
332+
{% for auto_populated_field in auto_populated_fields %}
341333
request.{{ auto_populated_field }} = "{{ auto_populated_field_sample_value }}"
342334
{% endfor %}
343-
{% endif %}{# if method_settings is not none #}
344-
{% endwith %}{# method_settings #}
345335
assert args[0] == request
346336
{% endif %}
347337

@@ -378,12 +368,6 @@ async def test_{{ method_name }}_async(transport: str = 'grpc_asyncio', request_
378368
{% endif %}{# oneof/optional #}
379369
{% endfor %}
380370
{% endif %}
381-
{% endwith %}{# auto_populated_field_sample_value #}
382-
383-
384-
@pytest.mark.asyncio
385-
async def test_{{ method_name }}_async_from_dict():
386-
await test_{{ method_name }}_async(request_type=dict)
387371
{% endif %}{# full_extended_lro #}
388372

389373
{% if method.field_headers and not method.client_streaming and not method.explicit_routing %}

0 commit comments

Comments
 (0)