Skip to content

Conversation

@ohmayr
Copy link
Contributor

@ohmayr ohmayr commented Sep 11, 2024

This PR refactors the macro for testing a rest method call which raises a google.api_core.exceptions.BadRequest error.

For now, the macro is guarded and only generates tests for sync REST methods. Once we implement the relevant logic in async rest, the guard can be removed. (This will be addressed in a follow up PR.)

This PR should be reviewed and merged after: #2140.

@ohmayr ohmayr marked this pull request as ready for review September 11, 2024 00:58
@ohmayr ohmayr requested a review from a team as a code owner September 11, 2024 00:58
@product-auto-label product-auto-label bot added the size: xl Pull request size is extra large. label Sep 11, 2024
@ohmayr ohmayr force-pushed the refactor-rest-bad-request-test branch from 4c2462f to 3eebe32 Compare September 11, 2024 03:31
Copy link
Contributor

@parthea parthea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added non-blocking comment. Feel free to follow up in a later PR

{# rest_method_bad_request_test generates tests for rest methods
# which raise a google.api.core.exceptions.BadRequest error.
#}
{% macro rest_method_bad_request_test(service, method, method_name, transport_name, async_prefix, await_prefix, async_decorator, is_async) %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a TODO bug to follow up on making this test more generic to cover gRPC as well? For example, we could simulate a 400 response using either gRPC or REST

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed.

@ohmayr ohmayr force-pushed the implement-async-rest-call-method-class branch 2 times, most recently from c562932 to 8fd829c Compare September 13, 2024 00:49
Base automatically changed from implement-async-rest-call-method-class to async-rest-support-in-gapics September 13, 2024 15:51
@ohmayr ohmayr force-pushed the refactor-rest-bad-request-test branch from 3eebe32 to b466c69 Compare September 13, 2024 16:15
# 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) %}
Copy link
Contributor

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:

Suggested change
{% macro rest_method_bad_request_test(service, method, transport, is_async) %}
{% macro bad_request_test(service, method, transport, is_async) %}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed.

# 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) %}
Copy link
Contributor

Choose a reason for hiding this comment

The 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 "rest" in transport that will cause a generation-time exception if we call the macro for non-REST. We can adapt/remove that as we factor gRPC functionality in here.

assert transport.kind == "rest"


def test_list_instances_rest_bad_request(request_type=cloud_redis.ListInstancesRequest):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do any of the goldens test the await_prefix, async_prefix, etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not here but in one of the follow up PRs where we turn on this test for async.

in str(not_implemented_error.value)
)

{% endif %}{# if 'rest' in transport #}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have an else to raise a generation time error.

req.return_value = response_value
{{ await_prefix }}client.{{ method_name }}(request)

{% endif %}{# if 'rest' in transport #}
Copy link
Contributor

@vchudnov-g vchudnov-g Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have an else to raise a generation time error. It's even more important here because we don't have rest in the name (as we shouldn't, since we pass transport).

Otherwise, if somebody else comes along and see the call to this macro, unless they look inside they could assume it will work for gRPC, and delete the existing gRPC-testing functionality where it is now in favor of calling this macro....and things will "work" because all that would do is remove the test code, not move it around.

Having the failsafe is a second line of defense. I strongly encourage it. I know I harp on this a lot, but I've found it useful to assume that future developers, including myself, will overlook something that is obvious now.

Copy link
Contributor Author

@ohmayr ohmayr Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, we'll be raising a runtime error within the macro just to safeguard if it's called incorrectly without updating it.

This will be addressed in a separate PR.

@ohmayr ohmayr merged commit f4c128b into async-rest-support-in-gapics Sep 13, 2024
@ohmayr ohmayr deleted the refactor-rest-bad-request-test branch September 13, 2024 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: xl Pull request size is extra large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants