cognitive_mapping_probe_4 / tests /test_app_logic.py
neuralworm's picture
update tests
11cf050
raw
history blame
2.53 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: Rekonstruiert DataFrames aus den serialisierten `dict`-Werten
der Gradio-Komponenten, um die tatsächliche API-Nutzung widerzuspiegeln.
"""
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')
dataframe_component, plot_component, raw_json_str = run_auto_suite_display(
"mock-model", 100, 42, "mock_exp", progress=mocker.MagicMock()
)
# KORREKTUR: Die `.value` Eigenschaft einer gr.DataFrame Komponente ist ein Dictionary.
# Wir müssen den pandas.DataFrame daraus rekonstruieren, um ihn zu vergleichen.
assert isinstance(dataframe_component, gr.DataFrame)
assert isinstance(dataframe_component.value, dict)
reconstructed_summary_df = pd.DataFrame(
data=dataframe_component.value['data'],
columns=dataframe_component.value['headers']
)
assert_frame_equal(reconstructed_summary_df, mock_summary_df)
# Dasselbe gilt für die LinePlot-Komponente
assert isinstance(plot_component, gr.LinePlot)
assert isinstance(plot_component.value, dict)
reconstructed_plot_df = pd.DataFrame(
data=plot_component.value['data'],
columns=plot_component.value['columns']
)
assert_frame_equal(reconstructed_plot_df, mock_plot_df)
# Der JSON-String bleibt ein String
assert isinstance(raw_json_str, str)
assert '"mean_delta": 1.5' in raw_json_str