text_to_solver_program/test_data_profile.py

49 lines
1.8 KiB
Python
Raw Normal View History

"""Profil beigelegter Datendateien – deterministisch, ohne LLM."""
import pytest
from data_profile import profile_file, profile_files, to_prompt
@pytest.fixture
def kurse(tmp_path):
datei = tmp_path / "kurse.csv"
datei.write_text("Aktie;Rendite;Risiko;Gebuehr\nA01;6,9;2,04;0,68\nA02;14,4;1,63;0,74\nA03;11,5;;0,39\n",
encoding="utf-8")
return datei
def test_german_numbers_are_recognised(kurse):
# deutsche Daten sind der Normalfall: "6,9" ist eine Zahl, keine Zeichenkette
profil = profile_file(kurse)
assert profil.rows == 3 and len(profil.columns) == 4
rendite = next(s for s in profil.columns if s.name == "Rendite")
assert rendite.dtype == "Zahl"
assert rendite.minimum == pytest.approx(6.9) and rendite.maximum == pytest.approx(14.4)
def test_text_columns_and_gaps(kurse):
profil = profile_file(kurse)
assert next(s for s in profil.columns if s.name == "Aktie").dtype == "Text"
assert next(s for s in profil.columns if s.name == "Risiko").missing == 1
def test_separator_is_detected(tmp_path):
komma = tmp_path / "en.csv"
komma.write_text("name,value\nfoo,1.5\nbar,2.5\n")
profil = profile_file(komma)
assert [s.name for s in profil.columns] == ["name", "value"]
assert next(s for s in profil.columns if s.name == "value").mean == pytest.approx(2.0)
def test_unreadable_file_is_reported_not_raised(tmp_path):
profil = profile_file(tmp_path / "gibtsnicht.csv")
assert profil.error and "FileNotFoundError" in profil.error
assert profil.rows == 0
def test_prompt_names_columns_and_ranges(kurse):
text = to_prompt(profile_files([kurse]))
assert "kurse.csv: 3 Zeilen, 4 Spalten" in text
assert "Rendite (Zahl, von 6.9 bis 14.4" in text
assert "1 fehlend" in text
assert to_prompt([]) == ""