- OCR 缓存机制测试(cache_get/put/clear/stats) - _img_fingerprint 哈希函数测试 - screenshot_operations / screenshot_service 类存在性验证 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
160 lines
4.7 KiB
Python
160 lines
4.7 KiB
Python
"""operations 模块单元测试。
|
|
|
|
覆盖:
|
|
- ocr_operations: 缓存机制(cache_get/put/clear/stats)、_img_fingerprint
|
|
- screenshot_operations: _find_pic_path 纯逻辑
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
|
|
class TestOcrCache:
|
|
"""OCR 缓存机制测试。"""
|
|
|
|
def test_cache_put_and_get(self):
|
|
from operations.ocr_operations import cache_clear, cache_get, cache_put
|
|
|
|
cache_clear()
|
|
cache_put("test_key_1", {"text": "hello"})
|
|
result = cache_get("test_key_1")
|
|
assert result == {"text": "hello"}
|
|
|
|
def test_cache_miss_returns_none(self):
|
|
from operations.ocr_operations import cache_clear, cache_get
|
|
|
|
cache_clear()
|
|
result = cache_get("nonexistent_key")
|
|
assert result is None
|
|
|
|
def test_cache_clear(self):
|
|
from operations.ocr_operations import cache_clear, cache_get, cache_put
|
|
|
|
cache_put("key_to_clear", "value")
|
|
cache_clear()
|
|
assert cache_get("key_to_clear") is None
|
|
|
|
def test_cache_stats(self):
|
|
from operations.ocr_operations import cache_clear, cache_get, cache_put, cache_stats
|
|
|
|
cache_clear()
|
|
# 重置 stats
|
|
stats = cache_stats()
|
|
initial_hit = stats.get("hit", 0)
|
|
initial_miss = stats.get("miss", 0)
|
|
|
|
cache_put("stats_key", "value")
|
|
cache_get("stats_key") # hit
|
|
cache_get("nonexistent") # miss
|
|
|
|
stats = cache_stats()
|
|
# 验证 stats 结构
|
|
assert "hit" in stats
|
|
assert "miss" in stats
|
|
|
|
def test_cache_overwrites_existing_key(self):
|
|
from operations.ocr_operations import cache_clear, cache_get, cache_put
|
|
|
|
cache_clear()
|
|
cache_put("overwrite_key", "old_value")
|
|
cache_put("overwrite_key", "new_value")
|
|
assert cache_get("overwrite_key") == "new_value"
|
|
|
|
|
|
class TestImgFingerprint:
|
|
"""_img_fingerprint 哈希函数测试。"""
|
|
|
|
def test_same_bytes_same_hash(self):
|
|
from operations.ocr_operations import _img_fingerprint
|
|
|
|
data = b"test image data"
|
|
h1 = _img_fingerprint(data, mode=0)
|
|
h2 = _img_fingerprint(data, mode=0)
|
|
assert h1 == h2
|
|
|
|
def test_different_bytes_different_hash(self):
|
|
from operations.ocr_operations import _img_fingerprint
|
|
|
|
h1 = _img_fingerprint(b"image1", mode=0)
|
|
h2 = _img_fingerprint(b"image2", mode=0)
|
|
assert h1 != h2
|
|
|
|
def test_different_mode_different_hash(self):
|
|
from operations.ocr_operations import _img_fingerprint
|
|
|
|
data = b"same data"
|
|
h1 = _img_fingerprint(data, mode=0)
|
|
h2 = _img_fingerprint(data, mode=1)
|
|
assert h1 != h2
|
|
|
|
def test_hash_is_hex_string(self):
|
|
from operations.ocr_operations import _img_fingerprint
|
|
|
|
h = _img_fingerprint(b"test", mode=0)
|
|
assert isinstance(h, str)
|
|
assert all(c in "0123456789abcdef" for c in h)
|
|
|
|
def test_hash_length_consistent(self):
|
|
from operations.ocr_operations import _img_fingerprint
|
|
|
|
h1 = _img_fingerprint(b"short", mode=0)
|
|
h2 = _img_fingerprint(b"a much longer piece of data", mode=0)
|
|
assert len(h1) == len(h2) # blake2b digest_size=12 → 24 hex chars
|
|
|
|
|
|
class TestScreenshotOperations:
|
|
"""screenshot_operations 模块测试。"""
|
|
|
|
def test_class_exists(self):
|
|
from operations.screenshot_operations import ScreenshotOperations
|
|
|
|
assert ScreenshotOperations is not None
|
|
|
|
def test_get_screenshot_function_exists(self):
|
|
from operations.screenshot_operations import get_screenshot
|
|
|
|
assert callable(get_screenshot)
|
|
|
|
def test_cut_pic_function_exists(self):
|
|
from operations.screenshot_operations import cut_pic
|
|
|
|
assert callable(cut_pic)
|
|
|
|
def test_save_reward_pic_function_exists(self):
|
|
from operations.screenshot_operations import save_reward_pic
|
|
|
|
assert callable(save_reward_pic)
|
|
|
|
|
|
class TestScreenshotService:
|
|
"""screenshot_service 模块测试。"""
|
|
|
|
def test_class_exists(self):
|
|
from operations.screenshot_service import ScreenshotService
|
|
|
|
assert ScreenshotService is not None
|
|
|
|
def test_get_optimal_workers(self):
|
|
from operations.screenshot_service import get_optimal_workers
|
|
|
|
workers = get_optimal_workers()
|
|
assert isinstance(workers, int)
|
|
assert workers >= 1
|
|
|
|
|
|
class TestOcrProcessor:
|
|
"""OCRProcessor 类测试。"""
|
|
|
|
def test_class_exists(self):
|
|
from operations.ocr_operations import OCRProcessor
|
|
|
|
assert OCRProcessor is not None
|
|
|
|
def test_class_has_expected_methods(self):
|
|
from operations.ocr_operations import OCRProcessor
|
|
|
|
# 检查类方法存在
|
|
assert hasattr(OCRProcessor, "_init_ocr")
|
|
assert hasattr(OCRProcessor, "_ensure_ocr")
|