| """ |
| 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): |
| |
| 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}"} |
|
|
| |
| 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"] |
|
|
| |
| response = await client.get(f"/api/v1/documents/{doc_id}", headers=headers) |
| assert response.status_code == 200 |
| assert response.json()["id"] == doc_id |
|
|
| |
| 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 |
|
|
| |
| response = await client.get(f"/api/v1/documents/{doc_id}/status", headers=headers) |
| assert response.status_code == 200 |
| assert "status" in response.json() |
|
|
| |
| response = await client.delete(f"/api/v1/documents/{doc_id}", headers=headers) |
| assert response.status_code == 200 |
| assert response.json()["id"] == doc_id |
|
|