import httpx import pytest_asyncio BASE_URL = "http://127.0.0.1:8000" @pytest_asyncio.fixture(scope="session") def base_url(): """Fixture to provide the base URL for the tests.""" return BASE_URL @pytest_asyncio.fixture(scope="function") # <-- Change scope to "function" async def http_client(): """ Fixture to provide an async HTTP client for all tests in the session. A new client is created and closed properly using a try/finally block to prevent "Event loop is closed" errors. """ client = httpx.AsyncClient(base_url=BASE_URL, timeout=60.0) try: yield client finally: await client.aclose() @pytest_asyncio.fixture(scope="function") async def session_id(http_client): """ Creates a new session before a test and cleans it up after. Returns the session ID. """ payload = {"user_id": "integration_tester", "model": "deepseek"} response = await http_client.post("/sessions", json=payload) assert response.status_code == 200 session_id = response.json()["id"] yield session_id # No explicit session deletion is needed for this example, # as sessions are typically managed by a database lifecycle. @pytest_asyncio.fixture(scope="function") async def document_id(http_client): """ Creates a new document before a test and ensures it's deleted afterward. Returns the document ID. """ doc_data = {"title": "Lifecycle Test Doc", "text": "This doc will be listed and deleted."} response = await http_client.post("/documents", json=doc_data) assert response.status_code == 200 try: message = response.json().get("message", "") document_id = int(message.split(" with ID ")[-1]) except (ValueError, IndexError): pytest.fail("Could not parse document ID from response message.") yield document_id # Teardown: Delete the document after the test delete_response = await http_client.delete(f"/documents/{document_id}") assert delete_response.status_code == 200