250 lines
8.9 KiB
Python
250 lines
8.9 KiB
Python
"""douyin_fudai_core_pkg.heavy_ocr 单元测试。
|
|
|
|
覆盖:
|
|
- _read_disable_flag: analyser 属性优先 / config.json fallback / 都缺时 False
|
|
- _ocr_digits_with_fallback: digits 成功 / digits 失败 word 兜底成功 / 都失败
|
|
- run_heavy_ocr: 各字段独立分支 / 配置开关 / 都失败时只剩 _duration_ms
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from douyin.fudai.douyin_fudai_core_pkg.heavy_ocr import (
|
|
_ocr_digits_with_fallback,
|
|
_read_disable_flag,
|
|
run_heavy_ocr,
|
|
)
|
|
from douyin.fudai.douyin_fudai_core_pkg.contain_helpers import should_use_heavy
|
|
|
|
|
|
def _mk_analyser():
|
|
a = MagicMock()
|
|
a._log_kv = MagicMock(return_value=None)
|
|
a.operation = MagicMock()
|
|
return a
|
|
|
|
|
|
# ============================================================
|
|
# _read_disable_flag
|
|
# ============================================================
|
|
|
|
class TestReadDisableFlag:
|
|
def test_attr_true_short_circuits(self):
|
|
a = _mk_analyser()
|
|
a.fudai_disable_task_recognition = True
|
|
assert _read_disable_flag(
|
|
a, "fudai_disable_task_recognition", "fudai_disable_task_recognition"
|
|
) is True
|
|
|
|
def test_attr_false_falls_back_to_config_true(self, tmp_path, monkeypatch):
|
|
a = _mk_analyser()
|
|
a.fudai_disable_task_recognition = False
|
|
cfg_path = tmp_path / "config.json"
|
|
cfg_path.write_text(
|
|
json.dumps({"fudai_disable_task_recognition": True}), encoding="utf-8"
|
|
)
|
|
from douyin.core import path_config
|
|
monkeypatch.setattr(path_config, "get_config_path", lambda: str(cfg_path))
|
|
assert _read_disable_flag(
|
|
a, "fudai_disable_task_recognition", "fudai_disable_task_recognition"
|
|
) is True
|
|
|
|
def test_attr_false_config_false_returns_false(self, tmp_path, monkeypatch):
|
|
a = _mk_analyser()
|
|
a.fudai_disable_task_recognition = False
|
|
cfg_path = tmp_path / "config.json"
|
|
cfg_path.write_text(json.dumps({}), encoding="utf-8")
|
|
from douyin.core import path_config
|
|
monkeypatch.setattr(path_config, "get_config_path", lambda: str(cfg_path))
|
|
assert _read_disable_flag(
|
|
a, "fudai_disable_task_recognition", "fudai_disable_task_recognition"
|
|
) is False
|
|
|
|
def test_missing_config_file_returns_false(self, tmp_path, monkeypatch):
|
|
a = _mk_analyser()
|
|
a.x = False
|
|
from douyin.core import path_config
|
|
monkeypatch.setattr(
|
|
path_config, "get_config_path", lambda: str(tmp_path / "nope.json")
|
|
)
|
|
assert _read_disable_flag(a, "x", "y") is False
|
|
|
|
|
|
# ============================================================
|
|
# _ocr_digits_with_fallback
|
|
# ============================================================
|
|
|
|
class TestOcrDigitsWithFallback:
|
|
def test_digits_succeeds_immediately(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_digits = MagicMock(return_value="42")
|
|
a.operation.analyse_pic_word = MagicMock(return_value="should_not_be_called")
|
|
assert _ocr_digits_with_fallback(a, "X") == "42"
|
|
a.operation.analyse_pic_word.assert_not_called()
|
|
|
|
def test_digits_fails_word_succeeds(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_digits = MagicMock(return_value="")
|
|
a.operation.analyse_pic_word = MagicMock(return_value="abc12def")
|
|
assert _ocr_digits_with_fallback(a, "X") == "12"
|
|
|
|
def test_digits_throws_word_succeeds(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_digits = MagicMock(side_effect=RuntimeError())
|
|
a.operation.analyse_pic_word = MagicMock(return_value="3")
|
|
assert _ocr_digits_with_fallback(a, "X") == "3"
|
|
|
|
def test_both_fail_returns_empty(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_digits = MagicMock(return_value=None)
|
|
a.operation.analyse_pic_word = MagicMock(return_value=None)
|
|
assert _ocr_digits_with_fallback(a, "X") == ""
|
|
|
|
def test_both_throw_returns_empty(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_digits = MagicMock(side_effect=RuntimeError())
|
|
a.operation.analyse_pic_word = MagicMock(side_effect=RuntimeError())
|
|
assert _ocr_digits_with_fallback(a, "X") == ""
|
|
|
|
|
|
# ============================================================
|
|
# run_heavy_ocr
|
|
# ============================================================
|
|
|
|
class TestRunHeavyOcr:
|
|
def test_empty_fields_returns_only_duration(self):
|
|
a = _mk_analyser()
|
|
result = run_heavy_ocr(a, set())
|
|
# 只应有 _duration_ms
|
|
assert set(result.keys()) == {"_duration_ms"}
|
|
assert isinstance(result["_duration_ms"], (int, float))
|
|
|
|
def test_content_field_calls_analyse_pic_word(self):
|
|
a = _mk_analyser()
|
|
a.fudai_disable_content_recognition = False
|
|
a.operation.analyse_pic_word = MagicMock(return_value="福袋内容文本")
|
|
with patch(
|
|
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
|
return_value=False,
|
|
):
|
|
result = run_heavy_ocr(a, {"content"})
|
|
assert result["content"] == "福袋内容文本"
|
|
|
|
def test_content_skipped_when_disabled(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_word = MagicMock()
|
|
with patch(
|
|
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
|
return_value=True,
|
|
):
|
|
result = run_heavy_ocr(a, {"content"})
|
|
# content 不应在结果中
|
|
assert "content" not in result
|
|
# 但应有跳过日志
|
|
assert any("跳过" in str(c) for c in a._log_kv.call_args_list)
|
|
|
|
def test_task_list_skipped_when_disabled(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_word = MagicMock()
|
|
with patch(
|
|
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
|
return_value=True,
|
|
):
|
|
result = run_heavy_ocr(a, {"task_list"})
|
|
assert "task_list" not in result
|
|
|
|
def test_countdown_mm_uses_digits_path(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_digits = MagicMock(return_value="23")
|
|
with patch(
|
|
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
|
return_value=False,
|
|
):
|
|
result = run_heavy_ocr(a, {"countdown_mm"})
|
|
assert result["countdown_mm"] == "23"
|
|
|
|
def test_countdown_ss_uses_digits_path(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_digits = MagicMock(return_value="45")
|
|
with patch(
|
|
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
|
return_value=False,
|
|
):
|
|
result = run_heavy_ocr(a, {"countdown_ss"})
|
|
assert result["countdown_ss"] == "45"
|
|
|
|
def test_value_field(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_word = MagicMock(return_value="¥500")
|
|
with patch(
|
|
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
|
return_value=False,
|
|
):
|
|
result = run_heavy_ocr(a, {"value"})
|
|
assert result["value"] == "¥500"
|
|
|
|
def test_button_field(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_word = MagicMock(return_value="参与抽奖")
|
|
with patch(
|
|
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
|
return_value=False,
|
|
):
|
|
result = run_heavy_ocr(a, {"button"})
|
|
assert result["button"] == "参与抽奖"
|
|
|
|
def test_all_fields_at_once(self):
|
|
a = _mk_analyser()
|
|
a.operation.analyse_pic_word = MagicMock(return_value="word_result")
|
|
a.operation.analyse_pic_digits = MagicMock(return_value="11")
|
|
with patch(
|
|
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
|
return_value=False,
|
|
):
|
|
result = run_heavy_ocr(
|
|
a,
|
|
{"task_list", "content", "countdown_mm", "countdown_ss", "value", "button"},
|
|
)
|
|
assert "task_list" in result
|
|
assert "content" in result
|
|
assert "countdown_mm" in result
|
|
assert "countdown_ss" in result
|
|
assert "value" in result
|
|
assert "button" in result
|
|
assert "_duration_ms" in result
|
|
|
|
|
|
class TestHeavyFieldSelection:
|
|
def test_task_list_heavy_is_off_by_default(self):
|
|
fields = should_use_heavy(
|
|
False,
|
|
True,
|
|
"",
|
|
"",
|
|
"",
|
|
"",
|
|
"",
|
|
"",
|
|
)
|
|
|
|
assert "task_list" not in fields
|
|
assert {"content", "countdown_mm", "countdown_ss", "value", "button"} <= fields
|
|
|
|
def test_task_list_heavy_can_be_enabled_explicitly(self):
|
|
fields = should_use_heavy(
|
|
False,
|
|
True,
|
|
"",
|
|
"",
|
|
"",
|
|
"",
|
|
"",
|
|
"",
|
|
allow_task_list_heavy=True,
|
|
)
|
|
|
|
assert "task_list" in fields
|