From 55ad6a7412e48e4a16e006509a27b7fbfe74b307 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 8 May 2026 16:55:15 +0000 Subject: [PATCH] chore: refactor templates to reduce duplication --- .../%name_%version/%sub/test_%service.py.j2 | 9 +++--- .../%name_%version/%sub/test_%service.py.j2 | 1 + .../gapic/%name_%version/%sub/test_macros.j2 | 6 ++-- .../test_storage_batch_operations.py | 31 ++++++++++--------- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/gapic-generator/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/packages/gapic-generator/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 6ef2d20d14f8..d86e53650218 100644 --- a/packages/gapic-generator/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/packages/gapic-generator/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -6,6 +6,7 @@ import os {% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|list %} import re +_UUID4_RE = re.compile(r"[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}") {% endif %} from unittest import mock from unittest.mock import AsyncMock @@ -71,7 +72,7 @@ from google.iam.v1 import policy_pb2 # type: ignore {{ shared_macros.add_google_api_core_version_header_import(service.version) }} -{% 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" @@ -692,7 +693,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"{{ uuid4_re }}", args[0].{{ auto_populated_field }}) + assert _UUID4_RE.match(args[0].{{ auto_populated_field }}) # clear UUID field so that the check below succeeds args[0].{{ auto_populated_field }} = None {% endfor %} @@ -730,7 +731,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"{{ uuid4_re }}", args[0].{{ auto_populated_field }}) + assert _UUID4_RE.match(args[0].{{ auto_populated_field }}) # clear UUID field so that the check below succeeds args[0].{{ auto_populated_field }} = None {% endfor %} @@ -2488,5 +2489,5 @@ def test_client_ctx(): pass close.assert_called() -{% endwith %}{# uuid4_re #} + {% endblock %} diff --git a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 0399b5e331a4..85ac7484cfdc 100644 --- a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -9,6 +9,7 @@ import os {% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|list %} import re +_UUID4_RE = re.compile(r"{{ test_macros.get_uuid4_re() }}") {% endif %} from unittest import mock from unittest.mock import AsyncMock diff --git a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 index c1bc8893df6f..62140b8b7a7e 100644 --- a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 +++ b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2 @@ -151,7 +151,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"{{ get_uuid4_re() }}", args[0].{{ auto_populated_field }}) + assert _UUID4_RE.match(args[0].{{ auto_populated_field }}) # clear UUID field so that the check below succeeds args[0].{{ auto_populated_field }} = None {% endfor %} @@ -1228,7 +1228,7 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide found_field = None for i, (key, value) in enumerate(req.call_args.kwargs['params']): if key == "{{ auto_populated_field|camel_case }}": - assert re.match(r"{{ get_uuid4_re() }}", value) + assert _UUID4_RE.match(value) found_field = i break if found_field is not None: @@ -1577,7 +1577,7 @@ def test_{{ method_name }}_rest_no_http_options(): {% 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"{{ get_uuid4_re() }}", args[0].{{ auto_populated_field }}) + assert _UUID4_RE.match(args[0].{{ auto_populated_field }}) # clear UUID field so that the check below succeeds args[0].{{ auto_populated_field }} = None {% endfor %}{# for auto_populated_field in method_settings.auto_populated_fields #} diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py index b3f7dc9a9e68..0bac80f6f54f 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py @@ -15,6 +15,7 @@ # import os import re +_UUID4_RE = re.compile(r"[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}") from unittest import mock from unittest.mock import AsyncMock @@ -1863,7 +1864,7 @@ def test_create_job_non_empty_request_with_auto_populated_field(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None assert args[0] == storage_batch_operations.CreateJobRequest( @@ -2215,7 +2216,7 @@ def test_delete_job_non_empty_request_with_auto_populated_field(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None assert args[0] == storage_batch_operations.DeleteJobRequest( @@ -2533,7 +2534,7 @@ def test_cancel_job_non_empty_request_with_auto_populated_field(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None assert args[0] == storage_batch_operations.CancelJobRequest( @@ -4139,7 +4140,7 @@ def test_create_job_rest_required_fields(request_type=storage_batch_operations.C found_field = None for i, (key, value) in enumerate(req.call_args.kwargs['params']): if key == "requestId": - 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) + assert _UUID4_RE.match(value) found_field = i break if found_field is not None: @@ -4318,7 +4319,7 @@ def test_delete_job_rest_required_fields(request_type=storage_batch_operations.D found_field = None for i, (key, value) in enumerate(req.call_args.kwargs['params']): if key == "requestId": - 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) + assert _UUID4_RE.match(value) found_field = i break if found_field is not None: @@ -4495,7 +4496,7 @@ def test_cancel_job_rest_required_fields(request_type=storage_batch_operations.C found_field = None for i, (key, value) in enumerate(req.call_args.kwargs['params']): if key == "requestId": - 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) + assert _UUID4_RE.match(value) found_field = i break if found_field is not None: @@ -5128,7 +5129,7 @@ def test_create_job_empty_call_grpc(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None request_msg = storage_batch_operations.CreateJobRequest() @@ -5155,7 +5156,7 @@ def test_delete_job_empty_call_grpc(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None request_msg = storage_batch_operations.DeleteJobRequest() @@ -5182,7 +5183,7 @@ def test_cancel_job_empty_call_grpc(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None request_msg = storage_batch_operations.CancelJobRequest() @@ -5333,7 +5334,7 @@ async def test_create_job_empty_call_grpc_asyncio(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None request_msg = storage_batch_operations.CreateJobRequest() @@ -5362,7 +5363,7 @@ async def test_delete_job_empty_call_grpc_asyncio(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None request_msg = storage_batch_operations.DeleteJobRequest() @@ -5392,7 +5393,7 @@ async def test_cancel_job_empty_call_grpc_asyncio(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None request_msg = storage_batch_operations.CancelJobRequest() @@ -6650,7 +6651,7 @@ def test_create_job_empty_call_rest(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None request_msg = storage_batch_operations.CreateJobRequest() @@ -6676,7 +6677,7 @@ def test_delete_job_empty_call_rest(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None request_msg = storage_batch_operations.DeleteJobRequest() @@ -6702,7 +6703,7 @@ def test_cancel_job_empty_call_rest(): call.assert_called() _, args, _ = call.mock_calls[0] # 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].request_id) + assert _UUID4_RE.match(args[0].request_id) # clear UUID field so that the check below succeeds args[0].request_id = None request_msg = storage_batch_operations.CancelJobRequest()