Skip to content

Commit 4cc1c93

Browse files
committed
refactor: create py uid and add tests
1 parent 72e1fb5 commit 4cc1c93

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/validators/uuid.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,8 @@ impl Validator for UuidValidator {
100100
input,
101101
))
102102
} else {
103-
let dc = create_class(class)?;
104103
let uuid = self.get_uuid(py, input)?;
105-
self.create_py_uuid(py, dc.as_ref(py), &uuid)?;
106-
Ok(dc)
104+
self.create_py_uuid(py, class, &uuid)
107105
}
108106
}
109107

@@ -157,20 +155,23 @@ impl UuidValidator {
157155
Ok(uuid)
158156
}
159157

160-
/// Sets the attributes in a Python dictionary object (`dc`) to represent a UUID.
161-
/// The function converts the UUID to a u128 integer and sets the corresponding attributes
162-
/// in the dictionary object to the converted value and a 'safe' flag.
158+
/// Sets the attributes in a Python type object (`py_type`) to represent a UUID class.
159+
/// The function creates the python class and converts the UUID to a u128 integer and
160+
/// sets the corresponding attributes in the dictionary object to the converted value
161+
/// and a 'safe' flag.
163162
///
164163
/// This implementation does not use the Python `__init__` function to speed up the process,
165164
/// as the `__init__` function in the Python `uuid` module performs extensive checks.
166-
fn create_py_uuid<'data>(&self, py: Python<'data>, dc: &PyAny, uuid: &Uuid) -> ValResult<'data, ()> {
165+
fn create_py_uuid<'py>(&self, py: Python<'py>, py_type: &PyType, uuid: &Uuid) -> ValResult<'py, Py<PyAny>> {
166+
let class = create_class(py_type)?;
167+
let dc = class.as_ref(py);
167168
let int = uuid.as_u128();
168169
let safe = py
169170
.import(intern!(py, "uuid"))?
170171
.getattr(intern!(py, "SafeUUID"))?
171172
.get_item("safe")?;
172173
force_setattr(py, dc, intern!(py, UUID_INT), int)?;
173174
force_setattr(py, dc, intern!(py, UUID_IS_SAFE), safe)?;
174-
Ok(())
175+
Ok(dc.to_object(py))
175176
}
176177
}

tests/validators/test_uuid.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
('c0a8f9a8-aa5e-482b-a067-9cb3a51f5c11', UUID('c0a8f9a8-aa5e-482b-a067-9cb3a51f5c11')),
2626
(b'\x12\x34\x56\x78' * 4, UUID('12345678-1234-5678-1234-567812345678')),
2727
(b'\x00\x00\x00\x00' * 4, UUID('00000000-0000-0000-0000-000000000000')),
28+
(UUID('12345678-1234-5678-1234-567812345678'), UUID('12345678-1234-5678-1234-567812345678')),
29+
(UUID('550e8400-e29b-41d4-a716-446655440000'), UUID('550e8400-e29b-41d4-a716-446655440000')),
2830
# Invalid UUIDs
2931
(
3032
'not-a-valid-uuid',
@@ -67,6 +69,8 @@
6769
'c0a8f9a8-aa5e-482b-a067-9cb3a51f5c1',
6870
Err('Input should be a valid UUID, invalid group length in group 4: expected 12, found 11'),
6971
),
72+
(0xA1A2A3A4B1B2C1C2D1D2D3D4D5D6D7D8, Err('UUID input should be a string, bytes or UUID object')),
73+
(00000000000000000000000000, Err('UUID input should be a string, bytes or UUID object')),
7074
],
7175
)
7276
def test_uuid(input_value, expected):
@@ -78,6 +82,7 @@ def test_uuid(input_value, expected):
7882
else:
7983
output = v.validate_python(input_value)
8084
assert output == expected
85+
assert isinstance(output, UUID)
8186

8287

8388
@pytest.mark.parametrize(
@@ -97,6 +102,7 @@ def test_uuid_strict(input_value, expected):
97102
else:
98103
output = v.validate_python(input_value)
99104
assert output == expected
105+
assert isinstance(output, UUID)
100106

101107

102108
@pytest.mark.parametrize(
@@ -122,6 +128,7 @@ def test_uuid_version(input_value, version, expected):
122128
else:
123129
output = v.validate_python(input_value)
124130
assert output == expected
131+
assert isinstance(output, UUID)
125132

126133

127134
@pytest.mark.parametrize(
@@ -148,17 +155,20 @@ def test_uuid_json(py_and_json: PyAndJson, input_value, expected):
148155
else:
149156
output = v.validate_test(input_value)
150157
assert output == expected
158+
assert isinstance(output, UUID)
151159

152160

153161
def test_uuid_deepcopy():
154162
output = SchemaValidator({'type': 'uuid'}).validate_python('a6cc5730-2261-11ee-9c43-2eb5a363657c')
155163
c = copy.deepcopy(output)
156164
assert repr(output) == "UUID('a6cc5730-2261-11ee-9c43-2eb5a363657c')"
157165
assert c == output
166+
assert isinstance(output, UUID)
158167

159168

160169
def test_uuid_copy():
161170
output = SchemaValidator({'type': 'uuid'}).validate_python('a6cc5730-2261-11ee-9c43-2eb5a363657c')
162171
c = copy.copy(output)
163172
assert repr(output) == "UUID('a6cc5730-2261-11ee-9c43-2eb5a363657c')"
164173
assert c == output
174+
assert isinstance(output, UUID)

0 commit comments

Comments
 (0)