File: test_enum.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 (135 lines) | stat: -rw-r--r-- 4,127 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from enum import Enum

import pytest

from pydantic_core import SchemaSerializer, core_schema


def test_plain_enum():
    class MyEnum(Enum):
        a = 1
        b = 2

    v = SchemaSerializer(core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())))

    # debug(v)
    assert v.to_python(MyEnum.a) is MyEnum.a
    assert v.to_python(MyEnum.a, mode='json') == 1
    assert v.to_json(MyEnum.a) == b'1'

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


def test_int_enum():
    class MyEnum(int, Enum):
        a = 1
        b = 2

    v = SchemaSerializer(core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values()), sub_type='int'))

    # debug(v)
    assert v.to_python(MyEnum.a) is MyEnum.a
    assert v.to_python(MyEnum.a, mode='json') == 1
    assert v.to_json(MyEnum.a) == b'1'

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


def test_str_enum():
    class MyEnum(str, Enum):
        a = 'a'
        b = 'b'

    v = SchemaSerializer(core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values()), sub_type='str'))

    # debug(v)
    assert v.to_python(MyEnum.a) is MyEnum.a
    assert v.to_python(MyEnum.a, mode='json') == 'a'
    assert v.to_json(MyEnum.a) == b'"a"'

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


def test_plain_dict_key():
    class MyEnum(Enum):
        a = 1
        b = 2

    v = SchemaSerializer(
        core_schema.dict_schema(
            core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())),
            core_schema.str_schema(),
        )
    )

    # debug(v)
    assert v.to_python({MyEnum.a: 'x'}) == {MyEnum.a: 'x'}
    assert v.to_python({MyEnum.a: 'x'}, mode='json') == {'1': 'x'}
    assert v.to_json({MyEnum.a: 'x'}) == b'{"1":"x"}'

    with pytest.warns(
        UserWarning,
        match=r"Expected `enum` - serialized value may not be as expected \[input_value='x', input_type=str\]",
    ):
        assert v.to_python({'x': 'x'}) == {'x': 'x'}
    with pytest.warns(
        UserWarning,
        match=r"Expected `enum` - serialized value may not be as expected \[input_value='x', input_type=str\]",
    ):
        assert v.to_json({'x': 'x'}) == b'{"x":"x"}'


def test_int_dict_key():
    class MyEnum(int, Enum):
        a = 1
        b = 2

    v = SchemaSerializer(
        core_schema.dict_schema(
            core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values()), sub_type='int'),
            core_schema.str_schema(),
        )
    )

    # debug(v)
    assert v.to_python({MyEnum.a: 'x'}) == {MyEnum.a: 'x'}
    assert v.to_python({MyEnum.a: 'x'}, mode='json') == {'1': 'x'}
    assert v.to_json({MyEnum.a: 'x'}) == b'{"1":"x"}'

    with pytest.warns(
        UserWarning,
        match=r"Expected `enum` - serialized value may not be as expected \[input_value='x', input_type=str\]",
    ):
        assert v.to_python({'x': 'x'}) == {'x': 'x'}
    with pytest.warns(
        UserWarning,
        match=r"Expected `enum` - serialized value may not be as expected \[input_value='x', input_type=str\]",
    ):
        assert v.to_json({'x': 'x'}) == b'{"x":"x"}'