|
1 | 1 | {% 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" -%} |
2 | 5 | {% 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 %} |
3 | 6 | @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 | + }, |
6 | 17 | ]) |
7 | 18 | def test_{{ method_name }}(request_type, transport: str = 'grpc'): |
8 | | - {% with auto_populated_field_sample_value = "explicit value for autopopulate-able field" %} |
9 | 19 | client = {{ service.client_name }}( |
10 | 20 | credentials=ga_credentials.AnonymousCredentials(), |
11 | 21 | transport=transport, |
12 | 22 | ) |
13 | 23 |
|
14 | 24 | # Everything is optional in proto3 as far as the runtime is concerned, |
15 | 25 | # 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 |
28 | 27 | {% if method.client_streaming %} |
29 | 28 | requests = [request] |
30 | 29 | {% endif %} |
@@ -71,13 +70,9 @@ def test_{{ method_name }}(request_type, transport: str = 'grpc'): |
71 | 70 | assert next(args[0]) == request |
72 | 71 | {% else %} |
73 | 72 | 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 %} |
77 | 74 | request.{{ auto_populated_field }} = "{{ auto_populated_field_sample_value }}" |
78 | 75 | {% endfor %} |
79 | | - {% endif %}{# if method_settings is not none #} |
80 | | - {% endwith %}{# method_settings #} |
81 | 76 | assert args[0] == request |
82 | 77 | {% endif %} |
83 | 78 |
|
@@ -118,10 +113,9 @@ def test_{{ method_name }}(request_type, transport: str = 'grpc'): |
118 | 113 | {% endif %}{# end oneof/optional #} |
119 | 114 | {% endfor %} |
120 | 115 | {% endif %} |
121 | | - {% endwith %}{# auto_populated_field_sample_value #} |
| 116 | + {%- if not method.client_streaming %} |
122 | 117 |
|
123 | 118 |
|
124 | | -{% if not method.client_streaming %} |
125 | 119 | def test_{{ method_name }}_non_empty_request_with_auto_populated_field(): |
126 | 120 | # This test is a coverage failsafe to make sure that UUID4 fields are |
127 | 121 | # 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 = " |
265 | 259 | assert mock_rpc.call_count == 2 |
266 | 260 |
|
267 | 261 | @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'): |
270 | 275 | client = {{ service.async_client_name }}( |
271 | 276 | credentials=async_anonymous_credentials(), |
272 | 277 | transport=transport, |
273 | 278 | ) |
274 | 279 |
|
275 | 280 | # Everything is optional in proto3 as far as the runtime is concerned, |
276 | 281 | # 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 |
289 | 283 | {% if method.client_streaming %} |
290 | 284 | requests = [request] |
291 | 285 | {% endif %} |
@@ -335,13 +329,9 @@ async def test_{{ method_name }}_async(transport: str = 'grpc_asyncio', request_ |
335 | 329 | assert next(args[0]) == request |
336 | 330 | {% else %} |
337 | 331 | 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 %} |
341 | 333 | request.{{ auto_populated_field }} = "{{ auto_populated_field_sample_value }}" |
342 | 334 | {% endfor %} |
343 | | - {% endif %}{# if method_settings is not none #} |
344 | | - {% endwith %}{# method_settings #} |
345 | 335 | assert args[0] == request |
346 | 336 | {% endif %} |
347 | 337 |
|
@@ -378,12 +368,6 @@ async def test_{{ method_name }}_async(transport: str = 'grpc_asyncio', request_ |
378 | 368 | {% endif %}{# oneof/optional #} |
379 | 369 | {% endfor %} |
380 | 370 | {% 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) |
387 | 371 | {% endif %}{# full_extended_lro #} |
388 | 372 |
|
389 | 373 | {% if method.field_headers and not method.client_streaming and not method.explicit_routing %} |
|
0 commit comments