File: test_compile_to_code.py

package info (click to toggle)
python-fastjsonschema 2.21.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,780 kB
  • sloc: python: 3,343; makefile: 88; sh: 18
file content (122 lines) | stat: -rw-r--r-- 3,791 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import pytest
import shutil

from fastjsonschema import JsonSchemaValueException
from fastjsonschema import compile_to_code, compile as compile_spec


def test_compile_to_code(tmp_path, monkeypatch):
    code = compile_to_code({
        'properties': {
            'a': {'type': 'string'},
            'b': {'type': 'integer'},
            'c': {'format': 'hostname'},  # Test generation of regex patterns to the file.
        }
    })
    with open(tmp_path / 'schema_1.py', 'w') as f:
        f.write(code)
    with monkeypatch.context() as m:
        m.syspath_prepend(tmp_path)
        from schema_1 import validate
    assert validate({
        'a': 'a',
        'b': 1, 
        'c': 'example.com',
    }) == {
        'a': 'a',
        'b': 1,
        'c': 'example.com',
    }

def test_compile_to_code_ipv6_regex(tmp_path, monkeypatch):
    code = compile_to_code({
        'properties': {
            'ip': {'format': 'ipv6'},
        }
    })
    with open(tmp_path / 'schema_2.py', 'w') as f:
        f.write(code)
    with monkeypatch.context() as m:
        m.syspath_prepend(tmp_path)
        from schema_2 import validate
    assert validate({
        'ip': '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
    }) == {
        'ip': '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
    }

# https://github.com/horejsek/python-fastjsonschema/issues/74
def test_compile_complex_one_of_all_of():
    compile_spec({
        "oneOf": [
            {
                "required": [
                    "schema"
                ]
            },
            {
                "required": [
                    "content"
                ],
                "allOf": [
                    {
                        "not": {
                            "required": [
                                "style"
                            ]
                        }
                    },
                    {
                        "not": {
                            "required": [
                                "explode"
                            ]
                        }
                    }
                ]
            }
        ]
    })


def test_compile_to_code_custom_format(tmp_path, monkeypatch):
    formats = {'my-format': str.isidentifier}
    code = compile_to_code({'type': 'string', 'format': 'my-format'}, formats=formats)
    with open(tmp_path / 'schema_3.py', 'w') as f:
        f.write(code)
    with monkeypatch.context() as m:
        m.syspath_prepend(tmp_path)
        from schema_3 import validate
    assert validate("valid", formats) == "valid"
    with pytest.raises(JsonSchemaValueException) as exc:
        validate("not-valid", formats)
    assert exc.value.message == "data must be my-format"


def test_compile_to_code_custom_format_with_refs(tmp_path, monkeypatch):
    schema = {
        'type': 'object',
        'properties': {
            'a': {'$ref': '#/definitions/a'}
        },
        'definitions': {
            'a': {
                '$id': '#/definitions/a',
                'type': 'array',
                'items': {'type': 'string', 'format': 'my-format'}
            }
        }
    }
    formats = {'my-format': str.isidentifier}
    code = compile_to_code(schema, formats=formats)

    (tmp_path / "schema_4.py").write_text(code, encoding="utf-8")
    with monkeypatch.context() as m:
        m.syspath_prepend(tmp_path)
        from schema_4 import validate

    assert validate({"a": ["identifier"]}, formats) is not None
    with pytest.raises(JsonSchemaValueException) as exc:
        validate({"a": ["identifier", "not-valid"]}, formats)
    assert exc.value.message == "data.a[1] must be my-format"