Skip to content

Commit

Permalink
remove duplication of uuid4 regex
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea committed Mar 22, 2024
1 parent 04f92bb commit 70f0c64
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ from google.iam.v1 import policy_pb2 # type: ignore
{% endif %}
{% endfilter %}

{% with uuid4_re = "[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}" %}

def client_cert_source_callback():
return b"cert bytes", b"key bytes"
Expand Down Expand Up @@ -658,7 +659,7 @@ def test_{{ method_name }}_empty_call():
{% if method_settings is not none %}
{% for auto_populated_field in method_settings.auto_populated_fields %}
# Ensure that the uuid4 field is set according to AIP 4235
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}", args[0].{{ auto_populated_field }})
assert re.match(r"{{ uuid4_re }}", args[0].{{ auto_populated_field }})
# clear UUID field so that the check below succeeds
args[0].{{ auto_populated_field }} = None
{% endfor %}
Expand Down Expand Up @@ -696,7 +697,7 @@ def test_{{ method_name }}_non_empty_request_with_auto_populated_field():
{% if method_settings is not none %}
{% for auto_populated_field in method_settings.auto_populated_fields %}
# Ensure that the uuid4 field is set according to AIP 4235
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}", args[0].{{ auto_populated_field }})
assert re.match(r"{{ uuid4_re }}", args[0].{{ auto_populated_field }})
# clear UUID field so that the check below succeeds
args[0].{{ auto_populated_field }} = None
{% endfor %}
Expand Down Expand Up @@ -2440,4 +2441,5 @@ def test_client_ctx():
pass
close.assert_called()

{% endwith %}{# uuid4_re #}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% macro grpc_required_tests(method, service, api, full_extended_lro=False) %}
{% with method_name = method.safe_name|snake_case + "_unary" if method.extended_lro and not full_extended_lro else method.safe_name|snake_case, method_output = method.extended_lro.operation_type if method.extended_lro and not full_extended_lro else method.output %}
{% with uuid4_re = "[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}" %}
@pytest.mark.parametrize("request_type", [
{{ method.input.ident }},
dict,
Expand Down Expand Up @@ -144,7 +145,7 @@ def test_{{ method_name }}_empty_call():
{% if method_settings is not none %}
{% for auto_populated_field in method_settings.auto_populated_fields %}
# Ensure that the uuid4 field is set according to AIP 4235
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}", args[0].{{ auto_populated_field }})
assert re.match(r"{{ uuid4_re }}", args[0].{{ auto_populated_field }})
# clear UUID field so that the check below succeeds
args[0].{{ auto_populated_field }} = None
{% endfor %}
Expand Down Expand Up @@ -182,7 +183,7 @@ def test_{{ method_name }}_non_empty_request_with_auto_populated_field():
{% if method_settings is not none %}
{% for auto_populated_field in method_settings.auto_populated_fields %}
# Ensure that the uuid4 field is set according to AIP 4235
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}", args[0].{{ auto_populated_field }})
assert re.match(r"{{ uuid4_re }}", args[0].{{ auto_populated_field }})
# clear UUID field so that the check below succeeds
args[0].{{ auto_populated_field }} = None
{% endfor %}
Expand Down Expand Up @@ -243,7 +244,7 @@ async def test_{{ method_name }}_empty_call_async():
{% if method_settings is not none %}
{% for auto_populated_field in method_settings.auto_populated_fields %}
# Ensure that the uuid4 field is set according to AIP 4235
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}", args[0].{{ auto_populated_field }})
assert re.match(r"{{ uuid4_re }}", args[0].{{ auto_populated_field }})
# clear UUID field so that the check below succeeds
args[0].{{ auto_populated_field }} = None
{% endfor %}
Expand Down Expand Up @@ -1014,6 +1015,7 @@ def test_{{ method_name }}_raw_page_lro():
response = {{ method.lro.response_type.ident }}()
assert response.raw_page is response
{% endif %}{# method.paged_result_field #}{% endwith %}
{% endwith %}{# uuid4_re #}
{% endmacro %}

{% macro rest_required_tests(method, service, numeric_enums=False, full_extended_lro=False) %}
Expand Down
22 changes: 6 additions & 16 deletions tests/system/test_unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

from google import showcase

UUID4_RE = r"[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}"


def test_unary_with_request_object(echo):
response = echo.echo(showcase.EchoRequest(
Expand All @@ -41,16 +43,10 @@ def test_unary_with_request_object(echo):
))
assert response.content == 'The hail in Wales falls mainly on the snails.'
# Ensure that the uuid4 field is set according to AIP 4235
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}",
response.request_id,
)
assert re.match(UUID4_RE, response.request_id)
assert len(response.request_id) == 36
# Ensure that the uuid4 field is set according to AIP 4235
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}",
response.other_request_id,
)
assert re.match(UUID4_RE, response.other_request_id)
assert len(response.other_request_id) == 36


Expand All @@ -72,16 +68,10 @@ def test_unary_with_dict(echo):
'content': 'The hail in Wales falls mainly on the snails.',
})
assert response.content == 'The hail in Wales falls mainly on the snails.'
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}",
response.request_id,
)
assert re.match(UUID4_RE, response.request_id)
assert len(response.request_id) == 36
# Ensure that the uuid4 field is set according to AIP 4235
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}",
response.other_request_id,
)
assert re.match(UUID4_RE, response.other_request_id)
assert len(response.other_request_id) == 36


Expand Down

0 comments on commit 70f0c64

Please sign in to comment.