orc-renaming/tests/test_pipeline.py

108 lines
3.0 KiB
Python

from __future__ import annotations
from pathlib import Path
import orc_renaming.pipeline as pipeline_module
from orc_renaming.pipeline import Pipeline
from orc_renaming.webdav import RemoteFile
class FakeWebDAV:
uploads: list[tuple[str, bool]]
moves: list[tuple[str, str]]
folders: list[str]
def __init__(self, _config) -> None:
self.uploads = []
self.moves = []
self.folders = []
def close(self) -> None:
pass
def list_pdfs(self, _folder: str) -> list[RemoteFile]:
return [
RemoteFile(
path="/Scanner/Eingang/scan001.pdf",
name="scan001.pdf",
)
]
def download(self, _remote_path: str, local_path: Path) -> None:
local_path.write_bytes(b"%PDF fake")
def exists(self, _remote_path: str) -> bool:
return False
def ensure_folder(self, folder: str) -> None:
self.folders.append(folder)
def upload(
self, local_path: Path, remote_path: str, overwrite: bool = False
) -> None:
assert local_path.exists()
self.uploads.append((remote_path, overwrite))
def move(self, source: str, destination: str, overwrite: bool = False) -> None:
assert not overwrite
self.moves.append((source, destination))
OCR_TEXT = """
Stadtwerke Musterstadt GmbH
Rechnung
Rechnungsnummer 2026-123
Rechnungsdatum: 18.07.2026
Lieferstelle Musterstraße 12
Vertragskonto 47110815
Rechnungsbetrag 123,45 EUR
"""
def _prepare(monkeypatch) -> None:
monkeypatch.setattr(pipeline_module, "WebDAVClient", FakeWebDAV)
monkeypatch.setattr(pipeline_module, "extract_pdf_text", lambda *_args, **_kwargs: OCR_TEXT)
monkeypatch.setattr(pipeline_module, "file_sha256", lambda _path: "abc123")
def test_dry_run_writes_only_local_report(config, monkeypatch) -> None:
_prepare(monkeypatch)
config.processing.dry_run = True
with Pipeline(config) as pipeline:
counters = pipeline.run()
assert pipeline.webdav.uploads == []
assert pipeline.webdav.moves == []
assert counters == {"dry-run": 1}
assert (config.processing.state_directory / "protokoll.xlsx").exists()
def test_live_run_uploads_pdf_report_and_archives(config, monkeypatch) -> None:
_prepare(monkeypatch)
config.processing.dry_run = False
with Pipeline(config) as pipeline:
counters = pipeline.run()
uploads = list(pipeline.webdav.uploads)
moves = list(pipeline.webdav.moves)
folders = list(pipeline.webdav.folders)
assert counters == {"success": 1}
assert (
"/Scanner/Eingang/umbenannt/260718_RE_Stadtwerke-Musterstadt-WH1.pdf",
False,
) in uploads
assert (
"/Scanner/Eingang/umbenannt/protokoll.xlsx",
True,
) in uploads
assert moves == [
(
"/Scanner/Eingang/scan001.pdf",
"/Scanner/Eingang/verarbeitet-originale/scan001.pdf",
)
]
assert config.nextcloud.review_folder in folders