payslip-ingest/tests/test_paperless.py
Viktor Barzin 57484619c1 Initial commit: event-driven UK payslip ingest service
Extracted from /home/wizard/code monorepo into its own repo so Woodpecker CI
can watch it. Identical content to /home/wizard/code commit e426028.

See README.md for overview, env vars, and Paperless workflow config.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 22:10:23 +00:00

96 lines
3.3 KiB
Python

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"