File: test_json_or_python.py

package info (click to toggle)
pydantic-core 2.37.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,784 kB
  • sloc: python: 34,800; javascript: 211; makefile: 126
file content (32 lines) | stat: -rw-r--r-- 1,027 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
import pytest

from pydantic_core import SchemaValidator, ValidationError
from pydantic_core import core_schema as cs


def test_json_or_python():
    class Foo(str):
        def __eq__(self, o: object) -> bool:
            if isinstance(o, Foo) and super().__eq__(o):
                return True
            return False

    s = cs.json_or_python_schema(
        json_schema=cs.no_info_after_validator_function(Foo, cs.str_schema()), python_schema=cs.is_instance_schema(Foo)
    )
    v = SchemaValidator(s)

    assert v.validate_python(Foo('abc')) == Foo('abc')
    with pytest.raises(ValidationError) as exc_info:
        v.validate_python('abc')
    assert exc_info.value.errors(include_url=False) == [
        {
            'type': 'is_instance_of',
            'loc': (),
            'msg': 'Input should be an instance of test_json_or_python.<locals>.Foo',
            'input': 'abc',
            'ctx': {'class': 'test_json_or_python.<locals>.Foo'},
        }
    ]

    assert v.validate_json('"abc"') == Foo('abc')