Commit a0ab5175 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][parameterwidget] Code / test cleanup

parent 7783d019
......@@ -118,121 +118,111 @@ def parameter_name(request):
class TestBoolSetupWidget:
"""Test the BoolSetupWidget is set up and works correctly"""
def test_load_and_dump(self, qtbot):
default_setup = {"default": True}
new_setup = {"default": False}
@pytest.fixture()
def default_state_dump(self):
return {"default": True}
@pytest.fixture()
def bool_setup_widget(self, qtbot, default_state_dump):
bool_setup_widget = BoolSetupWidget()
qtbot.addWidget(bool_setup_widget)
assert bool_setup_widget.dump() == default_setup
assert bool_setup_widget.dump() == default_state_dump
return bool_setup_widget
@pytest.mark.parametrize("default", [True, False])
def test_load_and_dump(self, qtbot, bool_setup_widget, default):
new_setup = {"default": default}
if default:
with qtbot.assertNotEmitted(bool_setup_widget.dataChanged):
bool_setup_widget.load(new_setup)
else:
with qtbot.waitSignal(bool_setup_widget.dataChanged):
bool_setup_widget.load(new_setup)
with qtbot.waitSignal(bool_setup_widget.dataChanged):
bool_setup_widget.load(new_setup)
assert bool_setup_widget.dump() == new_setup
def test_select_false_as_default(self, qtbot):
default_setup = {"default": True}
new_setup = {"default": False}
@pytest.mark.parametrize("default", [True, False])
def test_change_default_value(self, qtbot, bool_setup_widget, default):
new_setup = {"default": default}
bool_setup_widget = BoolSetupWidget()
qtbot.addWidget(bool_setup_widget)
assert bool_setup_widget.dump() == default_setup
if default:
with qtbot.assertNotEmitted(bool_setup_widget.dataChanged):
qtbot.mouseClick(bool_setup_widget.true_button, QtCore.Qt.LeftButton)
else:
with qtbot.waitSignal(bool_setup_widget.dataChanged):
qtbot.mouseClick(bool_setup_widget.false_button, QtCore.Qt.LeftButton)
with qtbot.waitSignal(bool_setup_widget.dataChanged):
qtbot.mouseClick(bool_setup_widget.false_button, QtCore.Qt.LeftButton)
assert bool_setup_widget.dump() == new_setup
class TestStringSetupWidget:
"""Test the StringSetupWidget is set up and works correctly"""
def test_load_and_dump_single(self, qtbot):
default_setup = {"default": ""}
new_setup = {"default": "text 2"}
@pytest.fixture()
def default_state_dump(self):
return {"default": ""}
@pytest.fixture()
def string_setup_widget(self, qtbot, default_state_dump):
string_setup_widget = StringSetupWidget()
qtbot.addWidget(string_setup_widget)
assert string_setup_widget.dump() == default_setup
assert string_setup_widget.dump() == default_state_dump
return string_setup_widget
def test_load_and_dump_single(self, string_setup_widget):
new_setup = {"default": "text 2"}
string_setup_widget.load(new_setup)
assert string_setup_widget.dump() == new_setup
def test_load_and_dump_choices(self, qtbot):
default_setup = {"default": ""}
def test_load_and_dump_choices(self, qtbot, string_setup_widget):
new_setup = {"default": "text 2", "choice": ["text 1", "text 2", "text 3"]}
string_setup_widget = StringSetupWidget()
qtbot.addWidget(string_setup_widget)
assert string_setup_widget.dump() == default_setup
with qtbot.waitSignal(string_setup_widget.dataChanged):
string_setup_widget.load(new_setup)
assert string_setup_widget.dump() == new_setup
def test_load_and_dump_invalid_same_choice(self, qtbot):
default_setup = {"default": ""}
def test_load_and_dump_invalid_same_choice(
self, string_setup_widget, default_state_dump
):
new_setup = {"default": "text 2", "choice": ["text 1", "text 2", "text 1"]}
string_setup_widget = StringSetupWidget()
qtbot.addWidget(string_setup_widget)
assert string_setup_widget.dump() == default_setup
with pytest.raises(RuntimeError) as excinfo:
string_setup_widget.load(new_setup)
assert "Invalid duplicate choice" in str(excinfo.value)
assert string_setup_widget.dump() == default_state_dump
assert string_setup_widget.dump() == default_setup
def test_load_and_dump_invalid_default_choice(self, qtbot):
default_setup = {"default": ""}
def test_load_and_dump_invalid_default_choice(
self, string_setup_widget, default_state_dump
):
new_setup = {"default": "text 4", "choice": ["text 1", "text 2", "text 3"]}
string_setup_widget = StringSetupWidget()
qtbot.addWidget(string_setup_widget)
assert string_setup_widget.dump() == default_setup
with pytest.raises(RuntimeError) as excinfo:
string_setup_widget.load(new_setup)
assert "Invalid default choice" in str(excinfo.value)
assert string_setup_widget.dump() == default_state_dump
assert string_setup_widget.dump() == default_setup
def test_select_choices_setup(self, qtbot):
default_setup = {"default": ""}
def test_select_choices_setup(self, qtbot, string_setup_widget):
setup_choices_clicked = {"default": "", "choice": []}
string_setup_widget = StringSetupWidget()
qtbot.addWidget(string_setup_widget)
assert string_setup_widget.dump() == default_setup
with qtbot.waitSignal(string_setup_widget.dataChanged):
qtbot.mouseClick(string_setup_widget.choices_button, QtCore.Qt.LeftButton)
assert string_setup_widget.dump() == setup_choices_clicked
def test_single_change_default(self, qtbot):
default_setup = {"default": ""}
def test_single_change_default(self, qtbot, string_setup_widget):
default_changed = {"default": "newstring"}
string_setup_widget = StringSetupWidget()
qtbot.addWidget(string_setup_widget)
assert string_setup_widget.dump() == default_setup
with qtbot.waitSignal(string_setup_widget.dataChanged):
qtbot.keyClicks(
string_setup_widget.single_default_ledit, default_changed["default"]
string_setup_widget.single_default_lineedit, default_changed["default"]
)
assert string_setup_widget.dump() == default_changed
def test_choices_click_add_select(self, qtbot, monkeypatch):
default_setup = {"default": ""}
def test_choices_click_add_select(self, qtbot, monkeypatch, string_setup_widget):
change_to_choices = {"default": "", "choice": []}
setup_choices = {"default": "text 2", "choice": ["text 1", "text 2", "text 3"]}
# loading widget
string_setup_widget = StringSetupWidget()
qtbot.addWidget(string_setup_widget)
assert string_setup_widget.dump() == default_setup
# click choices radio button
with qtbot.waitSignal(string_setup_widget.dataChanged):
qtbot.mouseClick(string_setup_widget.choices_button, QtCore.Qt.LeftButton)
......@@ -256,16 +246,10 @@ class TestStringSetupWidget:
assert string_setup_widget.dump() == setup_choices
def test_choices_click_remove(self, qtbot, monkeypatch):
default_setup = {"default": ""}
def test_choices_click_remove(self, qtbot, monkeypatch, string_setup_widget):
setup_choices = {"default": "text 2", "choice": ["text 1", "text 2", "text 3"]}
removed_choices = {"default": "text 3", "choice": ["text 1", "text 3"]}
# loading widget
string_setup_widget = StringSetupWidget()
qtbot.addWidget(string_setup_widget)
assert string_setup_widget.dump() == default_setup
# loading data
with qtbot.waitSignal(string_setup_widget.dataChanged):
string_setup_widget.load(setup_choices)
......@@ -278,8 +262,9 @@ class TestStringSetupWidget:
assert string_setup_widget.dump() == removed_choices
def test_choices_click_add_invalid_same_choice(self, qtbot, monkeypatch):
default_setup = {"default": ""}
def test_choices_click_add_invalid_same_choice(
self, qtbot, monkeypatch, string_setup_widget
):
change_to_choices = {"default": "", "choice": []}
wrong_setup_choices = {
"default": "text 2",
......@@ -287,11 +272,6 @@ class TestStringSetupWidget:
}
correct_setup_choices = {"default": "text 2", "choice": ["text 1", "text 2"]}
# loading widget
string_setup_widget = StringSetupWidget()
qtbot.addWidget(string_setup_widget)
assert string_setup_widget.dump() == default_setup
# click choices radio button
with qtbot.waitSignal(string_setup_widget.dataChanged):
qtbot.mouseClick(string_setup_widget.choices_button, QtCore.Qt.LeftButton)
......@@ -319,121 +299,99 @@ class TestStringSetupWidget:
class TestNumericalSetupWidget:
"""Test the NumericalSetupWidget is set up and works correctly"""
def test_load_and_dump_single(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
new_setup = {"default": 3}
@pytest.fixture()
def default_state_dump(self):
return {"default": 0}
@pytest.fixture()
def numerical_setup_widget(self, qtbot, default_state_dump, numerical_input_type):
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
assert numerical_setup_widget.dump() == default_state_dump
return numerical_setup_widget
def test_load_and_dump_single(self, numerical_setup_widget):
new_setup = {"default": 3}
numerical_setup_widget.load(new_setup)
assert numerical_setup_widget.dump() == new_setup
def test_load_and_dump_choices(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
def test_load_and_dump_choices(self, qtbot, numerical_setup_widget):
new_setup = {"default": 3, "choice": [2, 3, 4]}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
numerical_setup_widget.load(new_setup)
assert numerical_setup_widget.dump() == new_setup
def test_load_and_dump_invalid_same_choice(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
def test_load_and_dump_invalid_same_choice(
self, numerical_setup_widget, default_state_dump
):
new_setup = {"default": 3, "choice": [2, 3, 2]}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
with pytest.raises(RuntimeError) as excinfo:
numerical_setup_widget.load(new_setup)
assert "Invalid duplicate choice" in str(excinfo.value)
assert numerical_setup_widget.dump() == default_setup
assert numerical_setup_widget.dump() == default_state_dump
def test_load_and_dump_invalid_default_choice(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
def test_load_and_dump_invalid_default_choice(
self, numerical_setup_widget, default_state_dump
):
new_setup = {"default": 5, "choice": [2, 3, 4]}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
with pytest.raises(RuntimeError) as excinfo:
numerical_setup_widget.load(new_setup)
assert "Invalid default choice" in str(excinfo.value)
assert numerical_setup_widget.dump() == default_setup
assert numerical_setup_widget.dump() == default_state_dump
def test_load_and_dump_range(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
def test_load_and_dump_range(self, qtbot, numerical_setup_widget):
new_setup = {"default": 3, "range": [2, 4]}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
numerical_setup_widget.load(new_setup)
assert numerical_setup_widget.dump() == new_setup
def test_load_and_dump_invalid_same_range(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
def test_load_and_dump_invalid_same_range(
self, numerical_setup_widget, default_state_dump
):
new_setup = {"default": 2, "range": [2, 2]}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
with pytest.raises(RuntimeError) as excinfo:
numerical_setup_widget.load(new_setup)
assert "Invalid duplicate range" in str(excinfo.value)
assert numerical_setup_widget.dump() == default_setup
assert numerical_setup_widget.dump() == default_state_dump
def test_load_and_dump_invalid_default_range(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
def test_load_and_dump_invalid_default_range(
self, numerical_setup_widget, default_state_dump
):
new_setup = {"default": 5, "range": [2, 4]}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
with pytest.raises(RuntimeError) as excinfo:
numerical_setup_widget.load(new_setup)
assert "Invalid default or min/max range" in str(excinfo.value)
assert numerical_setup_widget.dump() == default_setup
assert numerical_setup_widget.dump() == default_state_dump
def test_load_and_dump_invalid_min_max_range(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
def test_load_and_dump_invalid_min_max_range(
self, numerical_setup_widget, default_state_dump
):
new_setup = {"default": 4, "range": [4, 2]}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
with pytest.raises(RuntimeError) as excinfo:
numerical_setup_widget.load(new_setup)
assert "Invalid default or min/max range" in str(excinfo.value)
assert numerical_setup_widget.dump() == default_setup
assert numerical_setup_widget.dump() == default_state_dump
def test_setup_type_selection(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
def test_setup_type_selection(
self, qtbot, numerical_setup_widget, default_state_dump
):
setup_choices_clicked = {"default": 0, "choice": []}
setup_range_clicked = {"default": 0, "range": [-10, 10]}
setup_range_clicked_uint = {"default": 0, "range": [0, 10]}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
qtbot.mouseClick(
numerical_setup_widget.choices_button, QtCore.Qt.LeftButton
......@@ -443,23 +401,20 @@ class TestNumericalSetupWidget:
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
qtbot.mouseClick(numerical_setup_widget.range_button, QtCore.Qt.LeftButton)
if numerical_input_type in UNSIGNED_TYPES:
if numerical_setup_widget.current_type in UNSIGNED_TYPES:
assert numerical_setup_widget.dump() == setup_range_clicked_uint
else:
assert numerical_setup_widget.dump() == setup_range_clicked
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
qtbot.mouseClick(numerical_setup_widget.single_button, QtCore.Qt.LeftButton)
assert numerical_setup_widget.dump() == default_setup
assert numerical_setup_widget.dump() == default_state_dump
def test_single_change_default(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
def test_single_change_default(
self, qtbot, numerical_setup_widget, default_state_dump
):
default_changed = {"default": 3}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
qtbot.keyClicks(
numerical_setup_widget.single_default_spinbox,
......@@ -468,14 +423,9 @@ class TestNumericalSetupWidget:
assert numerical_setup_widget.dump() == default_changed
def test_single_change_default_invalid_input(
self, qtbot, numerical_input_type, expected_values
self, qtbot, numerical_setup_widget, expected_values, default_state_dump
):
value = "44444444444444444444444444444444444444444444444444444444444"
default_setup = {"default": 0}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
spinbox = numerical_setup_widget.single_default_spinbox
......@@ -490,19 +440,13 @@ class TestNumericalSetupWidget:
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
qtbot.keyClicks(spinbox, value)
assert spinbox.value() == spinbox.numpy_type(expected_value)
default_setup["default"] = spinbox.value()
assert numerical_setup_widget.dump() == default_setup
default_state_dump["default"] = spinbox.value()
assert numerical_setup_widget.dump() == default_state_dump
def test_choices_click_add(self, qtbot, monkeypatch, numerical_input_type):
default_setup = {"default": 0}
def test_choices_click_add(self, qtbot, monkeypatch, numerical_setup_widget):
change_to_choices = {"default": 0, "choice": []}
setup_choices = {"default": 2, "choice": [1, 2, 3]}
# loading widget
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
# click choices radio button
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
qtbot.mouseClick(
......@@ -528,16 +472,10 @@ class TestNumericalSetupWidget:
assert numerical_setup_widget.dump() == setup_choices
def test_choices_click_remove(self, qtbot, monkeypatch, numerical_input_type):
default_setup = {"default": 0}
def test_choices_click_remove(self, qtbot, monkeypatch, numerical_setup_widget):
setup_choices = {"default": 2, "choice": [1, 2, 3]}
removed_choices = {"default": 3, "choice": [1, 3]}
# loading widget
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
# loading data
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
numerical_setup_widget.load(setup_choices)
......@@ -558,18 +496,12 @@ class TestNumericalSetupWidget:
assert numerical_setup_widget.dump() == removed_choices
def test_choices_click_add_invalid_same_choice(
self, qtbot, monkeypatch, numerical_input_type
self, qtbot, monkeypatch, numerical_setup_widget
):
default_setup = {"default": 0}
change_to_choices = {"default": 0, "choice": []}
wrong_setup_choices = {"default": 2, "choice": [1, 2, 1]}
correct_setup_choices = {"default": 2, "choice": [1, 2]}
# loading widget
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
# click choices radio button
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
qtbot.mouseClick(
......@@ -597,13 +529,12 @@ class TestNumericalSetupWidget:
assert numerical_setup_widget.dump() == correct_setup_choices
def test_choices_click_add_invalid_input(
self, qtbot, monkeypatch, numerical_input_type, expected_values
self, qtbot, monkeypatch, numerical_setup_widget, expected_values
):
value1 = "44444444444444444444444444444444444444444444444444444444444"
value2 = "44444444444444444444444444444444444444444444444444444444445"
value3 = "44444444444444444444444444444444444444444444444444444444446"
overflow_float_64 = 4.4444444444444445e58
default_setup = {"default": 0}
change_to_choices = {"default": 0, "choice": []}
setup_choices_float_32 = {"default": np.inf, "choice": [np.inf, np.inf, np.inf]}
setup_choices_float_64 = {
......@@ -611,11 +542,6 @@ class TestNumericalSetupWidget:
"choice": [overflow_float_64, overflow_float_64, overflow_float_64],
}
# loading widget
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
# click choices radio button
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
qtbot.mouseClick(
......@@ -680,8 +606,7 @@ class TestNumericalSetupWidget:
setup_choices_float_64["choice"].pop(0)
assert numerical_setup_widget.dump() == setup_choices_float_64
def test_range_change_default_min_max(self, qtbot, numerical_input_type):
default_setup = {"default": 0}
def test_range_change_default_min_max(self, qtbot, numerical_setup_widget):
setup_range_clicked = {"default": 0, "range": [-10, 10]}
setup_range_clicked_uint = {"default": 0, "range": [0, 10]}
range_min_type = 1
......@@ -689,13 +614,9 @@ class TestNumericalSetupWidget:
new_setup = {"default": 3, "range": [-101, 104]}
new_setup_uint = {"default": 3, "range": [1, 104]}
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
with qtbot.waitSignal(numerical_setup_widget.dataChanged):
qtbot.mouseClick(numerical_setup_widget.range_button, QtCore.Qt.LeftButton)
if numerical_input_type in UNSIGNED_TYPES:
if numerical_setup_widget.current_type in UNSIGNED_TYPES:
assert numerical_setup_widget.dump() == setup_range_clicked_uint
else:
assert numerical_setup_widget.dump() == setup_range_clicked
......@@ -712,24 +633,18 @@ class TestNumericalSetupWidget:
qtbot.keyClicks(
numerical_setup_widget.range_minimum_spinbox, str(range_min_type)
)
if numerical_input_type in UNSIGNED_TYPES:
if numerical_setup_widget.current_type in UNSIGNED_TYPES:
assert numerical_setup_widget.dump() == new_setup_uint
else:
assert numerical_setup_widget.dump() == new_setup
def test_range_add_invalid_inputs(
self, qtbot, monkeypatch, numerical_input_type, expected_values
self, qtbot, monkeypatch, numerical_setup_widget, expected_values
):
value = 12
value1 = 44444444444444444444444444444444444444444444444444444444444e608
default_setup = {"default": 0}
required_invalid_setup = {"default": value, "range": [value, value1]}
# loading widget
numerical_setup_widget = NumericalSetupWidget(numerical_input_type)
qtbot.addWidget(numerical_setup_widget)
assert numerical_setup_widget.dump() == default_setup
expected_value = None
for item in expected_values:
if numerical_setup_widget.current_type.np_type == item[0]:
......
......@@ -248,7 +248,7 @@ class StringSetupWidget(QWidget):
layout.addLayout(radio_buttons_layout)
# single items
self.single_default_ledit = QLineEdit()
self.single_default_lineedit = QLineEdit()
# choices items
self.choices_default_combobox = QComboBox()
......@@ -283,7 +283,7 @@ class StringSetupWidget(QWidget):
self.single_button.toggled.connect(self.dataChanged)
self.choices_button.toggled.connect(self.dataChanged)
self.single_default_ledit.textChanged.connect(self.dataChanged)
self.single_default_lineedit.textChanged.connect(self.dataChanged)
self.choices_default_combobox.currentIndexChanged.connect(self.dataChanged)
choices_model = self.choices_listwidget.model()
choices_model.rowsInserted.connect(self.dataChanged)
......@@ -318,7 +318,7 @@ class StringSetupWidget(QWidget):
raise RuntimeError("Invalid default value")
else:
self.single_button.setChecked(True)
self.single_default_ledit.setText(default)
self.single_default_lineedit.setText(default)
def dump(self):
"""Returns the json representation of the editor"""
......@@ -326,7 +326,7 @@ class StringSetupWidget(QWidget):
data = {}
if self.single_button.isChecked():
data["default"] = self.single_default_ledit.text()
data["default"] = self.single_default_lineedit.text()
else:
data["default"] = self.choices_default_combobox.currentText()
data["choice"] = [
......@@ -340,14 +340,14 @@ class StringSetupWidget(QWidget):
"""Reset the editor to its default state"""
self.single_button.setChecked(True)
self.single_default_ledit.clear()
self.single_default_lineedit.clear()
self.choices_listwidget.clear()
self.choices_default_combobox.clear()