This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
chore: refactor rest transport bad request test macro #2141
Merged
ohmayr
merged 4 commits into
async-rest-support-in-gapics
from
refactor-rest-bad-request-test
Sep 13, 2024
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1522,33 +1522,6 @@ def test_{{ method_name }}_rest_interceptors(null_interceptor): | |
| {% endif %}{# streaming #} | ||
|
|
||
|
|
||
| def test_{{ method_name }}_rest_bad_request(transport: str = 'rest', request_type={{ method.input.ident }}): | ||
| client = {{ service.client_name }}( | ||
| credentials=ga_credentials.AnonymousCredentials(), | ||
| transport=transport, | ||
| ) | ||
|
|
||
| # send a request that will satisfy transcoding | ||
| request_init = {{ method.http_options[0].sample_request(method) }} | ||
| request = request_type(**request_init) | ||
| {% if method.client_streaming %} | ||
| requests = [request] | ||
| {% endif %} | ||
|
|
||
| # 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 | ||
| {% if method.client_streaming %} | ||
| client.{{ method_name }}(iter(requests)) | ||
| {% else %} | ||
| client.{{ method_name }}(request) | ||
| {% endif %} | ||
|
|
||
|
|
||
| {% if method.flattened_fields and not method.client_streaming %} | ||
| def test_{{ method_name }}_rest_flattened(): | ||
| client = {{ service.client_name }}( | ||
|
|
@@ -1979,6 +1952,8 @@ def test_unsupported_parameter_rest_asyncio(): | |
| {% for method in service.methods.values() %} | ||
| {% if is_rest_unsupported_method(method, is_async) == 'True' or not method.http_options %} | ||
| {{ rest_method_not_implemented_error(service, method, transport, is_async) }} | ||
| {% else %} | ||
| {{ rest_method_bad_request_test(service, method, transport, is_async) }} | ||
| {% endif %}{# is_rest_unsupported_method(method, is_async) == 'False' and method.http_options #} | ||
| {% endfor %} | ||
| {% endif %}{# if 'rest' in transport #} | ||
|
|
@@ -2035,3 +2010,47 @@ def test_initialize_client_w_{{transport_name}}(): | |
| assert client is not None | ||
|
|
||
| {% endmacro %} | ||
|
|
||
| {# rest_method_bad_request_test generates tests for rest methods | ||
| # which raise a google.api.core.exceptions.BadRequest error. | ||
| # TODO(https://github.com/googleapis/gapic-generator-python/issues/2157): Refactor this macro to include `gRPC` coverage. | ||
| #} | ||
| {% macro rest_method_bad_request_test(service, method, transport, is_async) %} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non-blocking, but desirable: Same comment as in the other PR: I'd have a macro-level fail-safe check that |
||
| {% 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 = method.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={{ method.input.ident }}): | ||
| {% if transport_name == 'rest_asyncio' %} | ||
| if not HAS_GOOGLE_AUTH_AIO: | ||
| {# TODO(https://github.com/googleapis/google-auth-library-python/pull/1577): Update the version of google-auth once the linked PR is merged. #} | ||
| pytest.skip("google-auth > 2.x.x is required for async rest transport.") | ||
|
|
||
| {% endif %} | ||
| client = {{ get_client(service, is_async) }}( | ||
| credentials={{get_credentials(is_async)}}, | ||
| transport="{{transport_name}}" | ||
| ) | ||
| # send a request that will satisfy transcoding | ||
| request_init = {{ method.http_options[0].sample_request(method) }} | ||
| request = request_type(**request_init) | ||
|
|
||
| # 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 | ||
| response_value = mock.Mock() | ||
| {% if is_async %} | ||
| response_value.read = mock.AsyncMock(return_value=b'{}') | ||
| {% else %} | ||
| json_return_value = '' | ||
| response_value.json = mock.Mock(return_value={}) | ||
| {% endif %}{# if is_async #} | ||
| response_value.status_code = 400 | ||
| response_value.request = mock.Mock() | ||
| req.return_value = response_value | ||
| {{ await_prefix }}client.{{ method_name }}(request) | ||
|
|
||
| {% endmacro %} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're passing the transport in, and since you'll want to have gRPC in here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed.