1414# limitations under the License.
1515
1616import re
17- from unittest import mock
1817
1918import pytest
2019
@@ -43,33 +42,6 @@ def HasField(self, key):
4342 raise ValueError ("Mismatched field" )
4443
4544
46- class MockProtoPlusRequest :
47- def __init__ (self , pb_has_field = False , pb_raises = False , request_id = None ):
48- if request_id is not None :
49- self .request_id = request_id
50-
51- if pb_raises :
52-
53- class FailingPb :
54- def HasField (self , key ):
55- raise AttributeError ("No HasField on _pb" )
56-
57- self ._pb = FailingPb ()
58- else :
59-
60- class MockPb :
61- def __init__ (self , has_field ):
62- self ._has_field = has_field
63-
64- def HasField (self , key ):
65- return self ._has_field
66-
67- self ._pb = MockPb (pb_has_field )
68-
69- def HasField (self , key ):
70- raise AttributeError ("Proto-plus object HasField fails" )
71-
72-
7345# --- Parameterized Test ---
7446
7547UUID_REGEX = r"[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}"
@@ -89,14 +61,6 @@ def HasField(self, key):
8961 (MockProtoRequest (request_id = "already_set" ), True , "already_set" ),
9062 # ValueError case
9163 (MockValueErrorRequest (), True , "uuid" ),
92- # ProtoPlus _pb cases
93- (MockProtoPlusRequest (pb_has_field = False ), True , "uuid" ),
94- (
95- MockProtoPlusRequest (pb_has_field = True , request_id = "already_set" ),
96- True ,
97- "already_set" ,
98- ),
99- (MockProtoPlusRequest (pb_raises = True ), True , "uuid" ),
10064 # Dict cases
10165 ({}, True , "uuid" ),
10266 ({"request_id" : None }, True , "uuid" ),
@@ -117,9 +81,6 @@ def HasField(self, key):
11781 "proto3_optional_not_in_request_proto" ,
11882 "proto3_optional_already_in_request_proto" ,
11983 "value_error_fallback" ,
120- "proto_plus_pb_not_set" ,
121- "proto_plus_pb_already_set" ,
122- "proto_plus_pb_raises" ,
12384 "dict_proto3_optional_not_in_request" ,
12485 "dict_proto3_optional_value_none" ,
12586 "dict_proto3_optional_already_in_request" ,
@@ -152,17 +113,15 @@ def test_setup_request_id(request_obj, is_proto3_optional, expected):
152113 assert value == expected
153114
154115
155- def test_setup_request_id_assertion_strictness ():
116+ def test_setup_request_id_assertion_strictness (mocker ):
156117 # Mock uuid.uuid4 to return a UUID with trailing characters
157- with mock .patch ("uuid.uuid4" ) as mock_uuid :
158- mock_uuid .return_value .__str__ .return_value = (
159- "12345678-1234-4123-8123-123456789012-extra"
160- )
161-
162- # We expect test_setup_request_id to fail (raise AssertionError) because the UUID is invalid.
163- # If test_setup_request_id uses re.fullmatch, it will fail (raise AssertionError), so pytest.raises passes.
164- # If test_setup_request_id uses re.match, it will NOT fail, so pytest.raises fails!
165- with pytest .raises (AssertionError ):
166- test_setup_request_id (
167- MockRequest (), is_proto3_optional = True , expected = "uuid"
168- )
118+ mock_uuid = mocker .patch ("uuid.uuid4" )
119+ mock_uuid .return_value .__str__ .return_value = (
120+ "12345678-1234-4123-8123-123456789012-extra"
121+ )
122+
123+ # We expect test_setup_request_id to fail (raise AssertionError) because the UUID is invalid.
124+ # If test_setup_request_id uses re.fullmatch, it will fail (raise AssertionError), so pytest.raises passes.
125+ # If test_setup_request_id uses re.match, it will NOT fail, so pytest.raises fails!
126+ with pytest .raises (AssertionError ):
127+ test_setup_request_id (MockRequest (), is_proto3_optional = True , expected = "uuid" )
0 commit comments