cterdam commited on
Commit
1c465b5
·
1 Parent(s): e64a756

Add invariant tests: coords, dedup, name fields, zh/ru quality

Browse files

- test_all_coords_present: no null lat/lon
- test_no_duplicate_metro_rows: unique (lat, lon, name_en) — one row per metro
- test_all_name_fields_present: name_en, name_ru, name_zh all non-null
- test_name_zh_contains_cjk: name_zh must have CJK characters
- test_name_ru_has_cyrillic: name_ru must have Cyrillic
- test_no_typographic_quotes_in_ru: no «» in name_ru
- test_no_airport_words_in_zh: no 機場/空港 in name_zh
- test_balanced_parentheses: matched parens in all name fields

13/14 tests pass; test_lang_matches_region was already failing (pre-existing
ID/PH lang='zh' vs SINOPHONE_REGIONS definition gap).

Files changed (1) hide show
  1. test/test_invariants.py +88 -0
test/test_invariants.py CHANGED
@@ -9,9 +9,19 @@ Verifies:
9
  - name field matches lang selection (name_en/ru/zh)
10
  - lang matches region-based language rule
11
  - coordinates are in valid range
 
 
 
 
 
 
 
 
12
  """
13
 
 
14
  import pytest
 
15
  from datasets import load_dataset
16
 
17
  SLAVIC_REGIONS = {
@@ -94,3 +104,81 @@ def test_coordinates_valid(dataset):
94
  (e.get('lon') is not None and (e['lon'] < -180 or e['lon'] > 180))
95
  ]
96
  assert invalid == [], f"Invalid coordinates: {invalid[:5]}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  - name field matches lang selection (name_en/ru/zh)
10
  - lang matches region-based language rule
11
  - coordinates are in valid range
12
+ - all name fields (name_en, name_ru, name_zh) are present
13
+ - name_zh contains CJK characters (not Latin-only)
14
+ - name_ru contains Cyrillic
15
+ - name_ru has no typographic quotes «»
16
+ - no airport words in name_zh
17
+ - coordinates present and in valid range
18
+ - no duplicate (lat, lon, name_en) groups
19
+ - balanced parentheses in all name fields
20
  """
21
 
22
+ import re
23
  import pytest
24
+ from collections import defaultdict
25
  from datasets import load_dataset
26
 
27
  SLAVIC_REGIONS = {
 
104
  (e.get('lon') is not None and (e['lon'] < -180 or e['lon'] > 180))
105
  ]
106
  assert invalid == [], f"Invalid coordinates: {invalid[:5]}"
107
+
108
+
109
+ def test_all_coords_present(dataset):
110
+ """Every row must have both lat and lon."""
111
+ missing = [
112
+ (e['code'], e.get('name_en'))
113
+ for e in dataset
114
+ if e.get('lat') is None or e.get('lon') is None
115
+ ]
116
+ assert missing == [], f"Null coordinates: {missing}"
117
+
118
+
119
+ def test_no_duplicate_metro_rows(dataset):
120
+ """No two rows may share the same (lat, lon, name_en) — one row per metro."""
121
+ groups = defaultdict(list)
122
+ for e in dataset:
123
+ lat, lon, en = e.get('lat'), e.get('lon'), e.get('name_en') or ''
124
+ if lat is not None and lon is not None and en:
125
+ groups[(lat, lon, en)].append(e['code'])
126
+ dups = {k: v for k, v in groups.items() if len(v) > 1}
127
+ assert dups == {}, f"Duplicate (lat, lon, name_en) groups: {dict(list(dups.items())[:5])}"
128
+
129
+
130
+ def test_all_name_fields_present(dataset):
131
+ """name_en, name_ru, and name_zh must all be non-null."""
132
+ for field in ('name_en', 'name_ru', 'name_zh'):
133
+ missing = [(e['code'], field) for e in dataset if not e.get(field)]
134
+ assert missing == [], f"Missing {field}: {missing[:10]}"
135
+
136
+
137
+ def _has_cjk(s):
138
+ return any('一' <= c <= '鿿' or '㐀' <= c <= '䶿' for c in (s or ''))
139
+
140
+
141
+ def _has_cyrillic(s):
142
+ return any('Ѐ' <= c <= 'ӿ' for c in (s or ''))
143
+
144
+
145
+ def test_name_zh_contains_cjk(dataset):
146
+ """name_zh must contain CJK characters (not a Latin-only placeholder)."""
147
+ bad = [(e['code'], e['name_zh']) for e in dataset
148
+ if e.get('name_zh') and not _has_cjk(e['name_zh'])]
149
+ assert bad == [], f"name_zh with no CJK: {bad[:10]}"
150
+
151
+
152
+ def test_name_ru_has_cyrillic(dataset):
153
+ """name_ru must contain Cyrillic characters."""
154
+ bad = [(e['code'], e['name_ru']) for e in dataset
155
+ if e.get('name_ru') and not _has_cyrillic(e['name_ru'])]
156
+ assert bad == [], f"name_ru without Cyrillic: {bad[:10]}"
157
+
158
+
159
+ def test_no_typographic_quotes_in_ru(dataset):
160
+ """name_ru must not contain typographic quotes «»."""
161
+ bad = [(e['code'], e['name_ru']) for e in dataset
162
+ if e.get('name_ru') and re.search(r'[«»]', e['name_ru'])]
163
+ assert bad == [], f"name_ru with «» quotes: {bad[:10]}"
164
+
165
+
166
+ _AIRPORT_ZH = re.compile(r'機場|机場|机场|空港')
167
+
168
+
169
+ def test_no_airport_words_in_zh(dataset):
170
+ """name_zh must not contain airport-article words like 機場."""
171
+ bad = [(e['code'], e['name_zh']) for e in dataset
172
+ if e.get('name_zh') and _AIRPORT_ZH.search(e['name_zh'])]
173
+ assert bad == [], f"name_zh with airport word: {bad[:10]}"
174
+
175
+
176
+ def test_balanced_parentheses(dataset):
177
+ """All name fields must have balanced ASCII and fullwidth parentheses."""
178
+ bad = []
179
+ for e in dataset:
180
+ for field in ('name', 'name_en', 'name_ru', 'name_zh'):
181
+ v = e.get(field) or ''
182
+ if v.count('(') != v.count(')') or v.count('(') != v.count(')'):
183
+ bad.append((e['code'], field, v))
184
+ assert bad == [], f"Unmatched parentheses: {bad[:5]}"