Skip to content

Commit

Permalink
🐛 Fix handling of UUID values having UUID.version=None (#7566)
Browse files Browse the repository at this point in the history
  • Loading branch information
lig committed Sep 22, 2023
1 parent 416ab7f commit 56df0cf
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions tests/test_types.py
Expand Up @@ -2758,17 +2758,19 @@ class UUIDModel(BaseModel):
b: UUID3
c: UUID4
d: UUID5
e: UUID

a = uuid.uuid1()
b = uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
c = uuid.uuid4()
d = uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
e = UUID('{00000000-7fff-4000-7fff-000000000000}')

m = UUIDModel(a=a, b=b, c=c, d=d)
assert m.model_dump() == {'a': a, 'b': b, 'c': c, 'd': d}
m = UUIDModel(a=a, b=b, c=c, d=d, e=e)
assert m.model_dump() == {'a': a, 'b': b, 'c': c, 'd': d, 'e': e}

with pytest.raises(ValidationError) as exc_info:
UUIDModel(a=d, b=c, c=b, d=a)
UUIDModel(a=d, b=c, c=b, d=a, e=e)
# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.errors(include_url=False) == [
{
Expand Down Expand Up @@ -2801,6 +2803,39 @@ class UUIDModel(BaseModel):
},
]

with pytest.raises(ValidationError) as exc_info:
UUIDModel(a=e, b=e, c=e, d=e, e=e)
assert exc_info.value.errors(include_url=False) == [
{
'type': 'uuid_version',
'loc': ('a',),
'msg': 'UUID version 1 expected',
'input': e,
'ctx': {'expected_version': 1},
},
{
'type': 'uuid_version',
'loc': ('b',),
'msg': 'UUID version 3 expected',
'input': e,
'ctx': {'expected_version': 3},
},
{
'type': 'uuid_version',
'loc': ('c',),
'msg': 'UUID version 4 expected',
'input': e,
'ctx': {'expected_version': 4},
},
{
'type': 'uuid_version',
'loc': ('d',),
'msg': 'UUID version 5 expected',
'input': e,
'ctx': {'expected_version': 5},
},
]


def test_uuid_strict() -> None:
class UUIDModel(BaseModel):
Expand Down

0 comments on commit 56df0cf

Please sign in to comment.