Skip to content

Commit

Permalink
chore: refactor success call mixins test
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmayr committed Sep 23, 2024
1 parent 231ea3d commit d49df24
Show file tree
Hide file tree
Showing 5 changed files with 572 additions and 517 deletions.
Original file line number Diff line number Diff line change
@@ -1,49 +1,3 @@
{% if 'rest' in opts.transport %}
{% for name, sig in api.mixin_api_signatures.items() %}

@pytest.mark.parametrize("request_type", [
{{ sig.request_type }},
dict,
])
def test_{{ name|snake_case }}_rest(request_type):
client = {{ service.client_name }}(
credentials=ga_credentials.AnonymousCredentials(),
transport="rest",
)
request_init = {{ api.mixin_http_options["{}".format(name)][0].sample_request }}
request = request_type(**request_init)
# Mock the http request call within the method and fake a response.
with mock.patch.object(type(client.transport._session), 'request') as req:
# Designate an appropriate value for the returned response.
{% if sig.response_type == "None" %}
return_value = None
{% else %}
return_value = {{ sig.response_type }}()
{% endif %}

# Wrap the value into a proper Response obj
response_value = Response()
response_value.status_code = 200
{% if sig.response_type == "None" %}
json_return_value = '{}'
{% else %}
json_return_value = json_format.MessageToJson(return_value)
{% endif %}

response_value._content = json_return_value.encode('UTF-8')
req.return_value = response_value

response = client.{{ name|snake_case }}(request)

# Establish that the response is the type that we expect.
{% if sig.response_type == "None" %}
assert response is None
{% else %}
assert isinstance(response, {{ sig.response_type }})
{% endif %}
{% endfor %}
{% endif %}

{% if api.has_operations_mixin and ('grpc' in opts.transport or 'grpc_asyncio' in opts.transport) %}

{% if "DeleteOperation" in api.mixin_api_methods %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,7 @@ def test_transport_kind_{{ transport_name }}():
)
assert transport.kind == "{{ transport_name }}"

{% endmacro %}
{% endmacro %}{# transport_kind_test #}


{% macro transport_close_test(service, api, transport, is_async) %}
Expand Down Expand Up @@ -1887,6 +1887,7 @@ def test_unsupported_parameter_rest_asyncio():
{# TODO: Remove the following condition as we implement async mixins #}
{% if not is_async %}
{{ bad_request_mixins_test(service, api, name, sig, transport, is_async) }}
{{ call_success_mixins_test(service, api, name, sig, transport, is_async) }}
{% endif %}{# if not is_async #}
{% endif %}{# if 'rest' in transport #}
{% endfor %}
Expand Down Expand Up @@ -2285,7 +2286,78 @@ def test_initialize_client_w_{{transport_name}}():

{% endif %}{# if not (method.server_streaming or method.lro or method.extended_lro or method.paged_result_field) #}
{% endif %}{# if 'grpc' in transport #}
{% endmacro %}{# transport_kind_test #}
{% endmacro %}{# call_success_test #}

{# call_success_mixins_test generates tests for rest mixin methods
# when they make a successful request.
#}
{% macro call_success_mixins_test(service, api, name, sig, transport, is_async=False) %}
{% set await_prefix = get_await_prefix(is_async) %}
{% set async_prefix = get_async_prefix(is_async) %}
{% set async_decorator = get_async_decorator(is_async) %}
{% set transport_name = get_transport_name(transport, is_async) %}
{% set method_name = name|snake_case %}
{% set mocked_session = "AsyncAuthorizedSession" if is_async else "Session" %}
{{ async_decorator }}
@pytest.mark.parametrize("request_type", [
{{ sig.request_type }},
dict,
])
{{ async_prefix }}def test_{{ method_name }}_{{ transport_name }}(request_type):
{% if 'grpc' in transport %}
raise NotImplementedError("gRPC is currently not supported for this test case.")
{% else %}{# 'rest' in transport #}
{% if transport_name == 'rest_asyncio' %}
if not HAS_GOOGLE_AUTH_AIO:
pytest.skip("google-auth >= 2.35.0 is required for async rest transport.")
elif not HAS_AIOHTTP_INSTALLED:
pytest.skip("aiohttp is required for async rest transport.")
elif not HAS_ASYNC_REST_SUPPORT_IN_CORE:
pytest.skip("google-api-core >= 2.20.0 is required for async rest transport.")

{% endif %}
client = {{ get_client(service, is_async) }}(
credentials={{get_credentials(is_async)}},
transport="{{transport_name}}",
)

request_init = {{ api.mixin_http_options["{}".format(name)][0].sample_request }}
request = request_type(**request_init)
# Mock the http request call within the method and fake a response.
with mock.patch.object({{mocked_session}}, 'request') as req:
# Designate an appropriate value for the returned response.
{% if sig.response_type == "None" %}
return_value = None
{% else %}
return_value = {{ sig.response_type }}()
{% endif %}

# Wrap the value into a proper Response obj
response_value = mock.Mock()
response_value.status_code = 200
{% if sig.response_type == "None" %}
json_return_value = '{}'
{% else %}
json_return_value = json_format.MessageToJson(return_value)
{% endif %}
{% if is_async %}
response_value.read = mock.AsyncMock(return_value=json_return_value.encode('UTF-8'))
{% else %}
response_value.content = json_return_value.encode('UTF-8')
{% endif %}

req.return_value = response_value

response = {{ await_prefix }}client.{{ method_name }}(request)

# Establish that the response is the type that we expect.
{% if sig.response_type == "None" %}
assert response is None
{% else %}
assert isinstance(response, {{ sig.response_type }})
{% endif %}
{% endif %}{# if 'grpc' in transport #}
{% endmacro %}{# call_success_mixins_test #}

{% macro empty_call_test(service, api, transport, is_async) %}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2159):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16532,6 +16532,37 @@ def test_get_operation_rest_bad_request(request_type=operations_pb2.GetOperation
req.return_value = response_value
client.get_operation(request)


@pytest.mark.parametrize("request_type", [
operations_pb2.GetOperationRequest,
dict,
])
def test_get_operation_rest(request_type):
client = AssetServiceClient(
credentials=ga_credentials.AnonymousCredentials(),
transport="rest",
)

request_init = {'name': 'sample1/sample2/operations/sample3/sample4'}
request = request_type(**request_init)
# Mock the http request call within the method and fake a response.
with mock.patch.object(Session, 'request') as req:
# Designate an appropriate value for the returned response.
return_value = operations_pb2.Operation()

# Wrap the value into a proper Response obj
response_value = mock.Mock()
response_value.status_code = 200
json_return_value = json_format.MessageToJson(return_value)
response_value.content = json_return_value.encode('UTF-8')

req.return_value = response_value

response = client.get_operation(request)

# Establish that the response is the type that we expect.
assert isinstance(response, operations_pb2.Operation)

def test_initialize_client_w_rest():
client = AssetServiceClient(
credentials=ga_credentials.AnonymousCredentials(),
Expand Down Expand Up @@ -17303,36 +17334,6 @@ def test_client_with_default_client_info():
prep.assert_called_once_with(client_info)


@pytest.mark.parametrize("request_type", [
operations_pb2.GetOperationRequest,
dict,
])
def test_get_operation_rest(request_type):
client = AssetServiceClient(
credentials=ga_credentials.AnonymousCredentials(),
transport="rest",
)
request_init = {'name': 'sample1/sample2/operations/sample3/sample4'}
request = request_type(**request_init)
# Mock the http request call within the method and fake a response.
with mock.patch.object(type(client.transport._session), 'request') as req:
# Designate an appropriate value for the returned response.
return_value = operations_pb2.Operation()

# Wrap the value into a proper Response obj
response_value = Response()
response_value.status_code = 200
json_return_value = json_format.MessageToJson(return_value)

response_value._content = json_return_value.encode('UTF-8')
req.return_value = response_value

response = client.get_operation(request)

# Establish that the response is the type that we expect.
assert isinstance(response, operations_pb2.Operation)


def test_get_operation(transport: str = "grpc"):
client = AssetServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport,
Expand Down
Loading

0 comments on commit d49df24

Please sign in to comment.