Skip to content

Conversation

@parthea
Copy link
Contributor

@parthea parthea commented Sep 3, 2024

Towards #2091

empty_call_test was refactored in #2110. This PR refactors the test further to use a shared marco method_call_test to reduce code duplication in similar tests.

The following test functions have similar code:

{% macro empty_call_test(method, method_name, service, api, uuid4_re, is_async=False) %}

def test_{{ method.name|snake_case }}_routing_parameters():

In PR #2133, I've updated the function def test_{{ method.name|snake_case }}_routing_parameters(): to use the shared marco method_call_test added in this PR

@product-auto-label product-auto-label bot added the size: xl Pull request size is extra large. label Sep 3, 2024
@parthea parthea added the owlbot:run Add this label to trigger the Owlbot post processor. label Sep 9, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Sep 9, 2024
@parthea parthea marked this pull request as ready for review September 9, 2024 12:39
@parthea parthea requested a review from a team as a code owner September 9, 2024 12:39

{% macro empty_call_test(method, method_name, service, api, uuid4_re, is_async=False) %}
{% macro method_call_test(test_name, method, service, api, request, is_async=False) %}
{% with method_name = method.name|snake_case + "_unary" if method.operation_service else method.name|snake_case %}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider this for clarity:

Suggested change
{% with method_name = method.name|snake_case + "_unary" if method.operation_service else method.name|snake_case %}
{% with method_name = (method.name + ("_unary" if method.operation_service else "")) | snake_case %}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 99a8e25

assert transport.kind == "{{ transport_name }}"

{% endmacro %}
{% endmacro %}{# transport_kind_test #}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: for consistency, consider removing the empty line just above this one, though that may involve tweaking the spacing elsewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 7c67469

{% for method in service.methods.values() %}{# method #}
{% if not method.client_streaming %}
# This test is a coverage failsafe to make sure that totally empty calls,
# i.e. request == None and no flattened fields passed, work.
Copy link
Contributor

Choose a reason for hiding this comment

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

But we're applying this test even to methods where the API requires a non-empty request, right? It would be great if we type-hinted methods sufficiently so that for methods requiring a request, the type is specified as the proper request object, and for methods with optional requests, the type is specified as a nullable request of the appropriate type. Then this macro would be applied to just the latter set of methods (and we could also test that sending request=None to the former set would raise warnings or errors).

I realize you are just refactoring what was here before, so this is no worse, but it might be worth tracking this is an improvement for the future. WDYT? (Feel free to clarify anything I am misunderstanding.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I've filed #2158 to follow up on it. It seems reasonable to raise an exception if a field is marked as required in the request message but is not set. In #2158, I also suggested to update Client Library AIPs. Resolving comment as this is now tracked in #2158



{% macro empty_call_test(method, method_name, service, api, uuid4_re, is_async=False) %}
{% macro method_call_test(test_name, method, service, api, request, is_async=False) %}
Copy link
Contributor

Choose a reason for hiding this comment

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

The comments within this macro suggest it's gRPC-only, so we should either clarify that in the name or add a TODO to say our intent is to expand this to REST as well.

Copy link
Contributor Author

@parthea parthea Sep 14, 2024

Choose a reason for hiding this comment

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

Good catch! This test only supports gRPC. I filed a bug to follow up on adding support for REST: #2159 . Also see 9c34f79 where I added the transport name as part of the test name.



{% macro empty_call_test(method, method_name, service, api, uuid4_re, is_async=False) %}
{% macro method_call_test(test_name, method, service, api, request, is_async=False) %}
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also make the name more specific, because most of our tests involve calling a method, but I take it this method will not be used in all our test cases?

Copy link
Contributor Author

@parthea parthea Sep 14, 2024

Choose a reason for hiding this comment

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

Tests in this code base have a lot of duplication. Ideally method_call_test will be used in the majority test cases, and where it can't be used, the name of other macros could be more descriptive. method_call_test_<with_customization>.

I've updated method_call_test to method_call_test_generic to reflect that tests that can't use this could be named method_call_test_<with_customization>.

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 in 5204db8 and 374e444

@parthea parthea enabled auto-merge (squash) September 14, 2024 11:50
@parthea parthea merged commit 4c5de87 into main Sep 14, 2024
@parthea parthea deleted the refactor-empty-call-test branch September 14, 2024 12:08
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.

2 participants