签入版本
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
"""
|
||||
城市 full_name/pinyin 自动补全测试
|
||||
=============================================================
|
||||
|
||||
验证新增/编辑城市时自动补全派生字段:
|
||||
- full_name:沿 pid 链从首层城市拼接(如 台湾省,云林,元长,华鹿)
|
||||
- pinyin:由城市简称生成无音调拼音,音节首字母大写(如 台湾 -> TaiWan、华鹿 -> HuaLu)
|
||||
|
||||
覆盖新增、编辑改名、编辑变更父级三种场景。
|
||||
"""
|
||||
|
||||
from src.core.database import db
|
||||
from src.modules.city.repository import CityRepository
|
||||
from src.modules.city.service import CityService
|
||||
|
||||
|
||||
def _payload(name, short_name, pid, level):
|
||||
"""构造合法的城市表单数据(不含派生字段)"""
|
||||
return {
|
||||
"cityCode": "012",
|
||||
"areaCode": "640300",
|
||||
"parentCode": "0",
|
||||
"zipCode": "750000",
|
||||
"level": level,
|
||||
"name": name,
|
||||
"shortName": short_name,
|
||||
"pid": pid,
|
||||
"lng": "106.2",
|
||||
"lat": "23.7",
|
||||
}
|
||||
|
||||
|
||||
def _add_city(app, name, short_name, pid, level):
|
||||
"""经 CityService.add 新增城市并返回其ID"""
|
||||
with app.app_context():
|
||||
CityService.add(_payload(name, short_name, pid, level))
|
||||
db.session.commit()
|
||||
return CityRepository.first_by({"name": name}).id
|
||||
|
||||
|
||||
class TestCityFullName:
|
||||
"""full_name 沿 pid 链从首层拼接"""
|
||||
|
||||
def test_full_name_chain(self, app):
|
||||
province_id = _add_city(app, "台湾省", "台湾", 0, 1)
|
||||
city_id = _add_city(app, "云林", "云林", province_id, 2)
|
||||
district_id = _add_city(app, "元长", "元长", city_id, 3)
|
||||
street_id = _add_city(app, "华鹿", "华鹿", district_id, 4)
|
||||
|
||||
with app.app_context():
|
||||
province = CityRepository.get_by_id(province_id)
|
||||
city = CityRepository.get_by_id(city_id)
|
||||
district = CityRepository.get_by_id(district_id)
|
||||
street = CityRepository.get_by_id(street_id)
|
||||
|
||||
assert province.full_name == "台湾省"
|
||||
assert city.full_name == "台湾省,云林"
|
||||
assert district.full_name == "台湾省,云林,元长"
|
||||
assert street.full_name == "台湾省,云林,元长,华鹿"
|
||||
|
||||
def test_update_rename_recomputes(self, app):
|
||||
province_id = _add_city(app, "广东省", "广东", 0, 1)
|
||||
city_id = _add_city(app, "广州市", "广州", province_id, 2)
|
||||
|
||||
with app.app_context():
|
||||
data = _payload("东莞市", "东莞", province_id, 2)
|
||||
data["id"] = city_id
|
||||
CityService.update(data)
|
||||
db.session.commit()
|
||||
db.session.expire_all()
|
||||
city = CityRepository.get_by_id(city_id)
|
||||
|
||||
assert city.name == "东莞市"
|
||||
assert city.full_name == "广东省,东莞市"
|
||||
|
||||
def test_update_reparent_recomputes(self, app):
|
||||
prov_a = _add_city(app, "A省", "A省", 0, 1)
|
||||
prov_b = _add_city(app, "B省", "B省", 0, 1)
|
||||
city_id = _add_city(app, "旧城", "旧城", prov_a, 2)
|
||||
|
||||
with app.app_context():
|
||||
data = _payload("新城", "新城", prov_b, 2)
|
||||
data["id"] = city_id
|
||||
CityService.update(data)
|
||||
db.session.commit()
|
||||
db.session.expire_all()
|
||||
city = CityRepository.get_by_id(city_id)
|
||||
|
||||
assert city.full_name == "B省,新城"
|
||||
|
||||
|
||||
class TestCityPinyin:
|
||||
"""pinyin 由简称生成(无音调)"""
|
||||
|
||||
def test_pinyin_uses_short_name(self, app):
|
||||
province_id = _add_city(app, "台湾省", "台湾", 0, 1)
|
||||
city_id = _add_city(app, "云林", "云林", province_id, 2)
|
||||
|
||||
with app.app_context():
|
||||
province = CityRepository.get_by_id(province_id)
|
||||
city = CityRepository.get_by_id(city_id)
|
||||
|
||||
assert province.pinyin == "TaiWan"
|
||||
assert city.pinyin == "YunLin"
|
||||
|
||||
def test_update_rename_recomputes_pinyin(self, app):
|
||||
province_id = _add_city(app, "广东省", "广东", 0, 1)
|
||||
city_id = _add_city(app, "广州市", "广州", province_id, 2)
|
||||
|
||||
with app.app_context():
|
||||
data = _payload("深圳市", "深圳", province_id, 2)
|
||||
data["id"] = city_id
|
||||
CityService.update(data)
|
||||
db.session.commit()
|
||||
db.session.expire_all()
|
||||
city = CityRepository.get_by_id(city_id)
|
||||
|
||||
assert city.pinyin == "ShenZhen"
|
||||
Reference in New Issue
Block a user