File: test_definitions.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 (122 lines) | stat: -rw-r--r-- 4,515 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
import pytest

from pydantic_core import SchemaError, SchemaSerializer, core_schema


def test_custom_ser():
    s = SchemaSerializer(
        core_schema.definitions_schema(
            core_schema.list_schema(core_schema.definition_reference_schema('foobar')),
            [core_schema.int_schema(ref='foobar', serialization=core_schema.to_string_ser_schema(when_used='always'))],
        )
    )
    assert s.to_python([1, 2, 3]) == ['1', '2', '3']


def test_ignored_def():
    s = SchemaSerializer(
        core_schema.definitions_schema(
            core_schema.list_schema(core_schema.int_schema()),
            [core_schema.int_schema(ref='foobar', serialization=core_schema.to_string_ser_schema(when_used='always'))],
        )
    )
    assert s.to_python([1, 2, 3]) == [1, 2, 3]


def test_repeated_ref():
    with pytest.raises(SchemaError, match='SchemaError: Duplicate ref: `foobar`'):
        SchemaSerializer(
            core_schema.tuple_positional_schema(
                [
                    core_schema.definitions_schema(
                        core_schema.definition_reference_schema('foobar'), [core_schema.int_schema(ref='foobar')]
                    ),
                    core_schema.definitions_schema(
                        core_schema.definition_reference_schema('foobar'), [core_schema.int_schema(ref='foobar')]
                    ),
                ]
            )
        )


def test_repeat_after():
    with pytest.raises(SchemaError, match='SchemaError: Duplicate ref: `foobar`'):
        SchemaSerializer(
            core_schema.definitions_schema(
                core_schema.tuple_positional_schema(
                    [
                        core_schema.definitions_schema(
                            core_schema.definition_reference_schema('foobar'), [core_schema.int_schema(ref='foobar')]
                        ),
                        core_schema.definition_reference_schema('foobar'),
                    ]
                ),
                [core_schema.int_schema(ref='foobar')],
            )
        )


def test_deep():
    v = SchemaSerializer(
        core_schema.typed_dict_schema(
            {
                'a': core_schema.typed_dict_field(core_schema.int_schema()),
                'b': core_schema.typed_dict_field(
                    core_schema.definitions_schema(
                        core_schema.typed_dict_schema(
                            {
                                'c': core_schema.typed_dict_field(core_schema.int_schema()),
                                'd': core_schema.typed_dict_field(core_schema.definition_reference_schema('foobar')),
                            }
                        ),
                        [
                            core_schema.int_schema(
                                ref='foobar', serialization=core_schema.to_string_ser_schema(when_used='always')
                            )
                        ],
                    )
                ),
            }
        )
    )
    assert v.to_python({'a': 1, 'b': {'c': 2, 'd': 3}}) == {'a': 1, 'b': {'c': 2, 'd': '3'}}


def test_use_after():
    v = SchemaSerializer(
        core_schema.tuple_positional_schema(
            [
                core_schema.definitions_schema(
                    core_schema.definition_reference_schema('foobar'),
                    [
                        core_schema.int_schema(
                            ref='foobar', serialization=core_schema.to_string_ser_schema(when_used='always')
                        )
                    ],
                ),
                core_schema.definition_reference_schema('foobar'),
            ]
        )
    )
    assert v.to_python((1, 2)) == ('1', '2')


def test_defs_with_dict():
    s = SchemaSerializer(
        core_schema.definitions_schema(
            schema=core_schema.typed_dict_schema(
                {
                    'foo': core_schema.typed_dict_field(
                        core_schema.dict_schema(
                            keys_schema=core_schema.definition_reference_schema('key'),
                            values_schema=core_schema.definition_reference_schema('val'),
                        )
                    )
                }
            ),
            definitions=[core_schema.str_schema(ref='key'), core_schema.str_schema(ref='val')],
        )
    )

    assert s.to_json({'foo': {'key': 'val'}}) == b'{"foo":{"key":"val"}}'
    assert s.to_python({'foo': {'key': 'val'}}) == {'foo': {'key': 'val'}}