refactor(fudai): 拆出 _handle_in_fudai_detail 数据验证失败善后(3663→3632,-31 行)
handle_validation_failure_cleanup:步骤2 数据验证失败时关闭详情页 + 写日志 + 清除任务数量标志(与 detail_is_none 共享同一套清理动作)。 新增 2 个单元测试覆盖正常路径与 close 异常分支,1071 → 1073 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
b6cd3ac8a3
commit
2d971ca7d8
3 files changed
+79
-33
No files matched your search
@@ -2571,39 +2571,8 @@ class fudai_analyse:
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 关闭详情页并返回
|
||||
try:
|
||||
douyin_fudai_detail.close_fudai_detail(self)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 步骤2结束计时
|
||||
step2_elapsed = (time.time() - step2_start) * 1000
|
||||
self._log_kv(
|
||||
"详情处理",
|
||||
"步骤2[读取福袋详情]-完成",
|
||||
总耗时毫秒=step2_elapsed,
|
||||
)
|
||||
|
||||
# 清除任务数量标志
|
||||
try:
|
||||
self._fudai_detail_task_count_from_stage = None
|
||||
try:
|
||||
self._log_kv(
|
||||
"福袋详情",
|
||||
"_handle_in_fudai_detail:清除任务数量标志",
|
||||
log_type="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(数据验证失败)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
fudai_detail_blocks.handle_validation_failure_cleanup(
|
||||
self, start_time, step2_start
|
||||
)
|
||||
return
|
||||
except Exception:
|
||||
|
||||
@@ -104,3 +104,52 @@ def handle_detail_is_none(analyser: Any, start_time: float) -> bool:
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
|
||||
|
||||
def handle_validation_failure_cleanup(
|
||||
analyser: Any, start_time: float, step2_start: float
|
||||
) -> None:
|
||||
"""步骤2 数据验证失败的善后:关闭详情页 + 写日志 + 清除任务数量。
|
||||
|
||||
与上面 handle_* 系列不同:本函数不返回布尔值,调用方在 if not is_valid
|
||||
分支中调用本函数后立即 return。这是因为该分支已经在外层的 try 中。
|
||||
"""
|
||||
try:
|
||||
from douyin.fudai import douyin_fudai_detail
|
||||
|
||||
douyin_fudai_detail.close_fudai_detail(analyser)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
step2_elapsed = (time.time() - step2_start) * 1000
|
||||
analyser._log_kv(
|
||||
"详情处理",
|
||||
"步骤2[读取福袋详情]-完成",
|
||||
总耗时毫秒=step2_elapsed,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
analyser._fudai_detail_task_count_from_stage = None
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"福袋详情",
|
||||
"_handle_in_fudai_detail:清除任务数量标志",
|
||||
log_type="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
elapsed = time.time() - start_time
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(数据验证失败)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
@@ -93,3 +93,31 @@ def test_detail_is_none_swallows_log_exceptions():
|
||||
# 应当不抛错
|
||||
result = fudai_detail_blocks.handle_detail_is_none(a, _time.time())
|
||||
assert result is True
|
||||
|
||||
|
||||
# ---- handle_validation_failure_cleanup ---- #
|
||||
|
||||
def test_validation_cleanup_closes_detail_and_logs():
|
||||
a = MagicMock()
|
||||
a._log_kv = MagicMock()
|
||||
a._fudai_detail_task_count_from_stage = 7
|
||||
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as cd:
|
||||
fudai_detail_blocks.handle_validation_failure_cleanup(
|
||||
a, _time.time() - 0.5, _time.time() - 0.3
|
||||
)
|
||||
cd.assert_called_once_with(a)
|
||||
assert a._fudai_detail_task_count_from_stage is None
|
||||
# 至少 3 次日志:步骤2完成 / 清除标志 / 完成(数据验证失败)
|
||||
assert a._log_kv.call_count >= 2
|
||||
|
||||
|
||||
def test_validation_cleanup_close_failure_does_not_break():
|
||||
a = MagicMock()
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.close_fudai_detail",
|
||||
side_effect=RuntimeError("boom"),
|
||||
):
|
||||
fudai_detail_blocks.handle_validation_failure_cleanup(
|
||||
a, _time.time(), _time.time()
|
||||
)
|
||||
assert a._fudai_detail_task_count_from_stage is None
|
||||
Reference in new issue
Block a user