Spaces:
Running
Running
| import os | |
| import pytest | |
| from unittest.mock import patch | |
| from rag_pipeline.pdf_parser import parse_pdf_from_file | |
| async def test_pdf_parser_fallback(tmp_path): | |
| # Tworzymy symulowany plik PDF | |
| pdf_file = tmp_path / "test.pdf" | |
| # Tworzymy w locie minimalny, prawdziwy plik PDF (PyPDF musi móc go odczytać) | |
| minimal_pdf = ( | |
| b"%PDF-1.4\n" | |
| 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] /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> >> >> /Contents 4 0 R >>\nendobj\n" | |
| b"4 0 obj\n<< /Length 44 >>\nstream\nBT /F1 12 Tf 100 700 Td (Test fallback) Tj ET\nendstream\nendobj\n" | |
| b"xref\n0 5\n" | |
| b"0000000000 65535 f \n" | |
| b"0000000009 00000 n \n" | |
| b"0000000058 00000 n \n" | |
| b"0000000115 00000 n \n" | |
| b"0000000288 00000 n \n" | |
| b"trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n381\n%%EOF" | |
| ) | |
| pdf_file.write_bytes(minimal_pdf) | |
| # Test 1: LlamaParse fail, fallback to PyPDF | |
| with patch.dict(os.environ, {"LLAMA_CLOUD_API_KEY": "fake_key"}): | |
| with patch("rag_pipeline.pdf_parser._parse_llamaparse_sync", side_effect=Exception("LlamaParse API Error")): | |
| result = await parse_pdf_from_file(str(pdf_file)) | |
| assert result["parser"] == "pypdf" | |
| assert "Test fallback" in result["text"] or "PAGE 1" in result["text"] | |