File: test_format_failure.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 (85 lines) | stat: -rw-r--r-- 2,023 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
import json

FORMAT_SCHEMA = {
    "$schema": "http://json-schema.org/draft-07/schema",
    "properties": {
        "title": {
            "type": "string",
        },
        "date": {
            "type": "string",
            "format": "date",
        },
    },
}

PASSING_DOCUMENT = {
    "title": "doc one",
    "date": "2021-10-28",
}

FAILING_DOCUMENT = {
    "title": "doc one",
    "date": "foo",
}


def test_format_check_passing(run_line_simple, tmp_path):
    schemafile = tmp_path / "schema.json"
    schemafile.write_text(json.dumps(FORMAT_SCHEMA))

    doc1 = tmp_path / "doc1.json"
    doc1.write_text(json.dumps(PASSING_DOCUMENT))

    run_line_simple(["--schemafile", str(schemafile), str(doc1)])


def test_format_failure_exit_error(run_line, tmp_path):
    schemafile = tmp_path / "schema.json"
    schemafile.write_text(json.dumps(FORMAT_SCHEMA))

    doc1 = tmp_path / "doc1.json"
    doc1.write_text(json.dumps(FAILING_DOCUMENT))

    res = run_line(["check-jsonschema", "--schemafile", str(schemafile), str(doc1)])
    assert res.exit_code == 1


def test_format_failure_ignore(run_line_simple, tmp_path):
    schemafile = tmp_path / "schema.json"
    schemafile.write_text(json.dumps(FORMAT_SCHEMA))

    doc1 = tmp_path / "doc1.json"
    doc1.write_text(json.dumps(FAILING_DOCUMENT))

    run_line_simple(
        [
            "--disable-formats",
            "*",
            "--schemafile",
            str(schemafile),
            str(doc1),
        ]
    )


def test_format_failure_ignore_multidoc(run_line_simple, tmp_path):
    schemafile = tmp_path / "schema.json"
    schemafile.write_text(json.dumps(FORMAT_SCHEMA))

    doc1 = tmp_path / "doc1.json"
    doc1.write_text(json.dumps(FAILING_DOCUMENT))

    doc2 = tmp_path / "doc2.json"
    doc2.write_text(json.dumps(PASSING_DOCUMENT))

    run_line_simple(
        [
            "--disable-formats",
            "*",
            "--schemafile",
            str(schemafile),
            str(doc1),
            str(doc2),
        ]
    )