-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_schemas.py
355 lines (301 loc) · 15.7 KB
/
test_schemas.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""
Tests to test wikidata entityschemas against wikidata items
"""
import unittest
import requests
from app import app
class MyTestCase(unittest.TestCase):
"""
Testcases to test wikidata entityschemas against wikidata items
"""
def setUp(self) -> None:
app.config["TESTING"] = True
self.app = app.test_client()
def tearDown(self) -> None:
# We don't need to tear anything down after the test
pass
def test_specific_wikidata_item_against_schema(self):
"""
Tests a specific entity against a certain schema and checks that
a statements and a properties response are returned
"""
test_pairs: dict = {"E236": "Q1728820"}
for key in test_pairs:
with self.subTest(key=key):
value = test_pairs[key]
response = self.app.get(f'/api/v2?entityschema={key}&entity={value}&language=en',
follow_redirects=True)
self.assertIsNotNone(response.json["statements"])
self.assertIsNotNone(response.json["properties"])
def test_lexical_category(self):
"""
This test checks that a lexicalCategory response is returned when a
lexeme is tested against a schema looking for a lexical category
"""
test_pairs: dict = {"E56": "Lexeme:L42"}
for key in test_pairs:
with self.subTest(key=key):
value = test_pairs[key]
response = self.app.get(f'/api/v2?entityschema={key}&entity={value}&language=en',
follow_redirects=True)
self.assertIsNotNone(response.json["general"][0]["lexicalCategory"])
self.assertIsNotNone(response.json["general"][0]["language"])
def test_wikidata_entityschemas(self) -> None:
"""
Tests all wikidata entityschemas return 200
This test iterates through each entityschema on wikidata and checks to see that
each one returns a "200" code when compared with a specific entity
The "200" code tells us that the entityschema was successfully compared
with the entity, but not whether it passes or not. This will give us a list of
entityschemas that we have problems with. This may be due to a bug in entityshape
or a problem with the entityschema itself.
"""
skips: list = []
url: str = "https://www.wikidata.org/w/api.php?" \
"action=query&format=json&list=allpages&aplimit=max&apnamespace=640"
response = requests.get(url)
json_text: dict = response.json()
entity_schemas: list = []
for schema in json_text["query"]["allpages"]:
entity_schemas.append(schema["title"][13:])
for schema in entity_schemas:
with self.subTest(schema=schema):
if schema in skips:
self.skipTest(f"Schema {schema} not supported")
response = self.app.get(f'/api/v2?entityschema={schema}&entity=Q100532807&language=en',
follow_redirects=True)
self.assertEqual(response.status_code, 200)
def test_specific_entityschema(self) -> None:
"""
This test tests a specified schema against an entity and checks that a 200 response
is received
"""
schema: str = "E236"
response = self.app.get(f'/api/v2?entityschema={schema}&entity=Q100532807&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
self.assertEqual("Member of the Oireachtas", response.json["name"][0])
self.assertEqual({'name': 'occupation', 'necessity': 'required', 'response': 'missing'},
response.json["properties"][0]["P106"])
def test_entityschema_e3(self):
"""
Tests that shapes with [] are evaluated
This test tests entityschema E3 against entity Q85396849 (Drumlohan).
The schema has a shape <wikimedia_projects> [ ]. The test makes sure
that the schema returns a 200 response, indicating that it evaluates []
"""
response = self.app.get('/api/v2?entityschema=E3&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e121(self):
"""
Tests that blank schemas doesn't fail
This test tests entityschema E121 against entity Q85396849 (Drumlohan).
The schema is blank. The test makes sure that this still returns a 200 response
"""
response = self.app.get('/api/v2?entityschema=E121&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e126(self):
"""
Tests that a comment line # without a space after passes
This test tests entityschema E126 against entity Q85396849.
The schema has a comment line that starts with #{ rather than # {.
The test checks that we get a 200 response
"""
response = self.app.get('/api/v2?entityschema=E126&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e135(self):
"""
Tests that blank schemas doesn't fail
This test tests entityschema E135 against entity Q85396849 (Drumlohan).
The schema is blank. The test makes sure that this still returns a 200 response
"""
response = self.app.get('/api/v2?entityschema=E135&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e176(self):
"""
Tests that where <shape> and { are on different lines, it works
This test tests entityschema E176 against entity Q85396849 (Drumlohan).
The schema contains a shape where the name and { are on different lines.
The test checks that this evaluates correctly
"""
response = self.app.get('/api/v2?entityschema=E176&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e187(self):
"""
Tests that shapes with no brackets work
This test tests entityschema E187 against entity Q85396849 (Drumlohan).
The schema has a shape of the form <shape> geo:Literal. The test
makes sure that this evaluates
"""
response = self.app.get('/api/v2?entityschema=E187&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e228(self):
"""
Tests that schemas importing other schemas don't fail
This test tests entityschema E275 against entity Q85396849 (Drumlohan).
The schema imports another schema and references it. The test makes sure
that this still returns a 200 response
"""
response = self.app.get('/api/v2?entityschema=E228&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e236(self):
"""
Tests all properties of Q1728820 pass E236
This test tests entityschema E236 (Member of the Oireachtas) against entity
Q1728820 (Leo Varadkar). All properties in the schema should pass
"""
response = self.app.get('/api/v2?entityschema=E236&entity=Q1728820&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
properties: list = ["P102", "P18", "P31", "P734", "P735", "P39", "P21",
"P27", "P106", "P569", "P4690"]
for prop in properties:
with self.subTest(prop=prop):
self.assertIn(response.json["properties"][0][prop]["response"], ["correct", "present"])
def test_entityschema_e236_2(self):
"""
Tests all properties of Q185272 pass E236
This test tests entityschema E236 (Member of the Oireachtas) against entity
Q185272 (Brian Cowen). P39 and P106 both have EXTRA designation and should pass
"""
response = self.app.get('/api/v2?entityschema=E236&entity=Q185272&language=en',
follow_redirects=True)
response2 = self.app.get('/api?entityschema=E236&entity=Q185272&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
properties: list = ["P39", "P106", "P18", "P4690"]
for prop in properties:
with self.subTest(prop=prop):
self.assertIn(response.json["properties"][0][prop]["response"], ["correct", "present", "allowed"])
self.assertEqual(response.json["properties"][0][prop]["response"],
response2.json["properties"][prop]["response"])
def test_entityschema_e239(self):
"""
Tests that blank schemas doesn't fail
This test tests entityschema E135 against entity Q85396849 (Drumlohan).
The schema is blank. The test makes sure that this still returns a 200 response
"""
response = self.app.get('/api/v2?entityschema=E239&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e247(self):
"""
Tests that blank schemas doesn't fail
This test tests entityschema E247 against entity Q85396849 (Drumlohan).
The schema is blank. The test makes sure that this still returns a 200 response
"""
response = self.app.get('/api/v2?entityschema=E247&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e275(self):
"""
Tests that schemas importing other schemas don't fail
This test tests entityschema E275 against entity Q85396849 (Drumlohan).
The schema imports another schema and references it. The test makes sure
that this still returns a 200 response
"""
response = self.app.get('/api/v2?entityschema=E275&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e278(self):
"""
Tests that {} containing non cardinalities aren't evaluated as cardinalities
This test tests entityschema E278 against entity Q85396849 (Drumlohan).
The schema contains a line ps:P279 { wdt:P31 wd:Q67101749 }. The test makes
sure that we get a 200 and that the contents of the {} aren't
evaluated as a cardinality
"""
response = self.app.get('/api/v2?entityschema=E278&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e295(self):
"""
Tests item with cardinality of 0 evaluates correctly
This test tests entityschema E295 (townland) against entity Q85396849 (Drumlohan).
The schema has a P361 (part of) with a cardinality of 0, meaning the item should
not contain any P361. The test checks that the response is false for this item
"""
response = self.app.get('/api/v2?entityschema=E295&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
properties: list = ["P361"]
for prop in properties:
with self.subTest(prop=prop):
self.assertIn(response.json["properties"][0][prop]["response"], ["too many statements"])
self.assertIn(response.json["properties"][0][prop]["necessity"], ["absent"])
def test_entityschema_e297(self):
"""
Tests item with repeated properties works correctly
This test tests entityschema E297 (sailboat class) against entity
Q97179551 (J/92s). The schema has multiple statements about the same property.
The test checks to ensure that the correct cardinality is calculated for
the relevant property
"""
response = self.app.get('/api/v2?entityschema=E297&entity=Q97179551&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
properties: list = ["P2043", "P2067"]
for prop in properties:
with self.subTest(prop=prop):
self.assertIn(response.json["properties"][0][prop]["response"],
["correct", "present", "too many statements"])
def test_entityschema_e300(self):
"""
Tests item with optional qualifiers is evaluated correctly
This test tests entityschema E300 (auto racing series) against entity Q92821370
(2022 FIA Formula One Season). The schema has a P3450 (Sports league of competition)
with optional qualifiers. The test checks that the response is correct for this item
"""
response = self.app.get('/api/v2?entityschema=E300&entity=Q92821370&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
properties: list = ["P3450"]
for prop in properties:
with self.subTest(prop=prop):
self.assertIn(response.json["properties"][0][prop]["response"], ["present"])
self.assertIn(response.json["properties"][0][prop]["necessity"], ["required"])
def test_entityschema_e340(self):
"""
Tests that blank schemas doesn't fail
This test tests entityschema E135 against entity Q85396849 (Drumlohan).
The schema is blank. The test makes sure that this still returns a 200 response
"""
response = self.app.get('/api/v2?entityschema=E340&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e349(self):
"""
Tests that blank schemas doesn't fail
This test tests entityschema E135 against entity Q85396849 (Drumlohan).
The schema is blank. The test makes sure that this still returns a 200 response
"""
response = self.app.get('/api/v2?entityschema=E349&entity=Q85396849&language=en',
follow_redirects=True)
self.assertEqual(200, response.status_code)
def test_entityschema_e351(self):
"""
Tests that blank schemas doesn't fail
This test tests entityschema E351 (pharmaceutical product) against entity Q743656 (Daytona Road Course).
P31 should return as incorrect
"""
response = self.app.get('/api/v2?entityschema=E351&entity=Q743656&language=en',
follow_redirects=True)
self.assertIn(response.json["properties"][0]["P31"]["response"], ["not enough correct statements"])
def test_entityschema_e438(self):
"""
Tests that a schemas with only 1 expression evaluates correctly
This test tests entityschema E438 (wikimedia disambiguation page) against entity Q11645745.
P31 should return as present
"""
response = self.app.get('/api/v2?entityschema=E438&entity=Q11645745&language=en',
follow_redirects=True)
self.assertIn(response.json["properties"][0]["P31"]["response"], ["correct", "present"])
if __name__ == '__main__':
unittest.main()