Debian Codegraph @

citeproc/data/schema/tests/schemas/input/test_json.py

raw
1 import json
2 import pathlib
3
4 import jsonschema
5
6 import pytest
7
8 """
9 The following section defines pytest fixtures, which can be used as arguments
10 to tests, causing those tests to be run for all fixture values.
11 """
12
13 root = pathlib.Path(__file__).parent.parent.parent.parent
14 json_paths = list(root.glob('**/*.json'))
15
16
17 @pytest.fixture(params=json_paths, ids=[x.name for x in json_paths])
18 def json_path(request):
19 """
20 Paths that correspond to JSON files.
21 """
22 return root.joinpath(request.param)
23
24
25 @pytest.fixture(params=[
26 'schemas/input/csl-citation.json',
27 'schemas/input/csl-data.json',
28 ])
29 def json_schema(request):
30 """
31 JSON objects that correspond to JSON schemas.
32 """
33 path = root.joinpath(request.param)
34 text = path.read_text()
35 schema = json.loads(text)
36 return schema
37
38
39 @pytest.fixture
40 def csl_data_validator():
41 text = root.joinpath('schemas/input/csl-data.json').read_text()
42 schema = json.loads(text)
43 Validator = jsonschema.validators.validator_for(schema)
44 return Validator(schema)
45
46
47 @pytest.fixture
48 def csl_citation_validator():
49 text = root.joinpath('schemas/input/csl-citation.json').read_text()
50 schema = json.loads(text)
51 Validator = jsonschema.validators.validator_for(schema)
52 return Validator(schema)
53
54
55 """
56 The following section contains the tests.
57 """
58
59
60 def test_read_json(json_path):
61 """
62 Test that a JSON file can be read into Python.
63 """
64 text = json_path.read_text()
65 json.loads(text)
66
67
68 def test_schema_property_specified(json_schema):
69 """
70 Test that JSON schemas set the $schema property.
71 https://json-schema.org/understanding-json-schema/reference/schema.html
72 https://github.com/citation-style-language/schema/pull/153
73 """
74 json_schema['$schema']
75
76
77 def test_schema_is_valid(json_schema):
78 """
79 Infer the JSON Schema draft from $schema and then check that the input
80 schema is valid.
81 """
82 Validator = jsonschema.validators.validator_for(json_schema)
83 Validator.check_schema(json_schema)
84
85
86 def test_basic_data_schema_validates(csl_data_validator):
87 csl = [{
88 'id': 'example-id',
89 'type': 'report',
90 }]
91 csl_data_validator.validate(csl)
92
93
94 def test_basic_citation_schema_validates(csl_citation_validator):
95 cite = {
96 "schema": "https://resource.citationstyles.org/schema/latest/input/json/csl-citation.json",
97 "citationID": "item1",
98 "citationItems": [
99 {
100 "id": "item1",
101 "itemData": {
102 "id": "item1",
103 "type": "book",
104 "title": "test title"
105 }
106 }
107 ]
108 }
109 csl_citation_validator.validate(cite)
110
111
112 def test_basic_data_schema_with_author_validates(csl_data_validator):
113 csl = [{
114 'id': 'example-id',
115 'type': 'report',
116 "author": [
117 {"given": "Jane", "family": "Roe"},
118 {"literal": "John Doe"}
119 ],
120 }]
121 csl_data_validator.validate(csl)
122
123
124 def test_data_schema_with_extra_property_fails(csl_data_validator):
125 csl = [{
126 'id': 'example-id',
127 'type': 'report',
128 'not-a-csl-key': None,
129 }]
130 with pytest.raises(jsonschema.exceptions.ValidationError):
131 csl_data_validator.validate(csl)
132
133 def test_data_schema_with_empty_date_parts(csl_data_validator):
134 """
135 empty arrays in date-parts can cause downstream citeproc failures
136 https://github.com/citation-style-language/schema/pull/158
137 """
138 csl = [{
139 'id': 'example-id',
140 'type': 'report',
141 'issued': {
142 "date-parts": []
143 },
144 }]
145 with pytest.raises(jsonschema.exceptions.ValidationError):
146 csl_data_validator.validate(csl)