Skip to content

Commit

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

def test_{{ name|snake_case }}_rest_bad_request(transport: str = 'rest', request_type={{ sig.request_type }}):
client = {{ service.client_name }}(
credentials=ga_credentials.AnonymousCredentials(),
transport=transport,
)

request = request_type()
request = json_format.ParseDict({{ api.mixin_http_options["{}".format(name)][0].sample_request }}, request)

# Mock the http request call within the method and fake a BadRequest error.
with mock.patch.object(Session, 'request') as req, pytest.raises(core_exceptions.BadRequest):
# Wrap the value into a proper Response obj
response_value = Response()
response_value.status_code = 400
response_value.request = Request()
req.return_value = response_value
client.{{ name|snake_case }}(request)

@pytest.mark.parametrize("request_type", [
{{ sig.request_type }},
dict,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,14 @@ def test_unsupported_parameter_rest_asyncio():
{% endif %}{# if 'rest' in transport #}
{% endif %}{# is_rest_unsupported_method(method, is_async) == 'False' and method.http_options #}
{% endfor %}{# for method in service.methods.values() #}
{% for name, sig in api.mixin_api_signatures.items() %}
{% if 'rest' in transport %}
{# 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) }}
{% endif %}{# if not is_async #}
{% endif %}{# if 'rest' in transport #}
{% endfor %}
{{ initialize_client_with_transport_test(service, transport, is_async) }}
{% endmacro %}

Expand Down Expand Up @@ -1994,6 +2002,61 @@ def test_initialize_client_w_{{transport_name}}():
{% endif %}{# if 'grpc' in transport #}
{% endmacro %}

{# bad_request_mixins_test generates tests for rest mixin methods
# which raise a google.api.core.exceptions.BadRequest error.
#}
{% macro bad_request_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 }}
{{ async_prefix }}def test_{{ method_name }}_{{ transport_name }}_bad_request(request_type={{ sig.request_type }}):
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2157): Refactor this macro to include `gRPC` coverage. #}
{% 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 = json_format.ParseDict({{ api.mixin_http_options["{}".format(name)][0].sample_request }}, request)

# Mock the http request call within the method and fake a BadRequest error.
with mock.patch.object({{mocked_session}}, 'request') as req, pytest.raises(core_exceptions.BadRequest):
# Wrap the value into a proper Response obj
{% if is_async %}
response_value = mock.Mock()
response_value.read = mock.AsyncMock(return_value=b'{}')
{% else %}
response_value = Response()
json_return_value = ''
response_value.json = mock.Mock(return_value={})
{% endif %}{# if is_async #}
response_value.status_code = 400
{% if is_async %}
response_value.request = mock.Mock()
{% else %}
response_value.request = Request()
{% endif %}
req.return_value = response_value
{{ await_prefix }}client.{{ method_name }}(request)
{% endif %}{# if 'grpc' in transport #}
{% endmacro %}

{# call_success_test generates tests for rest methods
# when they make a successful request.
# NOTE: Currently, this macro does not support the following method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16512,6 +16512,26 @@ def test_analyze_org_policy_governed_assets_rest_interceptors(null_interceptor):
pre.assert_called_once()
post.assert_called_once()


def test_get_operation_rest_bad_request(request_type=operations_pb2.GetOperationRequest):
client = AssetServiceClient(
credentials=ga_credentials.AnonymousCredentials(),
transport="rest",
)
request = request_type()
request = json_format.ParseDict({'name': 'sample1/sample2/operations/sample3/sample4'}, request)

# Mock the http request call within the method and fake a BadRequest error.
with mock.patch.object(Session, 'request') as req, pytest.raises(core_exceptions.BadRequest):
# Wrap the value into a proper Response obj
response_value = Response()
json_return_value = ''
response_value.json = mock.Mock(return_value={})
response_value.status_code = 400
response_value.request = Request()
req.return_value = response_value
client.get_operation(request)

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


def test_get_operation_rest_bad_request(transport: str = 'rest', request_type=operations_pb2.GetOperationRequest):
client = AssetServiceClient(
credentials=ga_credentials.AnonymousCredentials(),
transport=transport,
)

request = request_type()
request = json_format.ParseDict({'name': 'sample1/sample2/operations/sample3/sample4'}, request)

# Mock the http request call within the method and fake a BadRequest error.
with mock.patch.object(Session, 'request') as req, pytest.raises(core_exceptions.BadRequest):
# Wrap the value into a proper Response obj
response_value = Response()
response_value.status_code = 400
response_value.request = Request()
req.return_value = response_value
client.get_operation(request)

@pytest.mark.parametrize("request_type", [
operations_pb2.GetOperationRequest,
dict,
Expand Down
Loading

0 comments on commit 231ea3d

Please sign in to comment.