File: test_json.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 (47 lines) | stat: -rw-r--r-- 1,853 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
from pydantic_core import SchemaSerializer, core_schema


def test_json_int():
    s = SchemaSerializer(core_schema.json_schema(core_schema.int_schema()))

    assert s.to_python(1) == 1
    assert s.to_python(1, round_trip=True) == '1'
    assert s.to_python(1, mode='json') == 1
    assert s.to_python(1, mode='json', round_trip=True) == '1'
    assert s.to_json(1) == b'1'
    assert s.to_json(1, round_trip=True) == b'"1"'


def test_list_json():
    s = SchemaSerializer(core_schema.list_schema(core_schema.json_schema()))

    v = ['a', [1, 2], None]
    assert s.to_python(v) == v
    assert s.to_python(v, round_trip=True) == ['"a"', '[1,2]', 'null']
    assert s.to_python(v, mode='json') == v
    assert s.to_python(v, mode='json', round_trip=True) == ['"a"', '[1,2]', 'null']
    assert s.to_json(v) == b'["a",[1,2],null]'
    assert s.to_json(v, round_trip=True) == b'["\\"a\\"","[1,2]","null"]'


def test_dict_key_json():
    s = SchemaSerializer(core_schema.dict_schema(core_schema.json_schema(), core_schema.any_schema()))

    v = {(1, 2): 3, (4, 5): 9}
    assert s.to_python(v) == v
    assert s.to_python(v, round_trip=True) == {'[1,2]': 3, '[4,5]': 9}

    assert s.to_python(v, mode='json') == {'1,2': 3, '4,5': 9}
    assert s.to_python(v, mode='json', round_trip=True) == {'[1,2]': 3, '[4,5]': 9}

    assert s.to_json(v) == b'{"1,2":3,"4,5":9}'
    assert s.to_json(v, round_trip=True) == b'{"[1,2]":3,"[4,5]":9}'


def test_custom_serializer():
    s = SchemaSerializer(core_schema.any_schema(serialization=core_schema.simple_ser_schema('json')))
    assert s.to_python({1: 2}) == {1: 2}
    assert s.to_python({1: 2}, mode='json') == {'1': 2}
    assert s.to_python({1: 2}, mode='json', round_trip=True) == '{"1":2}'
    assert s.to_json({1: 2}) == b'{"1":2}'
    assert s.to_json({1: 2}, round_trip=True) == b'"{\\"1\\":2}"'