-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
71 lines (54 loc) · 2.18 KB
/
conftest.py
File metadata and controls
71 lines (54 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#######################
## AI-generated test ##
#######################
#set up fixtures for tests
import pytest
import tempfile
import os
from fastapi.testclient import TestClient
from contextlib import contextmanager
from pathlib import Path
from io import BytesIO
# Import your actual FastAPI app
from pdf_api import app # Adjust import path if necessary
import pdf_api # To access DATA_DIR for cleanup
@pytest.fixture
def client():
"""Provide a test client for each test"""
with TestClient(app) as test_client:
yield test_client
@pytest.fixture
def sample_pdf_content():
"""Provide sample PDF content for tests
Creates a very small but valid PDF that can be used across tests
"""
return b'%PDF-1.4\n1 0 obj\n<<\n/Type /Catalog\n/Pages 2 0 R\n>>\nendobj\n2 0 obj\n<<\n/Type /Pages\n/Kids [3 0 R]\n/Count 1\n>>\nendobj\n3 0 obj\n<<\n/Type /Page\n/MediaBox [0 0 612 792]\n/Parent 2 0 R\n>>\nendobj\nxref\n0 4\ntrailer <<\n/Size 4\n/Root 1 0 R\n>>\nstartxref\n233\n%%EOF'
@pytest.fixture
def mock_auth_env_vars(monkeypatch):
"""Mock required environment variables"""
monkeypatch.setenv("USR", "test_user")
monkeypatch.setenv("PASSWORD", "test_pass")
# Create DATA_DIR and set it
data_dir = tempfile.mkdtemp()
monkeypatch.setenv("DATA_DIR", data_dir)
return {"USR": "test_user", "PASSWORD": "test_pass", "DATA_DIR": data_dir}
@pytest.fixture
def create_test_pdf_file(mock_auth_env_vars, sample_pdf_content):
"""Create an actual temporary PDF file for file-based operations"""
temp_path = os.path.join(tempfile.gettempdir(), f"test_pdf_{pytest.__version__[:5].replace('.', '_')}.pdf")
with open(temp_path, 'wb') as f:
f.write(sample_pdf_content)
yield temp_path
# Cleanup
if os.path.exists(temp_path):
os.remove(temp_path)
@pytest.fixture(autouse=True)
def cleanup_data_dir(mock_auth_env_vars):
"""Automatically clean up DATA_DIR after tests"""
data_dir = mock_auth_env_vars["DATA_DIR"]
yield
# Cleanup after all tests in session finish
if os.path.exists(data_dir):
for f in os.listdir(data_dir):
os.remove(os.path.join(data_dir, f))
os.rmdir(data_dir)