Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /GRID-INTELLIGENCE_GRID /tests /api /conftest.py
| """ | |
| Test fixtures for Activity Resonance API tests. | |
| Provides TestClient, WebSocket client, and service mocks for testing. | |
| """ | |
| from __future__ import annotations | |
| import asyncio | |
| from typing import TYPE_CHECKING | |
| import pytest | |
| from fastapi import FastAPI | |
| from fastapi.testclient import TestClient | |
| if TYPE_CHECKING: | |
| from application.resonance.services.resonance_service import ResonanceService | |
| def app() -> FastAPI: | |
| """ | |
| Create FastAPI application for testing. | |
| Returns: | |
| FastAPI application instance | |
| """ | |
| from application.resonance.api.router import router | |
| app = FastAPI() | |
| app.include_router(router, prefix="/api/v1/resonance", tags=["resonance"]) | |
| return app | |
| def client(app: FastAPI) -> TestClient: | |
| """ | |
| Create TestClient for API testing with dev-test-token authentication. | |
| The ENABLE_DEV_TOKEN=1 environment variable is set in tests/conftest.py, | |
| allowing the dev-test-token to bypass authentication in test environments. | |
| Args: | |
| app: FastAPI application | |
| Returns: | |
| TestClient instance with Authorization header set | |
| """ | |
| return TestClient( | |
| app, | |
| headers={"Authorization": "Bearer dev-test-token"}, | |
| ) | |
| def service() -> ResonanceService: | |
| """ | |
| Create ResonanceService instance for testing. | |
| Returns: | |
| ResonanceService instance | |
| """ | |
| from application.resonance.api.dependencies import ( | |
| get_resonance_service, | |
| reset_resonance_service, | |
| ) | |
| reset_resonance_service() | |
| return get_resonance_service() | |
| def cleanup_service(): | |
| """ | |
| Cleanup service after each test. | |
| This ensures tests don't interfere with each other. | |
| """ | |
| yield | |
| try: | |
| from application.resonance.api.dependencies import reset_resonance_service | |
| reset_resonance_service() | |
| except ImportError: | |
| pass | |
| def sample_activity_id(service: ResonanceService) -> str: | |
| """ | |
| Create a sample activity for testing. | |
| Simplified fixture that works with sync tests. | |
| Uses asyncio.run() which is safe when no event loop is running | |
| (pytest-asyncio auto mode ensures this). | |
| Args: | |
| service: ResonanceService instance | |
| Returns: | |
| Activity ID | |
| """ | |
| # Use asyncio.run() - simpler and safer than nested event loops | |
| # pytest-asyncio auto mode ensures no event loop is running here | |
| activity_id, _ = asyncio.run( | |
| service.process_activity( | |
| query="test activity", | |
| activity_type="general", | |
| context={}, | |
| ) | |
| ) | |
| return activity_id | |
| def websocket_client(app: FastAPI): | |
| """ | |
| Create WebSocket test client. | |
| Args: | |
| app: FastAPI application | |
| Returns: | |
| WebSocket test client function | |
| """ | |
| client = TestClient(app, headers={"Authorization": "Bearer dev-test-token"}) | |
| def connect_websocket(path: str): | |
| """Connect to WebSocket endpoint.""" | |
| return client.websocket_connect(path) | |
| return connect_websocket | |
Xet Storage Details
- Size:
- 3.11 kB
- Xet hash:
- 2fa703c7a89488d196df1d1ff9faf836ff617df9dd7a3afec92f35cc84d09f07
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.