S SmartDocs
Serie: Ricky python 79 righe · Aggiornato 2026-03-23

test_ingestion_manifest.py

Ricky/RAG_project/tests/test_ingestion_manifest.py

"""
Unit tests for ingestion/manifest.py — file-system operations mocked.
"""
import os
import json
import tempfile
import pytest

from ingestion.manifest import (
    compute_raw_data_hash,
    load_manifest,
    save_manifest,
    needs_rebuild,
)


@pytest.fixture
def tmp_raw_dir(tmp_path):
    """A temporary directory with a couple of fake files."""
    raw = tmp_path / "raw"
    raw.mkdir()
    (raw / "file_a.txt").write_text("content A")
    (raw / "file_b.txt").write_text("content B")
    return str(raw)


@pytest.fixture
def tmp_chroma_dir(tmp_path):
    db = tmp_path / "chroma_db"
    db.mkdir()
    return str(db)


class TestComputeRawDataHash:
    def test_returns_string(self, tmp_raw_dir):
        h = compute_raw_data_hash([tmp_raw_dir])
        assert isinstance(h, str)
        assert len(h) == 32  # MD5 hex

    def test_same_files_same_hash(self, tmp_raw_dir):
        h1 = compute_raw_data_hash([tmp_raw_dir])
        h2 = compute_raw_data_hash([tmp_raw_dir])
        assert h1 == h2

    def test_new_file_changes_hash(self, tmp_raw_dir):
        h1 = compute_raw_data_hash([tmp_raw_dir])
        open(os.path.join(tmp_raw_dir, "new_file.txt"), "w").close()
        h2 = compute_raw_data_hash([tmp_raw_dir])
        assert h1 != h2

    def test_missing_dir_does_not_crash(self):
        h = compute_raw_data_hash(["/nonexistent/path"])
        assert isinstance(h, str)


class TestLoadSaveManifest:
    def test_load_empty_when_no_file(self, tmp_chroma_dir):
        result = load_manifest(tmp_chroma_dir)
        assert result == {}

    def test_save_then_load(self, tmp_raw_dir, tmp_chroma_dir):
        save_manifest(tmp_chroma_dir, [tmp_raw_dir])
        manifest = load_manifest(tmp_chroma_dir)
        assert "raw_data_hash" in manifest
        assert isinstance(manifest["raw_data_hash"], str)


class TestNeedsRebuild:
    def test_needs_rebuild_when_no_manifest(self, tmp_raw_dir, tmp_chroma_dir):
        assert needs_rebuild(tmp_chroma_dir, [tmp_raw_dir])

    def test_no_rebuild_after_save(self, tmp_raw_dir, tmp_chroma_dir):
        save_manifest(tmp_chroma_dir, [tmp_raw_dir])
        assert not needs_rebuild(tmp_chroma_dir, [tmp_raw_dir])

    def test_rebuild_after_file_added(self, tmp_raw_dir, tmp_chroma_dir):
        save_manifest(tmp_chroma_dir, [tmp_raw_dir])
        open(os.path.join(tmp_raw_dir, "added.txt"), "w").close()
        assert needs_rebuild(tmp_chroma_dir, [tmp_raw_dir])

Articoli correlati