Skip to content

Commit 9f7e356

Browse files
committed
add python tests
1 parent dba435a commit 9f7e356

File tree

1 file changed

+95
-2
lines changed

1 file changed

+95
-2
lines changed

tests/validators/test_uuid.py

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
from pydantic_core import Uuid, core_schema
1+
from copy import deepcopy
2+
from typing import Dict
23

3-
from ..conftest import PyAndJson
4+
import pytest
5+
6+
from pydantic_core import SchemaValidator, Uuid, ValidationError, core_schema
7+
8+
from ..conftest import Err, PyAndJson
49

510

611
def test_uuid_ok(py_and_json: PyAndJson):
@@ -24,3 +29,91 @@ def test_uuid_from_constructor_ok():
2429
assert uuid.urn == 'urn:uuid:12345678-1234-5678-1234-567812345678'
2530
assert uuid.version == 5
2631
assert uuid.variant == 'NCS'
32+
33+
34+
@pytest.fixture(scope='module', name='uuid_validator')
35+
def uuid_validator_fixture():
36+
return SchemaValidator(core_schema.uuid_schema())
37+
38+
39+
ORDERED_TEST_CASES = [
40+
('00000000-0000-0000-0000-000000000000', 'urn:uuid:00000000-0000-0000-0000-000000000000', 'NCS', 0),
41+
('00010203-0405-0607-0809-0a0b0c0d0e0f', 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', 'NCS', 0),
42+
('02d9e6d5-9467-382e-8f9b-9300a64ac3cd', 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd', 'RFC4122', 3),
43+
('6ba7b810-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8', 'RFC4122', 1),
44+
('6ba7b811-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8', 'RFC4122', 1),
45+
('6ba7b812-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8', 'RFC4122', 1),
46+
('6ba7b814-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8', 'RFC4122', 1),
47+
('7d444840-9dc0-11d1-b245-5ffdce74fad2', 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2', 'RFC4122', 1),
48+
('e902893a-9d22-3c7e-a7b8-d6e313b71d9f', 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f', 'RFC4122', 3),
49+
('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', 'RFC4122', 4),
50+
('f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'RFC4122', 1),
51+
]
52+
53+
54+
@pytest.mark.parametrize('uuid,urn,variant,version', ORDERED_TEST_CASES)
55+
def test_uuid_cases_ok(uuid_validator, uuid, urn, variant, version):
56+
output_uuid = uuid_validator.validate_python(uuid)
57+
assert isinstance(output_uuid, Uuid)
58+
assert str(output_uuid) == uuid
59+
assert output_uuid.urn == urn
60+
assert output_uuid.version == version
61+
assert output_uuid.variant == variant
62+
63+
64+
TEST_CASES_ERR = [
65+
('', Err('invalid length: expected length 32 for simple format, found 0')),
66+
('abc', Err('invalid length: expected length 32 for simple format, found 3')),
67+
('1234567812345678123456781234567', Err('invalid length: expected length 32 for simple format, found 31')),
68+
('123456781234567812345678123456789', Err('invalid length: expected length 32 for simple format, found 33')),
69+
(
70+
'123456781234567812345678z2345678',
71+
Err('invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-fA-F-], found `z` at 25'),
72+
),
73+
]
74+
75+
76+
@pytest.mark.parametrize('uuid,expected', TEST_CASES_ERR)
77+
def test_uuid_error(uuid_validator, uuid, expected):
78+
with pytest.raises(ValidationError) as exc_info:
79+
uuid_validator.validate_python(uuid)
80+
assert exc_info.value.error_count() == 1
81+
error = exc_info.value.errors(include_url=False)[0]
82+
assert error['type'] == 'uuid_parsing'
83+
assert error['ctx']['error'] == expected.message
84+
85+
86+
def test_uuid_comparison():
87+
ascending = []
88+
for uuid, _, _, _ in ORDERED_TEST_CASES:
89+
ascending.append(Uuid(uuid))
90+
91+
for i in range(len(ascending)):
92+
for j in range(len(ascending)):
93+
assert (i < j) == (ascending[i] < ascending[j])
94+
assert (i <= j) == (ascending[i] <= ascending[j])
95+
assert (i == j) == (ascending[i] == ascending[j])
96+
assert (i > j) == (ascending[i] > ascending[j])
97+
assert (i >= j) == (ascending[i] >= ascending[j])
98+
assert (i != j) == (ascending[i] != ascending[j])
99+
100+
101+
def test_uuid_hash():
102+
data: Dict[Uuid, int] = {}
103+
104+
data[Uuid('12345678-1234-5678-1234-567812345678')] = 1
105+
assert data == {Uuid('12345678-1234-5678-1234-567812345678'): 1}
106+
107+
data[Uuid('00010203-0405-0607-0809-0a0b0c0d0e0f')] = 2
108+
assert data == {Uuid('12345678-1234-5678-1234-567812345678'): 1, Uuid('00010203-0405-0607-0809-0a0b0c0d0e0f'): 2}
109+
110+
data[Uuid('00010203-0405-0607-0809-0a0b0c0d0e0f')] = 3
111+
assert data == {Uuid('12345678-1234-5678-1234-567812345678'): 1, Uuid('00010203-0405-0607-0809-0a0b0c0d0e0f'): 3}
112+
113+
114+
def test_uuid_bool():
115+
assert bool(Uuid('12345678-1234-5678-1234-567812345678')) is True
116+
117+
118+
def test_uuid_deepcopy():
119+
assert deepcopy(Uuid('12345678-1234-5678-1234-567812345678')) == Uuid('12345678-1234-5678-1234-567812345678')

0 commit comments

Comments
 (0)