Add 19 new invariant tests; fix BE-WAL, TJ-RA, JP-01, AD-07, MK-205, BY-HM, JO-JA, UA-30
Browse files- test/test_invariants.py +214 -35
test/test_invariants.py
CHANGED
|
@@ -8,9 +8,22 @@ Run from dataset root after cloning:
|
|
| 8 |
Verifies:
|
| 9 |
- name field matches lang selection (name_en/ru/zh)
|
| 10 |
- lang matches region-based language rule
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
"""
|
| 12 |
|
|
|
|
| 13 |
import pytest
|
|
|
|
| 14 |
from datasets import load_dataset
|
| 15 |
|
| 16 |
SLAVIC_REGIONS = {
|
|
@@ -24,7 +37,6 @@ SINOPHONE_REGIONS = {
|
|
| 24 |
|
| 25 |
|
| 26 |
def determine_expected_lang(region_code: str) -> str:
|
| 27 |
-
"""Determine expected lang from region code."""
|
| 28 |
if region_code in SINOPHONE_REGIONS:
|
| 29 |
return 'zh'
|
| 30 |
elif region_code in SLAVIC_REGIONS:
|
|
@@ -35,13 +47,16 @@ def determine_expected_lang(region_code: str) -> str:
|
|
| 35 |
|
| 36 |
@pytest.fixture(scope="session")
|
| 37 |
def dataset():
|
| 38 |
-
"""Load dataset from HF Hub."""
|
| 39 |
return load_dataset('cterdam/iso_3166-2', split='train')
|
| 40 |
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def test_lang_values_valid(dataset):
|
| 43 |
"""lang field must be one of: en, ru, zh."""
|
| 44 |
-
invalid = [(
|
| 45 |
assert invalid == [], f"Invalid lang values: {invalid[:5]}"
|
| 46 |
|
| 47 |
|
|
@@ -49,49 +64,33 @@ def test_name_matches_lang_field(dataset):
|
|
| 49 |
"""name field must equal name_en/ru/zh based on lang."""
|
| 50 |
lang_to_field = {'en': 'name_en', 'ru': 'name_ru', 'zh': 'name_zh'}
|
| 51 |
mismatches = []
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
lang = entry.get('lang')
|
| 56 |
if not name or not lang:
|
| 57 |
continue
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
if name != lang_field_value:
|
| 63 |
-
mismatches.append((i, entry.get('code'), lang, name[:20], lang_field_value[:20] if lang_field_value else None))
|
| 64 |
-
if len(mismatches) >= 5:
|
| 65 |
-
break
|
| 66 |
-
|
| 67 |
-
assert mismatches == [], f"Name/lang field mismatches: {mismatches}"
|
| 68 |
|
| 69 |
|
| 70 |
def test_lang_matches_parent_region(dataset):
|
| 71 |
"""lang field must match parent region's language rule."""
|
| 72 |
-
mismatches = []
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
lang = entry.get('lang')
|
| 77 |
-
expected_lang = determine_expected_lang(region)
|
| 78 |
-
|
| 79 |
-
if lang != expected_lang:
|
| 80 |
-
mismatches.append((i, entry.get('code'), region, lang, expected_lang))
|
| 81 |
-
if len(mismatches) >= 5:
|
| 82 |
-
break
|
| 83 |
-
|
| 84 |
-
assert mismatches == [], f"Lang mismatches: {mismatches}"
|
| 85 |
|
| 86 |
|
| 87 |
def _is_cjk(c):
|
| 88 |
cp = ord(c)
|
| 89 |
return (
|
| 90 |
-
0x3400 <= cp <= 0x9FFF
|
| 91 |
-
or 0xF900 <= cp <= 0xFAFF
|
| 92 |
-
or 0x20000 <= cp <= 0x2A6DF
|
| 93 |
-
or 0x2A700 <= cp <= 0x2EBEF
|
| 94 |
-
or 0x30000 <= cp <= 0x323AF
|
| 95 |
)
|
| 96 |
|
| 97 |
|
|
@@ -119,3 +118,183 @@ def test_name_ru_only_cyrillic_letters(dataset):
|
|
| 119 |
if foreign:
|
| 120 |
bad.append((e['code'], v, foreign))
|
| 121 |
assert bad == [], f"name_ru/alts with non-Cyrillic letters: {bad[:10]}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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/ru/zh)
|
| 10 |
- lang matches region-based language rule
|
| 11 |
+
- code format and uniqueness
|
| 12 |
+
- region prefix matches code
|
| 13 |
+
- all name fields in correct script
|
| 14 |
+
- no typographic quotes in name_ru
|
| 15 |
+
- balanced parentheses in all name fields
|
| 16 |
+
- no whitespace anomalies in name fields
|
| 17 |
+
- name_en starts uppercase
|
| 18 |
+
- name_en has no trailing punctuation
|
| 19 |
+
- no duplicate values within an entry's name fields
|
| 20 |
+
- alt lists: primary not duplicated, no CJK/Cyrillic in names_en_alt
|
| 21 |
+
- links are valid URLs with no duplicates per row
|
| 22 |
"""
|
| 23 |
|
| 24 |
+
import re
|
| 25 |
import pytest
|
| 26 |
+
from collections import Counter
|
| 27 |
from datasets import load_dataset
|
| 28 |
|
| 29 |
SLAVIC_REGIONS = {
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
def determine_expected_lang(region_code: str) -> str:
|
|
|
|
| 40 |
if region_code in SINOPHONE_REGIONS:
|
| 41 |
return 'zh'
|
| 42 |
elif region_code in SLAVIC_REGIONS:
|
|
|
|
| 47 |
|
| 48 |
@pytest.fixture(scope="session")
|
| 49 |
def dataset():
|
|
|
|
| 50 |
return load_dataset('cterdam/iso_3166-2', split='train')
|
| 51 |
|
| 52 |
|
| 53 |
+
# ---------------------------------------------------------------------------
|
| 54 |
+
# Existing tests
|
| 55 |
+
# ---------------------------------------------------------------------------
|
| 56 |
+
|
| 57 |
def test_lang_values_valid(dataset):
|
| 58 |
"""lang field must be one of: en, ru, zh."""
|
| 59 |
+
invalid = [(e['code'], e['lang']) for e in dataset if e.get('lang') not in ('en', 'ru', 'zh')]
|
| 60 |
assert invalid == [], f"Invalid lang values: {invalid[:5]}"
|
| 61 |
|
| 62 |
|
|
|
|
| 64 |
"""name field must equal name_en/ru/zh based on lang."""
|
| 65 |
lang_to_field = {'en': 'name_en', 'ru': 'name_ru', 'zh': 'name_zh'}
|
| 66 |
mismatches = []
|
| 67 |
+
for e in dataset:
|
| 68 |
+
name = e.get('name')
|
| 69 |
+
lang = e.get('lang')
|
|
|
|
| 70 |
if not name or not lang:
|
| 71 |
continue
|
| 72 |
+
lang_val = e.get(lang_to_field.get(lang, ''))
|
| 73 |
+
if name != lang_val:
|
| 74 |
+
mismatches.append((e['code'], lang, name[:30], (lang_val or '')[:30]))
|
| 75 |
+
assert mismatches == [], f"Name/lang field mismatches: {mismatches[:5]}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
def test_lang_matches_parent_region(dataset):
|
| 79 |
"""lang field must match parent region's language rule."""
|
| 80 |
+
mismatches = [(e['code'], e.get('region'), e.get('lang'), determine_expected_lang(e.get('region', '')))
|
| 81 |
+
for e in dataset
|
| 82 |
+
if e.get('lang') != determine_expected_lang(e.get('region', ''))]
|
| 83 |
+
assert mismatches == [], f"Lang mismatches: {mismatches[:5]}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
def _is_cjk(c):
|
| 87 |
cp = ord(c)
|
| 88 |
return (
|
| 89 |
+
0x3400 <= cp <= 0x9FFF
|
| 90 |
+
or 0xF900 <= cp <= 0xFAFF
|
| 91 |
+
or 0x20000 <= cp <= 0x2A6DF
|
| 92 |
+
or 0x2A700 <= cp <= 0x2EBEF
|
| 93 |
+
or 0x30000 <= cp <= 0x323AF
|
| 94 |
)
|
| 95 |
|
| 96 |
|
|
|
|
| 118 |
if foreign:
|
| 119 |
bad.append((e['code'], v, foreign))
|
| 120 |
assert bad == [], f"name_ru/alts with non-Cyrillic letters: {bad[:10]}"
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# ---------------------------------------------------------------------------
|
| 124 |
+
# New tests
|
| 125 |
+
# ---------------------------------------------------------------------------
|
| 126 |
+
|
| 127 |
+
def test_code_format(dataset):
|
| 128 |
+
"""code must match ^[A-Z]{2}-[A-Z0-9]{1,6}$."""
|
| 129 |
+
bad = [(e['code'],) for e in dataset
|
| 130 |
+
if not re.match(r'^[A-Z]{2}-[A-Z0-9]{1,6}$', e.get('code', ''))]
|
| 131 |
+
assert bad == [], f"Malformed codes: {bad[:10]}"
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def test_code_unique(dataset):
|
| 135 |
+
"""Each code must be unique across the dataset."""
|
| 136 |
+
counts = Counter(e['code'] for e in dataset)
|
| 137 |
+
dups = [(c, n) for c, n in counts.items() if n > 1]
|
| 138 |
+
assert dups == [], f"Duplicate codes: {dups[:10]}"
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def test_region_matches_code_prefix(dataset):
|
| 142 |
+
"""region must equal the country prefix of the code (e.g. AD-02 → region=AD)."""
|
| 143 |
+
bad = [(e['code'], e.get('region')) for e in dataset
|
| 144 |
+
if e.get('region') != (e.get('code', '') or '')[:2]]
|
| 145 |
+
assert bad == [], f"region != code prefix: {bad[:10]}"
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def test_no_typographic_quotes_in_ru(dataset):
|
| 149 |
+
"""name_ru must not contain typographic quotes «»."""
|
| 150 |
+
bad = [(e['code'], e['name_ru']) for e in dataset
|
| 151 |
+
if e.get('name_ru') and re.search(r'[«»]', e['name_ru'])]
|
| 152 |
+
assert bad == [], f"name_ru with «» quotes: {bad[:10]}"
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def test_balanced_parentheses(dataset):
|
| 156 |
+
"""All name fields must have balanced ASCII and fullwidth parentheses."""
|
| 157 |
+
bad = []
|
| 158 |
+
for e in dataset:
|
| 159 |
+
for f in ('name_en', 'name_ru', 'name_zh'):
|
| 160 |
+
v = e.get(f) or ''
|
| 161 |
+
if v.count('(') != v.count(')') or v.count('(') != v.count(')'):
|
| 162 |
+
bad.append((e['code'], f, v))
|
| 163 |
+
assert bad == [], f"Unbalanced parens: {bad[:10]}"
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def test_no_whitespace_in_names(dataset):
|
| 167 |
+
"""Name fields must not have leading or trailing whitespace."""
|
| 168 |
+
bad = [(e['code'], f) for e in dataset for f in ('name_en', 'name_ru', 'name_zh')
|
| 169 |
+
if e.get(f) and e[f] != e[f].strip()]
|
| 170 |
+
assert bad == [], f"Leading/trailing whitespace: {bad[:10]}"
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def test_no_double_spaces_in_names(dataset):
|
| 174 |
+
"""Name fields must not contain consecutive spaces."""
|
| 175 |
+
bad = [(e['code'], f) for e in dataset for f in ('name_en', 'name_ru', 'name_zh')
|
| 176 |
+
if e.get(f) and ' ' in e[f]]
|
| 177 |
+
assert bad == [], f"Double spaces: {bad[:10]}"
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def test_no_newlines_in_names(dataset):
|
| 181 |
+
"""Name fields must not contain newline or tab characters."""
|
| 182 |
+
bad = [(e['code'], f) for e in dataset for f in ('name_en', 'name_ru', 'name_zh')
|
| 183 |
+
if any(c in (e.get(f) or '') for c in '\n\t\r')]
|
| 184 |
+
assert bad == [], f"Newline/tab in names: {bad[:10]}"
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def test_no_ideographic_space(dataset):
|
| 188 |
+
"""Name fields and alt lists must not contain ideographic space U+3000."""
|
| 189 |
+
bad = []
|
| 190 |
+
for e in dataset:
|
| 191 |
+
for f in ('name_en', 'name_ru', 'name_zh'):
|
| 192 |
+
if ' ' in (e.get(f) or ''):
|
| 193 |
+
bad.append((e['code'], f))
|
| 194 |
+
for lst in ('names_en_alt', 'names_ru_alt', 'names_zh_alt'):
|
| 195 |
+
for v in (e.get(lst) or []):
|
| 196 |
+
if ' ' in v:
|
| 197 |
+
bad.append((e['code'], lst))
|
| 198 |
+
assert bad == [], f"Ideographic space: {bad[:10]}"
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def test_name_en_starts_uppercase(dataset):
|
| 202 |
+
"""name_en first alphabetic character must be uppercase."""
|
| 203 |
+
bad = [(e['code'], e['name_en']) for e in dataset
|
| 204 |
+
if e.get('name_en') and any(c.isalpha() for c in e['name_en'])
|
| 205 |
+
and next(c for c in e['name_en'] if c.isalpha()).islower()]
|
| 206 |
+
assert bad == [], f"name_en starts lowercase: {bad[:10]}"
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def test_name_en_not_allcaps(dataset):
|
| 210 |
+
"""name_en must not be entirely uppercase."""
|
| 211 |
+
bad = [(e['code'], e['name_en']) for e in dataset
|
| 212 |
+
if e.get('name_en')
|
| 213 |
+
and e['name_en'] == e['name_en'].upper()
|
| 214 |
+
and e['name_en'].replace(' ', '').replace('-', '').isalpha()]
|
| 215 |
+
assert bad == [], f"name_en all-caps: {bad[:10]}"
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
def test_name_en_no_trailing_punctuation(dataset):
|
| 219 |
+
"""name_en must not end with ., ,, ;, :, !, ?."""
|
| 220 |
+
bad = [(e['code'], e['name_en']) for e in dataset
|
| 221 |
+
if e.get('name_en') and e['name_en'][-1] in '.,:;!?']
|
| 222 |
+
assert bad == [], f"name_en trailing punctuation: {bad[:10]}"
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def test_alt_names_not_duplicate_primary(dataset):
|
| 226 |
+
"""No alt list item may equal its primary name field."""
|
| 227 |
+
bad = []
|
| 228 |
+
for e in dataset:
|
| 229 |
+
for field, primary_field in [
|
| 230 |
+
('names_en_alt', 'name_en'),
|
| 231 |
+
('names_ru_alt', 'name_ru'),
|
| 232 |
+
('names_zh_alt', 'name_zh'),
|
| 233 |
+
]:
|
| 234 |
+
primary = e.get(primary_field)
|
| 235 |
+
for v in (e.get(field) or []):
|
| 236 |
+
if v == primary:
|
| 237 |
+
bad.append((e['code'], field, v))
|
| 238 |
+
assert bad == [], f"Alt duplicates primary: {bad[:10]}"
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
def test_no_duplicate_names_within_entry(dataset):
|
| 242 |
+
"""No value may appear twice across all name fields of a single row."""
|
| 243 |
+
bad = []
|
| 244 |
+
for e in dataset:
|
| 245 |
+
vals = []
|
| 246 |
+
for f in ('name_en', 'name_ru', 'name_zh'):
|
| 247 |
+
if e.get(f):
|
| 248 |
+
vals.append(e[f])
|
| 249 |
+
for lst in ('names_en_alt', 'names_ru_alt', 'names_zh_alt'):
|
| 250 |
+
vals += (e.get(lst) or [])
|
| 251 |
+
seen = set()
|
| 252 |
+
duped = set()
|
| 253 |
+
for v in vals:
|
| 254 |
+
if v in seen:
|
| 255 |
+
duped.add(v)
|
| 256 |
+
seen.add(v)
|
| 257 |
+
if duped:
|
| 258 |
+
bad.append((e['code'], sorted(duped)))
|
| 259 |
+
assert bad == [], f"Duplicate values within entry: {bad[:10]}"
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def test_links_are_urls(dataset):
|
| 263 |
+
"""Every link must start with http:// or https://."""
|
| 264 |
+
bad = [(e['code'], l) for e in dataset for l in (e.get('links') or [])
|
| 265 |
+
if not l.startswith(('http://', 'https://'))]
|
| 266 |
+
assert bad == [], f"Bad links: {bad[:10]}"
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
def test_no_duplicate_links(dataset):
|
| 270 |
+
"""No duplicate URLs within a row's link list."""
|
| 271 |
+
bad = [(e['code'],) for e in dataset
|
| 272 |
+
if len(e.get('links') or []) != len(set(e.get('links') or []))]
|
| 273 |
+
assert bad == [], f"Duplicate links: {bad[:10]}"
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
def test_alt_lists_balanced_parens(dataset):
|
| 277 |
+
"""Alt list items must have balanced parentheses."""
|
| 278 |
+
bad = []
|
| 279 |
+
for e in dataset:
|
| 280 |
+
for lst in ('names_en_alt', 'names_ru_alt', 'names_zh_alt'):
|
| 281 |
+
for v in (e.get(lst) or []):
|
| 282 |
+
if v.count('(') != v.count(')') or v.count('(') != v.count(')'):
|
| 283 |
+
bad.append((e['code'], lst, v))
|
| 284 |
+
assert bad == [], f"Unbalanced parens in alt lists: {bad[:10]}"
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def test_alt_lists_en_no_cjk(dataset):
|
| 288 |
+
"""names_en_alt items must not contain CJK characters."""
|
| 289 |
+
bad = [(e['code'], v) for e in dataset
|
| 290 |
+
for v in (e.get('names_en_alt') or [])
|
| 291 |
+
if any(_is_cjk(c) for c in v)]
|
| 292 |
+
assert bad == [], f"CJK in names_en_alt: {bad[:10]}"
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
def test_alt_lists_en_no_cyrillic(dataset):
|
| 296 |
+
"""names_en_alt items must not contain Cyrillic characters."""
|
| 297 |
+
bad = [(e['code'], v) for e in dataset
|
| 298 |
+
for v in (e.get('names_en_alt') or [])
|
| 299 |
+
if any('Ѐ' <= c <= 'ӿ' for c in v)]
|
| 300 |
+
assert bad == [], f"Cyrillic in names_en_alt: {bad[:10]}"
|