Datasets:
File size: 8,581 Bytes
ab1e9bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | """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()
|