diff --git a/optuna_dashboard/streamlit/_streamlit_helper.py b/optuna_dashboard/streamlit/_streamlit_helper.py index 719b5c0a..814a6327 100644 --- a/optuna_dashboard/streamlit/_streamlit_helper.py +++ b/optuna_dashboard/streamlit/_streamlit_helper.py @@ -66,7 +66,7 @@ def _render_widgets( ) elif widget["type"] == "text": # NOTE: Current implementation ignores "optional". - value = st.text_input(_format_description(widget["description"])) + value = st.text_input(_format_description(widget["description"])) # type: ignore elif widget["type"] == "user_attr": value = trial.user_attrs[widget["key"]] else: diff --git a/python_tests/streamlit/__init__.py b/python_tests/streamlit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python_tests/streamlit/test_streamlit_helper.py b/python_tests/streamlit/test_streamlit_helper.py new file mode 100644 index 00000000..3bd903d8 --- /dev/null +++ b/python_tests/streamlit/test_streamlit_helper.py @@ -0,0 +1,98 @@ +import itertools +from typing import Sequence + +import optuna +from optuna_dashboard import ChoiceWidget +from optuna_dashboard import register_objective_form_widgets +from optuna_dashboard import register_user_attr_form_widgets +from optuna_dashboard import save_note +from optuna_dashboard import SliderWidget +from optuna_dashboard import TextInputWidget +from optuna_dashboard.streamlit import render_objective_form_widgets +from optuna_dashboard.streamlit import render_trial_note +from optuna_dashboard.streamlit import render_user_attr_form_widgets +import pytest + + +@pytest.mark.parametrize("note", ["test", ""]) +def test_render_trial_note(note: str) -> None: + study = optuna.create_study() + + trial = study.ask() + save_note(trial, note) + + render_trial_note(study, study.trials[0]) + + +def test_render_trial_note_without_note() -> None: + study = optuna.create_study() + + study.ask() + + render_trial_note(study, study.trials[0]) + + +widget_list_for_user_attr = [ + ChoiceWidget( + choices=["Good", "Bad"], + values=[1, -1], + description="description", + user_attr_key="choice", + ), + SliderWidget( + min=1, + max=5, + step=1, + labels=[(1, "Bad"), (5, "Good")], + description="description", + user_attr_key="slider", + ), + TextInputWidget(description="description", user_attr_key="text1"), + TextInputWidget(description="description", user_attr_key="text2"), +] + +widgets_combinations_for_user_attr = [] +# Test widget combinations. +for r in range(len(widget_list_for_user_attr) + 1): + widgets_combinations_for_user_attr += list( + itertools.combinations(widget_list_for_user_attr, r) + ) + + +@pytest.mark.parametrize("widgets", widgets_combinations_for_user_attr) +def test_render_user_attr_form_widgets( + widgets: Sequence[ChoiceWidget | SliderWidget | TextInputWidget], +) -> None: + study = optuna.create_study() + register_user_attr_form_widgets(study, widgets) # type: ignore + + study.ask() + render_user_attr_form_widgets(study, study.trials[0]) + + +widget_list_for_objective = [ + ChoiceWidget( + choices=["Good", "Bad"], + values=[1, -1], + description="description", + ), + SliderWidget( + min=1, + max=5, + step=1, + labels=[(1, "Bad"), (5, "Good")], + description="description", + ), + TextInputWidget(description="description"), +] + + +@pytest.mark.parametrize("widget", widget_list_for_objective) +def test_render_objective_form_widgets( + widget: ChoiceWidget | SliderWidget | TextInputWidget, +) -> None: + study = optuna.create_study() + register_objective_form_widgets(study, [widget]) # type: ignore + + study.ask() + render_objective_form_widgets(study, study.trials[0])