| """ |
| Invariant tests for cterdam/city dataset. |
| |
| Run from dataset root after cloning: |
| pip install datasets pytest |
| pytest test/test_invariants.py -v |
| |
| Verifies: |
| - name field matches lang selection (name_en/ru/zh) |
| - lang matches region-based language rule |
| - coordinates are in valid range and present |
| - all name fields (name_en, name_ru, name_zh) are present |
| - name_ru has no typographic quotes «» |
| - no airport words in name_zh |
| - no duplicate (lat, lon, name_en) groups |
| - balanced parentheses in all name fields |
| - code is 3 uppercase letters and unique |
| - region is a valid code from cterdam/country |
| - subdiv (when present) is a valid code from cterdam/region |
| - no whitespace in name fields |
| - name_en and name_ru start uppercase |
| - name_zh has no state/country qualifier in parentheses |
| - name_ru has no admin/region qualifier in parentheses |
| - name_zh is not a country or US state name (fill errors) |
| """ |
|
|
| import re |
| import pytest |
| from collections import defaultdict |
| from datasets import load_dataset |
|
|
| |
| |
| SLAVIC_REGIONS = { |
| 'RU', 'UA', 'BY', 'LT', 'LV', 'EE', 'MD', 'GE', 'AM', 'AZ', 'KZ', 'KG', |
| 'TJ', 'TM', 'UZ', 'AL', 'BG', 'CZ', 'HU', 'PL', 'RO', 'SK', 'BA', 'HR', |
| 'ME', 'MK', 'RS', 'SI', 'FI', |
| } |
|
|
| SINOPHONE_REGIONS = { |
| 'CN', 'HK', 'MO', 'TW', 'KP', 'KR', 'MN', 'JP', 'VN', 'LA', 'MY', 'TH', |
| 'SG', 'PH', 'MM', 'ID', 'KH', 'BN', |
| } |
|
|
|
|
| def determine_expected_lang(region_code: str) -> str: |
| if region_code in SINOPHONE_REGIONS: |
| return 'zh' |
| if region_code in SLAVIC_REGIONS: |
| return 'ru' |
| return 'en' |
|
|
|
|
| @pytest.fixture(scope="session") |
| def valid_regions(): |
| """Valid ISO 3166-1 alpha-2 codes from cterdam/country.""" |
| ds = load_dataset('cterdam/country', split='train') |
| return {r['code'] for r in ds} |
|
|
|
|
| @pytest.fixture(scope="session") |
| def valid_subdivs(): |
| """Valid ISO 3166-2 codes from cterdam/region.""" |
| ds = load_dataset('cterdam/region', split='train') |
| return {r['code'] for r in ds} |
|
|
|
|
| @pytest.fixture(scope="session") |
| def subdiv_region_map(): |
| """Map from ISO 3166-2 subdivision code to its parent country code.""" |
| ds = load_dataset('cterdam/region', split='train') |
| return {r['code']: r['region'] for r in ds if r.get('region')} |
|
|
|
|
| @pytest.fixture(scope="session") |
| def dataset(): |
| """Load dataset from HF Hub.""" |
| return load_dataset('cterdam/city', split='train') |
|
|
|
|
| def test_lang_values_valid(dataset): |
| """lang field must be one of: en, ru, zh.""" |
| invalid = [(i, e['lang']) for i, e in enumerate(dataset) if e.get('lang') not in ('en', 'ru', 'zh')] |
| assert invalid == [], f"Invalid lang values: {invalid[:5]}" |
|
|
|
|
| def test_name_matches_lang_field(dataset): |
| """name field must equal name_en/ru/zh based on lang.""" |
| lang_to_field = {'en': 'name_en', 'ru': 'name_ru', 'zh': 'name_zh'} |
| mismatches = [] |
|
|
| for i, entry in enumerate(dataset): |
| name = entry.get('name') |
| lang = entry.get('lang') |
| if not name or not lang: |
| continue |
|
|
| field_name = lang_to_field.get(lang) |
| lang_field_value = entry.get(field_name) |
|
|
| if name != lang_field_value: |
| mismatches.append((i, entry.get('code'), lang, name[:20], lang_field_value[:20] if lang_field_value else None)) |
| if len(mismatches) >= 5: |
| break |
|
|
| assert mismatches == [], f"Name/lang field mismatches: {mismatches}" |
|
|
|
|
| def test_lang_matches_region(dataset): |
| """lang field must match the region's language rule.""" |
| mismatches = [] |
|
|
| for i, entry in enumerate(dataset): |
| region = entry.get('region') or '' |
| lang = entry.get('lang') |
| expected = determine_expected_lang(region) |
|
|
| if lang != expected: |
| mismatches.append((i, entry.get('code'), region, lang, expected)) |
| if len(mismatches) >= 5: |
| break |
|
|
| assert mismatches == [], f"Lang mismatches: {mismatches}" |
|
|
|
|
| def test_coordinates_valid(dataset): |
| """Coordinates must be valid (lat: -90..90, lon: -180..180).""" |
| invalid = [ |
| (i, e['code'], e.get('lat'), e.get('lon')) |
| for i, e in enumerate(dataset) |
| if (e.get('lat') is not None and (e['lat'] < -90 or e['lat'] > 90)) or |
| (e.get('lon') is not None and (e['lon'] < -180 or e['lon'] > 180)) |
| ] |
| assert invalid == [], f"Invalid coordinates: {invalid[:5]}" |
|
|
|
|
| def test_all_coords_present(dataset): |
| """Every row must have both lat and lon.""" |
| missing = [ |
| (e['code'], e.get('name_en')) |
| for e in dataset |
| if e.get('lat') is None or e.get('lon') is None |
| ] |
| assert missing == [], f"Null coordinates: {missing}" |
|
|
|
|
| def test_no_duplicate_metro_rows(dataset): |
| """No two rows may share the same (lat, lon, name_en) — one row per metro.""" |
| groups = defaultdict(list) |
| for e in dataset: |
| lat, lon, en = e.get('lat'), e.get('lon'), e.get('name_en') or '' |
| if lat is not None and lon is not None and en: |
| groups[(lat, lon, en)].append(e['code']) |
| dups = {k: v for k, v in groups.items() if len(v) > 1} |
| assert dups == {}, f"Duplicate (lat, lon, name_en) groups: {dict(list(dups.items())[:5])}" |
|
|
|
|
| def test_all_name_fields_present(dataset): |
| """name_en, name_ru, and name_zh must all be non-null.""" |
| for field in ('name_en', 'name_ru', 'name_zh'): |
| missing = [(e['code'], field) for e in dataset if not e.get(field)] |
| assert missing == [], f"Missing {field}: {missing[:10]}" |
|
|
|
|
| def test_no_typographic_quotes_in_ru(dataset): |
| """name_ru must not contain typographic quotes «».""" |
| bad = [(e['code'], e['name_ru']) for e in dataset |
| if e.get('name_ru') and re.search(r'[«»]', e['name_ru'])] |
| assert bad == [], f"name_ru with «» quotes: {bad[:10]}" |
|
|
|
|
| _AIRPORT_ZH = re.compile(r'機場|机場|机场|空港|航空') |
|
|
| |
| _ZH_QUALIFIER_WORDS = [ |
| |
| '阿拉巴馬州', '亞拉巴馬州', '阿拉斯加州', '亞利桑那州', '阿肯色州', |
| '加利福尼亞州', '加州', '科羅拉多州', '康涅狄格州', '特拉華州', |
| '佛羅裡達州', '佛羅里達州', '喬治亞州', '夏威夷州', '愛達荷州', |
| '伊利諾伊州', '伊利諾州', '印第安納州', '印第安那州', '愛荷華州', |
| '堪薩斯州', '肯塔基州', '路易斯安那州', '路易斯安納州', '緬因州', |
| '馬裡蘭州', '馬里蘭州', '馬薩諸塞州', '麻薩諸塞州', '密歇根州', |
| '密西根州', '明尼蘇達州', '密西西比州', '密蘇裡州', '密蘇里州', |
| '蒙大拿州', '內布拉斯加州', '內華達州', '新罕布什爾州', '新澤西州', |
| '新墨西哥州', '紐約州', '北卡羅來納州', '北卡羅萊納州', '北達科他州', |
| '俄亥俄州', '俄克拉荷馬州', '俄勒岡州', '奧勒岡州', '賓夕法尼亞州', |
| '羅德島州', '羅得島州', '南卡羅來納州', '南卡羅萊納州', '南達科他州', |
| '田納西州', '德克薩斯州', '德州', '猶他州', '佛蒙特州', '弗吉尼亞州', |
| '維吉尼亞州', '華盛頓州', '西弗吉尼亞州', '威斯康星州', '懷俄明州', |
| |
| '不列顛哥倫比亞', '阿爾伯塔省', '艾伯塔省', '薩斯喀徹溫', '曼尼托巴', |
| '安大略省', '魁北克', '紐芬蘭-拉布拉多', '紐芬蘭', '新不倫瑞克省', |
| '新斯科舍', '愛德華王子島', '育空', '西北地區', '努納武特', |
| |
| '加拿大', '美國', '巴西', '墨西哥', '阿根廷', '巴拉圭', '哥倫比亞', |
| '土耳其', '安圭拉', '多明尼加', '哥斯大黎加', '巴哈馬', '厄瓜多', |
| '塔斯馬尼亞州', '俄羅斯', '法國', '德國', '英國', '蘇格蘭', '澳大利亞', |
| '美屬維爾京群島', '挪威', '中國', '日本', '韓國', '印度', '伊朗', |
| |
| '漢特-曼西自治區', |
| ] |
| _ZH_QUAL_RE = re.compile( |
| r'[((](?:[^))]*?(?:' + |
| '|'.join(re.escape(w) for w in _ZH_QUALIFIER_WORDS) + |
| r')[^))]*?)[))]' |
| ) |
|
|
| |
| _RU_QUAL_RE = re.compile( |
| r'\s*\((?:' |
| r'Аляска|Канзас|Нью-Йорк|Флорида|Канада|Сеара' |
| r'|Шотландия|Северная Каролина|Колумбия|Айова' |
| r'|Британская Колумбия|Ньюфаундленд и Лабрадор' |
| r'|провинция Антике|Якутия' |
| r'|штат Айова|шт\. Айова|г\. Айова' |
| r'|остров|деревня|город|село' |
| r'|округ|провинция|область' |
| r')[^)]*?\)$' |
| ) |
|
|
| _COUNTRY_ZH = { |
| '南非', '緬甸', '伊朗', '泰國', '阿曼', '約旦', '加彭', '瑞典', |
| '馬達加斯加', '吉里巴斯', '馬紹爾群島', '巴布亞紐幾內亞', '巴布亞', |
| '索羅門群島', '斐濟', '衣索比亞', '奈米比亞', '剛果民主共和國', |
| '肯亞', '肯尼亞', '巴基斯坦', '俄羅斯', '加拿大', '美國', '美国', |
| '澳大利亞', '澳洲', '英國', '法國', '德國', '中國', '印度', |
| '意大利', '義大利', '西班牙', '希臘', '土耳其', '埃及', |
| '尼日利亞', '墨西哥', '巴西', '阿根廷', '智利', '波蘭', |
| '比利時', '荷蘭', '丹麥', '挪威', '芬蘭', '韓國', '日本', |
| '新西蘭', '紐西蘭', '越南', '印尼', '印度尼西亞', '馬來西亞', |
| '菲律賓', |
| } |
|
|
| _STATE_ZH = { |
| '阿拉巴馬州', '阿拉斯加州', '亞利桑那州', '阿肯色州', '加利福尼亞州', |
| '科羅拉多州', '康涅狄格州', '特拉華州', '佛羅里達州', '喬治亞州', |
| '佐治亞州', '夏威夷州', '愛達荷州', '伊利諾伊州', '印第安納州', |
| '愛荷華州', '堪薩斯州', '肯塔基州', '路易斯安那州', '緬因州', |
| '馬裡蘭州', '馬里蘭州', '馬薩諸塞州', '密歇根州', '明尼蘇達州', |
| '密西西比州', '密蘇里州', '蒙大拿州', '內布拉斯加州', '內華達州', |
| '新罕布什爾州', '新澤西州', '新墨西哥州', '紐約州', '北卡羅來納州', |
| '北達科他州', '俄亥俄州', '俄克拉荷馬州', '俄勒岡州', '賓夕法尼亞州', |
| '羅得島州', '南卡羅來納州', '南達科他州', '田納西州', '德克薩斯州', |
| '猶他州', '佛蒙特州', '弗吉尼亞州', '華盛頓州', '西弗吉尼亞州', |
| '威斯康星州', '懷俄明州', |
| } |
|
|
|
|
| def test_no_airport_words_in_zh(dataset): |
| """name_zh must not contain airport/aviation words like 機場 or 航空.""" |
| bad = [(e['code'], e['name_zh']) for e in dataset |
| if e.get('name_zh') and _AIRPORT_ZH.search(e['name_zh'])] |
| assert bad == [], f"name_zh with airport word: {bad[:10]}" |
|
|
|
|
| def test_balanced_parentheses(dataset): |
| """All name fields and alt lists must have balanced ASCII and fullwidth parentheses.""" |
| bad = [] |
| for e in dataset: |
| for field in ('name', 'name_en', 'name_ru', 'name_zh'): |
| v = e.get(field) or '' |
| if v.count('(') != v.count(')') or v.count('(') != v.count(')'): |
| bad.append((e['code'], field, v)) |
| for field in ('names_en_alt', 'names_ru_alt', 'names_zh_alt'): |
| for v in (e.get(field) or []): |
| if v.count('(') != v.count(')') or v.count('(') != v.count(')'): |
| bad.append((e['code'], field, v)) |
| assert bad == [], f"Unmatched parentheses: {bad[:5]}" |
|
|
|
|
| _CODE_RE = re.compile(r'^[A-Z]{3}$') |
|
|
|
|
| def test_code_format(dataset): |
| """code must be exactly 3 uppercase ASCII letters.""" |
| bad = [(e['code'],) for e in dataset if not _CODE_RE.match(e.get('code') or '')] |
| assert bad == [], f"Malformed codes: {bad[:10]}" |
|
|
|
|
| def test_code_unique(dataset): |
| """Every IATA code must appear exactly once.""" |
| seen = defaultdict(int) |
| for e in dataset: |
| seen[e['code']] += 1 |
| dups = {c: n for c, n in seen.items() if n > 1} |
| assert dups == {}, f"Duplicate codes: {dups}" |
|
|
|
|
| def test_region_valid(dataset, valid_regions): |
| """region must be a valid ISO 3166-1 alpha-2 code per cterdam/country.""" |
| bad = [(e['code'], e['region']) for e in dataset |
| if e.get('region') and e['region'] not in valid_regions] |
| assert bad == [], f"Invalid region codes: {bad[:10]}" |
|
|
|
|
| def test_subdiv_valid(dataset, valid_subdivs): |
| """subdiv, when present, must be a valid ISO 3166-2 code per cterdam/region.""" |
| bad = [(e['code'], e['subdiv']) for e in dataset |
| if e.get('subdiv') and e['subdiv'] not in valid_subdivs] |
| assert bad == [], f"Invalid subdiv codes: {bad[:10]}" |
|
|
|
|
| def test_no_whitespace_in_names(dataset): |
| """No name field may have leading or trailing whitespace.""" |
| bad = [] |
| for e in dataset: |
| for field in ('name', 'name_en', 'name_ru', 'name_zh'): |
| v = e.get(field) |
| if v and v != v.strip(): |
| bad.append((e['code'], field, repr(v))) |
| assert bad == [], f"Whitespace in name fields: {bad[:10]}" |
|
|
|
|
| def test_name_en_starts_uppercase(dataset): |
| """name_en and names_en_alt must not start with a lowercase letter.""" |
| bad = [(e['code'], v) for e in dataset |
| for v in [e.get('name_en')] + list(e.get('names_en_alt') or []) |
| if v and v[0].islower()] |
| assert bad == [], f"name_en/alts starting lowercase: {bad[:10]}" |
|
|
|
|
| def test_name_ru_starts_uppercase(dataset): |
| """name_ru and names_ru_alt must not start with a lowercase character.""" |
| bad = [(e['code'], v) for e in dataset |
| for v in [e.get('name_ru')] + list(e.get('names_ru_alt') or []) |
| if v and v[0].islower()] |
| assert bad == [], f"name_ru/alts starting lowercase: {bad[:10]}" |
|
|
|
|
| def test_subdiv_matches_region(dataset, subdiv_region_map, valid_subdivs): |
| """subdiv's parent country in iso_3166-2 must match the row's region. |
| |
| Overseas territories are exempt: they carry their own ISO 3166-1 alpha-2 |
| code as region but their subdivisions are recorded under the administrative |
| parent (e.g. French Polynesia region=PF, subdiv=FR-PF; Guadeloupe |
| region=GP, subdiv=FR-971). We skip a mismatch when either the canonical |
| territory subdiv code (parent+'-'+region) exists in iso_3166-2, or the |
| (region, parent) pair is a known territory-to-parent mapping. |
| """ |
| |
| TERRITORY_PARENTS = { |
| 'GP': 'FR', |
| 'GF': 'FR', |
| 'MQ': 'FR', |
| 'RE': 'FR', |
| 'YT': 'FR', |
| 'BQ': 'NL', |
| 'SJ': 'NO', |
| 'AX': 'FI', |
| 'XK': 'RS', |
| 'KX': 'UA', |
| } |
| bad = [] |
| for e in dataset: |
| subdiv = e.get('subdiv') |
| region = e.get('region') |
| if subdiv and region: |
| expected = subdiv_region_map.get(subdiv) |
| if expected and expected != region: |
| if TERRITORY_PARENTS.get(region) == expected: |
| continue |
| if (expected + '-' + region) in valid_subdivs: |
| continue |
| bad.append((e['code'], region, subdiv, expected)) |
| assert bad == [], f"Subdiv/region mismatch: {bad[:10]}" |
|
|
|
|
| def test_no_null_island(dataset): |
| """No entry may have lat=0.0 and lon=0.0 simultaneously.""" |
| bad = [(e['code'], e.get('name_en')) for e in dataset |
| if e.get('lat') == 0.0 and e.get('lon') == 0.0] |
| assert bad == [], f"Null Island coordinates: {bad}" |
|
|
|
|
| def test_links_are_urls(dataset): |
| """Each link, when present, must be an http:// or https:// URL.""" |
| bad = [] |
| for e in dataset: |
| for link in (e.get('links') or []): |
| if not (link.startswith('http://') or link.startswith('https://')): |
| bad.append((e['code'], link)) |
| assert bad == [], f"Non-URL links: {bad[:10]}" |
|
|
|
|
| def test_no_newlines_in_names(dataset): |
| """Name fields must not contain newline or tab characters.""" |
| bad = [] |
| for e in dataset: |
| for field in ('name', 'name_en', 'name_ru', 'name_zh'): |
| v = e.get(field) or '' |
| if any(c in v for c in ('\n', '\r', '\t')): |
| bad.append((e['code'], field)) |
| assert bad == [], f"Newlines/tabs in name fields: {bad[:10]}" |
|
|
|
|
| def test_name_zh_only_cjk_letters(dataset): |
| """All letter chars in name_zh and names_zh_alt must be CJK.""" |
| bad = [] |
| for e in dataset: |
| for v in [e.get('name_zh')] + list(e.get('names_zh_alt') or []): |
| if not v: |
| continue |
| foreign = [c for c in v if c.isalpha() and not ( |
| '㐀' <= c <= '鿿' or '豈' <= c <= '' |
| )] |
| if foreign: |
| bad.append((e['code'], v, foreign)) |
| assert bad == [], f"name_zh/alts with non-CJK letters: {bad[:10]}" |
|
|
|
|
| def test_name_ru_only_cyrillic_letters(dataset): |
| """All letter chars in name_ru and names_ru_alt must be Cyrillic.""" |
| bad = [] |
| for e in dataset: |
| for v in [e.get('name_ru')] + list(e.get('names_ru_alt') or []): |
| if not v: |
| continue |
| foreign = [c for c in v if c.isalpha() and not ('Ѐ' <= c <= 'ӿ')] |
| if foreign: |
| bad.append((e['code'], v, foreign)) |
| assert bad == [], f"name_ru/alts with non-Cyrillic letters: {bad[:10]}" |
|
|
|
|
| def test_no_double_spaces_in_names(dataset): |
| """Name fields must not contain consecutive spaces.""" |
| bad = [] |
| for e in dataset: |
| for field in ('name', 'name_en', 'name_ru', 'name_zh'): |
| v = e.get(field) or '' |
| if ' ' in v: |
| bad.append((e['code'], field, repr(v))) |
| assert bad == [], f"Double spaces in name fields: {bad[:10]}" |
|
|
|
|
| def test_name_en_no_trailing_punctuation(dataset): |
| """name_en and names_en_alt must not end with . , ; : ! ?""" |
| bad = [(e['code'], v) for e in dataset |
| for v in [e.get('name_en')] + list(e.get('names_en_alt') or []) |
| if v and v[-1] in '.,;:!?'] |
| assert bad == [], f"name_en/alts with trailing punctuation: {bad[:10]}" |
|
|
|
|
| def test_no_duplicate_names_within_entry(dataset): |
| """All name values across all name fields of a row must be unique.""" |
| bad = [] |
| for e in dataset: |
| all_names = [] |
| for field in ('name_en', 'name_ru', 'name_zh'): |
| v = e.get(field) |
| if v: |
| all_names.append(v) |
| for field in ('names_en_alt', 'names_ru_alt', 'names_zh_alt', 'names_other'): |
| for v in (e.get(field) or []): |
| if v: |
| all_names.append(v) |
| seen = set() |
| for n in all_names: |
| if n in seen: |
| bad.append((e['code'], repr(n))) |
| break |
| seen.add(n) |
| assert bad == [], f"Duplicate names within entry: {bad[:10]}" |
|
|
|
|
| def test_subdiv_prefix_matches_region(dataset, subdiv_region_map, valid_subdivs): |
| """subdiv must start with region+'-', except for overseas territories.""" |
| TERRITORY_PARENTS = { |
| 'GP': 'FR', 'GF': 'FR', 'MQ': 'FR', 'RE': 'FR', 'YT': 'FR', |
| 'BQ': 'NL', 'SJ': 'NO', 'AX': 'FI', 'XK': 'RS', 'KX': 'UA', |
| } |
| bad = [] |
| for e in dataset: |
| subdiv = e.get('subdiv') |
| region = e.get('region') |
| if not subdiv or not region: |
| continue |
| if subdiv.startswith(region + '-'): |
| continue |
| parent = subdiv_region_map.get(subdiv) |
| if parent and (TERRITORY_PARENTS.get(region) == parent or |
| (parent + '-' + region) in valid_subdivs): |
| continue |
| bad.append((e['code'], region, subdiv)) |
| assert bad == [], f"Subdiv prefix mismatch: {bad[:10]}" |
|
|
|
|
| def test_no_duplicate_links(dataset): |
| """No URL may appear twice in a row's links list.""" |
| bad = [] |
| for e in dataset: |
| links = e.get('links') or [] |
| seen = set() |
| dups = [] |
| for link in links: |
| if link in seen: |
| dups.append(link) |
| seen.add(link) |
| if dups: |
| bad.append((e['code'], dups)) |
| assert bad == [], f"Duplicate links: {bad[:10]}" |
|
|
|
|
| def test_alt_lists_no_airport_words_zh(dataset): |
| """names_zh_alt items must not contain airport/aviation words.""" |
| bad = [] |
| for e in dataset: |
| for v in (e.get('names_zh_alt') or []): |
| if _AIRPORT_ZH.search(v): |
| bad.append((e['code'], v)) |
| assert bad == [], f"names_zh_alt with airport words: {bad[:10]}" |
|
|
|
|
| def test_name_en_not_allcaps(dataset): |
| """name_en and names_en_alt must not be written in ALL CAPS.""" |
| bad = [(e['code'], v) for e in dataset |
| for v in [e.get('name_en')] + list(e.get('names_en_alt') or []) |
| if v and v == v.upper() and v.replace(' ', '').replace('-', '').isalpha()] |
| assert bad == [], f"name_en/alts in ALL CAPS: {bad[:10]}" |
|
|
|
|
| def test_name_en_no_cjk_or_cyrillic(dataset): |
| """name_en and names_en_alt must not contain CJK or Cyrillic characters.""" |
| bad = [(e['code'], v) for e in dataset |
| for v in [e.get('name_en')] + list(e.get('names_en_alt') or []) |
| if v and any( |
| '㐀' <= c <= '鿿' or '豈' <= c <= '' or 'Ѐ' <= c <= 'ӿ' |
| for c in v |
| )] |
| assert bad == [], f"name_en/alts with CJK or Cyrillic: {bad[:10]}" |
|
|
|
|
| def test_subdiv_not_empty_string(dataset): |
| """subdiv must be null when absent, not an empty string.""" |
| bad = [(e['code'],) for e in dataset if e.get('subdiv') == ''] |
| assert bad == [], f"subdiv is empty string: {bad[:10]}" |
|
|
|
|
| def test_no_ideographic_space(dataset): |
| """No name field may contain the ideographic space (U+3000).""" |
| bad = [] |
| for e in dataset: |
| for field in ('name', 'name_en', 'name_ru', 'name_zh'): |
| v = e.get(field) or '' |
| if ' ' in v: |
| bad.append((e['code'], field)) |
| for field in ('names_en_alt', 'names_ru_alt', 'names_zh_alt'): |
| for v in (e.get(field) or []): |
| if ' ' in v: |
| bad.append((e['code'], field)) |
| assert bad == [], f"Ideographic space in names: {bad[:10]}" |
|
|
|
|
| def test_name_zh_no_state_country_parenthetical(dataset): |
| """name_zh must not contain state/country qualifiers in parentheses.""" |
| bad = [(e['code'], e['name_zh']) for e in dataset |
| if e.get('name_zh') and _ZH_QUAL_RE.search(e['name_zh'])] |
| assert bad == [], f"name_zh with state/country parenthetical: {bad[:10]}" |
|
|
|
|
| def test_name_ru_no_admin_parenthetical(dataset): |
| """name_ru must not contain admin/region qualifiers in parentheses.""" |
| bad = [(e['code'], e['name_ru']) for e in dataset |
| if e.get('name_ru') and _RU_QUAL_RE.search(e['name_ru'])] |
| assert bad == [], f"name_ru with admin parenthetical: {bad[:10]}" |
|
|
|
|
| def test_name_zh_not_country_name(dataset): |
| """name_zh must not be a bare country name (indicates country-fill error).""" |
| bad = [(e['code'], e['name_zh'], e.get('name_en')) for e in dataset |
| if e.get('name_zh') in _COUNTRY_ZH] |
| assert bad == [], f"name_zh is a country name: {bad[:10]}" |
|
|
|
|
| def test_name_zh_not_us_state_name(dataset): |
| """name_zh must not be a US state name (indicates state-fill error).""" |
| bad = [(e['code'], e['name_zh'], e.get('name_en')) for e in dataset |
| if e.get('name_zh') in _STATE_ZH] |
| assert bad == [], f"name_zh is a US state name: {bad[:10]}" |
|
|