""" Tool_OCR - Pytest Fixtures and Configuration Shared fixtures for all tests """ import pytest import tempfile import shutil from pathlib import Path from PIL import Image import io from app.services.preprocessor import DocumentPreprocessor @pytest.fixture def temp_dir(): """Create a temporary directory for test files""" temp_path = Path(tempfile.mkdtemp()) yield temp_path # Cleanup after test shutil.rmtree(temp_path, ignore_errors=True) @pytest.fixture def sample_image_path(temp_dir): """Create a valid PNG image file for testing""" image_path = temp_dir / "test_image.png" # Create a simple 100x100 white image img = Image.new('RGB', (100, 100), color='white') img.save(image_path, 'PNG') return image_path @pytest.fixture def sample_jpg_path(temp_dir): """Create a valid JPG image file for testing""" image_path = temp_dir / "test_image.jpg" # Create a simple 100x100 white image img = Image.new('RGB', (100, 100), color='white') img.save(image_path, 'JPEG') return image_path @pytest.fixture def sample_pdf_path(temp_dir): """Create a valid PDF file for testing""" pdf_path = temp_dir / "test_document.pdf" # Create minimal valid PDF pdf_content = b"""%PDF-1.4 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> >> >> >> endobj 4 0 obj << /Length 44 >> stream BT /F1 12 Tf 100 700 Td (Test PDF) Tj ET endstream endobj xref 0 5 0000000000 65535 f 0000000009 00000 n 0000000058 00000 n 0000000115 00000 n 0000000317 00000 n trailer << /Size 5 /Root 1 0 R >> startxref 410 %%EOF """ with open(pdf_path, 'wb') as f: f.write(pdf_content) return pdf_path @pytest.fixture def corrupted_image_path(temp_dir): """Create a corrupted image file for testing""" image_path = temp_dir / "corrupted.png" # Write invalid PNG data with open(image_path, 'wb') as f: f.write(b'\x89PNG\r\n\x1a\n\x00\x00\x00corrupted data') return image_path @pytest.fixture def large_file_path(temp_dir): """Create a valid PNG file larger than the upload limit""" file_path = temp_dir / "large_file.png" # Create a large PNG image with random data (to prevent compression) # 15000x15000 with random pixels should be > 20MB import numpy as np random_data = np.random.randint(0, 256, (15000, 15000, 3), dtype=np.uint8) img = Image.fromarray(random_data, 'RGB') img.save(file_path, 'PNG', compress_level=0) # No compression # Verify it's actually large file_size = file_path.stat().st_size assert file_size > 20 * 1024 * 1024, f"File only {file_size / (1024*1024):.2f} MB" return file_path @pytest.fixture def unsupported_file_path(temp_dir): """Create a file with unsupported format""" file_path = temp_dir / "test.txt" with open(file_path, 'w') as f: f.write("This is a text file, not an image") return file_path @pytest.fixture def preprocessor(): """Create a DocumentPreprocessor instance""" return DocumentPreprocessor() @pytest.fixture def sample_image_with_text(): """Return path to a real image with text from demo_docs for OCR testing""" # Use the english.png sample from demo_docs demo_image_path = Path(__file__).parent.parent.parent / "demo_docs" / "basic" / "english.png" # Check if demo image exists, otherwise skip the test if not demo_image_path.exists(): pytest.skip(f"Demo image not found at {demo_image_path}") return demo_image_path