-
Notifications
You must be signed in to change notification settings - Fork 22
/
test_translate.py
110 lines (96 loc) · 4.48 KB
/
test_translate.py
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
import re
from translate import translate
def test_translate_examples_in_readme():
# 1984年的四川省涪陵地区南川县
# 对应于2018年的重庆市南川区
assert translate("512323", 1984, 2018) == ["500119"]
# 2000年的湖南省衡阳市城北区
# 对应于2018年的湖南省衡阳市石鼓区、蒸湘区
assert translate("430404", 2000, 2018) == ["430407", "430408"]
# 2010年的北京市西城区
# 对应于2000年的北京市西城区、宣武区
assert translate("110102", 2010, 2000) == ["110102", "110104"]
def test_translate_code_change():
"""
1989年,锦西市升为地级市,锦州市的葫芦岛区……划归锦西市管辖
1994年,锦西市更名为葫芦岛市,葫芦岛区更名为龙港区
"""
"""
* 辽宁省-锦州市-葫芦岛区(210705)
-> 辽宁省-锦西市-葫芦岛区(211405) 1989
-> 辽宁省-葫芦岛市-龙港区(211403) 1994
"""
assert translate("210705", 1984, 1988) == ["210705"]
assert translate("210705", 1984, 1989) == ["211405"]
assert translate("210705", 1984, 1990) == ["211405"]
assert translate("210705", 1984, 1993) == ["211405"]
assert translate("210705", 1984, 1994) == ["211403"]
assert translate("210705", 1984, 1995) == ["211403"]
assert translate("210705", 1984, 2018) == ["211403"]
assert translate("211405", 1989, 1993) == ["211405"]
assert translate("211405", 1989, 1994) == ["211403"]
assert translate("211405", 1989, 1995) == ["211403"]
assert translate("211405", 1989, 2018) == ["211403"]
# 反向查询
assert translate("211405", 1989, 1984) == ["210705"]
assert translate("211403", 1994, 1984) == ["210705"]
assert translate("211403", 1995, 1989) == ["210705"]
assert translate("211403", 1995, 1990) == ["211405"]
assert translate("211403", 2018, 1984) == ["210705"]
def test_translate_merge():
"""
2010年:撤销北京市西城区、宣武区,设立新的北京市西城区,
以原西城区、宣武区的行政区域为西城区的行政区域
"""
"""
* 北京市-宣武区(110104)
-> 北京市-西城区(110102) 2010
"""
assert translate("110104", 2000, 2010) == ["110102"]
# 反向查询
assert translate("110102", 2010, 2000) == ["110102", "110104"]
def test_translate_split():
"""
1997年:
(1)撤销曲靖地区和县级曲靖市,设立地级曲靖市。
(2)曲靖市设立麒麟区和沾益县。
麒麟区辖原县级曲靖市的……4个镇和6个乡。
沾益县辖原县级曲靖市的……2个镇和8个乡。
"""
"""
* 云南省-曲靖地区-曲靖市(532201)
-> 云南省-曲靖市-麒麟区(530302), 云南省-曲靖市-沾益县(530328) 1997
"""
assert translate("532201", 1990, 1997) == ["530302", "530328"]
# 反向查询
assert translate("530302", 1997, 1996) == ["532201"]
assert translate("530328", 1997, 1996) == ["532201"]
def test_translate_full():
"""
全量数据测试
"""
for target_year in range(1985, 2022):
current_code_list: dict[str] = {}
with open(f"tables/{target_year}.csv", encoding="utf-8") as f:
for line in f:
cells = line.strip().split(",")
if len(cells) < 2:
continue
if re.match(r"^\d{6}$", cells[0]) is not None:
current_code_list[cells[0]] = cells[1]
for original_year in range(1984, target_year+1):
with open(f"tables/{original_year}.csv", encoding="utf-8") as f:
for line in f:
cells = line.strip().split(",")
if len(cells) < 2:
continue
if re.match(r"^\d{6}$", cells[0]) is not None:
code = cells[0]
name = cells[1]
changed_code_list: list[str] = translate(code, original_year=original_year, target_year=target_year)
for changed_code in changed_code_list:
if changed_code not in current_code_list:
print(
f"year: {original_year}, code: {code}, name: {name} -> {target_year} {changed_code} not in {target_year} code list"
)
assert changed_code in current_code_list