forked from lucaswerkmeister/tool-lexeme-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_templates.py
246 lines (208 loc) · 9.44 KB
/
test_templates.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
import mwapi # type: ignore
import pytest
import app
from language import lang_lex2int
import templates
import translations
def test_entities_exist():
entity_ids = set()
def add_from_statements(statements):
for property_id, statement_group in statements.items():
entity_ids.add(property_id)
for statement in statement_group:
entity_ids.add(statement['mainsnak']['datavalue']['value']['id'])
for template in templates.templates_without_redirects.values():
if template.get('test', False):
continue
entity_ids.add(template['language_item_id'])
entity_ids.add(template['lexical_category_item_id'])
for form in template['forms']:
entity_ids.update(form['grammatical_features_item_ids'])
add_from_statements(form.get('statements', {}))
add_from_statements(template.get('statements', {}))
entity_ids = list(entity_ids)
session = mwapi.Session('https://www.wikidata.org', user_agent='lexeme-forms test (https://lexeme-forms.toolforge.org/; [email protected])')
missing_entity_ids = set()
while entity_ids:
chunk, entity_ids = entity_ids[:50], entity_ids[50:]
result = session.get(action='wbgetentities',
ids=chunk,
redirects='no',
props=[])
for entity_id, entity in result['entities'].items():
if 'missing' in entity:
missing_entity_ids.add(entity_id)
assert not missing_entity_ids
def test_translations_available():
missing_language_codes = set()
for template in templates.templates_without_redirects.values():
language_code = lang_lex2int(template['language_code'])
if language_code not in translations.translations:
missing_language_codes.add(language_code)
# language code disabled on translatewiki.net, needs more consideration?
missing_language_codes.remove('zh')
assert not missing_language_codes
def test_template_labels_distinct_per_language():
template_names_by_language_and_label = {}
for template_name, template in templates.templates_without_redirects.items():
template_names_by_language_and_label\
.setdefault(template['language_code'], {})\
.setdefault(template['label'], [])\
.append(template_name)
ambiguous_template_names_by_language_and_label = {}
for language, template_names_by_label in template_names_by_language_and_label.items():
for template_label, template_names in template_names_by_label.items():
if len(template_names) > 1:
ambiguous_template_names_by_language_and_label.setdefault(language, {})[template_label] = template_names
assert not ambiguous_template_names_by_language_and_label
@pytest.mark.parametrize('template_name, form', [
(template_name, form)
for template_name, template in templates.templates_without_redirects.items()
for form in template['forms']
])
def test_labels_not_valid_examples(template_name, form):
fake_form = {'example': form['label']}
with pytest.raises(Exception):
app.split_example(fake_form)
@pytest.mark.parametrize('template_name, form', [
(template_name, form)
for template_name, template in templates.templates_without_redirects.items()
for form in template['forms']
])
def test_examples_valid(template_name, form):
prefix, placeholder, suffix = app.split_example(form)
assert placeholder != ''
@pytest.mark.parametrize('template_name, form', [
(template_name, form)
for template_name, template in templates.templates_without_redirects.items()
if not template_name.startswith('armenian-') # https://www.wikidata.org/wiki/Wikidata_talk:Wikidata_Lexeme_Forms/Armenian#Initial_uppercase_in_some_examples
for form in template['forms']
])
def test_examples_no_misleading_capitalization(template_name, form):
"""Examples like '[Come] here.' can mislead users
into entering forms with wrong capitalization."""
prefix, placeholder, suffix = app.split_example(form)
assert prefix != '' or placeholder.lower() == placeholder
expected_example_counts = {
'bengali-verb': {
'তুমি [দেখো]।': 2,
'সে [দেখুক]।': 2,
'তিনি [দেখুন]।': 2,
'তুই [দেখিস]।': 3,
'সে [দেখবে]।': 2,
'সে [দেখিবে]।': 2,
'তিনি [দেখবেন]।': 2,
'তিনি [দেখিবেন]।': 2,
},
'bengali-verb-ano': {
'তুমি [দেখাও]।': 3,
'তুই [দেখাস]।': 3,
'তিনি [দেখান]।': 3,
'সে [দেখাক]।': 2,
'সে [দেখাবে]।': 2,
'সে [দেখাইবে]।': 2,
'তিনি [দেখাবেন]।': 2,
'তিনি [দেখাইবেন]।': 2,
},
'manbhumi-verb': {
'উ [ভাইলবেক]।': 2,
'উনি [ভাইলবেন]।': 2,
},
'czech-verb-imperfective': {
'Ona byla [zpívána].': 2,
'Ona to včera [zpívala].': 2,
},
'czech-verb-perfective': {
'Ona by [postavila].': 2,
},
}
@pytest.mark.parametrize('template_name', templates.templates_without_redirects.keys())
def test_examples_distinct(template_name):
template = templates.templates_without_redirects[template_name]
examples = {}
for form in template['forms']:
example = form['example']
examples[example] = examples.get(example, 0) + 1
actual_counts = {example: count for example, count in examples.items() if count > 1}
expected_counts = expected_example_counts.get(template_name, {})
assert actual_counts == expected_counts
@pytest.mark.parametrize('template_name, form', [
(template_name, form)
for template_name, template in templates.templates_without_redirects.items()
for form in template['forms']
])
def test_grammatical_feature_item_ids_distinct(template_name, form):
grammatical_features_item_ids = form['grammatical_features_item_ids']
assert len(set(grammatical_features_item_ids)) == len(grammatical_features_item_ids)
@pytest.mark.parametrize('template_name, form', [
(template_name, form)
for template_name, template in templates.templates_without_redirects.items()
for form in template['forms']
if 'grammatical_features_item_ids_optional' in form
])
def test_optional_grammatical_features_subset(template_name, form):
"""Check that grammatical features specified as optional are actually
grammatical features of the form.
It doesn’t make sense to have an optional feature which is not
going to be added or used for matching."""
assert form['grammatical_features_item_ids_optional'].issubset(form['grammatical_features_item_ids'])
@pytest.mark.parametrize('template_name', templates.templates_without_redirects.keys())
def test_attribution_available(template_name):
template = templates.templates_without_redirects[template_name]
assert '@attribution' in template
attribution = template['@attribution']
assert isinstance(attribution, dict)
assert 'users' in attribution
users = attribution['users']
assert isinstance(users, list)
assert users
if not template.get('test', False):
assert 'title' in attribution
title = attribution['title']
assert isinstance(title, str)
assert title
@pytest.mark.parametrize('template_name', templates.templates_without_redirects.keys())
def test_sections_declared(template_name):
"""Check that every template whose forms contain section breaks also
declares whether its sections should have two columns or not in
the header.
The inverse case, that a template declares two-column sections but
has no section breaks, is allowed, since it can be useful to have
all forms in a single two-column section. However, a template may
not declare that it does *not* use two-column sections in that
case, since that would be redundant.
"""
template = templates.templates_without_redirects[template_name]
has_section_breaks = False
for form in template['forms']:
if form.get('section_break', False):
has_section_breaks = True
break
if has_section_breaks:
assert 'two_column_sections' in template
else:
assert template.get('two_column_sections', True)
@pytest.mark.parametrize('template_name', [
template_name
for template_name, template in templates.templates.items()
if isinstance(template, (str, list))
])
def test_redirects_resolve_directly(template_name):
template = templates.templates[template_name]
if isinstance(template, str):
template = [template]
for target in template:
assert target in templates.templates
assert target in templates.templates_without_redirects
@pytest.mark.parametrize('template_name', [
template_name
for template_name, template in templates.templates.items()
if isinstance(template, list)
])
def test_ambiguous_templates(template_name):
"""Check that ambiguous templates (where the replacement is a list)
are actually ambiguous, i.e. have more than one replacement template.
Otherwise, the single replacement should be specified as a string instead,
so that we can directly redirect to it."""
template = templates.templates[template_name]
assert len(template) > 1