import httpx import pytest import respx from payslip_ingest.paperless import PaperlessClient, PaperlessError @pytest.fixture() def client() -> PaperlessClient: return PaperlessClient(base_url="http://paperless.test", api_token="tok") async def test_get_tag_id_happy_path(client: PaperlessClient) -> None: with respx.mock(base_url="http://paperless.test") as mock: mock.get("/api/tags/", params={ "name__iexact": "payslip" }).mock(return_value=httpx.Response(200, json={"results": [{ "id": 7, "name": "payslip" }]})) assert await client.get_tag_id("payslip") == 7 async def test_get_tag_id_zero_results_raises(client: PaperlessClient) -> None: with respx.mock(base_url="http://paperless.test") as mock: mock.get("/api/tags/").mock(return_value=httpx.Response(200, json={"results": []})) with pytest.raises(PaperlessError): await client.get_tag_id("payslip") async def test_get_tag_id_many_results_raises(client: PaperlessClient) -> None: with respx.mock(base_url="http://paperless.test") as mock: mock.get("/api/tags/").mock(return_value=httpx.Response( 200, json={"results": [{ "id": 1, "name": "payslip" }, { "id": 2, "name": "Payslip" }]}, )) with pytest.raises(PaperlessError): await client.get_tag_id("payslip") async def test_list_tagged_documents_paginates(client: PaperlessClient) -> None: with respx.mock() as mock: mock.get("http://paperless.test/api/tags/").mock( return_value=httpx.Response(200, json={"results": [{ "id": 7, "name": "payslip" }]})) mock.get("http://paperless.test/api/documents/?tags__id=7").mock( return_value=httpx.Response( 200, json={ "results": [{ "id": 1 }, { "id": 2 }], "next": "http://paperless.test/api/documents/?tags__id=7&page=2", }, )) mock.get("http://paperless.test/api/documents/?tags__id=7&page=2").mock( return_value=httpx.Response( 200, json={ "results": [{ "id": 3 }], "next": None }, )) ids = [doc["id"] async for doc in client.list_tagged_documents("payslip")] assert ids == [1, 2, 3] async def test_get_document_returns_metadata(client: PaperlessClient) -> None: with respx.mock(base_url="http://paperless.test") as mock: mock.get("/api/documents/42/").mock( return_value=httpx.Response(200, json={ "id": 42, "title": "Payslip Mar" })) data = await client.get_document(42) assert data["title"] == "Payslip Mar" async def test_download_document_returns_bytes(client: PaperlessClient) -> None: with respx.mock(base_url="http://paperless.test") as mock: mock.get("/api/documents/42/download/").mock( return_value=httpx.Response(200, content=b"PDFDATA")) data = await client.download_document(42) assert data == b"PDFDATA"