File: test_set_frozenset.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 (60 lines) | stat: -rw-r--r-- 2,090 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
import json

import pytest
from dirty_equals import IsList

from pydantic_core import SchemaSerializer, core_schema


def test_set_any():
    v = SchemaSerializer(core_schema.set_schema(core_schema.any_schema()))
    assert v.to_python({'a', 'b', 'c'}) == {'a', 'b', 'c'}
    assert v.to_python({'a', 'b', 'c'}, mode='json') == IsList('a', 'b', 'c', check_order=False)
    assert json.loads(v.to_json({'a', 'b', 'c'})) == IsList('a', 'b', 'c', check_order=False)


def test_frozenset_any():
    v = SchemaSerializer(core_schema.frozenset_schema(core_schema.any_schema()))
    fs = frozenset(['a', 'b', 'c'])
    output = v.to_python(fs)
    assert output == {'a', 'b', 'c'}
    assert type(output) == frozenset
    assert v.to_python(fs, mode='json') == IsList('a', 'b', 'c', check_order=False)
    assert json.loads(v.to_json(fs)) == IsList('a', 'b', 'c', check_order=False)


@pytest.mark.parametrize(
    'input_value,json_output,expected_type',
    [
        ('apple', 'apple', r'set\[int\]'),
        ([1, 2, 3], [1, 2, 3], r'set\[int\]'),
        ((1, 2, 3), [1, 2, 3], r'set\[int\]'),
        (
            frozenset([1, 2, 3]),
            IsList(1, 2, 3, check_order=False),
            r'set\[int\]',
        ),
        ({1, 2, 'a'}, IsList(1, 2, 'a', check_order=False), 'int'),
    ],
)
def test_set_fallback(input_value, json_output, expected_type):
    v = SchemaSerializer(core_schema.set_schema(core_schema.int_schema()))
    assert v.to_python({1, 2, 3}) == {1, 2, 3}

    with pytest.warns(
        UserWarning,
        match=f'Expected `{expected_type}` - serialized value may not be as expected',
    ):
        assert v.to_python(input_value) == input_value

    with pytest.warns(
        UserWarning,
        match=f'Expected `{expected_type}` - serialized value may not be as expected',
    ):
        assert v.to_python(input_value, mode='json') == json_output

    with pytest.warns(
        UserWarning,
        match=f'Expected `{expected_type}` - serialized value may not be as expected',
    ):
        assert json.loads(v.to_json(input_value)) == json_output