From 478ecd8a21241b4253189687598b357ba282b41f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 5 May 2026 16:22:05 +0000 Subject: [PATCH 1/5] tests: fix unit test failure when auto_populated_fields is set --- .../storage_batch_operations/async_client.py | 10 +++ .../storage_batch_operations/client.py | 10 +++ .../test_storage_batch_operations.py | 79 +++++++++++++++++++ .../storagebatchoperations_v1.yaml | 10 +++ 4 files changed, 109 insertions(+) diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/async_client.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/async_client.py index fa743e28de70..3e773abb4bf8 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/async_client.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/async_client.py @@ -17,6 +17,7 @@ from collections import OrderedDict import re from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, Sequence, Tuple, Type, Union +import uuid from google.cloud.storagebatchoperations_v1 import gapic_version as package_version @@ -620,6 +621,9 @@ async def sample_create_job(): )), ) + if not request.request_id: + request.request_id = str(uuid.uuid4()) + # Validate the universe domain. self._client._validate_universe_domain() @@ -724,6 +728,9 @@ async def sample_delete_job(): )), ) + if not request.request_id: + request.request_id = str(uuid.uuid4()) + # Validate the universe domain. self._client._validate_universe_domain() @@ -824,6 +831,9 @@ async def sample_cancel_job(): )), ) + if not request.request_id: + request.request_id = str(uuid.uuid4()) + # Validate the universe domain. self._client._validate_universe_domain() diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py index 298d4b24467c..c01ac435ad6c 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py @@ -20,6 +20,7 @@ import os import re from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, Sequence, Tuple, Type, Union, cast +import uuid import warnings from google.cloud.storagebatchoperations_v1 import gapic_version as package_version @@ -1004,6 +1005,9 @@ def sample_create_job(): )), ) + if not request.request_id: + request.request_id = str(uuid.uuid4()) + # Validate the universe domain. self._validate_universe_domain() @@ -1107,6 +1111,9 @@ def sample_delete_job(): )), ) + if not request.request_id: + request.request_id = str(uuid.uuid4()) + # Validate the universe domain. self._validate_universe_domain() @@ -1206,6 +1213,9 @@ def sample_cancel_job(): )), ) + if not request.request_id: + request.request_id = str(uuid.uuid4()) + # Validate the universe domain. self._validate_universe_domain() 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 b0110ab7446d..063378afa9d3 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 @@ -14,6 +14,7 @@ # limitations under the License. # import os +import re from unittest import mock from unittest.mock import AsyncMock @@ -1813,6 +1814,10 @@ def test_create_job(request_type, transport: str = 'grpc'): # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = request_type() + if isinstance(request, dict): + request['request_id'] = "explicit value for autopopulate-able field" + else: + request.request_id = "explicit value for autopopulate-able field" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -1826,6 +1831,7 @@ def test_create_job(request_type, transport: str = 'grpc'): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] request = storage_batch_operations.CreateJobRequest() + request.request_id = "explicit value for autopopulate-able field" assert args[0] == request # Establish that the response is the type that we expect. @@ -1856,6 +1862,10 @@ def test_create_job_non_empty_request_with_auto_populated_field(): client.create_job(request=request) 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None assert args[0] == storage_batch_operations.CreateJobRequest( parent='parent_value', job_id='job_id_value', @@ -1947,6 +1957,10 @@ async def test_create_job_async(transport: str = 'grpc_asyncio', request_type=st # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = request_type() + if isinstance(request, dict): + request['request_id'] = "explicit value for autopopulate-able field" + else: + request.request_id = "explicit value for autopopulate-able field" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -1962,6 +1976,7 @@ async def test_create_job_async(transport: str = 'grpc_asyncio', request_type=st assert len(call.mock_calls) _, args, _ = call.mock_calls[0] request = storage_batch_operations.CreateJobRequest() + request.request_id = "explicit value for autopopulate-able field" assert args[0] == request # Establish that the response is the type that we expect. @@ -2152,6 +2167,10 @@ def test_delete_job(request_type, transport: str = 'grpc'): # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = request_type() + if isinstance(request, dict): + request['request_id'] = "explicit value for autopopulate-able field" + else: + request.request_id = "explicit value for autopopulate-able field" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -2165,6 +2184,7 @@ def test_delete_job(request_type, transport: str = 'grpc'): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] request = storage_batch_operations.DeleteJobRequest() + request.request_id = "explicit value for autopopulate-able field" assert args[0] == request # Establish that the response is the type that we expect. @@ -2194,6 +2214,10 @@ def test_delete_job_non_empty_request_with_auto_populated_field(): client.delete_job(request=request) 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None assert args[0] == storage_batch_operations.DeleteJobRequest( name='name_value', ) @@ -2274,6 +2298,10 @@ async def test_delete_job_async(transport: str = 'grpc_asyncio', request_type=st # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = request_type() + if isinstance(request, dict): + request['request_id'] = "explicit value for autopopulate-able field" + else: + request.request_id = "explicit value for autopopulate-able field" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -2287,6 +2315,7 @@ async def test_delete_job_async(transport: str = 'grpc_asyncio', request_type=st assert len(call.mock_calls) _, args, _ = call.mock_calls[0] request = storage_batch_operations.DeleteJobRequest() + request.request_id = "explicit value for autopopulate-able field" assert args[0] == request # Establish that the response is the type that we expect. @@ -2455,6 +2484,10 @@ def test_cancel_job(request_type, transport: str = 'grpc'): # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = request_type() + if isinstance(request, dict): + request['request_id'] = "explicit value for autopopulate-able field" + else: + request.request_id = "explicit value for autopopulate-able field" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -2469,6 +2502,7 @@ def test_cancel_job(request_type, transport: str = 'grpc'): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] request = storage_batch_operations.CancelJobRequest() + request.request_id = "explicit value for autopopulate-able field" assert args[0] == request # Establish that the response is the type that we expect. @@ -2498,6 +2532,10 @@ def test_cancel_job_non_empty_request_with_auto_populated_field(): client.cancel_job(request=request) 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None assert args[0] == storage_batch_operations.CancelJobRequest( name='name_value', ) @@ -2578,6 +2616,10 @@ async def test_cancel_job_async(transport: str = 'grpc_asyncio', request_type=st # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = request_type() + if isinstance(request, dict): + request['request_id'] = "explicit value for autopopulate-able field" + else: + request.request_id = "explicit value for autopopulate-able field" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -2592,6 +2634,7 @@ async def test_cancel_job_async(transport: str = 'grpc_asyncio', request_type=st assert len(call.mock_calls) _, args, _ = call.mock_calls[0] request = storage_batch_operations.CancelJobRequest() + request.request_id = "explicit value for autopopulate-able field" assert args[0] == request # Establish that the response is the type that we expect. @@ -5048,6 +5091,10 @@ def test_create_job_empty_call_grpc(): # Establish that the underlying stub method was called. 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None request_msg = storage_batch_operations.CreateJobRequest() assert args[0] == request_msg @@ -5071,6 +5118,10 @@ def test_delete_job_empty_call_grpc(): # Establish that the underlying stub method was called. 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None request_msg = storage_batch_operations.DeleteJobRequest() assert args[0] == request_msg @@ -5094,6 +5145,10 @@ def test_cancel_job_empty_call_grpc(): # Establish that the underlying stub method was called. 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None request_msg = storage_batch_operations.CancelJobRequest() assert args[0] == request_msg @@ -5241,6 +5296,10 @@ async def test_create_job_empty_call_grpc_asyncio(): # Establish that the underlying stub method was called. 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None request_msg = storage_batch_operations.CreateJobRequest() assert args[0] == request_msg @@ -5266,6 +5325,10 @@ async def test_delete_job_empty_call_grpc_asyncio(): # Establish that the underlying stub method was called. 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None request_msg = storage_batch_operations.DeleteJobRequest() assert args[0] == request_msg @@ -5292,6 +5355,10 @@ async def test_cancel_job_empty_call_grpc_asyncio(): # Establish that the underlying stub method was called. 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None request_msg = storage_batch_operations.CancelJobRequest() assert args[0] == request_msg @@ -6546,6 +6613,10 @@ def test_create_job_empty_call_rest(): # Establish that the underlying stub method was called. 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None request_msg = storage_batch_operations.CreateJobRequest() assert args[0] == request_msg @@ -6568,6 +6639,10 @@ def test_delete_job_empty_call_rest(): # Establish that the underlying stub method was called. 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None request_msg = storage_batch_operations.DeleteJobRequest() assert args[0] == request_msg @@ -6590,6 +6665,10 @@ def test_cancel_job_empty_call_rest(): # Establish that the underlying stub method was called. 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) + # clear UUID field so that the check below succeeds + args[0].request_id = None request_msg = storage_batch_operations.CancelJobRequest() assert args[0] == request_msg diff --git a/packages/gapic-generator/tests/integration/storagebatchoperations_v1.yaml b/packages/gapic-generator/tests/integration/storagebatchoperations_v1.yaml index 56cdc70278df..3e75ec7c3f7f 100644 --- a/packages/gapic-generator/tests/integration/storagebatchoperations_v1.yaml +++ b/packages/gapic-generator/tests/integration/storagebatchoperations_v1.yaml @@ -56,6 +56,16 @@ authentication: https://www.googleapis.com/auth/cloud-platform publishing: + method_settings: + - selector: google.cloud.storagebatchoperations.v1.StorageBatchOperations.CreateJob + auto_populated_fields: + - request_id + - selector: google.cloud.storagebatchoperations.v1.StorageBatchOperations.CancelJob + auto_populated_fields: + - request_id + - selector: google.cloud.storagebatchoperations.v1.StorageBatchOperations.DeleteJob + auto_populated_fields: + - request_id new_issue_uri: https://issuetracker.google.com/issues/new?component=815827&template=1395449 documentation_uri: https://cloud.google.com/storage/docs/batch-operations/overview api_short_name: storagebatchoperations From 69a9623af94c972c37daeb0e90622ac34c14d3b2 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 5 May 2026 16:34:20 +0000 Subject: [PATCH 2/5] fix *rest_required_fields test actual vs expected when field is auto-populated --- .../%name_%version/%sub/test_%service.py.j2 | 4 +-- .../gapic/%name_%version/%sub/test_macros.j2 | 20 ++++++++++- .../test_storage_batch_operations.py | 36 +++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) 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 cb5f9c4d77df..0399b5e331a4 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 @@ -1114,10 +1114,10 @@ def test_{{ service.client_name|snake_case }}_create_channel_credentials_file(cl {% for method in service.methods.values() if 'rest' in opts.transport %} {% if method.extended_lro %} -{{ test_macros.rest_required_tests(method, service, numeric_enums=opts.rest_numeric_enums, full_extended_lro=True) }} +{{ test_macros.rest_required_tests(api, method, service, numeric_enums=opts.rest_numeric_enums, full_extended_lro=True) }} {% endif %} -{{ test_macros.rest_required_tests(method, service, numeric_enums=opts.rest_numeric_enums) }} +{{ test_macros.rest_required_tests(api, method, service, numeric_enums=opts.rest_numeric_enums) }} {% endfor -%} {#- method in methods for rest #} 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 ff6a15014e10..c1bc8893df6f 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 @@ -1018,7 +1018,7 @@ def test_{{ method_name }}_raw_page_lro(): {% endif %}{# method.paged_result_field #}{% endwith %} {% endmacro %} -{% macro rest_required_tests(method, service, numeric_enums=False, full_extended_lro=False) %} +{% macro rest_required_tests(api, method, service, numeric_enums=False, full_extended_lro=False) %} {% 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 %} {# TODO(kbandes): remove this if condition when lro and client streaming are supported. #} {% if not method.client_streaming %} @@ -1218,6 +1218,24 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide ('$alt', 'json;enum-encoding=int') {% endif %} ] + {% with method_settings = api.all_method_settings.get(method.meta.address.proto) %} + {% 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 + # and remove it so the expected/actual comparison succeeds. + # Otherwise, the actual will differ from the expected since + # this field was automatically populated. + 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) + found_field = i + break + if found_field is not None: + del req.call_args.kwargs['params'][found_field] + {% endfor %} + {% endif %} + {% endwith %} actual_params = req.call_args.kwargs['params'] assert expected_params == actual_params 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 063378afa9d3..b3f7dc9a9e68 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 @@ -4132,6 +4132,18 @@ def test_create_job_rest_required_fields(request_type=storage_batch_operations.C "", ), ] + # Ensure that the uuid4 field is set according to AIP 4235 + # and remove it so the expected/actual comparison succeeds. + # Otherwise, the actual will differ from the expected since + # this field was automatically populated. + 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) + found_field = i + break + if found_field is not None: + del req.call_args.kwargs['params'][found_field] actual_params = req.call_args.kwargs['params'] assert expected_params == actual_params @@ -4299,6 +4311,18 @@ def test_delete_job_rest_required_fields(request_type=storage_batch_operations.D expected_params = [ ] + # Ensure that the uuid4 field is set according to AIP 4235 + # and remove it so the expected/actual comparison succeeds. + # Otherwise, the actual will differ from the expected since + # this field was automatically populated. + 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) + found_field = i + break + if found_field is not None: + del req.call_args.kwargs['params'][found_field] actual_params = req.call_args.kwargs['params'] assert expected_params == actual_params @@ -4464,6 +4488,18 @@ def test_cancel_job_rest_required_fields(request_type=storage_batch_operations.C expected_params = [ ] + # Ensure that the uuid4 field is set according to AIP 4235 + # and remove it so the expected/actual comparison succeeds. + # Otherwise, the actual will differ from the expected since + # this field was automatically populated. + 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) + found_field = i + break + if found_field is not None: + del req.call_args.kwargs['params'][found_field] actual_params = req.call_args.kwargs['params'] assert expected_params == actual_params From 9b99fa113ef359bf388db3de4a6903514f6ebc99 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Mon, 11 May 2026 16:50:20 +0000 Subject: [PATCH 3/5] address review feedback --- .../gapic/%name_%version/%sub/test_macros.j2 | 15 ++-- .../unit/gapic/asset_v1/test_asset_service.py | 46 ++++++------ .../credentials_v1/test_iam_credentials.py | 8 +- .../unit/gapic/eventarc_v1/test_eventarc.py | 74 +++++++++---------- .../unit/gapic/redis_v1/test_cloud_redis.py | 22 +++--- .../unit/gapic/redis_v1/test_cloud_redis.py | 10 +-- .../test_storage_batch_operations.py | 53 +++++++------ 7 files changed, 112 insertions(+), 116 deletions(-) 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..475f9f3d3f13 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 @@ -1222,22 +1222,21 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide {% 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 - # and remove it so the expected/actual comparison succeeds. - # Otherwise, the actual will differ from the expected since - # this field was automatically populated. - 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) - found_field = i break - if found_field is not None: - del req.call_args.kwargs['params'][found_field] + + # Include {{ auto_populated_field|camel_case }} within expected_params with value mock.ANY + expected_params = [p for p in expected_params if p[0] != "{{ auto_populated_field|camel_case }}"] + expected_params.append( + ("{{ auto_populated_field|camel_case }}", mock.ANY) + ) {% endfor %} {% endif %} {% endwith %} actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_{{ method_name }}_rest_unset_required_fields(): diff --git a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py index bf4e8882e38c..92d6246592c3 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py @@ -9196,7 +9196,7 @@ def test_export_assets_rest_required_fields(request_type=asset_service.ExportAss expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_export_assets_rest_unset_required_fields(): @@ -9310,7 +9310,7 @@ def test_list_assets_rest_required_fields(request_type=asset_service.ListAssetsR expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_assets_rest_unset_required_fields(): @@ -9540,7 +9540,7 @@ def test_batch_get_assets_history_rest_required_fields(request_type=asset_servic expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_batch_get_assets_history_rest_unset_required_fields(): @@ -9657,7 +9657,7 @@ def test_create_feed_rest_required_fields(request_type=asset_service.CreateFeedR expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_feed_rest_unset_required_fields(): @@ -9823,7 +9823,7 @@ def test_get_feed_rest_required_fields(request_type=asset_service.GetFeedRequest expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_feed_rest_unset_required_fields(): @@ -9989,7 +9989,7 @@ def test_list_feeds_rest_required_fields(request_type=asset_service.ListFeedsReq expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_feeds_rest_unset_required_fields(): @@ -10151,7 +10151,7 @@ def test_update_feed_rest_required_fields(request_type=asset_service.UpdateFeedR expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_update_feed_rest_unset_required_fields(): @@ -10314,7 +10314,7 @@ def test_delete_feed_rest_required_fields(request_type=asset_service.DeleteFeedR expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_feed_rest_unset_required_fields(): @@ -10480,7 +10480,7 @@ def test_search_all_resources_rest_required_fields(request_type=asset_service.Se expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_search_all_resources_rest_unset_required_fields(): @@ -10714,7 +10714,7 @@ def test_search_all_iam_policies_rest_required_fields(request_type=asset_service expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_search_all_iam_policies_rest_unset_required_fields(): @@ -10941,7 +10941,7 @@ def test_analyze_iam_policy_rest_required_fields(request_type=asset_service.Anal expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_analyze_iam_policy_rest_unset_required_fields(): @@ -11050,7 +11050,7 @@ def test_analyze_iam_policy_longrunning_rest_required_fields(request_type=asset_ expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_analyze_iam_policy_longrunning_rest_unset_required_fields(): @@ -11175,7 +11175,7 @@ def test_analyze_move_rest_required_fields(request_type=asset_service.AnalyzeMov ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_analyze_move_rest_unset_required_fields(): @@ -11288,7 +11288,7 @@ def test_query_assets_rest_required_fields(request_type=asset_service.QueryAsset expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_query_assets_rest_unset_required_fields(): @@ -11414,7 +11414,7 @@ def test_create_saved_query_rest_required_fields(request_type=asset_service.Crea ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_saved_query_rest_unset_required_fields(): @@ -11584,7 +11584,7 @@ def test_get_saved_query_rest_required_fields(request_type=asset_service.GetSave expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_saved_query_rest_unset_required_fields(): @@ -11752,7 +11752,7 @@ def test_list_saved_queries_rest_required_fields(request_type=asset_service.List expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_saved_queries_rest_unset_required_fields(): @@ -11978,7 +11978,7 @@ def test_update_saved_query_rest_required_fields(request_type=asset_service.Upda expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_update_saved_query_rest_unset_required_fields(): @@ -12143,7 +12143,7 @@ def test_delete_saved_query_rest_required_fields(request_type=asset_service.Dele expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_saved_query_rest_unset_required_fields(): @@ -12320,7 +12320,7 @@ def test_batch_get_effective_iam_policies_rest_required_fields(request_type=asse ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_batch_get_effective_iam_policies_rest_unset_required_fields(): @@ -12445,7 +12445,7 @@ def test_analyze_org_policies_rest_required_fields(request_type=asset_service.An ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_analyze_org_policies_rest_unset_required_fields(): @@ -12690,7 +12690,7 @@ def test_analyze_org_policy_governed_containers_rest_required_fields(request_typ ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_analyze_org_policy_governed_containers_rest_unset_required_fields(): @@ -12935,7 +12935,7 @@ def test_analyze_org_policy_governed_assets_rest_required_fields(request_type=as ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_analyze_org_policy_governed_assets_rest_unset_required_fields(): diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py index f890571724a9..1471eeb32a38 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py @@ -2405,7 +2405,7 @@ def test_generate_access_token_rest_required_fields(request_type=common.Generate expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_generate_access_token_rest_unset_required_fields(): @@ -2582,7 +2582,7 @@ def test_generate_id_token_rest_required_fields(request_type=common.GenerateIdTo expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_generate_id_token_rest_unset_required_fields(): @@ -2759,7 +2759,7 @@ def test_sign_blob_rest_required_fields(request_type=common.SignBlobRequest): expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_sign_blob_rest_unset_required_fields(): @@ -2934,7 +2934,7 @@ def test_sign_jwt_rest_required_fields(request_type=common.SignJwtRequest): expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_sign_jwt_rest_unset_required_fields(): diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py index 82607adb3511..deb7cf124a69 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py @@ -15561,7 +15561,7 @@ def test_get_trigger_rest_required_fields(request_type=eventarc.GetTriggerReques expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_trigger_rest_unset_required_fields(): @@ -15729,7 +15729,7 @@ def test_list_triggers_rest_required_fields(request_type=eventarc.ListTriggersRe expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_triggers_rest_unset_required_fields(): @@ -15972,7 +15972,7 @@ def test_create_trigger_rest_required_fields(request_type=eventarc.CreateTrigger ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_trigger_rest_unset_required_fields(): @@ -16237,7 +16237,7 @@ def test_delete_trigger_rest_required_fields(request_type=eventarc.DeleteTrigger expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_trigger_rest_unset_required_fields(): @@ -16403,7 +16403,7 @@ def test_get_channel_rest_required_fields(request_type=eventarc.GetChannelReques expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_channel_rest_unset_required_fields(): @@ -16571,7 +16571,7 @@ def test_list_channels_rest_required_fields(request_type=eventarc.ListChannelsRe expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_channels_rest_unset_required_fields(): @@ -16814,7 +16814,7 @@ def test_create_channel_rest_required_fields(request_type=eventarc.CreateChannel ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_channel_rest_unset_required_fields(): @@ -17077,7 +17077,7 @@ def test_delete_channel_rest_required_fields(request_type=eventarc.DeleteChannel expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_channel_rest_unset_required_fields(): @@ -17241,7 +17241,7 @@ def test_get_provider_rest_required_fields(request_type=eventarc.GetProviderRequ expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_provider_rest_unset_required_fields(): @@ -17409,7 +17409,7 @@ def test_list_providers_rest_required_fields(request_type=eventarc.ListProviders expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_providers_rest_unset_required_fields(): @@ -17637,7 +17637,7 @@ def test_get_channel_connection_rest_required_fields(request_type=eventarc.GetCh expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_channel_connection_rest_unset_required_fields(): @@ -17805,7 +17805,7 @@ def test_list_channel_connections_rest_required_fields(request_type=eventarc.Lis expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_channel_connections_rest_unset_required_fields(): @@ -18048,7 +18048,7 @@ def test_create_channel_connection_rest_required_fields(request_type=eventarc.Cr ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_channel_connection_rest_unset_required_fields(): @@ -18217,7 +18217,7 @@ def test_delete_channel_connection_rest_required_fields(request_type=eventarc.De expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_channel_connection_rest_unset_required_fields(): @@ -18381,7 +18381,7 @@ def test_get_google_channel_config_rest_required_fields(request_type=eventarc.Ge expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_google_channel_config_rest_unset_required_fields(): @@ -18545,7 +18545,7 @@ def test_update_google_channel_config_rest_required_fields(request_type=eventarc expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_update_google_channel_config_rest_unset_required_fields(): @@ -18713,7 +18713,7 @@ def test_get_message_bus_rest_required_fields(request_type=eventarc.GetMessageBu expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_message_bus_rest_unset_required_fields(): @@ -18881,7 +18881,7 @@ def test_list_message_buses_rest_required_fields(request_type=eventarc.ListMessa expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_message_buses_rest_unset_required_fields(): @@ -19111,7 +19111,7 @@ def test_list_message_bus_enrollments_rest_required_fields(request_type=eventarc expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_message_bus_enrollments_rest_unset_required_fields(): @@ -19354,7 +19354,7 @@ def test_create_message_bus_rest_required_fields(request_type=eventarc.CreateMes ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_message_bus_rest_unset_required_fields(): @@ -19521,7 +19521,7 @@ def test_update_message_bus_rest_required_fields(request_type=eventarc.UpdateMes expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_update_message_bus_rest_unset_required_fields(): @@ -19690,7 +19690,7 @@ def test_delete_message_bus_rest_required_fields(request_type=eventarc.DeleteMes expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_message_bus_rest_unset_required_fields(): @@ -19856,7 +19856,7 @@ def test_get_enrollment_rest_required_fields(request_type=eventarc.GetEnrollment expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_enrollment_rest_unset_required_fields(): @@ -20024,7 +20024,7 @@ def test_list_enrollments_rest_required_fields(request_type=eventarc.ListEnrollm expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_enrollments_rest_unset_required_fields(): @@ -20267,7 +20267,7 @@ def test_create_enrollment_rest_required_fields(request_type=eventarc.CreateEnro ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_enrollment_rest_unset_required_fields(): @@ -20434,7 +20434,7 @@ def test_update_enrollment_rest_required_fields(request_type=eventarc.UpdateEnro expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_update_enrollment_rest_unset_required_fields(): @@ -20603,7 +20603,7 @@ def test_delete_enrollment_rest_required_fields(request_type=eventarc.DeleteEnro expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_enrollment_rest_unset_required_fields(): @@ -20769,7 +20769,7 @@ def test_get_pipeline_rest_required_fields(request_type=eventarc.GetPipelineRequ expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_pipeline_rest_unset_required_fields(): @@ -20937,7 +20937,7 @@ def test_list_pipelines_rest_required_fields(request_type=eventarc.ListPipelines expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_pipelines_rest_unset_required_fields(): @@ -21180,7 +21180,7 @@ def test_create_pipeline_rest_required_fields(request_type=eventarc.CreatePipeli ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_pipeline_rest_unset_required_fields(): @@ -21347,7 +21347,7 @@ def test_update_pipeline_rest_required_fields(request_type=eventarc.UpdatePipeli expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_update_pipeline_rest_unset_required_fields(): @@ -21516,7 +21516,7 @@ def test_delete_pipeline_rest_required_fields(request_type=eventarc.DeletePipeli expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_pipeline_rest_unset_required_fields(): @@ -21682,7 +21682,7 @@ def test_get_google_api_source_rest_required_fields(request_type=eventarc.GetGoo expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_google_api_source_rest_unset_required_fields(): @@ -21850,7 +21850,7 @@ def test_list_google_api_sources_rest_required_fields(request_type=eventarc.List expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_google_api_sources_rest_unset_required_fields(): @@ -22093,7 +22093,7 @@ def test_create_google_api_source_rest_required_fields(request_type=eventarc.Cre ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_google_api_source_rest_unset_required_fields(): @@ -22260,7 +22260,7 @@ def test_update_google_api_source_rest_required_fields(request_type=eventarc.Upd expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_update_google_api_source_rest_unset_required_fields(): @@ -22429,7 +22429,7 @@ def test_delete_google_api_source_rest_required_fields(request_type=eventarc.Del expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_google_api_source_rest_unset_required_fields(): diff --git a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py index 1856ce1728f0..725e06e6e2ab 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -4931,7 +4931,7 @@ def test_list_instances_rest_required_fields(request_type=cloud_redis.ListInstan expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_instances_rest_unset_required_fields(): @@ -5159,7 +5159,7 @@ def test_get_instance_rest_required_fields(request_type=cloud_redis.GetInstanceR expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_instance_rest_unset_required_fields(): @@ -5325,7 +5325,7 @@ def test_get_instance_auth_string_rest_required_fields(request_type=cloud_redis. expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_instance_auth_string_rest_unset_required_fields(): @@ -5506,7 +5506,7 @@ def test_create_instance_rest_required_fields(request_type=cloud_redis.CreateIns ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_instance_rest_unset_required_fields(): @@ -5673,7 +5673,7 @@ def test_update_instance_rest_required_fields(request_type=cloud_redis.UpdateIns expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_update_instance_rest_unset_required_fields(): @@ -5845,7 +5845,7 @@ def test_upgrade_instance_rest_required_fields(request_type=cloud_redis.UpgradeI expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_upgrade_instance_rest_unset_required_fields(): @@ -6013,7 +6013,7 @@ def test_import_instance_rest_required_fields(request_type=cloud_redis.ImportIns expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_import_instance_rest_unset_required_fields(): @@ -6181,7 +6181,7 @@ def test_export_instance_rest_required_fields(request_type=cloud_redis.ExportIns expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_export_instance_rest_unset_required_fields(): @@ -6349,7 +6349,7 @@ def test_failover_instance_rest_required_fields(request_type=cloud_redis.Failove expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_failover_instance_rest_unset_required_fields(): @@ -6516,7 +6516,7 @@ def test_delete_instance_rest_required_fields(request_type=cloud_redis.DeleteIns expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_instance_rest_unset_required_fields(): @@ -6682,7 +6682,7 @@ def test_reschedule_maintenance_rest_required_fields(request_type=cloud_redis.Re expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_reschedule_maintenance_rest_unset_required_fields(): diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py index 0ae3649f7fda..9a0875ed1764 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -2979,7 +2979,7 @@ def test_list_instances_rest_required_fields(request_type=cloud_redis.ListInstan expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_instances_rest_unset_required_fields(): @@ -3207,7 +3207,7 @@ def test_get_instance_rest_required_fields(request_type=cloud_redis.GetInstanceR expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_instance_rest_unset_required_fields(): @@ -3388,7 +3388,7 @@ def test_create_instance_rest_required_fields(request_type=cloud_redis.CreateIns ), ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_instance_rest_unset_required_fields(): @@ -3555,7 +3555,7 @@ def test_update_instance_rest_required_fields(request_type=cloud_redis.UpdateIns expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_update_instance_rest_unset_required_fields(): @@ -3722,7 +3722,7 @@ def test_delete_instance_rest_required_fields(request_type=cloud_redis.DeleteIns expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_instance_rest_unset_required_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..8bd77279fea4 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 @@ -3724,7 +3724,7 @@ def test_list_jobs_rest_required_fields(request_type=storage_batch_operations.Li expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_jobs_rest_unset_required_fields(): @@ -3952,7 +3952,7 @@ def test_get_job_rest_required_fields(request_type=storage_batch_operations.GetJ expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_job_rest_unset_required_fields(): @@ -4133,19 +4133,18 @@ def test_create_job_rest_required_fields(request_type=storage_batch_operations.C ), ] # Ensure that the uuid4 field is set according to AIP 4235 - # and remove it so the expected/actual comparison succeeds. - # Otherwise, the actual will differ from the expected since - # this field was automatically populated. - 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) - found_field = i break - if found_field is not None: - del req.call_args.kwargs['params'][found_field] + + # Include requestId within expected_params with value mock.ANY + expected_params = [p for p in expected_params if p[0] != "requestId"] + expected_params.append( + ("requestId", mock.ANY) + ) actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_create_job_rest_unset_required_fields(): @@ -4312,19 +4311,18 @@ def test_delete_job_rest_required_fields(request_type=storage_batch_operations.D expected_params = [ ] # Ensure that the uuid4 field is set according to AIP 4235 - # and remove it so the expected/actual comparison succeeds. - # Otherwise, the actual will differ from the expected since - # this field was automatically populated. - 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) - found_field = i break - if found_field is not None: - del req.call_args.kwargs['params'][found_field] + + # Include requestId within expected_params with value mock.ANY + expected_params = [p for p in expected_params if p[0] != "requestId"] + expected_params.append( + ("requestId", mock.ANY) + ) actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_delete_job_rest_unset_required_fields(): @@ -4489,19 +4487,18 @@ def test_cancel_job_rest_required_fields(request_type=storage_batch_operations.C expected_params = [ ] # Ensure that the uuid4 field is set according to AIP 4235 - # and remove it so the expected/actual comparison succeeds. - # Otherwise, the actual will differ from the expected since - # this field was automatically populated. - 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) - found_field = i break - if found_field is not None: - del req.call_args.kwargs['params'][found_field] + + # Include requestId within expected_params with value mock.ANY + expected_params = [p for p in expected_params if p[0] != "requestId"] + expected_params.append( + ("requestId", mock.ANY) + ) actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_cancel_job_rest_unset_required_fields(): @@ -4669,7 +4666,7 @@ def test_list_bucket_operations_rest_required_fields(request_type=storage_batch_ expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_list_bucket_operations_rest_unset_required_fields(): @@ -4897,7 +4894,7 @@ def test_get_bucket_operation_rest_required_fields(request_type=storage_batch_op expected_params = [ ] actual_params = req.call_args.kwargs['params'] - assert expected_params == actual_params + assert sorted(expected_params) == sorted(actual_params) def test_get_bucket_operation_rest_unset_required_fields(): From 69b6a0afd13afc5e211fc10eb0bfd2adffcfa40c Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 8 May 2026 16:55:15 +0000 Subject: [PATCH 4/5] 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 475f9f3d3f13..dabfc6b50217 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 %} @@ -1224,7 +1224,7 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide # Ensure that the uuid4 field is set according to AIP 4235 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) break # Include {{ auto_populated_field|camel_case }} within expected_params with value mock.ANY @@ -1576,7 +1576,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 8bd77279fea4..25521153ff52 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( @@ -4135,7 +4136,7 @@ def test_create_job_rest_required_fields(request_type=storage_batch_operations.C # Ensure that the uuid4 field is set according to AIP 4235 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) break # Include requestId within expected_params with value mock.ANY @@ -4313,7 +4314,7 @@ def test_delete_job_rest_required_fields(request_type=storage_batch_operations.D # Ensure that the uuid4 field is set according to AIP 4235 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) break # Include requestId within expected_params with value mock.ANY @@ -4489,7 +4490,7 @@ def test_cancel_job_rest_required_fields(request_type=storage_batch_operations.C # Ensure that the uuid4 field is set according to AIP 4235 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) break # Include requestId within expected_params with value mock.ANY @@ -5125,7 +5126,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() @@ -5152,7 +5153,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() @@ -5179,7 +5180,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() @@ -5330,7 +5331,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() @@ -5359,7 +5360,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() @@ -5389,7 +5390,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() @@ -6647,7 +6648,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() @@ -6673,7 +6674,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() @@ -6699,7 +6700,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() From 5f6c5ee41a64c8e72a633f2e25fdaa9500cb959f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 8 May 2026 21:07:26 +0000 Subject: [PATCH 5/5] wip --- .../%name_%version/%sub/test_%service.py.j2 | 2 +- .../gapic-generator/gapic/generator/generator.py | 3 +++ .../%name_%version/%sub/test_%service.py.j2 | 2 +- .../gapic/%name_%version/%sub/test_macros.j2 | 2 +- packages/gapic-generator/gapic/utils/__init__.py | 2 ++ .../gapic-generator/gapic/utils/constants.py | 16 ++++++++++++++++ 6 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 packages/gapic-generator/gapic/utils/constants.py 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 d86e53650218..091525b19f41 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,7 +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}") +_UUID4_RE = re.compile(r"{{ uuid4_re }}") {% endif %} from unittest import mock from unittest.mock import AsyncMock diff --git a/packages/gapic-generator/gapic/generator/generator.py b/packages/gapic-generator/gapic/generator/generator.py index e9e006e74884..9fe56aa9de1d 100644 --- a/packages/gapic-generator/gapic/generator/generator.py +++ b/packages/gapic-generator/gapic/generator/generator.py @@ -75,6 +75,9 @@ def __init__(self, opts: Options) -> None: self._env.tests["str_field_pb"] = utils.is_str_field_pb self._env.tests["msg_field_pb"] = utils.is_msg_field_pb + # Add global variables. + self._env.globals["uuid4_re"] = utils.UUID4_RE + self._sample_configs = opts.sample_configs def get_response(self, api_schema: api.API, opts: Options) -> CodeGeneratorResponse: 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 85ac7484cfdc..16469f365877 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,7 +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() }}") +_UUID4_RE = re.compile(r"{{ 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 dabfc6b50217..12527e8014dc 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 @@ -2213,7 +2213,7 @@ def test_initialize_client_w_{{transport_name}}(): {% endmacro %}{# empty_call_test #} {% macro get_uuid4_re() -%} -[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12} +{{ uuid4_re }} {%- endmacro %}{# uuid_re #} {% macro routing_parameter_test(service, api, transport, is_async) %} diff --git a/packages/gapic-generator/gapic/utils/__init__.py b/packages/gapic-generator/gapic/utils/__init__.py index 23c573915695..18d78c31dd1e 100644 --- a/packages/gapic-generator/gapic/utils/__init__.py +++ b/packages/gapic-generator/gapic/utils/__init__.py @@ -22,6 +22,7 @@ from gapic.utils.code import nth from gapic.utils.code import partition from gapic.utils.code import make_private +from gapic.utils.constants import UUID4_RE from gapic.utils.doc import doc from gapic.utils.filename import to_valid_filename from gapic.utils.filename import to_valid_module_name @@ -52,5 +53,6 @@ "to_camel_case", "to_valid_filename", "to_valid_module_name", + "UUID4_RE", "wrap", ) diff --git a/packages/gapic-generator/gapic/utils/constants.py b/packages/gapic-generator/gapic/utils/constants.py new file mode 100644 index 000000000000..e21f7cd2170f --- /dev/null +++ b/packages/gapic-generator/gapic/utils/constants.py @@ -0,0 +1,16 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# The regex for a UUID4 as specified in AIP-4235. +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}"