File: test_none.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 (39 lines) | stat: -rw-r--r-- 935 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
import pytest

from pydantic_core import SchemaSerializer, core_schema

all_scalars = (
    'int',
    'bool',
    'float',
    'none',
    'str',
    'bytes',
    'datetime',
    'date',
    'time',
    'timedelta',
    'url',
    'multi-host-url',
)
all_types = all_scalars + ('list', 'dict', 'set', 'frozenset')


@pytest.mark.parametrize('schema_type', all_types)
def test_none_fallback(schema_type):
    s = SchemaSerializer({'type': schema_type})
    assert s.to_python(None) is None

    assert s.to_python(None, mode='json') is None

    assert s.to_json(None) == b'null'


@pytest.mark.parametrize('schema_type', all_scalars)
def test_none_fallback_key(schema_type):
    s = SchemaSerializer(core_schema.dict_schema({'type': schema_type}, core_schema.int_schema()))
    assert s.to_python({None: 1}) == {None: 1}

    assert s.to_python({None: 1}, mode='json') == {'None': 1}

    assert s.to_json({None: 1}) == b'{"None":1}'