|
| 1 | +"""Pytest configuration and fixtures.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import tempfile |
| 7 | +import shutil |
| 8 | +from pathlib import Path |
| 9 | +from fastapi.testclient import TestClient |
| 10 | +from httpx import AsyncClient |
| 11 | + |
| 12 | +# Add the project root to Python path for imports |
| 13 | +PROJECT_ROOT = Path(__file__).parent.parent |
| 14 | +sys.path.insert(0, str(PROJECT_ROOT)) |
| 15 | + |
| 16 | +# Now import the app and configuration |
| 17 | +from claude_code_api.main import app |
| 18 | +from claude_code_api.core.config import settings |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(scope="session", autouse=True) |
| 22 | +def setup_test_environment(): |
| 23 | + """Setup test environment before all tests.""" |
| 24 | + # Create temporary directory for testing |
| 25 | + temp_dir = tempfile.mkdtemp(prefix="claude_api_test_") |
| 26 | + |
| 27 | + # Store original settings |
| 28 | + original_settings = { |
| 29 | + "project_root": getattr(settings, "project_root", None), |
| 30 | + "require_auth": getattr(settings, "require_auth", False), |
| 31 | + "claude_binary_path": getattr(settings, "claude_binary_path", "claude"), |
| 32 | + "database_url": getattr(settings, "database_url", "sqlite:///./test.db"), |
| 33 | + "debug": getattr(settings, "debug", False) |
| 34 | + } |
| 35 | + |
| 36 | + # Set test settings |
| 37 | + settings.project_root = os.path.join(temp_dir, "projects") |
| 38 | + settings.require_auth = False |
| 39 | + # Keep the real Claude binary path - DO NOT mock it! |
| 40 | + # settings.claude_binary_path should remain as found by find_claude_binary() |
| 41 | + settings.database_url = f"sqlite:///{temp_dir}/test.db" |
| 42 | + settings.debug = True |
| 43 | + |
| 44 | + # Create directories |
| 45 | + os.makedirs(settings.project_root, exist_ok=True) |
| 46 | + |
| 47 | + yield temp_dir |
| 48 | + |
| 49 | + # Cleanup |
| 50 | + try: |
| 51 | + shutil.rmtree(temp_dir) |
| 52 | + except Exception as e: |
| 53 | + print(f"Cleanup warning: {e}") |
| 54 | + |
| 55 | + # Restore original settings (if they existed) |
| 56 | + for key, value in original_settings.items(): |
| 57 | + if value is not None: |
| 58 | + setattr(settings, key, value) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def test_client(): |
| 63 | + """Create a test client for the FastAPI app.""" |
| 64 | + with TestClient(app) as client: |
| 65 | + yield client |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture |
| 69 | +async def async_test_client(): |
| 70 | + """Create an async test client.""" |
| 71 | + async with AsyncClient(app=app, base_url="http://test") as client: |
| 72 | + yield client |
| 73 | + |
| 74 | + |
| 75 | +@pytest.fixture |
| 76 | +def sample_chat_request(): |
| 77 | + """Sample chat completion request.""" |
| 78 | + return { |
| 79 | + "model": "claude-3-5-sonnet-20241022", |
| 80 | + "messages": [ |
| 81 | + {"role": "user", "content": "Hi"} |
| 82 | + ], |
| 83 | + "stream": False |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +@pytest.fixture |
| 88 | +def sample_streaming_request(): |
| 89 | + """Sample streaming chat completion request.""" |
| 90 | + return { |
| 91 | + "model": "claude-3-5-sonnet-20241022", |
| 92 | + "messages": [ |
| 93 | + {"role": "user", "content": "Tell me a joke"} |
| 94 | + ], |
| 95 | + "stream": True |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture |
| 100 | +def sample_project_request(): |
| 101 | + """Sample project creation request.""" |
| 102 | + return { |
| 103 | + "name": "Test Project", |
| 104 | + "description": "A test project" |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | +@pytest.fixture |
| 109 | +def sample_session_request(): |
| 110 | + """Sample session creation request.""" |
| 111 | + return { |
| 112 | + "project_id": "test-project", |
| 113 | + "title": "Test Session", |
| 114 | + "model": "claude-3-5-sonnet-20241022" |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | +# Configure pytest |
| 119 | +def pytest_configure(config): |
| 120 | + """Configure pytest.""" |
| 121 | + config.addinivalue_line( |
| 122 | + "markers", "slow: marks tests as slow (deselect with '-m \"not slow\"')" |
| 123 | + ) |
| 124 | + config.addinivalue_line( |
| 125 | + "markers", "integration: marks tests as integration tests" |
| 126 | + ) |
| 127 | + config.addinivalue_line( |
| 128 | + "markers", "unit: marks tests as unit tests" |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +def pytest_collection_modifyitems(config, items): |
| 133 | + """Modify test collection.""" |
| 134 | + # Add markers based on test names/paths |
| 135 | + for item in items: |
| 136 | + if "integration" in item.nodeid: |
| 137 | + item.add_marker(pytest.mark.integration) |
| 138 | + elif "unit" in item.nodeid: |
| 139 | + item.add_marker(pytest.mark.unit) |
| 140 | + |
| 141 | + # Mark slow tests |
| 142 | + if any(keyword in item.name.lower() for keyword in ["concurrent", "performance", "large"]): |
| 143 | + item.add_marker(pytest.mark.slow) |
0 commit comments