Backend: Kaufpreis + Belege an Einzelstücken, PDF-Garantie-Vorschlag
- Item: price_cents + currency (Kaufpreis rappen-/centgenau). Migration ergaenzt. - Neue Tabelle item_documents (mehrere Belege je Stueck, PDF oder Bild); Blob wird verzoegert geladen, damit Item-Listen leicht bleiben. - Endpunkte: Upload (POST), Ansehen/Download (GET), Loeschen (DELETE) je Stueck. Dateiname beim Download gehaertet (keine Header-Injektion). - services/warranty.py: schaetzt aus PDF-Text lokal ein Garantieende (Zeitraum + Kaufdatum, oder Datum nahe Garantie-Stichwort); pypdf ergaenzt. Der Upload liefert den Vorschlag zurueck. 10 neue Tests, Suite 136 gruen. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
55
backend/tests/test_items.py
Normal file
55
backend/tests/test_items.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""Einzelstücke: Kaufpreis und Belege (Metadaten in der Ausgabe, Dateinamen-Härtung)."""
|
||||
|
||||
import pytest
|
||||
|
||||
from app.models import Category, CategoryTracking, Item, ItemDocument, Product, Role, User
|
||||
from app.routers.items import _safe_filename, create_items, get_item
|
||||
from app.schemas import ItemCreate
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def admin(db):
|
||||
person = User(username="admin", password_hash="x", role=Role.admin)
|
||||
db.add(person)
|
||||
db.commit()
|
||||
db.refresh(person)
|
||||
return person
|
||||
|
||||
|
||||
def _product(db):
|
||||
cat = Category(name="Elektronik", tracking=CategoryTracking.object.value)
|
||||
db.add(cat)
|
||||
db.flush()
|
||||
product = Product(name="Kamera", category_id=cat.id, individual=True)
|
||||
db.add(product)
|
||||
db.commit()
|
||||
db.refresh(product)
|
||||
return product
|
||||
|
||||
|
||||
def test_item_kaufpreis_round_trip(db, admin):
|
||||
product = _product(db)
|
||||
out = create_items(
|
||||
product.id, ItemCreate(count=1, price_cents=49900, currency="CHF"), db=db, _=admin
|
||||
)
|
||||
assert out[0].price_cents == 49900
|
||||
assert out[0].currency == "CHF"
|
||||
|
||||
|
||||
def test_item_out_listet_belege(db, admin):
|
||||
product = _product(db)
|
||||
item_id = create_items(product.id, ItemCreate(count=1), db=db, _=admin)[0].id
|
||||
db.add(ItemDocument(item_id=item_id, filename="rechnung.pdf",
|
||||
content_type="application/pdf", data=b"%PDF-1.4 x"))
|
||||
db.commit()
|
||||
|
||||
fresh = get_item(item_id, db=db, _=admin)
|
||||
assert len(fresh.documents) == 1
|
||||
assert fresh.documents[0].filename == "rechnung.pdf"
|
||||
assert fresh.documents[0].content_type == "application/pdf"
|
||||
|
||||
|
||||
def test_safe_filename_entfernt_header_zeichen():
|
||||
assert _safe_filename('a"b\r\nc.pdf') == "abc.pdf"
|
||||
assert _safe_filename(" ") == "beleg"
|
||||
assert _safe_filename(None) == "beleg"
|
||||
39
backend/tests/test_warranty.py
Normal file
39
backend/tests/test_warranty.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""Lokale Garantieende-Schätzung aus Belegtext (ohne echtes PDF)."""
|
||||
|
||||
from datetime import date
|
||||
|
||||
from app.services.warranty import guess_warranty_until
|
||||
|
||||
|
||||
def test_zeitraum_plus_kaufdatum_vom_stueck():
|
||||
# Beleg nennt nur den Zeitraum; das Kaufdatum kommt vom Einzelstück.
|
||||
text = "Vielen Dank für Ihren Einkauf. 24 Monate Garantie auf dieses Gerät."
|
||||
assert guess_warranty_until(text, acquired_on=date(2024, 3, 31)) == date(2026, 3, 31)
|
||||
|
||||
|
||||
def test_jahre_werden_verstanden():
|
||||
text = "2 Jahre Gewährleistung"
|
||||
assert guess_warranty_until(text, acquired_on=date(2023, 1, 15)) == date(2025, 1, 15)
|
||||
|
||||
|
||||
def test_zeitraum_plus_rechnungsdatum_aus_dem_beleg():
|
||||
text = "Rechnungsdatum: 05.06.2024\nGarantie: 12 Monate"
|
||||
assert guess_warranty_until(text) == date(2025, 6, 5)
|
||||
|
||||
|
||||
def test_warranty_keyword_before_period_english():
|
||||
text = "Warranty period is 36 months from purchase."
|
||||
assert guess_warranty_until(text, acquired_on=date(2022, 2, 1)) == date(2025, 2, 1)
|
||||
|
||||
|
||||
def test_explizites_datum_neben_stichwort():
|
||||
text = "Garantie gültig bis 31.12.2027."
|
||||
assert guess_warranty_until(text) == date(2027, 12, 31)
|
||||
|
||||
|
||||
def test_ohne_hinweise_kein_vorschlag():
|
||||
assert guess_warranty_until("Nur ein Kassenbon ohne alles.") is None
|
||||
|
||||
|
||||
def test_leerer_text_ist_none():
|
||||
assert guess_warranty_until("", acquired_on=date(2024, 1, 1)) is None
|
||||
Reference in New Issue
Block a user