Skip to content

Commit f195518

Browse files
committed
fix *rest_required_fields test actual vs expected when field is auto-populated
1 parent 73abe3b commit f195518

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

packages/gapic-generator/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
@@ -1114,10 +1114,10 @@ def test_{{ service.client_name|snake_case }}_create_channel_credentials_file(cl
11141114

11151115
{% for method in service.methods.values() if 'rest' in opts.transport %}
11161116
{% if method.extended_lro %}
1117-
{{ test_macros.rest_required_tests(method, service, numeric_enums=opts.rest_numeric_enums, full_extended_lro=True) }}
1117+
{{ test_macros.rest_required_tests(api, method, service, numeric_enums=opts.rest_numeric_enums, full_extended_lro=True) }}
11181118

11191119
{% endif %}
1120-
{{ test_macros.rest_required_tests(method, service, numeric_enums=opts.rest_numeric_enums) }}
1120+
{{ test_macros.rest_required_tests(api, method, service, numeric_enums=opts.rest_numeric_enums) }}
11211121

11221122
{% endfor -%} {#- method in methods for rest #}
11231123

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ def test_{{ method_name }}_raw_page_lro():
10181018
{% endif %}{# method.paged_result_field #}{% endwith %}
10191019
{% endmacro %}
10201020

1021-
{% macro rest_required_tests(method, service, numeric_enums=False, full_extended_lro=False) %}
1021+
{% macro rest_required_tests(api, method, service, numeric_enums=False, full_extended_lro=False) %}
10221022
{% with method_name = method.client_method_name|snake_case + "_unary" if method.extended_lro and not full_extended_lro else method.client_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 %}
10231023
{# TODO(kbandes): remove this if condition when lro and client streaming are supported. #}
10241024
{% if not method.client_streaming %}
@@ -1218,6 +1218,24 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide
12181218
('$alt', 'json;enum-encoding=int')
12191219
{% endif %}
12201220
]
1221+
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
1222+
{% if method_settings is not none %}
1223+
{% for auto_populated_field in method_settings.auto_populated_fields %}
1224+
# Ensure that the uuid4 field is set according to AIP 4235
1225+
# and remove it so the expected/actual comparison succeeds.
1226+
# Otherwise, the actual will differ from the expected since
1227+
# this field was automatically populated.
1228+
found_field = None
1229+
for i, (key, value) in enumerate(req.call_args.kwargs['params']):
1230+
if key == "{{ auto_populated_field|camel_case }}":
1231+
assert re.match(r"{{ get_uuid4_re() }}", value)
1232+
found_field = i
1233+
break
1234+
if found_field is not None:
1235+
del req.call_args.kwargs['params'][found_field]
1236+
{% endfor %}
1237+
{% endif %}
1238+
{% endwith %}
12211239
actual_params = req.call_args.kwargs['params']
12221240
assert expected_params == actual_params
12231241

packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4132,6 +4132,18 @@ def test_create_job_rest_required_fields(request_type=storage_batch_operations.C
41324132
"",
41334133
),
41344134
]
4135+
# Ensure that the uuid4 field is set according to AIP 4235
4136+
# and remove it so the expected/actual comparison succeeds.
4137+
# Otherwise, the actual will differ from the expected since
4138+
# this field was automatically populated.
4139+
found_field = None
4140+
for i, (key, value) in enumerate(req.call_args.kwargs['params']):
4141+
if key == "requestId":
4142+
assert re.match(r"[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}", value)
4143+
found_field = i
4144+
break
4145+
if found_field is not None:
4146+
del req.call_args.kwargs['params'][found_field]
41354147
actual_params = req.call_args.kwargs['params']
41364148
assert expected_params == actual_params
41374149

@@ -4299,6 +4311,18 @@ def test_delete_job_rest_required_fields(request_type=storage_batch_operations.D
42994311

43004312
expected_params = [
43014313
]
4314+
# Ensure that the uuid4 field is set according to AIP 4235
4315+
# and remove it so the expected/actual comparison succeeds.
4316+
# Otherwise, the actual will differ from the expected since
4317+
# this field was automatically populated.
4318+
found_field = None
4319+
for i, (key, value) in enumerate(req.call_args.kwargs['params']):
4320+
if key == "requestId":
4321+
assert re.match(r"[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}", value)
4322+
found_field = i
4323+
break
4324+
if found_field is not None:
4325+
del req.call_args.kwargs['params'][found_field]
43024326
actual_params = req.call_args.kwargs['params']
43034327
assert expected_params == actual_params
43044328

@@ -4464,6 +4488,18 @@ def test_cancel_job_rest_required_fields(request_type=storage_batch_operations.C
44644488

44654489
expected_params = [
44664490
]
4491+
# Ensure that the uuid4 field is set according to AIP 4235
4492+
# and remove it so the expected/actual comparison succeeds.
4493+
# Otherwise, the actual will differ from the expected since
4494+
# this field was automatically populated.
4495+
found_field = None
4496+
for i, (key, value) in enumerate(req.call_args.kwargs['params']):
4497+
if key == "requestId":
4498+
assert re.match(r"[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}", value)
4499+
found_field = i
4500+
break
4501+
if found_field is not None:
4502+
del req.call_args.kwargs['params'][found_field]
44674503
actual_params = req.call_args.kwargs['params']
44684504
assert expected_params == actual_params
44694505

0 commit comments

Comments
 (0)