523 lines
19 KiB
Python
523 lines
19 KiB
Python
"""douyin_fudai_core_pkg.ocr_tasks 单元测试。
|
||
|
||
覆盖 OcrTaskRunner 的 7 个方法 + 内部 helper:
|
||
- _safe_preprocess: 失败时回退原 name
|
||
- ocr_task_list / ocr_content: disable_flag 跳过 / 正常路径 / 异常
|
||
- ocr_countdown_mm / ocr_countdown_ss: digits 提取 / 异常
|
||
- ocr_value / ocr_button: 文本路径 / button 关键词短路 / 异常
|
||
- ocr_live_room_title: 委托到 _ocr_try_room_title
|
||
- 各方法的耗时 (ms) 都进 perf_durations
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import threading
|
||
from unittest.mock import MagicMock, patch
|
||
|
||
import pytest
|
||
|
||
from douyin.fudai.douyin_fudai_core_pkg.ocr_tasks import (
|
||
OcrTaskRunner,
|
||
OcrTaskRunnerFallback,
|
||
_BUTTON_KEYWORDS,
|
||
_choose_countdown_vote,
|
||
_cv_detect_two_zero_digits,
|
||
_read_countdown_digits,
|
||
_safe_preprocess,
|
||
)
|
||
|
||
|
||
def _mk_analyser():
|
||
a = MagicMock()
|
||
a._log_kv = MagicMock(return_value=None)
|
||
a.operation = MagicMock()
|
||
return a
|
||
|
||
|
||
def _mk_runner(analyser=None, *, disable_content=False):
|
||
if analyser is None:
|
||
analyser = _mk_analyser()
|
||
durations = {}
|
||
lock = threading.Lock()
|
||
runner = OcrTaskRunner(
|
||
analyser,
|
||
durations,
|
||
lock,
|
||
disable_content_recognition=disable_content,
|
||
)
|
||
return runner, durations
|
||
|
||
|
||
# ============================================================
|
||
# _safe_preprocess
|
||
# ============================================================
|
||
|
||
class TestSafePreprocess:
|
||
def test_success(self):
|
||
with patch(
|
||
"douyin.utils.ocr_preprocess.preprocess_for_ocr",
|
||
return_value="processed_name",
|
||
):
|
||
assert _safe_preprocess(MagicMock(), "X") == "processed_name"
|
||
|
||
def test_throws_returns_original_name(self):
|
||
with patch(
|
||
"douyin.utils.ocr_preprocess.preprocess_for_ocr",
|
||
side_effect=RuntimeError("preprocess fail"),
|
||
):
|
||
assert _safe_preprocess(MagicMock(), "原名") == "原名"
|
||
|
||
|
||
# ============================================================
|
||
# ocr_task_list
|
||
# ============================================================
|
||
|
||
class TestOcrTaskList:
|
||
def test_disabled_skips(self):
|
||
runner, durations = _mk_runner()
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
||
return_value=True,
|
||
):
|
||
assert runner.ocr_task_list() is None
|
||
# 跳过路径仍记录耗时
|
||
assert "ocr_task_list" in durations
|
||
|
||
def test_success(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(
|
||
return_value="任务1\n任务2"
|
||
)
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
||
return_value=False,
|
||
):
|
||
assert runner.ocr_task_list() == "任务1\n任务2"
|
||
assert durations["ocr_task_list"] >= 0
|
||
|
||
def test_exception(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(
|
||
side_effect=RuntimeError("ocr fail")
|
||
)
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
||
return_value=False,
|
||
):
|
||
assert runner.ocr_task_list() is None
|
||
# 异常仍写日志 + 耗时
|
||
assert "ocr_task_list" in durations
|
||
|
||
|
||
# ============================================================
|
||
# ocr_content
|
||
# ============================================================
|
||
|
||
class TestOcrContent:
|
||
def test_disabled_skips(self):
|
||
runner, durations = _mk_runner(disable_content=True)
|
||
runner.analyser.operation.analyse_pic_word = MagicMock()
|
||
assert runner.ocr_content() is None
|
||
# 不应调用 OCR
|
||
runner.analyser.operation.analyse_pic_word.assert_not_called()
|
||
assert "ocr_content" in durations
|
||
|
||
def test_success(self):
|
||
runner, durations = _mk_runner(disable_content=False)
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(
|
||
return_value="福袋内容"
|
||
)
|
||
assert runner.ocr_content() == "福袋内容"
|
||
assert durations["ocr_content"] >= 0
|
||
|
||
|
||
# ============================================================
|
||
# ocr_countdown_mm / ocr_countdown_ss
|
||
# ============================================================
|
||
|
||
class TestOcrCountdown:
|
||
def test_mm_extracts_digits(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_digits = MagicMock(
|
||
return_value="abc23def"
|
||
)
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||
return_value="",
|
||
):
|
||
assert runner.ocr_countdown_mm() == "23"
|
||
assert "ocr_countdown_mm" in durations
|
||
|
||
def test_mm_uses_preprocessed_countdown_image(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_digits = MagicMock(
|
||
side_effect=["23", "", "", "", "", ""]
|
||
)
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._safe_preprocess",
|
||
return_value="福袋倒计时_分钟_preprocessed",
|
||
) as preprocess, patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||
return_value="",
|
||
):
|
||
assert runner.ocr_countdown_mm() == "23"
|
||
|
||
preprocess.assert_called_once_with(runner.analyser, "福袋倒计时_分钟")
|
||
runner.analyser.operation.analyse_pic_digits.assert_any_call(
|
||
"福袋倒计时_分钟_preprocessed", 0
|
||
)
|
||
runner.analyser.operation.analyse_pic_digits.assert_any_call(
|
||
"福袋倒计时_分钟", 0
|
||
)
|
||
|
||
def test_ss_uses_preprocessed_countdown_image(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_digits = MagicMock(
|
||
side_effect=["45", "", "", "", "", ""]
|
||
)
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._safe_preprocess",
|
||
return_value="福袋倒计时_秒_preprocessed",
|
||
) as preprocess:
|
||
assert runner.ocr_countdown_ss() == "45"
|
||
|
||
preprocess.assert_called_once_with(runner.analyser, "福袋倒计时_秒")
|
||
runner.analyser.operation.analyse_pic_digits.assert_any_call(
|
||
"福袋倒计时_秒_preprocessed", 0
|
||
)
|
||
runner.analyser.operation.analyse_pic_digits.assert_any_call(
|
||
"福袋倒计时_秒", 0
|
||
)
|
||
|
||
def test_mm_normalizes_ocr_zero_noise(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_digits = MagicMock(return_value="UU")
|
||
assert runner.ocr_countdown_mm() == "00"
|
||
runner.analyser.operation.analyse_pic_word.assert_not_called()
|
||
|
||
def test_ss_extracts_digits(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_digits = MagicMock(
|
||
return_value="45"
|
||
)
|
||
assert runner.ocr_countdown_ss() == "45"
|
||
|
||
def test_mm_empty(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_digits = MagicMock(
|
||
return_value=None
|
||
)
|
||
assert runner.ocr_countdown_mm() == ""
|
||
|
||
def test_mm_throws(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_digits = MagicMock(
|
||
side_effect=RuntimeError()
|
||
)
|
||
assert runner.ocr_countdown_mm() is None
|
||
|
||
def test_countdown_vote_prefers_majority(self):
|
||
assert _choose_countdown_vote(["08", "09", "08"]) == "08"
|
||
|
||
def test_countdown_vote_tie_returns_empty_to_avoid_wrong_join(self):
|
||
assert _choose_countdown_vote(["09", "08"]) == ""
|
||
|
||
def test_read_countdown_digits_uses_modes_and_fallback_pic(self):
|
||
analyser = _mk_analyser()
|
||
analyser.operation.analyse_pic_digits = MagicMock(
|
||
side_effect=["", "99", "08", "8", "08", ""]
|
||
)
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||
return_value="",
|
||
):
|
||
result = _read_countdown_digits(
|
||
analyser,
|
||
"福袋倒计时_分钟_preprocessed",
|
||
fallback_picnames=("福袋倒计时_分钟",),
|
||
)
|
||
|
||
assert result == "08"
|
||
assert analyser.operation.analyse_pic_digits.call_args_list[0].args == (
|
||
"福袋倒计时_分钟_preprocessed",
|
||
0,
|
||
)
|
||
assert analyser.operation.analyse_pic_digits.call_args_list[-1].args == (
|
||
"福袋倒计时_分钟",
|
||
2,
|
||
)
|
||
|
||
def test_read_countdown_digits_tie_prefers_primary_over_cv_zero(self):
|
||
analyser = _mk_analyser()
|
||
analyser.operation.analyse_pic_digits = MagicMock(
|
||
side_effect=["09", "09", "09", "00", "00", "00"]
|
||
)
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||
side_effect=["00"],
|
||
):
|
||
assert _read_countdown_digits(
|
||
analyser,
|
||
"福袋倒计时_分钟_preprocessed",
|
||
fallback_picnames=("福袋倒计时_分钟",),
|
||
) == "09"
|
||
|
||
def test_read_countdown_digits_tie_prefers_consistent_preprocessed(self):
|
||
analyser = _mk_analyser()
|
||
analyser.operation.analyse_pic_digits = MagicMock(
|
||
side_effect=["41", "41", "41", "50", "50", "50"]
|
||
)
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||
return_value="",
|
||
):
|
||
assert _read_countdown_digits(
|
||
analyser,
|
||
"福袋倒计时_秒_preprocessed",
|
||
fallback_picnames=("福袋倒计时_秒",),
|
||
) == "41"
|
||
|
||
def test_read_countdown_digits_cv_zero_cannot_override_nonzero_majority(self):
|
||
analyser = _mk_analyser()
|
||
analyser.operation.analyse_pic_digits = MagicMock(
|
||
side_effect=["09", "09", "09", "", "", ""]
|
||
)
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.ocr_tasks._cv_detect_two_zero_digits",
|
||
side_effect=["00"],
|
||
):
|
||
assert _read_countdown_digits(
|
||
analyser,
|
||
"福袋倒计时_分钟_preprocessed",
|
||
fallback_picnames=("福袋倒计时_分钟",),
|
||
) == "09"
|
||
|
||
def test_cv_detect_two_zero_digits_on_current_minute_crop(self):
|
||
assert _cv_detect_two_zero_digits("福袋倒计时_分钟") in ("", "00")
|
||
|
||
|
||
# ============================================================
|
||
# ocr_value
|
||
# ============================================================
|
||
|
||
class TestOcrValue:
|
||
def test_success(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(
|
||
return_value="¥500"
|
||
)
|
||
assert runner.ocr_value() == "¥500"
|
||
assert "ocr_value" in durations
|
||
|
||
def test_returns_empty_string_for_none(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(return_value=None)
|
||
# str(None or "") = ""
|
||
assert runner.ocr_value() == ""
|
||
|
||
def test_throws(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(
|
||
side_effect=RuntimeError()
|
||
)
|
||
assert runner.ocr_value() is None
|
||
|
||
|
||
# ============================================================
|
||
# ocr_button
|
||
# ============================================================
|
||
|
||
class TestOcrButton:
|
||
def test_keyword_short_circuits(self):
|
||
"""识别到关键词应立即返回,不再二次 OCR。"""
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(
|
||
return_value="参与抽奖"
|
||
)
|
||
assert runner.ocr_button() == "参与抽奖"
|
||
# 模式 0 一次就够
|
||
assert runner.analyser.operation.analyse_pic_word.call_count == 1
|
||
|
||
def test_no_keyword_returns_text_anyway(self):
|
||
"""识别到非关键词文本也会返回。"""
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(
|
||
return_value="未知按钮文本"
|
||
)
|
||
# 不在关键词里 → 仍返回,但可能再尝试一次(保留旧逻辑)
|
||
result = runner.ocr_button()
|
||
assert result == "未知按钮文本"
|
||
|
||
def test_empty_first_then_retry(self):
|
||
"""模式 0 完全失败时再试一次。"""
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(
|
||
side_effect=["", "重试得到的文本"]
|
||
)
|
||
assert runner.ocr_button() == "重试得到的文本"
|
||
assert runner.analyser.operation.analyse_pic_word.call_count == 2
|
||
|
||
def test_throws_returns_none(self):
|
||
runner, durations = _mk_runner()
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(
|
||
side_effect=RuntimeError()
|
||
)
|
||
assert runner.ocr_button() is None
|
||
|
||
def test_button_keywords_constant(self):
|
||
"""关键词里至少含一些核心字串。"""
|
||
for k in ("参与抽奖", "等待开奖", "未达成"):
|
||
assert k in _BUTTON_KEYWORDS
|
||
|
||
|
||
# ============================================================
|
||
# ocr_live_room_title
|
||
# ============================================================
|
||
|
||
class TestOcrLiveRoomTitle:
|
||
def test_delegates_to_ocr_try_room_title(self):
|
||
runner, durations = _mk_runner()
|
||
with patch(
|
||
"douyin.fudai.core.fudai_detail_room_title._ocr_try_room_title",
|
||
return_value=("主播间标题", "raw text"),
|
||
):
|
||
title, raw = runner.ocr_live_room_title()
|
||
assert title == "主播间标题"
|
||
assert raw == "raw text"
|
||
assert "ocr_live_room_title" in durations
|
||
|
||
def test_throws_returns_empty_tuple(self):
|
||
runner, durations = _mk_runner()
|
||
with patch(
|
||
"douyin.fudai.core.fudai_detail_room_title._ocr_try_room_title",
|
||
side_effect=RuntimeError(),
|
||
):
|
||
assert runner.ocr_live_room_title() == ("", "")
|
||
|
||
|
||
# ============================================================
|
||
# 通用:所有方法都记录耗时到共享 dict
|
||
# ============================================================
|
||
|
||
class TestPerfDurations:
|
||
def test_all_keys_present_after_one_call_each(self):
|
||
runner, durations = _mk_runner()
|
||
# 让 OCR 都返回固定值
|
||
runner.analyser.operation.analyse_pic_word = MagicMock(return_value="x")
|
||
runner.analyser.operation.analyse_pic_digits = MagicMock(return_value="1")
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
||
return_value=False,
|
||
), patch(
|
||
"douyin.fudai.core.fudai_detail_room_title._ocr_try_room_title",
|
||
return_value=("title", "raw"),
|
||
):
|
||
runner.ocr_task_list()
|
||
runner.ocr_content()
|
||
runner.ocr_countdown_mm()
|
||
runner.ocr_countdown_ss()
|
||
runner.ocr_value()
|
||
runner.ocr_live_room_title()
|
||
# ocr_button 不计耗时(保留原行为差异)
|
||
for k in (
|
||
"ocr_task_list",
|
||
"ocr_content",
|
||
"ocr_countdown_mm",
|
||
"ocr_countdown_ss",
|
||
"ocr_value",
|
||
"ocr_live_room_title",
|
||
):
|
||
assert k in durations, f"missing duration for {k}"
|
||
assert durations[k] >= 0
|
||
|
||
|
||
# ============================================================
|
||
# OcrTaskRunnerFallback
|
||
# ============================================================
|
||
|
||
class TestOcrTaskRunnerFallback:
|
||
def test_task_list_disabled_skips(self):
|
||
a = _mk_analyser()
|
||
runner = OcrTaskRunnerFallback(a)
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
||
return_value=True,
|
||
):
|
||
assert runner.ocr_task_list() is None
|
||
|
||
def test_task_list_success(self):
|
||
a = _mk_analyser()
|
||
a.operation.analyse_pic_word = MagicMock(return_value="任务文本")
|
||
runner = OcrTaskRunnerFallback(a)
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
||
return_value=False,
|
||
):
|
||
assert runner.ocr_task_list() == "任务文本"
|
||
|
||
def test_content_disabled_skips(self):
|
||
a = _mk_analyser()
|
||
runner = OcrTaskRunnerFallback(a, disable_content_recognition=True)
|
||
a.operation.analyse_pic_word = MagicMock()
|
||
assert runner.ocr_content() is None
|
||
a.operation.analyse_pic_word.assert_not_called()
|
||
|
||
def test_content_enabled_runs(self):
|
||
a = _mk_analyser()
|
||
a.operation.analyse_pic_word = MagicMock(return_value="content")
|
||
runner = OcrTaskRunnerFallback(a, disable_content_recognition=False)
|
||
assert runner.ocr_content() == "content"
|
||
|
||
def test_countdown_returns_raw_no_extraction(self):
|
||
"""fallback 不做数字提取,直接返回原始 OCR 结果。"""
|
||
a = _mk_analyser()
|
||
a.operation.analyse_pic_digits = MagicMock(return_value="abc12def")
|
||
runner = OcrTaskRunnerFallback(a)
|
||
# fallback 版本直接返回原始结果,不提取数字
|
||
assert runner.ocr_countdown_mm() == "abc12def"
|
||
assert runner.ocr_countdown_ss() == "abc12def"
|
||
|
||
def test_countdown_throws_returns_none(self):
|
||
a = _mk_analyser()
|
||
a.operation.analyse_pic_digits = MagicMock(side_effect=RuntimeError())
|
||
runner = OcrTaskRunnerFallback(a)
|
||
assert runner.ocr_countdown_mm() is None
|
||
assert runner.ocr_countdown_ss() is None
|
||
|
||
def test_value_passes_through(self):
|
||
a = _mk_analyser()
|
||
a.operation.analyse_pic_word = MagicMock(return_value="¥888")
|
||
runner = OcrTaskRunnerFallback(a)
|
||
assert runner.ocr_value() == "¥888"
|
||
|
||
def test_button_keyword_short_circuit(self):
|
||
a = _mk_analyser()
|
||
a.operation.analyse_pic_word = MagicMock(return_value="参与抽奖")
|
||
runner = OcrTaskRunnerFallback(a)
|
||
assert runner.ocr_button() == "参与抽奖"
|
||
# 含关键词,仅 1 次 OCR
|
||
assert a.operation.analyse_pic_word.call_count == 1
|
||
|
||
def test_button_no_keyword_retries(self):
|
||
"""无关键词时再试一次(保留旧 fallback 行为)。"""
|
||
a = _mk_analyser()
|
||
a.operation.analyse_pic_word = MagicMock(
|
||
side_effect=["未知文本", "重试结果"]
|
||
)
|
||
runner = OcrTaskRunnerFallback(a)
|
||
# 第一次返回非关键词 → 进 retry → 返回第二次结果
|
||
assert runner.ocr_button() == "重试结果"
|
||
assert a.operation.analyse_pic_word.call_count == 2
|
||
|
||
def test_button_throws(self):
|
||
a = _mk_analyser()
|
||
a.operation.analyse_pic_word = MagicMock(side_effect=RuntimeError())
|
||
runner = OcrTaskRunnerFallback(a)
|
||
assert runner.ocr_button() is None
|