File: test_string.py

package info (click to toggle)
pydantic-core 2.41.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,828 kB
  • sloc: python: 35,564; javascript: 211; makefile: 128
file content (182 lines) | stat: -rw-r--r-- 7,248 bytes parent folder | download | duplicates (2)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import json
from enum import Enum

import pytest

from pydantic_core import PydanticSerializationError, SchemaSerializer, core_schema


def test_str():
    v = SchemaSerializer(core_schema.str_schema())
    assert v.to_python('foobar') == 'foobar'
    assert v.to_python('emoji 💩') == 'emoji 💩'
    assert v.to_json('foobar') == b'"foobar"'
    assert v.to_json('foobar', indent=2) == b'"foobar"'
    assert v.to_json('emoji 💩') == b'"emoji \xf0\x9f\x92\xa9"'
    assert json.loads(v.to_json('emoji 💩')) == 'emoji 💩'

    assert v.to_python('foobar', mode='json') == 'foobar'

    json_emoji = v.to_json('emoji 💩')
    # note! serde_json serializes unicode characters differently to json.dumps, but it's still valid JSON
    assert json_emoji == b'"emoji \xf0\x9f\x92\xa9"'
    assert json.loads(json_emoji) == 'emoji 💩'


# Tests borrowed from:
# - https://github.com/python/cpython/blob/d87e7f35/Lib/test/test_json/test_encode_basestring_ascii.py
# - https://github.com/python/cpython/blob/d87e7f35/Lib/test/test_json/test_unicode.py
@pytest.mark.parametrize(
    ['input', 'expected'],
    [
        (
            '/\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\x08\x0c\n\r\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?',
            '"/\\\\\\"\\ucafe\\ubabe\\uab98\\ufcde\\ubcda\\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"',
        ),
        ('\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'),
        ('controls', '"controls"'),
        ('\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'),
        (
            '{"object with 1 member":["array with 1 element"]}',
            '"{\\"object with 1 member\\":[\\"array with 1 element\\"]}"',
        ),
        (' s p a c e d ', '" s p a c e d "'),
        ('\U0001d120', '"\\ud834\\udd20"'),
        ('\u03b1\u03a9', '"\\u03b1\\u03a9"'),
        ("`1~!@#$%^&*()_+-={':[,]}|;.</>?", '"`1~!@#$%^&*()_+-={\':[,]}|;.</>?"'),
        ('\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'),
        ('\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'),
        ('\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}', '"\\u03b1\\u03a9"'),
        ('\U0001d120', '"\\ud834\\udd20"'),
    ],
)
def test_str_ensure_ascii(input: str, expected: str) -> None:
    v = SchemaSerializer(core_schema.str_schema())
    assert v.to_json(input, ensure_ascii=True).decode('utf-8') == expected


def test_huge_str():
    v = SchemaSerializer(core_schema.int_schema())
    msg = r"Expected `int` - serialized value may not be as expected \[input_value='123456789012345678901234...89012345678901234567890', input_type=str\]"
    with pytest.warns(UserWarning, match=msg):
        v.to_python(
            '12345678901234567890123456789012345678901234567890123456789012345678901234567890\
                           12345678901234567890123456789012345678901234567890123456789012345678901234567890\
                           12345678901234567890123456789012345678901234567890123456789012345678901234567890\
                           12345678901234567890123456789012345678901234567890123456789012345678901234567890'
        )


def test_str_fallback():
    s = SchemaSerializer(core_schema.str_schema())
    assert s.to_python(None) is None
    assert s.to_python(None, mode='json') is None
    assert s.to_json(None) == b'null'
    with pytest.warns(
        UserWarning,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_python(123) == 123
    with pytest.warns(
        UserWarning,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_python(123, mode='json') == 123
    with pytest.warns(
        UserWarning,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_json(123) == b'123'
    with pytest.warns(
        UserWarning,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_python(123, warnings='warn') == 123
    with pytest.warns(
        UserWarning,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_python(123, mode='json', warnings='warn') == 123
    with pytest.warns(
        UserWarning,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_json(123, warnings='warn') == b'123'
    with pytest.warns(
        UserWarning,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_python(123, warnings=True) == 123
    with pytest.warns(
        UserWarning,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_python(123, mode='json', warnings=True) == 123
    with pytest.warns(
        UserWarning,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_json(123, warnings=True) == b'123'


def test_str_no_warnings():
    s = SchemaSerializer(core_schema.str_schema())
    assert s.to_python(123, warnings=False) == 123
    assert s.to_python(123, warnings='none') == 123
    assert s.to_python(123, mode='json', warnings=False) == 123
    assert s.to_python(123, mode='json', warnings='none') == 123
    assert s.to_json(123, warnings=False) == b'123'
    assert s.to_json(123, warnings='none') == b'123'


def test_str_errors():
    s = SchemaSerializer(core_schema.str_schema())
    with pytest.raises(
        PydanticSerializationError,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_python(123, warnings='error') == 123
    with pytest.raises(
        PydanticSerializationError,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_python(123, mode='json', warnings='error') == 123
    with pytest.raises(
        PydanticSerializationError,
        match=r'Expected `str` - serialized value may not be as expected \[input_value=123, input_type=int\]',
    ):
        assert s.to_json(123, warnings='error') == b'123'


class StrSubclass(str):
    pass


class BasicClass:
    pass


class StrMixin(str, BasicClass):
    pass


class StrEnum(str, Enum):
    foo = 'foo-value'
    bar = 'bar-value'


@pytest.mark.parametrize('schema_type', ['str', 'any'])
@pytest.mark.parametrize(
    'input_value,expected', [(StrSubclass('foo'), 'foo'), (StrMixin('foo'), 'foo'), (StrEnum.foo, 'foo-value')]
)
def test_subclass_str(schema_type, input_value, expected):
    s = SchemaSerializer({'type': schema_type})
    v = s.to_python(input_value)
    assert v == input_value
    assert type(v) == type(input_value)

    v = s.to_python(input_value, mode='json')
    assert v == expected
    assert type(v) == str

    assert s.to_json(input_value) == json.dumps(expected).encode('utf-8')