b5896427d4
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
"""FC-5: verify + ml_queue unit tests."""
|
|
import hashlib
|
|
|
|
import pytest
|
|
|
|
from backend.app.models import Artist, ImageRecord
|
|
from backend.app.services.migrators import ml_queue, verify
|
|
|
|
pytestmark = pytest.mark.integration
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_verify_row_counts_match(db):
|
|
db.add(Artist(name="vAlpha", slug="valpha", is_subscription=True))
|
|
db.add(Artist(name="vBeta", slug="vbeta", is_subscription=False))
|
|
await db.commit()
|
|
result = await verify.verify_async(
|
|
db, expected={"artist_subscriptions": 1},
|
|
)
|
|
assert result["artist_subscriptions"]["status"] == "ok"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_verify_sha256_sample_matches_disk(db, tmp_path):
|
|
f = tmp_path / "sample.bin"
|
|
f.write_bytes(b"hello world")
|
|
sha = hashlib.sha256(b"hello world").hexdigest()
|
|
db.add(ImageRecord(
|
|
path=str(f), sha256=sha, size_bytes=11, mime="application/octet-stream",
|
|
origin="imported_filesystem",
|
|
))
|
|
await db.commit()
|
|
result = await verify.verify_sha256_sample(db, sample_size=10)
|
|
assert result["matched"] >= 1
|
|
assert result["mismatched"] == 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_verify_sha256_sample_detects_missing_file(db, tmp_path):
|
|
sha = "f" * 64
|
|
db.add(ImageRecord(
|
|
path=str(tmp_path / "does-not-exist.bin"),
|
|
sha256=sha, size_bytes=1, mime="application/octet-stream",
|
|
origin="imported_filesystem",
|
|
))
|
|
await db.commit()
|
|
result = await verify.verify_sha256_sample(db, sample_size=10)
|
|
assert result["missing"] >= 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ml_queue_returns_count_for_unprocessed(db, tmp_path, monkeypatch):
|
|
queued: list[int] = []
|
|
|
|
class _FakeTask:
|
|
def delay(self, image_id):
|
|
queued.append(image_id)
|
|
|
|
monkeypatch.setattr(
|
|
"backend.app.tasks.ml.tag_and_embed", _FakeTask(),
|
|
)
|
|
|
|
f = tmp_path / "q.bin"
|
|
f.write_bytes(b"x")
|
|
db.add(ImageRecord(
|
|
path=str(f), sha256="q" * 64, size_bytes=1,
|
|
mime="application/octet-stream", origin="imported_filesystem",
|
|
))
|
|
await db.commit()
|
|
|
|
count = await ml_queue.queue_all_unprocessed_async(db)
|
|
assert count >= 1
|