Skip to content
Open
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
@@ -1,30 +1,29 @@
{% macro grpc_required_tests(method, service, api, full_extended_lro=False) %}
{%- set method_settings = api.all_method_settings.get(method.meta.address.proto) -%}
{%- set auto_populated_fields = method_settings.auto_populated_fields if method_settings else [] -%}
{%- set auto_populated_field_sample_value = "explicit value for autopopulate-able field" -%}
{% 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 %}
@pytest.mark.parametrize("request_type", [
{{ method.input.ident }},
dict,
{{ method.input.ident }}({
{% for auto_populated_field in auto_populated_fields %}
"{{ auto_populated_field }}": "{{ auto_populated_field_sample_value }}",
{% endfor %}
}),
{
{% for auto_populated_field in auto_populated_fields %}
"{{ auto_populated_field }}": "{{ auto_populated_field_sample_value }}",
{% endfor %}
},
])
def test_{{ method_name }}(request_type, transport: str = 'grpc'):
{% with auto_populated_field_sample_value = "explicit value for autopopulate-able field" %}
client = {{ service.client_name }}(
credentials=ga_credentials.AnonymousCredentials(),
transport=transport,
)

# Everything is optional in proto3 as far as the runtime is concerned,
# and we are mocking out the actual API, so just send an empty request.
request = request_type()
{# Set UUID4 fields so that they are not automatically populated. #}
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
{% if method_settings is not none %}
{% for auto_populated_field in method_settings.auto_populated_fields %}
if isinstance(request, dict):
request['{{ auto_populated_field }}'] = "{{ auto_populated_field_sample_value }}"
else:
request.{{ auto_populated_field }} = "{{ auto_populated_field_sample_value }}"
{% endfor %}
{% endif %}{# if method_settings is not none #}
{% endwith %}{# method_settings #}
request = request_type
{% if method.client_streaming %}
requests = [request]
{% endif %}
Expand Down Expand Up @@ -71,13 +70,9 @@ def test_{{ method_name }}(request_type, transport: str = 'grpc'):
assert next(args[0]) == request
{% else %}
request = {{ method.input.ident }}()
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
{% if method_settings is not none %}
{% for auto_populated_field in method_settings.auto_populated_fields %}
{% for auto_populated_field in auto_populated_fields %}
request.{{ auto_populated_field }} = "{{ auto_populated_field_sample_value }}"
{% endfor %}
{% endif %}{# if method_settings is not none #}
{% endwith %}{# method_settings #}
assert args[0] == request
{% endif %}

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


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

@pytest.mark.asyncio
async def test_{{ method_name }}_async(transport: str = 'grpc_asyncio', request_type={{ method.input.ident }}):
{% with auto_populated_field_sample_value = "explicit value for autopopulate-able field" %}
@pytest.mark.parametrize("request_type", [
{{ method.input.ident }}({
{%- for auto_populated_field in auto_populated_fields %}
"{{ auto_populated_field }}": "{{ auto_populated_field_sample_value }}",
{%- endfor %}
}),
{
{%- for auto_populated_field in auto_populated_fields %}
"{{ auto_populated_field }}": "{{ auto_populated_field_sample_value }}",
{%- endfor %}
},
])
async def test_{{ method_name }}_async(request_type, transport: str = 'grpc_asyncio'):
client = {{ service.async_client_name }}(
credentials=async_anonymous_credentials(),
transport=transport,
)

# Everything is optional in proto3 as far as the runtime is concerned,
# and we are mocking out the actual API, so just send an empty request.
request = request_type()
{# Set UUID4 fields so that they are not automatically populated. #}
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
{% if method_settings is not none %}
{% for auto_populated_field in method_settings.auto_populated_fields %}
if isinstance(request, dict):
request['{{ auto_populated_field }}'] = "{{ auto_populated_field_sample_value }}"
else:
request.{{ auto_populated_field }} = "{{ auto_populated_field_sample_value }}"
{% endfor %}
{% endif %}{# if method_settings is not none #}
{% endwith %}{# method_settings #}
request = request_type
{% if method.client_streaming %}
requests = [request]
{% endif %}
Expand Down Expand Up @@ -335,13 +329,9 @@ async def test_{{ method_name }}_async(transport: str = 'grpc_asyncio', request_
assert next(args[0]) == request
{% else %}
request = {{ method.input.ident }}()
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
{% if method_settings is not none %}
{% for auto_populated_field in method_settings.auto_populated_fields %}
{% for auto_populated_field in auto_populated_fields %}
request.{{ auto_populated_field }} = "{{ auto_populated_field_sample_value }}"
{% endfor %}
{% endif %}{# if method_settings is not none #}
{% endwith %}{# method_settings #}
assert args[0] == request
{% endif %}

Expand Down Expand Up @@ -378,12 +368,6 @@ async def test_{{ method_name }}_async(transport: str = 'grpc_asyncio', request_
{% endif %}{# oneof/optional #}
{% endfor %}
{% endif %}
{% endwith %}{# auto_populated_field_sample_value #}


@pytest.mark.asyncio
async def test_{{ method_name }}_async_from_dict():
await test_{{ method_name }}_async(request_type=dict)
{% endif %}{# full_extended_lro #}

{% if method.field_headers and not method.client_streaming and not method.explicit_routing %}
Expand Down
Loading
Loading