Datasets:
Download geoalign/tests/test_geoalign.py from WorldInsightLab/china-admin-divisions: direct link, hf CLI and curl.
- Browser
- Download file 8.58 kB
-
https://huggingface.co/datasets/WorldInsightLab/china-admin-divisions/resolve/main/geoalign/tests/test_geoalign.py
- Command line
-
hf download hf://datasets/WorldInsightLab/china-admin-divisions/geoalign/tests/test_geoalign.py
-
curl -L -o test_geoalign.py https://huggingface.co/datasets/WorldInsightLab/china-admin-divisions/resolve/main/geoalign/tests/test_geoalign.py
8.58 kB
| """geoalign 的测试。只用标准库:python -m unittest discover -s tests""" | |
| import csv, gzip, json, os, sys, unittest | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from geoalign.core import Gazetteer, stem, normalize, extra_aliases, autonomy_aliases, DATA # noqa: E402 | |
| G = Gazetteer() | |
| class TestScope(unittest.TestCase): | |
| """发布包里只有发布范围内的数据。""" | |
| def test_years_from_2012(self): | |
| with gzip.open(os.path.join(DATA, "years.csv.gz"), "rt", encoding="utf-8") as fh: | |
| years = {int(r["year"]) for r in csv.DictReader(fh)} | |
| self.assertEqual(min(years), 2012) | |
| self.assertEqual(max(years), 2025) | |
| def test_changes_from_2013(self): | |
| with gzip.open(os.path.join(DATA, "changes.csv.gz"), "rt", encoding="utf-8") as fh: | |
| ys = {int(r["year"]) for r in csv.DictReader(fh)} | |
| self.assertGreaterEqual(min(ys), 2013) | |
| def test_all_grade_a(self): | |
| meta = json.load(open(os.path.join(DATA, "years_meta.json"), encoding="utf-8")) | |
| self.assertEqual({m["source_grade"] for m in meta.values()}, {"A"}) | |
| self.assertFalse(any(m["rebuilt"] for m in meta.values())) | |
| def test_2025_is_year_end(self): | |
| # 岑岭县、草湖市是 2026 年公告设立的,不在 2025 年底的表里 | |
| self.assertNotIn("653132", G.snap[2025]) | |
| self.assertNotIn("659013", G.snap[2025]) | |
| class TestNames(unittest.TestCase): | |
| def test_stem(self): | |
| self.assertEqual(stem("荆州市"), "荆州") | |
| self.assertEqual(stem("杏花岭区"), "杏花岭") | |
| self.assertEqual(stem("神农架林区"), "神农架") | |
| def test_aliases(self): | |
| self.assertIn("凉山", autonomy_aliases("凉山彝族自治州")) | |
| self.assertIn("融水县", autonomy_aliases("融水苗族自治县")) | |
| self.assertIn("科右前旗", extra_aliases("科尔沁右翼前旗")) | |
| self.assertIn("顺河区", extra_aliases("顺河回族区")) | |
| def test_normalize(self): | |
| self.assertEqual(normalize(" 洛阳。 "), "洛阳") | |
| self.assertEqual(normalize("锦江 "), "锦江") | |
| class TestAlign(unittest.TestCase): | |
| def test_exact_current(self): | |
| m = G.align("密云区") | |
| self.assertEqual((m.code, m.confidence, m.method), ("110118", "high", "exact")) | |
| self.assertEqual(m.source_grade, "A") | |
| self.assertFalse(m.rebuilt) | |
| def test_old_name_to_current(self): | |
| m = G.align("密云县") # 2015 年撤县设区 | |
| self.assertEqual(m.code, "110118") | |
| self.assertEqual(m.method, "history") | |
| def test_year(self): | |
| m = G.align("密云县", year=2013) | |
| self.assertEqual(m.code, "110228") | |
| self.assertEqual(m.year, 2013) | |
| def test_year_before_data_gives_no_code(self): | |
| m = G.align("密云县", year=1990) | |
| self.assertIsNone(m.code) | |
| self.assertIn(m.confidence, ("low", "none")) | |
| self.assertIn("2012", m.note) | |
| def test_homonym_without_context_returns_all(self): | |
| m = G.align("朝阳区") | |
| self.assertIsNone(m.code) | |
| self.assertEqual(m.confidence, "low") | |
| self.assertGreaterEqual(len(m.candidates), 2) | |
| self.assertIn("220104", [c.code for c in m.candidates]) | |
| def test_homonym_with_parent(self): | |
| self.assertEqual(G.align("朝阳区", parent="长春市").code, "220104") | |
| self.assertEqual(G.align("朝阳区", parent="北京").code, "110105") | |
| def test_cross_level_ambiguity(self): | |
| m = G.align("吉林") | |
| self.assertIsNone(m.code) | |
| self.assertEqual({c.code for c in m.candidates}, {"220000", "220200"}) | |
| def test_short_name(self): | |
| self.assertEqual(G.align("凉山州").code, "513400") | |
| self.assertEqual(G.align("襄阳").code, "420600") | |
| def test_generic_names_not_confident(self): | |
| for q in ("市", "新区", "城区", "郊区", "矿区", "东区", "西区"): | |
| self.assertIsNone(G.align(q).code, q) | |
| self.assertEqual(G.align("东区", parent="攀枝花市").code, "510402") # 上级确定成一个单位时照常给 | |
| def test_concatenated_parent(self): | |
| m = G.align("天水市麦积区") | |
| self.assertEqual((m.code, m.method), ("620503", "split")) | |
| def test_not_found(self): | |
| m = G.align("不存在的地方") | |
| self.assertIsNone(m.code) | |
| self.assertEqual(m.confidence, "none") | |
| def test_garbage_parent_ignored(self): | |
| self.assertEqual(G.align("密云区", parent="市辖区").code, "110118") | |
| def test_typo_is_low_confidence(self): | |
| m = G.align("候马市") | |
| self.assertIsNone(m.code) | |
| self.assertEqual(m.best, "141081") | |
| self.assertEqual((m.method, m.confidence), ("fuzzy", "low")) | |
| def test_typo_wrong_suffix(self): | |
| m = G.align("盱眙市") | |
| self.assertEqual(m.best, "320830") | |
| self.assertEqual(m.method, "fuzzy") | |
| def test_merged_old_name(self): | |
| self.assertEqual(G.align("下城区").code, "330105") # 2021 年并入拱墅区 | |
| def test_split_main_successor(self): | |
| self.assertEqual(G.align("绍兴县").code, "330603") # 2013 年撤县设柯桥区 | |
| class TestToYear(unittest.TestCase): | |
| def test_forward(self): | |
| r = G.to_year("110228", 2025) | |
| self.assertEqual((r.codes, r.exact), (["110118"], True)) | |
| def test_backward(self): | |
| r = G.to_year("110118", 2013) | |
| self.assertEqual(r.codes, ["110228"]) | |
| def test_before_data(self): | |
| r = G.to_year("110118", 2000) | |
| self.assertEqual(r.codes, []) | |
| self.assertFalse(r.exact) | |
| def test_split_reports_side_transfer(self): | |
| r = G.to_year("330621", 2014) | |
| self.assertEqual(r.codes, ["330603"]) | |
| self.assertTrue(any("一部分划给 330602" in s for s in r.steps)) | |
| def test_backward_from_new_unit(self): | |
| r = G.to_year("440310", 2015) # 坪山区 2016 年从龙岗区划出 | |
| self.assertEqual(r.codes, ["440307"]) | |
| self.assertFalse(r.exact) | |
| def test_merge_many_to_one_backward(self): | |
| r = G.to_year("500157", 2024) # 两江新区 ← 江北区、渝北区 | |
| self.assertIn("500105", r.codes) | |
| self.assertIn("500112", r.codes) | |
| self.assertFalse(r.exact) | |
| class TestCorrections(unittest.TestCase): | |
| """逐条重读公告以后改正的行(改正清单:dataset/data/corrections_applied.csv)。""" | |
| def test_split_without_main_returns_all(self): | |
| r = G.to_year("130103", 2016) # 石家庄桥东区 2014 年撤销,分给长安区、桥西区 | |
| self.assertEqual(r.codes, ["130102", "130104"]) | |
| self.assertFalse(r.exact) | |
| def test_merge_into_new(self): | |
| self.assertEqual(G.to_year("140402", 2019).codes, ["140403"]) # 长治城区 → 潞州区 | |
| self.assertEqual(G.to_year("360105", 2020).codes, ["360112"]) # 湾里区 → 新建区 | |
| def test_level_change_same_name(self): | |
| r = G.to_year("469003", 2016) # 县级儋州市 → 地级儋州市 | |
| self.assertEqual((r.codes, r.exact), (["460400"], True)) | |
| def test_code_change_not_split(self): | |
| r = G.to_year("542430", 2018) # 尼玛县换代码,不是 2012 年的拆分 | |
| self.assertEqual((r.codes, r.exact), (["540629"], True)) | |
| def test_table_lag_row_kept(self): | |
| self.assertEqual(G.to_year("130604", 2018).codes, ["130606"]) # 南市区:代码表 2017 年才去掉 | |
| def test_split_main_unknown(self): | |
| r = G.to_year("130521", 2025) # 邢台县 2020 年撤销,分给襄都区、信都区,公告没说主要承继 | |
| self.assertEqual(r.codes, ["130502", "130503"]) | |
| self.assertFalse(r.exact) | |
| def test_event_recorded_once(self): | |
| self.assertEqual(len(G.history("130604")["events"]), 1) # 南市区并入莲池区只记一次 | |
| def test_new_unit_step_message(self): | |
| r = G.to_year("460202", 2012) # 三亚海棠区 2014 年由乡镇设立 | |
| self.assertEqual(r.codes, []) | |
| self.assertIn("新设", r.steps[-1]) | |
| self.assertNotIn("未查明", r.steps[-1]) | |
| def test_continuing_unit_not_main(self): | |
| r = G.to_year("330113", 2020) # 临平区由余杭区的一部分设立,余杭区继续存在 | |
| self.assertEqual(r.codes, ["330110"]) | |
| self.assertFalse(r.exact) | |
| class TestHistory(unittest.TestCase): | |
| def test_names(self): | |
| h = G.history("330105") | |
| self.assertIn("拱墅区", [s["name"] for s in h["names"]]) | |
| self.assertTrue(h["events"]) | |
| if __name__ == "__main__": | |
| unittest.main() | |