File: test_custom_validator_class.py

package info (click to toggle)
python-check-jsonschema 0.34.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 3,796 kB
  • sloc: python: 5,529; makefile: 4
file content (182 lines) | stat: -rw-r--r-- 4,784 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import json

import pytest

# define a calendar event schema and then use a custom validator to validate that there
# are no events with "Occult" in their names
SCHEMA = {
    "$schema": "http://json-schema.org/draft-07/schema",
    "definitions": {
        "calendar-event": {
            "type": "object",
            "properties": {
                "title": {"type": "string"},
                "start": {"type": "string", "format": "date-time"},
                "end": {"type": "string", "format": "date-time"},
            },
        }
    },
    "properties": {
        "events": {
            "type": "array",
            "items": {"$ref": "#/definitions/calendar-event"},
        },
    },
    "required": ["events"],
}
VALID_DOC = {
    "events": [
        {
            "title": "Weekly Production Meeting",
            "start": "2019-06-24T09:00:00-05:00",
            "end": "2019-06-24T10:00:00-05:00",
        },
        {
            "title": "Catch Up",
            "start": "2019-06-24T10:00:00-05:00",
            "end": "2019-06-24T10:30:00-05:00",
        },
    ]
}
INVALID_DOC = {
    "events": [
        {
            "title": "Weekly Production Meeting",
            "start": "2019-06-24T09:00:00-05:00",
            "end": "2019-06-24T10:00:00-05:00",
        },
        {
            "title": "Catch Up",
            "start": "2019-06-24T10:00:00-05:00",
            "end": "2019-06-24T10:30:00-05:00",
        },
        {
            "title": "Occult Study Session",
            "start": "2019-06-24T10:00:00-05:00",
            "end": "2019-06-24T12:00:00-05:00",
        },
    ]
}


@pytest.fixture(autouse=True)
def _foo_module(mock_module):
    mock_module(
        "foo.py",
        """\
import jsonschema


def check_occult_properties(validator, properties, instance, schema):
    if not validator.is_type(instance, "object"):
        return

    for property, subschema in properties.items():
        if property in instance:
            if property == "title" and "Occult" in instance["title"]:
                yield jsonschema.exceptions.ValidationError(
                    "Error! Occult event detected! Run!",
                    validator=validator,
                    validator_value=None,
                    instance=instance,
                    schema=schema,
                )
            yield from validator.descend(
                instance[property],
                subschema,
                path=property,
                schema_path=property,
            )

MyValidator = jsonschema.validators.extend(
    jsonschema.validators.Draft7Validator,
    {"properties": check_occult_properties},
)
""",
    )


def test_custom_validator_class_can_detect_custom_conditions(run_line, tmp_path):
    doc = tmp_path / "invalid.json"
    doc.write_text(json.dumps(INVALID_DOC))

    schema = tmp_path / "schema.json"
    schema.write_text(json.dumps(SCHEMA))

    result = run_line(
        [
            "check-jsonschema",
            "--schemafile",
            str(schema),
            str(doc),
        ]
    )
    assert result.exit_code == 0, result.stdout  # pass

    result = run_line(
        [
            "check-jsonschema",
            "--schemafile",
            str(schema),
            "--validator-class",
            "foo:MyValidator",
            str(doc),
        ],
    )
    assert result.exit_code == 1, result.stdout  # fail
    assert "Occult event detected" in result.stdout, result.stdout


def test_custom_validator_class_can_pass_when_valid(run_line, tmp_path):
    doc = tmp_path / "valid.json"
    doc.write_text(json.dumps(VALID_DOC))

    schema = tmp_path / "schema.json"
    schema.write_text(json.dumps(SCHEMA))

    result = run_line(
        [
            "check-jsonschema",
            "--schemafile",
            str(schema),
            str(doc),
        ]
    )
    assert result.exit_code == 0, result.stdout  # pass

    result = run_line(
        [
            "check-jsonschema",
            "--schemafile",
            str(schema),
            "--validator-class",
            "foo:MyValidator",
            str(doc),
        ],
    )
    assert result.exit_code == 0  # pass


@pytest.mark.parametrize(
    "add_opts",
    (
        ["--builtin-schema", "vendor.github-workflows"],
        ["--check-metaschema"],
    ),
)
def test_custom_validator_class_is_incompatible_with_schema_opts(
    run_line, tmp_path, add_opts
):
    doc = tmp_path / "instance.json"
    doc.write_text("{}")
    result = run_line(
        [
            "check-jsonschema",
            "--validator-class",
            "foo:MyValidator",
            str(doc),
        ]
        + add_opts
    )
    assert result.exit_code == 2
    assert "--validator-class can only be used with --schemafile" in result.stderr