Skip to content

Commit 1e6989d

Browse files
authored
chore: refactor tests related to AIP 4235 (#17027)
This PR depends on #16944. Please review #16944 first. The PR addresses the feedback in #16944 (comment) , specifically 2.
1 parent 6c2c7a6 commit 1e6989d

13 files changed

Lines changed: 386 additions & 677 deletions

File tree

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,20 @@ def test_{{ method_name }}_non_empty_request_with_auto_populated_field():
142142
call.assert_called()
143143
_, args, _ = call.mock_calls[0]
144144
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
145+
request_msg = {{ method.input.ident }}(
146+
{% for field in method.input.fields.values() if field.ident|string() == "str" and not field.uuid4 %}
147+
{{ field.name }}={{ field.mock_value }},
148+
{% endfor %}
149+
)
145150
{% if method_settings is not none %}
146151
{% for auto_populated_field in method_settings.auto_populated_fields %}
147152
# Ensure that the uuid4 field is set according to AIP 4235
148153
assert re.fullmatch(r"{{ get_uuid4_re() }}", args[0].{{ auto_populated_field }})
149-
# clear UUID field so that the check below succeeds
150-
args[0].{{ auto_populated_field }} = None
154+
request_msg.{{ auto_populated_field }} = args[0].{{ auto_populated_field }}
151155
{% endfor %}
152156
{% endif %}{# if method_settings is not none #}
153157
{% endwith %}{# method_settings #}
154-
assert args[0] == {{ method.input.ident }}(
155-
{% for field in method.input.fields.values() if field.ident|string() == "str" and not field.uuid4 %}
156-
{{ field.name }}={{ field.mock_value }},
157-
{% endfor %}
158-
)
158+
assert args[0] == request_msg
159159
{% endif %}
160160

161161
def test_{{ method_name }}_use_cached_wrapped_rpc():
@@ -1556,21 +1556,20 @@ def test_{{ method_name }}_rest_no_http_options():
15561556
# Establish that the underlying stub method was called.
15571557
call.assert_called()
15581558
_, args, {% if routing_param %}kw{% else %}_{% endif %} = call.mock_calls[0]
1559+
{% if request_dict %}
1560+
request_msg = {{ method.input.ident }}(**{{ request_dict }})
1561+
{% else %}
1562+
request_msg = {{ method.input.ident }}()
1563+
{% endif %}{# request_dict #}
15591564
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
15601565
{% if method_settings is not none %}
15611566
{% for auto_populated_field in method_settings.auto_populated_fields %}
15621567
# Ensure that the uuid4 field is set according to AIP 4235
15631568
assert re.fullmatch(r"{{ get_uuid4_re() }}", args[0].{{ auto_populated_field }})
1564-
# clear UUID field so that the check below succeeds
1565-
args[0].{{ auto_populated_field }} = None
1569+
request_msg.{{ auto_populated_field }} = args[0].{{ auto_populated_field }}
15661570
{% endfor %}{# for auto_populated_field in method_settings.auto_populated_fields #}
15671571
{% endif %}{# if method_settings is not none #}
15681572
{% endwith %}{# method_settings #}
1569-
{% if request_dict %}
1570-
request_msg = {{ method.input.ident }}(**{{ request_dict }})
1571-
{% else %}
1572-
request_msg = {{ method.input.ident }}()
1573-
{% endif %}{# request_dict #}
15741573
assert args[0] == request_msg
15751574

15761575
{% if routing_param %}

0 commit comments

Comments
 (0)