test: 端到端验证错误首选编码下自动识别不乱码

This commit is contained in:
nixevol
2026-09-17 09:41:31 +08:00
parent 72ad8a4f0c
commit 89afb234dd
+10 -5
View File
@@ -51,11 +51,12 @@ class TaskIntegrationTest(unittest.TestCase):
self.addCleanup(self.patch.stop) self.addCleanup(self.patch.stop)
self.addCleanup(self.temp.cleanup) self.addCleanup(self.temp.cleanup)
self.input = self.root / "楼宇轮廓.csv" self.input = self.root / "楼宇轮廓.csv"
with self.input.open("w", encoding="utf-8-sig", newline="") as output: # 文件是无 BOM 的 UTF-8 且含中文,而界面首选编码故意给 gbk:必须自动识别,结果不能乱码。
with self.input.open("w", encoding="utf-8", newline="") as output:
writer = csv.writer(output) writer = csv.writer(output)
writer.writerow(["WKT", "building_id"]) writer.writerow(["WKT", "building_id"])
writer.writerows((box(112 + i / 1000, 22, 112 + i / 1000 + 0.0005, 22.0005).wkt, f"JM{i}") for i in range(30)) writer.writerows((box(112 + i / 1000, 22, 112 + i / 1000 + 0.0005, 22.0005).wkt, f"楼宇{i}") for i in range(30))
self.params = {"INPUT_CSV": str(self.input), "INPUT_ENCODING": "utf-8-sig", "START_ROW": 1, self.params = {"INPUT_CSV": str(self.input), "INPUT_ENCODING": "gbk", "START_ROW": 1,
"LIMIT": 30, "CONCURRENCY": 2, "TIMEOUT": 5, "MAX_RETRIES": 0, "REQUEST_INTERVAL": 0} "LIMIT": 30, "CONCURRENCY": 2, "TIMEOUT": 5, "MAX_RETRIES": 0, "REQUEST_INTERVAL": 0}
def wait_for(self, task_id, condition, timeout=30): def wait_for(self, task_id, condition, timeout=30):
@@ -95,8 +96,12 @@ class TaskIntegrationTest(unittest.TestCase):
state = self.wait_for(task_id, lambda s: s["status"] not in tasks.ACTIVE) state = self.wait_for(task_id, lambda s: s["status"] not in tasks.ACTIVE)
self.assertEqual(state["status"], "success", state) self.assertEqual(state["status"], "success", state)
self.assertEqual(state["counts"]["查询坐标"], 3) self.assertEqual(state["counts"]["查询坐标"], 3)
with (Path(state["output"]) / "NRBuildingsData.csv").open(encoding="gbk", errors="replace") as source: self.assertEqual(state["params"]["INPUT_ENCODING"], "utf-8-sig")
self.assertEqual(len(list(csv.DictReader(source))), 3) with (Path(state["output"]) / "NRBuildingsData.csv").open(encoding="gbk", newline="") as source:
rows = list(csv.DictReader(source))
self.assertEqual(len(rows), 3)
self.assertEqual({row["查询序号"] for row in rows}, {"楼宇0", "楼宇1", "楼宇2"})
self.assertEqual({row["楼名"] for row in rows}, {"测试楼"})
history = (tasks.TASKS / task_id / "state.json").read_text(encoding="utf-8") + tasks.tail_log(tasks.TASKS / task_id) history = (tasks.TASKS / task_id / "state.json").read_text(encoding="utf-8") + tasks.tail_log(tasks.TASKS / task_id)
self.assertNotIn(credentials["COOKIES"], history) self.assertNotIn(credentials["COOKIES"], history)
self.assertNotIn("test-private-key", history) self.assertNotIn("test-private-key", history)