File: test_malformed_instances.py

package info (click to toggle)
python-check-jsonschema 0.34.0-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 3,560 kB
  • sloc: python: 5,527; makefile: 4
file content (100 lines) | stat: -rw-r--r-- 3,314 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
import json

import pytest

TITLE_SCHEMA = {
    "$schema": "http://json-schema.org/draft-07/schema",
    "properties": {"title": {"type": "string"}},
    "required": ["title"],
}


def test_non_json_instance(run_line, tmp_path):
    schema = tmp_path / "schema.json"
    instance = tmp_path / "instance.json"
    schema.write_text("{}")
    instance.write_text("{")

    res = run_line(["check-jsonschema", "--schemafile", str(schema), str(instance)])
    assert res.exit_code == 1
    assert f"Failed to parse {str(instance)}" in res.stdout


@pytest.mark.parametrize("outformat", ["TEXT", "JSON"])
def test_non_json_instance_mixed_with_valid_data(run_line, tmp_path, outformat):
    schema = tmp_path / "schema.json"
    malformed_instance = tmp_path / "malformed_instance.json"
    good_instance = tmp_path / "good_instance.json"
    schema.write_text(json.dumps(TITLE_SCHEMA))
    malformed_instance.write_text("{")
    good_instance.write_text('{"title": "ohai"}')

    res = run_line(
        [
            "check-jsonschema",
            "-o",
            outformat,
            "--schemafile",
            str(schema),
            str(malformed_instance),
            str(good_instance),
        ]
    )
    assert res.exit_code == 1
    if outformat == "TEXT":
        assert f"Failed to parse {str(malformed_instance)}" in res.stdout
    else:
        report = json.loads(res.stdout)
        assert report["status"] == "fail"
        assert "errors" in report
        assert report["errors"] == []
        assert "parse_errors" in report
        assert len(report["parse_errors"]) == 1
        error_item = report["parse_errors"][0]
        assert error_item["filename"] == str(malformed_instance)
        assert f"Failed to parse {str(malformed_instance)}" in error_item["message"]


@pytest.mark.parametrize("outformat", ["TEXT", "JSON"])
def test_non_json_instance_mixed_with_valid_and_invalid_data(
    run_line, tmp_path, outformat
):
    schema = tmp_path / "schema.json"
    malformed_instance = tmp_path / "malformed_instance.json"
    good_instance = tmp_path / "good_instance.json"
    bad_instance = tmp_path / "bad_instance.json"
    schema.write_text(json.dumps(TITLE_SCHEMA))
    malformed_instance.write_text("{")
    good_instance.write_text('{"title": "ohai"}')
    bad_instance.write_text('{"title": false}')

    res = run_line(
        [
            "check-jsonschema",
            "-o",
            outformat,
            "--schemafile",
            str(schema),
            str(good_instance),
            str(malformed_instance),
            str(bad_instance),
        ]
    )
    assert res.exit_code == 1
    if outformat == "TEXT":
        assert f"Failed to parse {str(malformed_instance)}" in res.stdout
        assert (
            f"{str(bad_instance)}::$.title: False is not of type 'string'" in res.stdout
        )
    else:
        report = json.loads(res.stdout)
        assert report["status"] == "fail"

        assert "errors" in report
        assert len(report["errors"]) == 1

        assert "parse_errors" in report
        assert len(report["parse_errors"]) == 1
        error_item = report["parse_errors"][0]
        assert error_item["filename"] == str(malformed_instance)
        assert f"Failed to parse {str(malformed_instance)}" in error_item["message"]