Download tests/test_rag.py from AdryanAzul/DocuMindAI_RAG_Application: direct link, hf CLI and curl.
- Browser
- Download file 12 kB
-
https://huggingface.co/spaces/AdryanAzul/DocuMindAI_RAG_Application/resolve/main/tests/test_rag.py
- Command line
-
hf download hf://spaces/AdryanAzul/DocuMindAI_RAG_Application/tests/test_rag.py
-
curl -L -o test_rag.py https://huggingface.co/spaces/AdryanAzul/DocuMindAI_RAG_Application/resolve/main/tests/test_rag.py
12 kB
| """Testes unitários, de integração, isolamento e segurança para o DocuMind AI (BYOK).""" | |
| import logging | |
| from concurrent.futures import ThreadPoolExecutor | |
| from io import BytesIO | |
| from pathlib import Path | |
| import pytest | |
| from src.document_loader import DocumentLoadError, DocumentPage, load_pdf | |
| from src.embeddings import EmbeddingLoadError, EmbeddingModel | |
| from src.llm import BYOKLLM, LLMConfigError, sanitize_sensitive_text | |
| from src.rag_pipeline import RAGPipeline | |
| from src.retriever import Retriever | |
| from src.text_splitter import split_documents | |
| from src.vector_store import StoredDocument, VectorStore | |
| def _build_simple_pdf(text: str) -> bytes: | |
| """Gera um PDF mínimo válido em memória contendo o texto informado.""" | |
| safe_text = text.replace("\\", "\\\\").replace("(", "\\(").replace(")", "\\)") | |
| stream = f"BT /F1 12 Tf 72 720 Td ({safe_text}) Tj ET".encode("latin-1", errors="replace") | |
| objects = [ | |
| b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n", | |
| b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n", | |
| ( | |
| b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] " | |
| b"/Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>\nendobj\n" | |
| ), | |
| f"4 0 obj\n<< /Length {len(stream)} >>\nstream\n".encode("ascii") | |
| + stream | |
| + b"\nendstream\nendobj\n", | |
| b"5 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n", | |
| ] | |
| pdf = bytearray(b"%PDF-1.4\n") | |
| offsets = [0] | |
| for obj in objects: | |
| offsets.append(len(pdf)) | |
| pdf.extend(obj) | |
| xref_offset = len(pdf) | |
| pdf.extend(f"xref\n0 {len(offsets)}\n".encode("ascii")) | |
| pdf.extend(b"0000000000 65535 f \n") | |
| for offset in offsets[1:]: | |
| pdf.extend(f"{offset:010d} 00000 n \n".encode("ascii")) | |
| pdf.extend( | |
| f"trailer\n<< /Size {len(offsets)} /Root 1 0 R >>\nstartxref\n{xref_offset}\n%%EOF\n".encode("ascii") | |
| ) | |
| return bytes(pdf) | |
| class FakeEmbeddingModel: | |
| """Modelo de embeddings determinístico para testes rápidos sem rede.""" | |
| def embed_documents(self, texts: list[str]) -> list[list[float]]: | |
| return [self.embed_query(text) for text in texts] | |
| def embed_query(self, query: str) -> list[float]: | |
| lowered = query.lower() | |
| return [ | |
| float(lowered.count("rag") + lowered.count("retrieval")), | |
| float(lowered.count("python") + lowered.count("streamlit")), | |
| float(len(lowered) % 17 + 1), | |
| ] | |
| class FakeLLM: | |
| """Stub de LLM que verifica o contexto recebido.""" | |
| def __init__(self) -> None: | |
| self.calls = 0 | |
| def generate_answer(self, question: str, contexts: list[StoredDocument]) -> str: | |
| self.calls += 1 | |
| return f"Resposta baseada em {len(contexts)} trecho(s): {contexts[0].text}" | |
| def test_local_embedding_model_init_error_and_mock_encode(monkeypatch: pytest.MonkeyPatch) -> None: | |
| def failing_st(*args: object, **kwargs: object) -> object: | |
| raise OSError("Erro de rede ao baixar pesos") | |
| monkeypatch.setattr("src.embeddings.SentenceTransformer", failing_st) | |
| with pytest.raises(EmbeddingLoadError, match="Falha ao inicializar"): | |
| EmbeddingModel() | |
| class DummyArray: | |
| def __init__(self, data: list[object]) -> None: | |
| self._data = data | |
| def tolist(self) -> list[object]: | |
| return self._data | |
| class DummyST: | |
| def __init__(self, *args: object, **kwargs: object) -> None: | |
| pass | |
| def get_sentence_embedding_dimension(self) -> int: | |
| return 384 | |
| def encode(self, texts: object, **kwargs: object) -> DummyArray: | |
| if isinstance(texts, list): | |
| return DummyArray([[0.1] * 384 for _ in texts]) | |
| return DummyArray([0.1] * 384) | |
| monkeypatch.setattr("src.embeddings.SentenceTransformer", DummyST) | |
| model = EmbeddingModel() | |
| assert model.dimension == 384 | |
| assert len(model.embed_query("teste")) == 384 | |
| assert len(model.embed_documents(["a", "b"])) == 2 | |
| def test_load_pdf_empty_invalid_and_oversized() -> None: | |
| with pytest.raises(DocumentLoadError, match="vazio"): | |
| load_pdf(BytesIO(b""), filename="vazio.pdf") | |
| with pytest.raises(DocumentLoadError, match="inválido"): | |
| load_pdf(BytesIO(b"nao e pdf"), filename="invalido.pdf") | |
| oversized = b"%PDF-1.4" + b"0" * (2 * 1024 * 1024) | |
| with pytest.raises(DocumentLoadError, match="excede o limite"): | |
| load_pdf(BytesIO(oversized), filename="grande.pdf", max_size_mb=1) | |
| def test_load_pdf_valid_and_split_documents() -> None: | |
| pdf_bytes = _build_simple_pdf("DocuMind AI utiliza RAG com Python e ChromaDB.") | |
| pages = load_pdf(BytesIO(pdf_bytes), filename="arquitetura.pdf") | |
| assert len(pages) == 1 | |
| assert pages[0].source == "arquitetura.pdf" | |
| assert pages[0].page == 1 | |
| long_text = " ".join(f"palavra{i}" for i in range(120)) | |
| chunks = split_documents([DocumentPage(text=long_text, source="manual.pdf", page=3)], chunk_size=120, chunk_overlap=30) | |
| assert len(chunks) > 1 | |
| assert all(c.source == "manual.pdf" and c.page == 3 and len(c.text) <= 120 for c in chunks) | |
| def test_byok_modes_and_missing_credentials(monkeypatch: pytest.MonkeyPatch) -> None: | |
| # Mesmo com chaves no ambiente, BYOKLLM não deve usar chaves do proprietário | |
| monkeypatch.setenv("GEMINI_API_KEY", "AIzaOwnerSecretKey123456") | |
| monkeypatch.setenv("GROQ_API_KEY", "gsk_OwnerSecretKey123456") | |
| with pytest.raises(LLMConfigError, match="GEMINI_API_KEY"): | |
| BYOKLLM(mode="gemini") | |
| with pytest.raises(LLMConfigError, match="GROQ_API_KEY"): | |
| BYOKLLM(mode="groq") | |
| with pytest.raises(LLMConfigError, match="GEMINI_API_KEY"): | |
| BYOKLLM(mode="gemini_groq", gemini_api_key="AIzaUser123456", groq_api_key="") | |
| def test_byok_gemini_only_mode(monkeypatch: pytest.MonkeyPatch) -> None: | |
| llm = BYOKLLM(mode="gemini", gemini_api_key="AIzaUserKey1234567890") | |
| class DummyResp: | |
| text = "Resposta via Gemini" | |
| monkeypatch.setattr(llm._client.models, "generate_content", lambda **kwargs: DummyResp()) # type: ignore[union-attr] | |
| result = llm.generate_result("Pergunta?", [StoredDocument(text="Contexto.", source="a.pdf", page=1)]) | |
| assert result.text == "Resposta via Gemini" | |
| assert result.provider == "gemini" | |
| assert result.used_fallback is False | |
| def test_byok_groq_only_mode(monkeypatch: pytest.MonkeyPatch) -> None: | |
| llm = BYOKLLM(mode="groq", groq_api_key="gsk_UserKey1234567890") | |
| class DummyHTTP: | |
| def raise_for_status(self) -> None: | |
| return None | |
| def json(self) -> dict[str, object]: | |
| return {"choices": [{"message": {"content": "Resposta direta Groq"}}]} | |
| monkeypatch.setattr("src.llm.httpx.post", lambda *args, **kwargs: DummyHTTP()) | |
| result = llm.generate_result("Pergunta?", [StoredDocument(text="Contexto.", source="a.pdf", page=1)]) | |
| assert result.text == "Resposta direta Groq" | |
| assert result.provider == "groq" | |
| assert result.used_fallback is False | |
| def test_byok_fallback_only_on_transient_error_without_duplicate_calls(monkeypatch: pytest.MonkeyPatch) -> None: | |
| llm = BYOKLLM( | |
| mode="gemini_groq", | |
| gemini_api_key="AIzaUserKey1234567890", | |
| groq_api_key="gsk_UserKey1234567890", | |
| ) | |
| gemini_calls = 0 | |
| groq_calls = 0 | |
| def transient_gemini(**kwargs: object) -> object: | |
| nonlocal gemini_calls | |
| gemini_calls += 1 | |
| raise TimeoutError("Tempo limite excedido") | |
| class DummyHTTP: | |
| def raise_for_status(self) -> None: | |
| return None | |
| def json(self) -> dict[str, object]: | |
| return {"choices": [{"message": {"content": "Resposta fallback Groq"}}]} | |
| def mock_groq(*args: object, **kwargs: object) -> DummyHTTP: | |
| nonlocal groq_calls | |
| groq_calls += 1 | |
| return DummyHTTP() | |
| monkeypatch.setattr(llm._client.models, "generate_content", transient_gemini) # type: ignore[union-attr] | |
| monkeypatch.setattr("src.llm.httpx.post", mock_groq) | |
| result = llm.generate_result("Pergunta?", [StoredDocument(text="Contexto.", source="a.pdf", page=1)]) | |
| assert result.text == "Resposta fallback Groq" | |
| assert result.provider == "groq" | |
| assert result.used_fallback is True | |
| assert gemini_calls == 1 | |
| assert groq_calls == 1 | |
| # Erro não transitório (ex.: chave inválida) NÃO deve acionar fallback | |
| def non_transient_gemini(**kwargs: object) -> object: | |
| raise ValueError("API key invalid: AIzaUserKey1234567890") | |
| monkeypatch.setattr(llm._client.models, "generate_content", non_transient_gemini) # type: ignore[union-attr] | |
| with pytest.raises(RuntimeError) as exc_info: | |
| llm.generate_result("Pergunta?", [StoredDocument(text="Contexto.", source="a.pdf", page=1)]) | |
| assert "AIzaUserKey1234567890" not in str(exc_info.value) | |
| assert groq_calls == 1 | |
| def test_session_isolation_cleanup_and_concurrency(tmp_path: Path) -> None: | |
| def make_session_pipeline(name: str) -> RAGPipeline: | |
| return RAGPipeline( | |
| persist_directory=tmp_path / name, | |
| collection_name=f"col_{name}", | |
| embedding_model=FakeEmbeddingModel(), # type: ignore[arg-type] | |
| llm=FakeLLM(), | |
| ) | |
| p1 = make_session_pipeline("sessao_a") | |
| p2 = make_session_pipeline("sessao_b") | |
| p1.index_pdf(BytesIO(_build_simple_pdf("Segredo exclusivo da sessao A sobre RAG.")), filename="a.pdf") | |
| p2.index_pdf(BytesIO(_build_simple_pdf("Conteudo exclusivo da sessao B sobre Python.")), filename="b.pdf") | |
| assert p1.list_indexed_sources() == ["a.pdf"] | |
| assert p2.list_indexed_sources() == ["b.pdf"] | |
| resp_a = p1.ask("O que diz sobre RAG?") | |
| assert "sessao A" in resp_a.answer | |
| assert "sessao B" not in resp_a.answer | |
| # Concorrência entre sessões isoladas | |
| def worker(idx: int) -> list[str]: | |
| p = make_session_pipeline(f"worker_{idx}") | |
| p.index_pdf(BytesIO(_build_simple_pdf(f"Documento paralelo {idx} sobre RAG.")), filename=f"doc_{idx}.pdf") | |
| sources = p.list_indexed_sources() | |
| p.cleanup_session() | |
| return sources | |
| with ThreadPoolExecutor(max_workers=4) as executor: | |
| results = list(executor.map(worker, range(4))) | |
| assert results == [[f"doc_{i}.pdf"] for i in range(4)] | |
| p1.cleanup_session() | |
| p2.cleanup_session() | |
| # Reabrindo o mesmo diretório após cleanup, nenhum documento deve existir | |
| reopened = make_session_pipeline("sessao_a") | |
| assert reopened.list_indexed_sources() == [] | |
| def test_sanitize_sensitive_text_prevents_log_leaks(caplog: pytest.LogCaptureFixture) -> None: | |
| secret_g = "AIzaVerySecretGeminiKey999" | |
| secret_q = "gsk_VerySecretGroqKey888" | |
| raw = f"Falha Authorization: Bearer {secret_q} e chave {secret_g}" | |
| cleaned = sanitize_sensitive_text(raw, [secret_g, secret_q]) | |
| with caplog.at_level(logging.ERROR): | |
| logging.error(cleaned) | |
| assert secret_g not in cleaned | |
| assert secret_q not in cleaned | |
| assert secret_g not in caplog.text | |
| assert secret_q not in caplog.text | |
| def test_retriever_empty_query_and_reindex_cleanup(tmp_path: Path) -> None: | |
| store = VectorStore(persist_directory=tmp_path / "chroma", collection_name="reindex_test") | |
| embedder = FakeEmbeddingModel() | |
| pipeline = RAGPipeline( | |
| embedding_model=embedder, # type: ignore[arg-type] | |
| vector_store=store, | |
| llm=FakeLLM(), | |
| chunk_size=60, | |
| chunk_overlap=10, | |
| ) | |
| long_pdf = _build_simple_pdf(" ".join(f"palavra{i}" for i in range(40))) | |
| assert pipeline.index_pdf(BytesIO(long_pdf), filename="relatorio.pdf") > 1 | |
| short_pdf = _build_simple_pdf("Texto curto atualizado.") | |
| assert pipeline.index_pdf(BytesIO(short_pdf), filename="relatorio.pdf") == 1 | |
| assert store.count() == 1 | |
| retriever = Retriever(embedding_model=embedder, vector_store=store) # type: ignore[arg-type] | |
| assert retriever.retrieve(" ") == [] | |
| assert retriever.retrieve("RAG", max_distance=0.0) == [] | |