Add invariant tests
Browse files- test/test_invariants.py +116 -0
test/test_invariants.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Invariant tests for cterdam/iso_3166-2 dataset.
|
| 3 |
+
|
| 4 |
+
Run from dataset root after cloning:
|
| 5 |
+
pytest test/test_invariants.py -v
|
| 6 |
+
|
| 7 |
+
Verifies:
|
| 8 |
+
- name field matches lang selection (name_en/ru/zh)
|
| 9 |
+
- hash = SHA256(name)
|
| 10 |
+
- lang matches parent region's language rule
|
| 11 |
+
- hash format is 64-char hex string
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import hashlib
|
| 15 |
+
import pytest
|
| 16 |
+
from datasets import load_dataset
|
| 17 |
+
|
| 18 |
+
SLAVIC_REGIONS = {
|
| 19 |
+
'RU', 'UA', 'BY', 'KZ', 'UZ', 'TJ', 'KG', 'TM', 'GE', 'AZ', 'AM', 'MD',
|
| 20 |
+
'LT', 'LV', 'EE', 'RS', 'ME', 'BA', 'HR', 'SI', 'SK', 'CZ', 'PL', 'BG', 'MK', 'FI'
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
SINOPHONE_REGIONS = {
|
| 24 |
+
'CN', 'TW', 'HK', 'MO', 'JP', 'KR', 'KP', 'MN', 'SG', 'VN', 'LA', 'TH', 'MM', 'KH', 'MY', 'BN'
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def determine_expected_lang(region: str) -> str:
|
| 29 |
+
"""Determine expected lang from parent region code."""
|
| 30 |
+
if region in SINOPHONE_REGIONS:
|
| 31 |
+
return 'zh'
|
| 32 |
+
elif region in SLAVIC_REGIONS:
|
| 33 |
+
return 'ru'
|
| 34 |
+
else:
|
| 35 |
+
return 'en'
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@pytest.fixture(scope="session")
|
| 39 |
+
def dataset():
|
| 40 |
+
return load_dataset('.', split='train')
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def test_lang_values_valid(dataset):
|
| 44 |
+
"""lang field must be one of: en, ru, zh."""
|
| 45 |
+
invalid = [(i, e['lang']) for i, e in enumerate(dataset) if e.get('lang') not in ('en', 'ru', 'zh')]
|
| 46 |
+
assert invalid == [], f"Invalid lang values: {invalid[:5]}"
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_hash_format_valid(dataset):
|
| 50 |
+
"""hash field must be 64-char hex string (if name exists)."""
|
| 51 |
+
invalid = [
|
| 52 |
+
(i, e['hash'])
|
| 53 |
+
for i, e in enumerate(dataset)
|
| 54 |
+
if e.get('hash') is not None and not (
|
| 55 |
+
isinstance(e['hash'], str) and
|
| 56 |
+
len(e['hash']) == 64 and
|
| 57 |
+
all(c in '0123456789abcdef' for c in e['hash'])
|
| 58 |
+
)
|
| 59 |
+
]
|
| 60 |
+
assert invalid == [], f"Invalid hash format: {invalid[:5]}"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def test_name_matches_lang_field(dataset):
|
| 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 |
+
|
| 68 |
+
for i, entry in enumerate(dataset):
|
| 69 |
+
name = entry.get('name')
|
| 70 |
+
lang = entry.get('lang')
|
| 71 |
+
if not name or not lang:
|
| 72 |
+
continue
|
| 73 |
+
|
| 74 |
+
field_name = lang_to_field.get(lang)
|
| 75 |
+
lang_field_value = entry.get(field_name)
|
| 76 |
+
|
| 77 |
+
if name != lang_field_value:
|
| 78 |
+
mismatches.append((i, entry.get('code'), lang, name[:20], lang_field_value[:20] if lang_field_value else None))
|
| 79 |
+
if len(mismatches) >= 5:
|
| 80 |
+
break
|
| 81 |
+
|
| 82 |
+
assert mismatches == [], f"Name/lang field mismatches: {mismatches}"
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def test_hash_matches_name(dataset):
|
| 86 |
+
"""hash field must equal SHA256(name)."""
|
| 87 |
+
mismatches = []
|
| 88 |
+
|
| 89 |
+
for i, entry in enumerate(dataset):
|
| 90 |
+
name = entry.get('name')
|
| 91 |
+
stored_hash = entry.get('hash')
|
| 92 |
+
if name and stored_hash:
|
| 93 |
+
expected_hash = hashlib.sha256(name.encode()).hexdigest()
|
| 94 |
+
if stored_hash != expected_hash:
|
| 95 |
+
mismatches.append((i, entry.get('code'), stored_hash[:8], expected_hash[:8]))
|
| 96 |
+
if len(mismatches) >= 5:
|
| 97 |
+
break
|
| 98 |
+
|
| 99 |
+
assert mismatches == [], f"Hash mismatches: {mismatches}"
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def test_lang_matches_parent_region(dataset):
|
| 103 |
+
"""lang field must match parent region's language rule."""
|
| 104 |
+
mismatches = []
|
| 105 |
+
|
| 106 |
+
for i, entry in enumerate(dataset):
|
| 107 |
+
region = entry.get('region')
|
| 108 |
+
lang = entry.get('lang')
|
| 109 |
+
expected_lang = determine_expected_lang(region)
|
| 110 |
+
|
| 111 |
+
if lang != expected_lang:
|
| 112 |
+
mismatches.append((i, entry.get('code'), region, lang, expected_lang))
|
| 113 |
+
if len(mismatches) >= 5:
|
| 114 |
+
break
|
| 115 |
+
|
| 116 |
+
assert mismatches == [], f"Lang mismatches: {mismatches}"
|