File: test_uuid.py

package info (click to toggle)
pydantic-core 2.41.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,828 kB
  • sloc: python: 35,564; javascript: 211; makefile: 128
file content (64 lines) | stat: -rw-r--r-- 2,738 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from uuid import UUID

import pytest

from pydantic_core import SchemaSerializer, core_schema


def test_uuid():
    v = SchemaSerializer(core_schema.uuid_schema())
    assert v.to_python(UUID('12345678-1234-5678-1234-567812345678')) == UUID('12345678-1234-5678-1234-567812345678')

    assert (
        v.to_python(UUID('12345678-1234-5678-1234-567812345678'), mode='json') == '12345678-1234-5678-1234-567812345678'
    )
    assert v.to_json(UUID('12345678-1234-5678-1234-567812345678')) == b'"12345678-1234-5678-1234-567812345678"'

    with pytest.warns(
        UserWarning,
        match=r'Expected `uuid` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert v.to_python(123, mode='json') == 123

    with pytest.warns(
        UserWarning,
        match=r'Expected `uuid` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert v.to_json(123) == b'123'


def test_uuid_key():
    v = SchemaSerializer(core_schema.dict_schema(core_schema.uuid_schema(), core_schema.uuid_schema()))
    assert v.to_python(
        {UUID('12345678-1234-5678-1234-567812345678'): UUID('12345678-1234-5678-1234-567812345678')}
    ) == {UUID('12345678-1234-5678-1234-567812345678'): UUID('12345678-1234-5678-1234-567812345678')}
    assert v.to_python(
        {UUID('12345678-1234-5678-1234-567812345678'): UUID('12345678-1234-5678-1234-567812345678')}, mode='json'
    ) == {'12345678-1234-5678-1234-567812345678': '12345678-1234-5678-1234-567812345678'}
    assert (
        v.to_json({UUID('12345678-1234-5678-1234-567812345678'): UUID('12345678-1234-5678-1234-567812345678')})
        == b'{"12345678-1234-5678-1234-567812345678":"12345678-1234-5678-1234-567812345678"}'
    )


@pytest.mark.parametrize(
    'value,expected',
    [
        (UUID('12345678-1234-5678-1234-567812345678'), '12345678-1234-5678-1234-567812345678'),
        (UUID('550e8400-e29b-41d4-a716-446655440000'), '550e8400-e29b-41d4-a716-446655440000'),
        (UUID('123e4567-e89b-12d3-a456-426655440000'), '123e4567-e89b-12d3-a456-426655440000'),
        (UUID('00000000-0000-0000-0000-000000000000'), '00000000-0000-0000-0000-000000000000'),
    ],
)
def test_uuid_json(value, expected):
    v = SchemaSerializer(core_schema.uuid_schema())
    assert v.to_python(value, mode='json') == expected
    assert v.to_json(value).decode() == f'"{expected}"'


def test_any_uuid_key():
    v = SchemaSerializer(core_schema.dict_schema())
    input_value = {UUID('12345678-1234-5678-1234-567812345678'): 1}

    assert v.to_python(input_value, mode='json') == {'12345678-1234-5678-1234-567812345678': 1}
    assert v.to_json(input_value) == b'{"12345678-1234-5678-1234-567812345678":1}'