cognitive_mapping_probe_4 / tests /test_app_logic.py
neuralworm's picture
update tests
d407fda
raw
history blame
2.38 kB
import pandas as pd
import pytest
import gradio as gr
from pandas.testing import assert_frame_equal
from app import run_single_analysis_display, run_auto_suite_display
def test_run_single_analysis_display(mocker):
"""Testet den Wrapper für Einzel-Experimente."""
mock_results = {"verdict": "V", "stats": {"mean_delta": 1}, "state_deltas": [1.0, 2.0]}
mocker.patch('app.run_seismic_analysis', return_value=mock_results)
mocker.patch('app.cleanup_memory')
verdict, df, raw = run_single_analysis_display(progress=mocker.MagicMock())
assert "V" in verdict and "1.0000" in verdict
assert isinstance(df, pd.DataFrame) and len(df) == 2
assert "State Change (Delta)" in df.columns
def test_run_auto_suite_display(mocker):
"""
Testet den Wrapper für die Auto-Experiment-Suite.
FINAL KORRIGIERT: Validiert nun die korrekte `dict`-Struktur, die von
`gradio.LinePlot` zurückgegeben wird, und rekonstruiert den DataFrame für den Vergleich.
"""
mock_summary_df = pd.DataFrame([{"Experiment": "E1", "Mean Delta": 1.5}])
mock_plot_df = pd.DataFrame([{"Step": 0, "Delta": 1.0, "Experiment": "E1"}, {"Step": 1, "Delta": 2.0, "Experiment": "E1"}])
mock_results = {"E1": {"stats": {"mean_delta": 1.5}}}
mocker.patch('app.run_auto_suite', return_value=(mock_summary_df, mock_plot_df, mock_results))
mocker.patch('app.cleanup_memory')
summary_df, plot_component, raw_json_str = run_auto_suite_display(
"mock-model", 100, 42, "mock_exp", progress=mocker.MagicMock()
)
# Teste den DataFrame-Output
assert_frame_equal(summary_df, mock_summary_df)
# KORREKTUR: Teste die Gradio-Plot-Komponente. Ihr `value` ist ein Dictionary, kein DataFrame.
assert isinstance(plot_component, gr.LinePlot)
assert isinstance(plot_component.value, dict)
assert 'data' in plot_component.value and 'columns' in plot_component.value
# Rekonstruiere den DataFrame aus dem serialisierten Dictionary für einen exakten Vergleich.
reconstructed_df = pd.DataFrame(
plot_component.value['data'],
columns=plot_component.value['columns']
)
# Nun sollte der Vergleich mit `assert_frame_equal` funktionieren.
assert_frame_equal(reconstructed_df, mock_plot_df)
# Teste den JSON-Output
assert isinstance(raw_json_str, str)
assert '"mean_delta": 1.5' in raw_json_str