Hintergrundverarbeitung, Bildaufbereitung, Sammelexport entfernt
All checks were successful
Build & Deploy / deploy (push) Successful in 1m24s
All checks were successful
Build & Deploy / deploy (push) Successful in 1m24s
Auslesen im Hintergrund: /api/scan antwortet, sobald die Karten freigestellt und gespeichert sind, und stößt die Extraktion als Task an. Ein Zwanzigerstapel blockierte vorher den Upload für die ganze Dauer aller Modellaufrufe. Die Oberfläche zeigt "wird gelesen" und lädt nach, solange etwas offen ist. Zwei Folgen davon sind mitbehandelt: Beim Schreiben der Ergebnisse steht COALESCE, damit ein Handeintrag während des Lesens nicht überschrieben wird, und offene Karten werden beim Start nachgeholt, statt dauerhaft in der Warteschlange zu hängen. Bildaufbereitung des Zuschnitts: - Der Einzug zieht die erkannten Ecken um 1,5 % zur Mitte, damit kein Untergrund im Zuschnitt bleibt. - Die Beleuchtung wird ausgeglichen (Division durch eine weichgezeichnete Fassung), damit das Papier weiß wird statt grau. Dunkle Karten bleiben unangetastet - bei ihnen ist das Dunkle das Papier, kein Schatten. - Das Modell meldet die nötige Drehung im Schema; das gespeicherte Bild wird entsprechend gedreht. Geometrisch ist die Lage nicht bestimmbar. Sammelexport entfernt: Der Button "Alle als vCard" ist weg, mit ihm der Endpunkt /vcf sowie vcard.build_many und db.execute_many, die dadurch keinen Aufrufer mehr hatten. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,16 +3,31 @@
|
||||
Ohne ANTHROPIC_API_KEY laeuft die Extraktion in den Leerlauf - Freistellen,
|
||||
Speichern, Suchen, Bearbeiten und Export werden trotzdem vollstaendig gepruft.
|
||||
"""
|
||||
import asyncio
|
||||
import time
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app import db, mailer, reminders
|
||||
from app import db, extract, mailer, reminders
|
||||
from app.main import app
|
||||
from tests.helpers import photo_bytes
|
||||
|
||||
|
||||
def warte_bis_gelesen(client, card_ids, timeout=15.0):
|
||||
"""Das Auslesen laeuft im Hintergrund - hier auf das Ende warten."""
|
||||
frist = time.monotonic() + timeout
|
||||
while time.monotonic() < frist:
|
||||
stati = [
|
||||
client.get(f"/api/cards/{i}").json()["extract_status"] for i in card_ids
|
||||
]
|
||||
if all(status != "offen" for status in stati):
|
||||
return stati
|
||||
time.sleep(0.1)
|
||||
raise AssertionError("Karten wurden nicht fertig gelesen")
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def client():
|
||||
with TestClient(app) as test_client:
|
||||
@@ -25,7 +40,9 @@ def scanned(client):
|
||||
"/api/scan", files={"file": ("tisch.jpg", photo_bytes(4), "image/jpeg")}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
return response.json()
|
||||
data = response.json()
|
||||
warte_bis_gelesen(client, [c["id"] for c in data["cards"]])
|
||||
return data
|
||||
|
||||
|
||||
def test_healthz(client):
|
||||
@@ -97,14 +114,6 @@ def test_filter_offen_blendet_exportierte_karten_aus(client, scanned):
|
||||
assert all(card["exported_at"] is None for card in offen)
|
||||
|
||||
|
||||
def test_sammelexport_mehrerer_karten(client, scanned):
|
||||
ids = ",".join(card["id"] for card in scanned["cards"][:3])
|
||||
|
||||
response = client.get("/vcf", params={"ids": ids})
|
||||
|
||||
assert response.text.count("BEGIN:VCARD") == 3
|
||||
|
||||
|
||||
def test_erinnerung_anlegen_und_filtern(client, scanned):
|
||||
card_id = scanned["cards"][2]["id"]
|
||||
due = datetime.now(timezone.utc) + timedelta(days=3)
|
||||
@@ -181,3 +190,75 @@ def test_pwa_dateien_werden_ausgeliefert(client):
|
||||
assert client.get("/sw.js").status_code == 200
|
||||
assert client.get("/manifest.webmanifest").status_code == 200
|
||||
assert client.get("/static/app.js").status_code == 200
|
||||
|
||||
|
||||
def test_scan_antwortet_bevor_das_auslesen_fertig_ist(client, monkeypatch):
|
||||
"""Der Kern der Hintergrundverarbeitung: die Antwort wartet nicht auf das
|
||||
Modell. Ohne das haengt ein Zwanzigerstapel eine Minute im Upload."""
|
||||
original = extract.extract_all
|
||||
|
||||
async def langsam(images):
|
||||
await asyncio.sleep(1.5)
|
||||
return await original(images)
|
||||
|
||||
monkeypatch.setattr(extract, "extract_all", langsam)
|
||||
|
||||
start = time.monotonic()
|
||||
response = client.post(
|
||||
"/api/scan", files={"file": ("einzel.jpg", photo_bytes(1), "image/jpeg")}
|
||||
)
|
||||
dauer = time.monotonic() - start
|
||||
card_id = response.json()["cards"][0]["id"]
|
||||
|
||||
assert dauer < 1.2, f"Antwort brauchte {dauer:.2f}s, hat also gewartet"
|
||||
assert client.get(f"/api/cards/{card_id}").json()["extract_status"] == "offen"
|
||||
|
||||
monkeypatch.undo()
|
||||
warte_bis_gelesen(client, [card_id])
|
||||
|
||||
|
||||
def test_offene_karten_zaehlen_nicht_als_unvollstaendig(client):
|
||||
"""Waehrend des Lesens sind Felder naturgemaess leer - das ist kein Mangel."""
|
||||
created = client.post(
|
||||
"/api/scan", files={"file": ("einzel.jpg", photo_bytes(1), "image/jpeg")}
|
||||
).json()
|
||||
card_id = created["cards"][0]["id"]
|
||||
warte_bis_gelesen(client, [card_id]) # sonst ueberschreibt der Hintergrundlauf
|
||||
db.execute("UPDATE cards SET extract_status = 'offen' WHERE id = ?", (card_id,))
|
||||
|
||||
unvollstaendig = client.get(
|
||||
"/api/cards", params={"filter": "unvollstaendig"}
|
||||
).json()["cards"]
|
||||
|
||||
assert card_id not in [c["id"] for c in unvollstaendig]
|
||||
assert client.get("/api/cards").json()["stats"]["laeuft"] >= 1
|
||||
|
||||
db.execute("UPDATE cards SET extract_status = 'ok' WHERE id = ?", (card_id,))
|
||||
|
||||
|
||||
def test_sammelexport_ist_entfernt(client):
|
||||
assert client.get("/vcf", params={"ids": "egal"}).status_code == 404
|
||||
|
||||
|
||||
def test_handeintrag_ueberlebt_den_hintergrundlauf(client, monkeypatch):
|
||||
"""Die Karte ist sichtbar, bevor sie gelesen ist. Wer in der Zwischenzeit
|
||||
etwas eintraegt, darf es nicht wieder verlieren."""
|
||||
original = extract.extract_all
|
||||
|
||||
async def langsam(images):
|
||||
await asyncio.sleep(1.0)
|
||||
return await original(images)
|
||||
|
||||
monkeypatch.setattr(extract, "extract_all", langsam)
|
||||
card_id = client.post(
|
||||
"/api/scan", files={"file": ("einzel.jpg", photo_bytes(1), "image/jpeg")}
|
||||
).json()["cards"][0]["id"]
|
||||
|
||||
client.patch(f"/api/cards/{card_id}", json={"full_name": "Von Hand", "notes": "Messe"})
|
||||
|
||||
monkeypatch.undo()
|
||||
warte_bis_gelesen(client, [card_id])
|
||||
|
||||
card = client.get(f"/api/cards/{card_id}").json()
|
||||
assert card["full_name"] == "Von Hand"
|
||||
assert card["notes"] == "Messe"
|
||||
|
||||
Reference in New Issue
Block a user