LOG_LEVEL was documented but never read; the level was fixed at INFO. The JSON handler now sits on the "src" package logger so all service modules share format and level. Invalid values fall back to INFO. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
125 lines
3.8 KiB
Python
125 lines
3.8 KiB
Python
import base64
|
|
import logging
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from src.main import app, configure_logging
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
return TestClient(app)
|
|
|
|
|
|
def test_extract_valid_zugferd(client):
|
|
with open("tests/fixtures/EN16931_Einfach.pdf", "rb") as f:
|
|
pdf_base64 = base64.b64encode(f.read()).decode()
|
|
|
|
response = client.post("/extract", json={"pdf_base64": pdf_base64})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["is_zugferd"] is True
|
|
assert data["zugferd_profil"] == "EN16931"
|
|
assert "xml_raw" in data
|
|
assert "xml_data" in data
|
|
assert "pdf_text" in data
|
|
assert "extraction_meta" in data
|
|
|
|
|
|
def test_extract_non_zugferd(client):
|
|
with open("tests/fixtures/EmptyPDFA1.pdf", "rb") as f:
|
|
pdf_base64 = base64.b64encode(f.read()).decode()
|
|
|
|
response = client.post("/extract", json={"pdf_base64": pdf_base64})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["is_zugferd"] is False
|
|
assert data["zugferd_profil"] is None
|
|
assert "pdf_text" in data
|
|
assert "extraction_meta" in data
|
|
|
|
|
|
def test_extract_invalid_base64(client):
|
|
response = client.post("/extract", json={"pdf_base64": "invalid!!!"})
|
|
assert response.status_code == 400
|
|
data = response.json()
|
|
assert data["error"] == "invalid_base64"
|
|
assert "message" in data
|
|
|
|
|
|
def test_extract_non_pdf(client):
|
|
pdf_base64 = base64.b64encode(b"Hello World").decode()
|
|
response = client.post("/extract", json={"pdf_base64": pdf_base64})
|
|
assert response.status_code == 400
|
|
data = response.json()
|
|
assert "error" in data
|
|
|
|
|
|
def test_validate_pflichtfelder(client):
|
|
response = client.post(
|
|
"/validate",
|
|
json={
|
|
"xml_data": {
|
|
"invoice_number": "RE-001",
|
|
"invoice_date": "2025-02-04",
|
|
"supplier": {"name": "Test GmbH", "vat_id": "DE123456789"},
|
|
"buyer": {"name": "Kunde AG"},
|
|
"totals": {
|
|
"net": 100.0,
|
|
"gross": 119.0,
|
|
"vat_total": 19.0,
|
|
"line_total_sum": 100.0,
|
|
},
|
|
"line_items": [
|
|
{
|
|
"position": 1,
|
|
"description": "Test",
|
|
"quantity": 1.0,
|
|
"unit": "Stück",
|
|
"unit_price": 100.0,
|
|
"line_total": 100.0,
|
|
}
|
|
],
|
|
},
|
|
"checks": ["pflichtfelder"],
|
|
},
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "result" in data
|
|
assert "is_valid" in data["result"]
|
|
|
|
|
|
def test_validate_empty_checks(client):
|
|
response = client.post("/validate", json={"xml_data": {}, "checks": []})
|
|
assert response.status_code == 200
|
|
|
|
|
|
class TestLogLevel:
|
|
"""LOG_LEVEL environment variable controls the service log level."""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _restore_level(self):
|
|
yield
|
|
logging.getLogger("src").setLevel(logging.INFO)
|
|
|
|
def test_log_level_from_env(self, monkeypatch):
|
|
monkeypatch.setenv("LOG_LEVEL", "debug")
|
|
configure_logging()
|
|
|
|
assert logging.getLogger("src").level == logging.DEBUG
|
|
assert logging.getLogger("src.extractor").getEffectiveLevel() == logging.DEBUG
|
|
|
|
def test_default_is_info(self, monkeypatch):
|
|
monkeypatch.delenv("LOG_LEVEL", raising=False)
|
|
configure_logging()
|
|
|
|
assert logging.getLogger("src").level == logging.INFO
|
|
|
|
def test_invalid_value_falls_back_to_info(self, monkeypatch):
|
|
monkeypatch.setenv("LOG_LEVEL", "chatty")
|
|
configure_logging()
|
|
|
|
assert logging.getLogger("src").level == logging.INFO
|