Tests and helpers

This commit is contained in:
2026-06-02 15:25:32 +02:00
parent faf5d440f3
commit d249c6a013
9 changed files with 372 additions and 121 deletions
+34
View File
@@ -0,0 +1,34 @@
import re
def format_currency(amount: float, symbol: str = "", is_german: bool = True) -> str:
"""Formats a numeric amount to a currency string representation."""
if is_german:
return f"{amount:.2f}".replace(".", ",") + f" {symbol}"
else:
return f"{symbol}{amount:.2f}"
def format_date(iso_timestamp: str) -> str:
"""Converts ISO timestamp to German date format DD.MM.YYYY."""
try:
date_part = iso_timestamp.split("T")[0]
parts = date_part.split("-")
if len(parts) == 3 and all(p.isdigit() for p in parts) and len(parts[0]) == 4:
return f"{parts[2]}.{parts[1]}.{parts[0]}"
except Exception:
pass
return iso_timestamp[:10]
def validate_pin(pin: str) -> bool:
"""Validates that a PIN consists of at least 4 numeric digits."""
return bool(re.match(r"^\d{4,}$", pin))
def parse_price(price_str: str) -> float:
"""Parses a string containing a price (handling comma and dot) to a float.
Raises ValueError if invalid.
"""
clean_str = price_str.replace(",", ".").strip()
price = float(clean_str)
if price < 0:
raise ValueError("Price cannot be negative")
return price