|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +# define a calendar event schema and then use a custom validator to validate that there |
| 6 | +# are no events with "Occult" in their names |
| 7 | +SCHEMA = { |
| 8 | + "$schema": "http://json-schema.org/draft-07/schema", |
| 9 | + "definitions": { |
| 10 | + "calendar-event": { |
| 11 | + "type": "object", |
| 12 | + "properties": { |
| 13 | + "title": {"type": "string"}, |
| 14 | + "start": {"type": "string", "format": "date-time"}, |
| 15 | + "end": {"type": "string", "format": "date-time"}, |
| 16 | + }, |
| 17 | + } |
| 18 | + }, |
| 19 | + "properties": { |
| 20 | + "events": { |
| 21 | + "type": "array", |
| 22 | + "items": {"$ref": "#/definitions/calendar-event"}, |
| 23 | + }, |
| 24 | + }, |
| 25 | + "required": ["events"], |
| 26 | +} |
| 27 | +VALID_DOC = { |
| 28 | + "events": [ |
| 29 | + { |
| 30 | + "title": "Weekly Production Meeting", |
| 31 | + "start": "2019-06-24T09:00:00-05:00", |
| 32 | + "end": "2019-06-24T10:00:00-05:00", |
| 33 | + }, |
| 34 | + { |
| 35 | + "title": "Catch Up", |
| 36 | + "start": "2019-06-24T10:00:00-05:00", |
| 37 | + "end": "2019-06-24T10:30:00-05:00", |
| 38 | + }, |
| 39 | + ] |
| 40 | +} |
| 41 | +INVALID_DOC = { |
| 42 | + "events": [ |
| 43 | + { |
| 44 | + "title": "Weekly Production Meeting", |
| 45 | + "start": "2019-06-24T09:00:00-05:00", |
| 46 | + "end": "2019-06-24T10:00:00-05:00", |
| 47 | + }, |
| 48 | + { |
| 49 | + "title": "Catch Up", |
| 50 | + "start": "2019-06-24T10:00:00-05:00", |
| 51 | + "end": "2019-06-24T10:30:00-05:00", |
| 52 | + }, |
| 53 | + { |
| 54 | + "title": "Occult Study Session", |
| 55 | + "start": "2019-06-24T10:00:00-05:00", |
| 56 | + "end": "2019-06-24T12:00:00-05:00", |
| 57 | + }, |
| 58 | + ] |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture(autouse=True) |
| 63 | +def _foo_module(mock_module): |
| 64 | + mock_module( |
| 65 | + "foo.py", |
| 66 | + """\ |
| 67 | +import jsonschema |
| 68 | +
|
| 69 | +class MyValidator: |
| 70 | + def __init__(self, schema, *args, **kwargs): |
| 71 | + self.schema = schema |
| 72 | + self.real_validator = jsonschema.validators.Draft7Validator( |
| 73 | + schema, *args, **kwargs |
| 74 | + ) |
| 75 | +
|
| 76 | + def iter_errors(self, data, *args, **kwargs): |
| 77 | + yield from self.real_validator.iter_errors(data, *args, **kwargs) |
| 78 | + for event in data["events"]: |
| 79 | + if "Occult" in event["title"]: |
| 80 | + yield jsonschema.exceptions.ValidationError( |
| 81 | + "Error! Occult event detected! Run!", |
| 82 | + validator=None, |
| 83 | + validator_value=None, |
| 84 | + instance=event, |
| 85 | + schema=self.schema, |
| 86 | + ) |
| 87 | +""", |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +def test_custom_validator_class_can_detect_custom_conditions(run_line, tmp_path): |
| 92 | + doc = tmp_path / "invalid.json" |
| 93 | + doc.write_text(json.dumps(INVALID_DOC)) |
| 94 | + |
| 95 | + schema = tmp_path / "schema.json" |
| 96 | + schema.write_text(json.dumps(SCHEMA)) |
| 97 | + |
| 98 | + result = run_line( |
| 99 | + [ |
| 100 | + "check-jsonschema", |
| 101 | + "--schemafile", |
| 102 | + str(schema), |
| 103 | + str(doc), |
| 104 | + ] |
| 105 | + ) |
| 106 | + assert result.exit_code == 0, result.stdout # pass |
| 107 | + |
| 108 | + result = run_line( |
| 109 | + [ |
| 110 | + "check-jsonschema", |
| 111 | + "--schemafile", |
| 112 | + str(schema), |
| 113 | + "--validator-class", |
| 114 | + "foo:MyValidator", |
| 115 | + str(doc), |
| 116 | + ], |
| 117 | + ) |
| 118 | + assert result.exit_code == 1 # fail |
| 119 | + assert "Occult event detected" in result.stdout, result.stdout |
| 120 | + |
| 121 | + |
| 122 | +def test_custom_validator_class_can_pass_when_valid(run_line, tmp_path): |
| 123 | + doc = tmp_path / "valid.json" |
| 124 | + doc.write_text(json.dumps(VALID_DOC)) |
| 125 | + |
| 126 | + schema = tmp_path / "schema.json" |
| 127 | + schema.write_text(json.dumps(SCHEMA)) |
| 128 | + |
| 129 | + result = run_line( |
| 130 | + [ |
| 131 | + "check-jsonschema", |
| 132 | + "--schemafile", |
| 133 | + str(schema), |
| 134 | + str(doc), |
| 135 | + ] |
| 136 | + ) |
| 137 | + assert result.exit_code == 0, result.stdout # pass |
| 138 | + |
| 139 | + result = run_line( |
| 140 | + [ |
| 141 | + "check-jsonschema", |
| 142 | + "--schemafile", |
| 143 | + str(schema), |
| 144 | + "--validator-class", |
| 145 | + "foo:MyValidator", |
| 146 | + str(doc), |
| 147 | + ], |
| 148 | + ) |
| 149 | + assert result.exit_code == 0 # pass |
0 commit comments