""" Unit tests for Document API and Ingestion Pipeline. """ from __future__ import annotations import io import pytest from httpx import AsyncClient from sqlalchemy.ext.asyncio import AsyncSession @pytest.mark.asyncio async def test_upload_document_flow(client: AsyncClient, db_session: AsyncSession): # 1. Register & login reg = await client.post("/api/v1/auth/register", json={ "email": "doc_test@company.com", "password": "Password123!", "full_name": "Doc Tester", "tenant_name": "Doc Corp" }) token = reg.json()["tokens"]["access_token"] headers = {"Authorization": f"Bearer {token}"} # 2. Upload text file file_content = b"This is an enterprise policy document regarding remote work and annual leaves." files = {"file": ("policy.txt", io.BytesIO(file_content), "text/plain")} data = {"doc_type": "hr"} response = await client.post("/api/v1/documents/upload", headers=headers, files=files, data=data) assert response.status_code == 202, response.text res_data = response.json() assert res_data["original_name"] == "policy.txt" assert res_data["doc_type"] == "hr" doc_id = res_data["id"] # 3. Get document details response = await client.get(f"/api/v1/documents/{doc_id}", headers=headers) assert response.status_code == 200 assert response.json()["id"] == doc_id # 4. List documents response = await client.get("/api/v1/documents/", headers=headers) assert response.status_code == 200 docs = response.json()["documents"] assert len(docs) >= 1 assert docs[0]["id"] == doc_id # 5. Check status endpoint response = await client.get(f"/api/v1/documents/{doc_id}/status", headers=headers) assert response.status_code == 200 assert "status" in response.json() # 6. Delete document response = await client.delete(f"/api/v1/documents/{doc_id}", headers=headers) assert response.status_code == 200 assert response.json()["id"] == doc_id