"""douyin/core/underlying_operations_pkg/ocr_pipeline 单元测试。 analyse_pic_word 主流程的早出和分支测试。OCR 实际调用通过 mock 完成。 """ from __future__ import annotations import os from unittest.mock import MagicMock, patch import pytest try: from PIL import Image except ImportError: # pragma: no cover pytest.skip("PIL unavailable", allow_module_level=True) from douyin.core.underlying_operations_pkg.ocr_pipeline import analyse_pic_word @pytest.fixture(autouse=True) def _clear_ocr_cache(): """每个测试前清 operations.ocr_operations 模块级 OCR 缓存,避免互相污染。""" try: from operations import ocr_operations with ocr_operations._ocr_cache_lock: ocr_operations._ocr_cache.clear() except Exception: pass yield try: from operations import ocr_operations with ocr_operations._ocr_cache_lock: ocr_operations._ocr_cache.clear() except Exception: pass def _mk_self(*, ocr=None, ocr_type=None, save_debug=False): s = MagicMock() s.ocr = ocr s.ocr_type = ocr_type s.save_ocr_debug_images = save_debug s._find_pic_path = MagicMock(return_value="") s._ocr_coords_lock = MagicMock() s._ocr_coords_lock.__enter__ = MagicMock(return_value=None) s._ocr_coords_lock.__exit__ = MagicMock(return_value=False) s._ocr_coords_cache = {} # extract_ocr_content 默认返回 ("", None) s.extract_ocr_content = MagicMock(return_value=("", None)) return s def _make_real_pic(tmp_path): """生成一张真实 PNG 文件作为 OCR 输入,避免 mock PIL.Image 引入复杂度。""" p = tmp_path / "tmp.png" Image.new("RGB", (50, 50), (200, 200, 200)).save(str(p)) return str(p) # ============================================================ # 早出条件 # ============================================================ class TestEarlyReturns: def test_pic_path_empty_returns_empty(self): s = _mk_self() s._find_pic_path = MagicMock(return_value="") result = analyse_pic_word(s, "X", change_color=0) assert result == "" def test_pic_path_nonexistent_returns_empty(self): s = _mk_self() s._find_pic_path = MagicMock(return_value="/nonexistent/path.png") result = analyse_pic_word(s, "X", change_color=0) assert result == "" def test_pic_path_nonexistent_with_confidence(self): s = _mk_self() s._find_pic_path = MagicMock(return_value="/nonexistent/path.png") text, conf = analyse_pic_word( s, "X", change_color=0, return_confidence=True ) assert text == "" assert conf is None def test_ocr_not_initialized_returns_empty(self, tmp_path): s = _mk_self(ocr=None) pic_path = _make_real_pic(tmp_path) result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path) assert result == "" # ============================================================ # OCR 调用路径 # ============================================================ class TestOcrCallPaths: def test_easyocr_path(self, tmp_path): ocr = MagicMock() ocr.readtext = MagicMock(return_value=[("bbox", "识别出的文本", 0.95)]) s = _mk_self(ocr=ocr, ocr_type="easyocr") s.extract_ocr_content = MagicMock( return_value=("识别出的文本", {"avg_confidence": 0.95, "item_count": 1}) ) pic_path = _make_real_pic(tmp_path) result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path) assert result == "识别出的文本" ocr.readtext.assert_called_once() def test_paddleocr_predict_path(self, tmp_path): ocr = MagicMock() ocr.predict = MagicMock(return_value=[{"some": "result"}]) s = _mk_self(ocr=ocr, ocr_type="paddleocr") s.extract_ocr_content = MagicMock(return_value=("paddle text", None)) pic_path = _make_real_pic(tmp_path) result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path) assert result == "paddle text" ocr.predict.assert_called_once() def test_paddleocr_ocr_fallback(self, tmp_path): """没有 predict 方法时回退到 ocr() 调用。""" ocr = MagicMock(spec=["ocr"]) ocr.ocr = MagicMock(return_value=[{"some": "result"}]) s = _mk_self(ocr=ocr, ocr_type="paddleocr") s.extract_ocr_content = MagicMock(return_value=("ocr text", None)) pic_path = _make_real_pic(tmp_path) result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path) assert result == "ocr text" ocr.ocr.assert_called_once() def test_paddleocr_returns_none_short_circuits(self, tmp_path): """PaddleOCR 返回 [None] 时直接返回空(不调用 extract_ocr_content)。""" ocr = MagicMock() ocr.predict = MagicMock(return_value=[None]) s = _mk_self(ocr=ocr, ocr_type="paddleocr") s.extract_ocr_content = MagicMock(return_value=("不应被调用", None)) pic_path = _make_real_pic(tmp_path) result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path) assert result == "" s.extract_ocr_content.assert_not_called() def test_ocr_throws_returns_empty(self, tmp_path): """OCR 调用抛异常时安全返回空。""" ocr = MagicMock() ocr.predict = MagicMock(side_effect=RuntimeError("OCR fail")) s = _mk_self(ocr=ocr, ocr_type="paddleocr") pic_path = _make_real_pic(tmp_path) result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path) assert result == "" # ============================================================ # 缓存路径 # ============================================================ class TestCachePath: def test_cache_hit_skips_ocr(self, tmp_path): """缓存命中时不应调用 OCR。""" ocr = MagicMock() ocr.predict = MagicMock(return_value=[{"X": 1}]) s = _mk_self(ocr=ocr, ocr_type="paddleocr") pic_path = _make_real_pic(tmp_path) with patch( "operations.ocr_operations._img_fingerprint", return_value="cache_key_X", ), patch( "operations.ocr_operations.cache_get", return_value=("缓存的文本", {"avg_confidence": 0.99}), ): result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path) assert result == "缓存的文本" # OCR 不应被调用 ocr.predict.assert_not_called() def test_cache_miss_runs_ocr_and_writes_back(self, tmp_path): ocr = MagicMock() ocr.predict = MagicMock(return_value=[{"X": 1}]) s = _mk_self(ocr=ocr, ocr_type="paddleocr") s.extract_ocr_content = MagicMock(return_value=("新识别的文本", None)) pic_path = _make_real_pic(tmp_path) with patch( "operations.ocr_operations._img_fingerprint", return_value="key1" ), patch( "operations.ocr_operations.cache_get", return_value=None ), patch( "operations.ocr_operations.cache_put" ) as mock_put: result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path) assert result == "新识别的文本" # OCR 被调用 + 结果写回缓存 ocr.predict.assert_called_once() mock_put.assert_called_once_with("key1", ("新识别的文本", None)) # ============================================================ # 颜色预处理分支 # ============================================================ class TestChangeColor: def test_mode_0_keeps_color(self, tmp_path): ocr = MagicMock() ocr.predict = MagicMock(return_value=[{"X": 1}]) s = _mk_self(ocr=ocr, ocr_type="paddleocr") s.extract_ocr_content = MagicMock(return_value=("text", None)) pic_path = _make_real_pic(tmp_path) # 不抛即可 result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path) assert isinstance(result, str) def test_mode_1_binarizes(self, tmp_path): ocr = MagicMock() ocr.predict = MagicMock(return_value=[{"X": 1}]) s = _mk_self(ocr=ocr, ocr_type="paddleocr") s.extract_ocr_content = MagicMock(return_value=("binarized text", None)) pic_path = _make_real_pic(tmp_path) # change_color=1 走二值化分支 result = analyse_pic_word(s, "X", change_color=1, pic_path=pic_path) assert isinstance(result, str) def test_mode_2_binarizes_high_threshold(self, tmp_path): ocr = MagicMock() ocr.predict = MagicMock(return_value=[{"X": 1}]) s = _mk_self(ocr=ocr, ocr_type="paddleocr") s.extract_ocr_content = MagicMock(return_value=("text", None)) pic_path = _make_real_pic(tmp_path) result = analyse_pic_word(s, "X", change_color=2, pic_path=pic_path) assert isinstance(result, str) # ============================================================ # return_confidence # ============================================================ class TestReturnConfidence: def test_returns_text_only_by_default(self, tmp_path): ocr = MagicMock() ocr.predict = MagicMock(return_value=[{"X": 1}]) s = _mk_self(ocr=ocr, ocr_type="paddleocr") s.extract_ocr_content = MagicMock(return_value=("text", {"item_count": 1})) pic_path = _make_real_pic(tmp_path) result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path) # 默认只返回字符串 assert isinstance(result, str) assert result == "text" def test_returns_tuple_with_flag(self, tmp_path): ocr = MagicMock() ocr.predict = MagicMock(return_value=[{"X": 1}]) conf = {"avg_confidence": 0.9, "item_count": 1} s = _mk_self(ocr=ocr, ocr_type="paddleocr") s.extract_ocr_content = MagicMock(return_value=("text", conf)) pic_path = _make_real_pic(tmp_path) text, ret_conf = analyse_pic_word( s, "X", change_color=0, pic_path=pic_path, return_confidence=True ) assert text == "text" assert ret_conf == conf