Skip to content

Commit 55ad6a7

Browse files
committed
chore: refactor templates to reduce duplication
1 parent 69a9623 commit 55ad6a7

4 files changed

Lines changed: 25 additions & 22 deletions

File tree

packages/gapic-generator/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|list %}
88
import re
9+
_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}")
910
{% endif %}
1011
from unittest import mock
1112
from unittest.mock import AsyncMock
@@ -71,7 +72,7 @@ from google.iam.v1 import policy_pb2 # type: ignore
7172
{{ shared_macros.add_google_api_core_version_header_import(service.version) }}
7273

7374

74-
{% 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}" %}
75+
7576

7677
def client_cert_source_callback():
7778
return b"cert bytes", b"key bytes"
@@ -692,7 +693,7 @@ def test_{{ method_name }}_empty_call():
692693
{% if method_settings is not none %}
693694
{% for auto_populated_field in method_settings.auto_populated_fields %}
694695
# Ensure that the uuid4 field is set according to AIP 4235
695-
assert re.match(r"{{ uuid4_re }}", args[0].{{ auto_populated_field }})
696+
assert _UUID4_RE.match(args[0].{{ auto_populated_field }})
696697
# clear UUID field so that the check below succeeds
697698
args[0].{{ auto_populated_field }} = None
698699
{% endfor %}
@@ -730,7 +731,7 @@ def test_{{ method_name }}_non_empty_request_with_auto_populated_field():
730731
{% if method_settings is not none %}
731732
{% for auto_populated_field in method_settings.auto_populated_fields %}
732733
# Ensure that the uuid4 field is set according to AIP 4235
733-
assert re.match(r"{{ uuid4_re }}", args[0].{{ auto_populated_field }})
734+
assert _UUID4_RE.match(args[0].{{ auto_populated_field }})
734735
# clear UUID field so that the check below succeeds
735736
args[0].{{ auto_populated_field }} = None
736737
{% endfor %}
@@ -2488,5 +2489,5 @@ def test_client_ctx():
24882489
pass
24892490
close.assert_called()
24902491

2491-
{% endwith %}{# uuid4_re #}
2492+
24922493
{% endblock %}

packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|list %}
1111
import re
12+
_UUID4_RE = re.compile(r"{{ test_macros.get_uuid4_re() }}")
1213
{% endif %}
1314
from unittest import mock
1415
from unittest.mock import AsyncMock

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def test_{{ method_name }}_non_empty_request_with_auto_populated_field():
151151
{% if method_settings is not none %}
152152
{% for auto_populated_field in method_settings.auto_populated_fields %}
153153
# Ensure that the uuid4 field is set according to AIP 4235
154-
assert re.match(r"{{ get_uuid4_re() }}", args[0].{{ auto_populated_field }})
154+
assert _UUID4_RE.match(args[0].{{ auto_populated_field }})
155155
# clear UUID field so that the check below succeeds
156156
args[0].{{ auto_populated_field }} = None
157157
{% endfor %}
@@ -1228,7 +1228,7 @@ def test_{{ method_name }}_rest_required_fields(request_type={{ method.input.ide
12281228
found_field = None
12291229
for i, (key, value) in enumerate(req.call_args.kwargs['params']):
12301230
if key == "{{ auto_populated_field|camel_case }}":
1231-
assert re.match(r"{{ get_uuid4_re() }}", value)
1231+
assert _UUID4_RE.match(value)
12321232
found_field = i
12331233
break
12341234
if found_field is not None:
@@ -1577,7 +1577,7 @@ def test_{{ method_name }}_rest_no_http_options():
15771577
{% if method_settings is not none %}
15781578
{% for auto_populated_field in method_settings.auto_populated_fields %}
15791579
# Ensure that the uuid4 field is set according to AIP 4235
1580-
assert re.match(r"{{ get_uuid4_re() }}", args[0].{{ auto_populated_field }})
1580+
assert _UUID4_RE.match(args[0].{{ auto_populated_field }})
15811581
# clear UUID field so that the check below succeeds
15821582
args[0].{{ auto_populated_field }} = None
15831583
{% endfor %}{# for auto_populated_field in method_settings.auto_populated_fields #}

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616
import os
1717
import re
18+
_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}")
1819
from unittest import mock
1920
from unittest.mock import AsyncMock
2021

@@ -1863,7 +1864,7 @@ def test_create_job_non_empty_request_with_auto_populated_field():
18631864
call.assert_called()
18641865
_, args, _ = call.mock_calls[0]
18651866
# Ensure that the uuid4 field is set according to AIP 4235
1866-
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)
1867+
assert _UUID4_RE.match(args[0].request_id)
18671868
# clear UUID field so that the check below succeeds
18681869
args[0].request_id = None
18691870
assert args[0] == storage_batch_operations.CreateJobRequest(
@@ -2215,7 +2216,7 @@ def test_delete_job_non_empty_request_with_auto_populated_field():
22152216
call.assert_called()
22162217
_, args, _ = call.mock_calls[0]
22172218
# Ensure that the uuid4 field is set according to AIP 4235
2218-
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)
2219+
assert _UUID4_RE.match(args[0].request_id)
22192220
# clear UUID field so that the check below succeeds
22202221
args[0].request_id = None
22212222
assert args[0] == storage_batch_operations.DeleteJobRequest(
@@ -2533,7 +2534,7 @@ def test_cancel_job_non_empty_request_with_auto_populated_field():
25332534
call.assert_called()
25342535
_, args, _ = call.mock_calls[0]
25352536
# Ensure that the uuid4 field is set according to AIP 4235
2536-
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)
2537+
assert _UUID4_RE.match(args[0].request_id)
25372538
# clear UUID field so that the check below succeeds
25382539
args[0].request_id = None
25392540
assert args[0] == storage_batch_operations.CancelJobRequest(
@@ -4139,7 +4140,7 @@ def test_create_job_rest_required_fields(request_type=storage_batch_operations.C
41394140
found_field = None
41404141
for i, (key, value) in enumerate(req.call_args.kwargs['params']):
41414142
if key == "requestId":
4142-
assert re.match(r"[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}", value)
4143+
assert _UUID4_RE.match(value)
41434144
found_field = i
41444145
break
41454146
if found_field is not None:
@@ -4318,7 +4319,7 @@ def test_delete_job_rest_required_fields(request_type=storage_batch_operations.D
43184319
found_field = None
43194320
for i, (key, value) in enumerate(req.call_args.kwargs['params']):
43204321
if key == "requestId":
4321-
assert re.match(r"[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}", value)
4322+
assert _UUID4_RE.match(value)
43224323
found_field = i
43234324
break
43244325
if found_field is not None:
@@ -4495,7 +4496,7 @@ def test_cancel_job_rest_required_fields(request_type=storage_batch_operations.C
44954496
found_field = None
44964497
for i, (key, value) in enumerate(req.call_args.kwargs['params']):
44974498
if key == "requestId":
4498-
assert re.match(r"[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}", value)
4499+
assert _UUID4_RE.match(value)
44994500
found_field = i
45004501
break
45014502
if found_field is not None:
@@ -5128,7 +5129,7 @@ def test_create_job_empty_call_grpc():
51285129
call.assert_called()
51295130
_, args, _ = call.mock_calls[0]
51305131
# Ensure that the uuid4 field is set according to AIP 4235
5131-
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)
5132+
assert _UUID4_RE.match(args[0].request_id)
51325133
# clear UUID field so that the check below succeeds
51335134
args[0].request_id = None
51345135
request_msg = storage_batch_operations.CreateJobRequest()
@@ -5155,7 +5156,7 @@ def test_delete_job_empty_call_grpc():
51555156
call.assert_called()
51565157
_, args, _ = call.mock_calls[0]
51575158
# Ensure that the uuid4 field is set according to AIP 4235
5158-
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)
5159+
assert _UUID4_RE.match(args[0].request_id)
51595160
# clear UUID field so that the check below succeeds
51605161
args[0].request_id = None
51615162
request_msg = storage_batch_operations.DeleteJobRequest()
@@ -5182,7 +5183,7 @@ def test_cancel_job_empty_call_grpc():
51825183
call.assert_called()
51835184
_, args, _ = call.mock_calls[0]
51845185
# Ensure that the uuid4 field is set according to AIP 4235
5185-
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)
5186+
assert _UUID4_RE.match(args[0].request_id)
51865187
# clear UUID field so that the check below succeeds
51875188
args[0].request_id = None
51885189
request_msg = storage_batch_operations.CancelJobRequest()
@@ -5333,7 +5334,7 @@ async def test_create_job_empty_call_grpc_asyncio():
53335334
call.assert_called()
53345335
_, args, _ = call.mock_calls[0]
53355336
# Ensure that the uuid4 field is set according to AIP 4235
5336-
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)
5337+
assert _UUID4_RE.match(args[0].request_id)
53375338
# clear UUID field so that the check below succeeds
53385339
args[0].request_id = None
53395340
request_msg = storage_batch_operations.CreateJobRequest()
@@ -5362,7 +5363,7 @@ async def test_delete_job_empty_call_grpc_asyncio():
53625363
call.assert_called()
53635364
_, args, _ = call.mock_calls[0]
53645365
# Ensure that the uuid4 field is set according to AIP 4235
5365-
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)
5366+
assert _UUID4_RE.match(args[0].request_id)
53665367
# clear UUID field so that the check below succeeds
53675368
args[0].request_id = None
53685369
request_msg = storage_batch_operations.DeleteJobRequest()
@@ -5392,7 +5393,7 @@ async def test_cancel_job_empty_call_grpc_asyncio():
53925393
call.assert_called()
53935394
_, args, _ = call.mock_calls[0]
53945395
# Ensure that the uuid4 field is set according to AIP 4235
5395-
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)
5396+
assert _UUID4_RE.match(args[0].request_id)
53965397
# clear UUID field so that the check below succeeds
53975398
args[0].request_id = None
53985399
request_msg = storage_batch_operations.CancelJobRequest()
@@ -6650,7 +6651,7 @@ def test_create_job_empty_call_rest():
66506651
call.assert_called()
66516652
_, args, _ = call.mock_calls[0]
66526653
# Ensure that the uuid4 field is set according to AIP 4235
6653-
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)
6654+
assert _UUID4_RE.match(args[0].request_id)
66546655
# clear UUID field so that the check below succeeds
66556656
args[0].request_id = None
66566657
request_msg = storage_batch_operations.CreateJobRequest()
@@ -6676,7 +6677,7 @@ def test_delete_job_empty_call_rest():
66766677
call.assert_called()
66776678
_, args, _ = call.mock_calls[0]
66786679
# Ensure that the uuid4 field is set according to AIP 4235
6679-
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)
6680+
assert _UUID4_RE.match(args[0].request_id)
66806681
# clear UUID field so that the check below succeeds
66816682
args[0].request_id = None
66826683
request_msg = storage_batch_operations.DeleteJobRequest()
@@ -6702,7 +6703,7 @@ def test_cancel_job_empty_call_rest():
67026703
call.assert_called()
67036704
_, args, _ = call.mock_calls[0]
67046705
# Ensure that the uuid4 field is set according to AIP 4235
6705-
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)
6706+
assert _UUID4_RE.match(args[0].request_id)
67066707
# clear UUID field so that the check below succeeds
67076708
args[0].request_id = None
67086709
request_msg = storage_batch_operations.CancelJobRequest()

0 commit comments

Comments
 (0)