Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 8839c6f

Browse files
authored
feat: Make REST unit tests support numeric enums (#1423)
Support numeric enums in generated unit tests: default templates and Ads templates
1 parent 2297559 commit 8839c6f

3 files changed

Lines changed: 11 additions & 5 deletions

File tree

gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,10 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide
12171217
{{ req_field.type.python_type(req_field.field_pb.default_value or 0) }},
12181218
{% endif %}{# default is str #}
12191219
),
1220-
{% endfor %}
1220+
{% endfor %}
1221+
{% if opts.rest_numeric_enums %}
1222+
('$alt', 'json;enum-encoding=int')
1223+
{% endif %}
12211224
]
12221225
actual_params = req.call_args.kwargs['params']
12231226
assert expected_params == actual_params

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,10 @@ def test_{{ service.client_name|snake_case }}_create_channel_credentials_file(cl
628628

629629
{% for method in service.methods.values() if 'rest' in opts.transport %}
630630
{% if method.extended_lro %}
631-
{{ test_macros.rest_required_tests(method, service, full_extended_lro=True) }}
631+
{{ test_macros.rest_required_tests(method, service, numeric_enums=opts.rest_numeric_enums, full_extended_lro=True) }}
632632

633633
{% endif %}
634-
{{ test_macros.rest_required_tests(method, service) }}
634+
{{ test_macros.rest_required_tests(method, service, numeric_enums=opts.rest_numeric_enums) }}
635635

636636
{% endfor -%} {#- method in methods for rest #}
637637

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ def test_{{ method_name }}_raw_page_lro():
833833
{% endif %}{# method.paged_result_field #}{% endwith %}
834834
{% endmacro %}
835835

836-
{% macro rest_required_tests(method, service, full_extended_lro=False) %}
836+
{% macro rest_required_tests(method, service, numeric_enums=False, full_extended_lro=False) %}
837837
{% with method_name = method.safe_name|snake_case + "_unary" if method.extended_lro and not full_extended_lro else method.name|snake_case, method_output = method.extended_lro.operation_type if method.extended_lro and not full_extended_lro else method.output %}{% if method.http_options %}
838838
{# TODO(kbandes): remove this if condition when lro and client streaming are supported. #}
839839
{% if not method.client_streaming %}
@@ -1076,7 +1076,10 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide
10761076
{{ req_field.type.python_type(req_field.field_pb.default_value or 0) }},
10771077
{% endif %}{# default is str #}
10781078
),
1079-
{% endfor %}
1079+
{% endfor %}
1080+
{% if numeric_enums %}
1081+
('$alt', 'json;enum-encoding=int')
1082+
{% endif %}
10801083
]
10811084
actual_params = req.call_args.kwargs['params']
10821085
assert expected_params == actual_params

0 commit comments

Comments
 (0)