from __future__ import annotations from unittest import TestCase from optuna_dashboard import ChoiceWidget from optuna_dashboard import dict_to_form_widget from optuna_dashboard import ObjectiveUserAttrRef from optuna_dashboard import SliderWidget from optuna_dashboard import TextInputWidget class FormWidgetsTestCase(TestCase): def test_widget_to_dict_from_dict(self) -> None: widgets = [ ChoiceWidget(choices=["Good", "Bad"], values=[1, -1]), ChoiceWidget( choices=["Good", "Bad"], values=[1, -1], description="description", user_attr_key="key", ), SliderWidget(min=1, max=5), SliderWidget( min=1, max=5, step=1, labels=[(1, "Bad"), (5, "Good")], description="description", user_attr_key="key", ), TextInputWidget(), TextInputWidget(description="description", user_attr_key="key"), ObjectiveUserAttrRef(key="key"), ] for i, widget in enumerate(widgets): with self.subTest(f"{widget.__class__}-{i}"): d = widget.to_dict() restored = dict_to_form_widget(d) assert widget == restored