Skip to content

Commit 8f55f89

Browse files
authored
test(api-core): use re.fullmatch for strict UUID validation (#17797)
### Description This PR addresses code review feedback regarding test assertion strictness: - Changed the regex assertion from `re.match` to `re.fullmatch` for strict UUID validation in `test_requests.py`, ensuring trailing characters are not accepted.
1 parent d272d50 commit 8f55f89

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

packages/google-api-core/tests/unit/gapic/test_requests.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ def test_setup_request_id(request_obj, is_proto3_optional, expected):
104104
)
105105

106106
if expected == "uuid":
107-
assert re.match(UUID_REGEX, value)
107+
assert re.fullmatch(UUID_REGEX, value)
108108
else:
109109
assert value == expected
110+
111+
112+
def test_setup_request_id_assertion_strictness(mocker):
113+
# Mock uuid.uuid4 to return a UUID with trailing characters
114+
mock_uuid = mocker.patch("uuid.uuid4")
115+
mock_uuid.return_value.__str__.return_value = (
116+
"12345678-1234-4123-8123-123456789012-extra"
117+
)
118+
119+
# We expect test_setup_request_id to fail (raise AssertionError) because the UUID is invalid.
120+
# If test_setup_request_id uses re.fullmatch, it will fail (raise AssertionError), so pytest.raises passes.
121+
# If test_setup_request_id uses re.match, it will NOT fail, so pytest.raises fails!
122+
with pytest.raises(AssertionError):
123+
test_setup_request_id(MockRequest(), is_proto3_optional=True, expected="uuid")

0 commit comments

Comments
 (0)