File: test_nullable.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 (18 lines) | stat: -rw-r--r-- 614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import pytest

from pydantic_core import SchemaSerializer, core_schema


def test_nullable():
    s = SchemaSerializer(core_schema.nullable_schema(core_schema.int_schema()))
    assert s.to_python(None) is None
    assert s.to_python(1) == 1
    assert s.to_python(None, mode='json') is None
    assert s.to_python(1, mode='json') == 1
    assert s.to_json(1) == b'1'
    assert s.to_json(None) == b'null'
    with pytest.warns(
        UserWarning,
        match=r"Expected `int` - serialized value may not be as expected \[input_value='aaa', input_type=str\]",
    ):
        assert s.to_json('aaa') == b'"aaa"'