improve fudai ocr debug captures
This commit is contained in:
1 parent
62c84939ee
commit
207cf0eadb
10 files changed
+382
-117
No files matched your search
@@ -1,93 +1,94 @@
|
||||
"""douyin/handlers/screenshot_manager 子模块单元测试。
|
||||
|
||||
覆盖:
|
||||
- save_image: _sanitize_filename_part 纯函数
|
||||
- save_files: 函数签名验证
|
||||
"""
|
||||
|
||||
"""douyin/handlers/screenshot_manager 子模块单元测试。
|
||||
|
||||
覆盖:
|
||||
- save_image: _sanitize_filename_part 纯函数
|
||||
- save_files: 函数签名验证
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from PIL import Image
|
||||
import pytest
|
||||
|
||||
|
||||
class TestSanitizeFilenamePart:
|
||||
"""_sanitize_filename_part 纯函数测试。"""
|
||||
|
||||
def test_empty_string(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("")
|
||||
assert result == ""
|
||||
|
||||
def test_none_input(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part(None)
|
||||
assert result == ""
|
||||
|
||||
def test_removes_invalid_chars(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part('file<>:"/\\|?*name')
|
||||
assert "<" not in result
|
||||
assert ">" not in result
|
||||
assert ":" not in result
|
||||
assert '"' not in result
|
||||
assert "/" not in result
|
||||
assert "\\" not in result
|
||||
assert "|" not in result
|
||||
assert "?" not in result
|
||||
assert "*" not in result
|
||||
|
||||
def test_normal_filename(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("normal_filename")
|
||||
assert "normal" in result
|
||||
assert "filename" in result
|
||||
|
||||
def test_chinese_filename(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("福袋截图")
|
||||
assert "福袋" in result or len(result) > 0
|
||||
|
||||
def test_extracts_basename_from_path(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("/path/to/file.png")
|
||||
assert "path" not in result or "file" in result
|
||||
|
||||
|
||||
class TestSaveFilesModule:
|
||||
"""save_files 模块测试。"""
|
||||
|
||||
def test_cleanup_ocr_dir_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import cleanup_ocr_dir
|
||||
|
||||
assert callable(cleanup_ocr_dir)
|
||||
|
||||
def test_save_latest_attempt_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_latest_attempt
|
||||
|
||||
assert callable(save_latest_attempt)
|
||||
|
||||
def test_save_last_success_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_last_success
|
||||
|
||||
assert callable(save_last_success)
|
||||
|
||||
def test_save_attempt_from_file_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_attempt_from_file
|
||||
|
||||
assert callable(save_attempt_from_file)
|
||||
|
||||
def test_save_success_from_file_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_success_from_file
|
||||
|
||||
assert callable(save_success_from_file)
|
||||
|
||||
|
||||
|
||||
|
||||
class TestSanitizeFilenamePart:
|
||||
"""_sanitize_filename_part 纯函数测试。"""
|
||||
|
||||
def test_empty_string(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("")
|
||||
assert result == ""
|
||||
|
||||
def test_none_input(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part(None)
|
||||
assert result == ""
|
||||
|
||||
def test_removes_invalid_chars(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part('file<>:"/\\|?*name')
|
||||
assert "<" not in result
|
||||
assert ">" not in result
|
||||
assert ":" not in result
|
||||
assert '"' not in result
|
||||
assert "/" not in result
|
||||
assert "\\" not in result
|
||||
assert "|" not in result
|
||||
assert "?" not in result
|
||||
assert "*" not in result
|
||||
|
||||
def test_normal_filename(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("normal_filename")
|
||||
assert "normal" in result
|
||||
assert "filename" in result
|
||||
|
||||
def test_chinese_filename(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("福袋截图")
|
||||
assert "福袋" in result or len(result) > 0
|
||||
|
||||
def test_extracts_basename_from_path(self):
|
||||
from douyin.handlers.screenshot_manager.save_image import _sanitize_filename_part
|
||||
|
||||
result = _sanitize_filename_part("/path/to/file.png")
|
||||
assert "path" not in result or "file" in result
|
||||
|
||||
|
||||
class TestSaveFilesModule:
|
||||
"""save_files 模块测试。"""
|
||||
|
||||
def test_cleanup_ocr_dir_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import cleanup_ocr_dir
|
||||
|
||||
assert callable(cleanup_ocr_dir)
|
||||
|
||||
def test_save_latest_attempt_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_latest_attempt
|
||||
|
||||
assert callable(save_latest_attempt)
|
||||
|
||||
def test_save_last_success_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_last_success
|
||||
|
||||
assert callable(save_last_success)
|
||||
|
||||
def test_save_attempt_from_file_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_attempt_from_file
|
||||
|
||||
assert callable(save_attempt_from_file)
|
||||
|
||||
def test_save_success_from_file_exists(self):
|
||||
from douyin.handlers.screenshot_manager.save_files import save_success_from_file
|
||||
|
||||
assert callable(save_success_from_file)
|
||||
|
||||
|
||||
class TestSaveImageModule:
|
||||
"""save_image 模块测试。"""
|
||||
|
||||
@@ -167,7 +168,7 @@ class TestSaveImageModule:
|
||||
)
|
||||
|
||||
assert filename.endswith(".png")
|
||||
assert len(filename.split("_", 3)[2]) == 6
|
||||
assert len(filename.split("_", 3)[2]) == 6
|
||||
|
||||
def test_cleanup_single_operation_debug_images_cleans_detection_only(self, tmp_path, monkeypatch):
|
||||
from douyin.handlers.screenshot_manager.save_image_helpers import (
|
||||
@@ -188,31 +189,66 @@ class TestSaveImageModule:
|
||||
assert not old_det.exists()
|
||||
assert old_px.exists()
|
||||
|
||||
def test_annotate_debug_image_uses_cn_maps_without_nameerror(self):
|
||||
from douyin.handlers.screenshot_manager.save_image_helpers import annotate_debug_image
|
||||
|
||||
class TestScreenshotManagerSubmodules:
|
||||
"""screenshot_manager 子模块存在性测试。"""
|
||||
image = Image.new("RGB", (200, 120), "white")
|
||||
annotated = annotate_debug_image(
|
||||
image=image,
|
||||
picname="fudai_content",
|
||||
step="after_ocr",
|
||||
coords=(10, 10, 100, 80),
|
||||
extra_info={"text_preview": "儿童装饰手表生活"},
|
||||
)
|
||||
|
||||
def test_paths_importable(self):
|
||||
from douyin.handlers.screenshot_manager import paths
|
||||
assert annotated.size == image.size
|
||||
|
||||
assert paths is not None
|
||||
def test_annotate_debug_image_draws_green_arrow_for_fudai_icon_detected(self):
|
||||
from douyin.handlers.screenshot_manager.save_image_helpers import annotate_debug_image
|
||||
|
||||
def test_cleanup_importable(self):
|
||||
from douyin.handlers.screenshot_manager import cleanup
|
||||
image = Image.new("RGB", (240, 160), "white")
|
||||
annotated = annotate_debug_image(
|
||||
image=image,
|
||||
picname="fudai_icon",
|
||||
step="detected",
|
||||
coords=(80, 60, 180, 100),
|
||||
extra_info={"detected_x_scaled": 150, "result": "success"},
|
||||
)
|
||||
|
||||
assert cleanup is not None
|
||||
|
||||
def test_async_save_importable(self):
|
||||
from douyin.handlers.screenshot_manager import async_save
|
||||
|
||||
assert async_save is not None
|
||||
|
||||
def test_save_files_importable(self):
|
||||
from douyin.handlers.screenshot_manager import save_files
|
||||
|
||||
assert save_files is not None
|
||||
|
||||
def test_save_image_importable(self):
|
||||
from douyin.handlers.screenshot_manager import save_image
|
||||
|
||||
assert save_image is not None
|
||||
assert annotated.size == image.size
|
||||
green_pixels = 0
|
||||
for x in range(30, 151):
|
||||
for y in range(10, 81):
|
||||
r, g, b = annotated.getpixel((x, y))
|
||||
if g >= 160 and r <= 80 and b <= 120:
|
||||
green_pixels += 1
|
||||
assert green_pixels > 20
|
||||
|
||||
|
||||
class TestScreenshotManagerSubmodules:
|
||||
"""screenshot_manager 子模块存在性测试。"""
|
||||
|
||||
def test_paths_importable(self):
|
||||
from douyin.handlers.screenshot_manager import paths
|
||||
|
||||
assert paths is not None
|
||||
|
||||
def test_cleanup_importable(self):
|
||||
from douyin.handlers.screenshot_manager import cleanup
|
||||
|
||||
assert cleanup is not None
|
||||
|
||||
def test_async_save_importable(self):
|
||||
from douyin.handlers.screenshot_manager import async_save
|
||||
|
||||
assert async_save is not None
|
||||
|
||||
def test_save_files_importable(self):
|
||||
from douyin.handlers.screenshot_manager import save_files
|
||||
|
||||
assert save_files is not None
|
||||
|
||||
def test_save_image_importable(self):
|
||||
from douyin.handlers.screenshot_manager import save_image
|
||||
|
||||
assert save_image is not None
|
||||
Reference in new issue
Block a user