Spaces:
Sleeping
Sleeping
File size: 2,080 Bytes
256d433 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | """Tests for src.cache.ocr_cache — SHA256 keying, hit/miss, disk I/O."""
from __future__ import annotations
from pathlib import Path
import pytest
from src.cache.ocr_cache import OcrCache
class TestOcrCacheKey:
"""Key generation is pure SHA256 of the raw bytes."""
def test_key_deterministic(self) -> None:
content = b"same content"
assert OcrCache.key(content) == OcrCache.key(content)
def test_key_content_sensitive(self) -> None:
assert OcrCache.key(b"a") != OcrCache.key(b"b")
def test_key_length(self) -> None:
assert len(OcrCache.key(b"x")) == 64 # SHA256 hex
class TestOcrCacheHitMiss:
"""get / put round-trips through the filesystem."""
def test_miss_on_empty_cache(self, tmp_path: Path) -> None:
cache = OcrCache(tmp_path / "ocr")
assert cache.get(b"unknown") is None
def test_put_and_get(self, tmp_path: Path) -> None:
cache = OcrCache(tmp_path / "ocr")
content = b"page image bytes"
cache.put(content, "# Markdown\n")
assert cache.get(content) == "# Markdown\n"
def test_put_overwrites(self, tmp_path: Path) -> None:
cache = OcrCache(tmp_path / "ocr")
content = b"same"
cache.put(content, "v1")
cache.put(content, "v2")
assert cache.get(content) == "v2"
def test_different_content_different_files(self, tmp_path: Path) -> None:
cache = OcrCache(tmp_path / "ocr")
c1 = b"page 1"
c2 = b"page 2"
cache.put(c1, "md1")
cache.put(c2, "md2")
assert cache.get(c1) == "md1"
assert cache.get(c2) == "md2"
def test_file_extension(self, tmp_path: Path) -> None:
cache = OcrCache(tmp_path / "ocr")
content = b"x"
key = cache.key(content)
cache.put(content, "text")
assert (cache.root / f"{key}.md").exists()
def test_directory_created(self, tmp_path: Path) -> None:
root = tmp_path / "nested" / "ocr"
assert not root.exists()
cache = OcrCache(root)
assert root.is_dir()
|