Skip to content

Commit a39fc70

Browse files
committed
Add unit tests for the plot style sidebar
Cover the behaviour that is easy to break and hard to notice: that a preset round-trips through the widgets unchanged, that it overrides widths a user has already chosen, that each reset button restores the defaults with a single re-render, that a role using the ensemble color cycle is reported without a color, and that Everest sees only the line style section.
1 parent df41072 commit a39fc70

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import math
2+
from unittest.mock import Mock
3+
4+
import pytest
5+
from PyQt6.QtWidgets import QPushButton, QWidget
6+
7+
from ert.gui.plotting.utils import StyleRole
8+
from ert.gui.plotting.utils.plot_style import (
9+
DEFAULT_STYLES,
10+
STATISTICS_DEFAULT,
11+
STATISTICS_PRESETS,
12+
)
13+
from ert.gui.plotting.widgets.plot_controls.style_options import StylePlotOptions
14+
15+
16+
def _create_options(qtbot, connection_point=None, *, is_everest=False):
17+
options = StylePlotOptions(connection_point or Mock(), is_everest=is_everest)
18+
qtbot.addWidget(options.get_widget())
19+
return options
20+
21+
22+
def _click_reset(options, name):
23+
options.get_widget().findChild(QPushButton, f"reset_{name}_button").click()
24+
25+
26+
@pytest.mark.parametrize("preset_name", list(STATISTICS_PRESETS))
27+
def test_that_a_statistics_preset_round_trips_through_the_controls(qtbot, preset_name):
28+
options = _create_options(qtbot)
29+
30+
options.apply_preset(preset_name)
31+
32+
preset = STATISTICS_PRESETS[preset_name]
33+
assert {role: options.styles()[role] for role in preset} == dict(preset)
34+
35+
36+
def test_that_a_preset_replaces_manually_chosen_widths(qtbot):
37+
options = _create_options(qtbot)
38+
options.apply_preset(STATISTICS_DEFAULT)
39+
options._rows[StyleRole.MEAN].thickness_spinner.setValue(4.0)
40+
41+
options.apply_preset("All statistics")
42+
43+
assert math.isclose(options.styles()[StyleRole.MEAN].linewidth, 1.0)
44+
45+
46+
def test_that_resetting_line_styles_restores_defaults_with_a_single_rerender(qtbot):
47+
connection_point = Mock()
48+
options = _create_options(qtbot, connection_point)
49+
options._rows[StyleRole.DEFAULT].thickness_spinner.setValue(4.0)
50+
calls_before_reset = connection_point.call_count
51+
52+
_click_reset(options, "line_styles")
53+
54+
assert options.styles()[StyleRole.DEFAULT] == DEFAULT_STYLES[StyleRole.DEFAULT]
55+
assert connection_point.call_count == calls_before_reset + 1
56+
57+
58+
def test_that_resetting_statistics_restores_the_preset_and_multiplier(qtbot):
59+
connection_point = Mock()
60+
options = _create_options(qtbot, connection_point)
61+
options.apply_preset("All statistics")
62+
options._std_dev_factor.setValue(3)
63+
calls_before_reset = connection_point.call_count
64+
65+
_click_reset(options, "statistics_styles")
66+
67+
default_preset = STATISTICS_PRESETS[STATISTICS_DEFAULT]
68+
assert {role: options.styles()[role] for role in default_preset} == dict(
69+
default_preset
70+
)
71+
assert options.std_dev_factor == 1
72+
assert connection_point.call_count == calls_before_reset + 1
73+
74+
75+
def test_that_a_color_palette_role_is_reported_without_a_color(qtbot):
76+
options = _create_options(qtbot)
77+
78+
assert options.styles()[StyleRole.DEFAULT].color is None
79+
assert options.styles()[StyleRole.OBSERVATIONS].color == "#000000"
80+
81+
82+
def test_that_the_line_section_is_hidden_once_all_its_rows_are_hidden(qtbot):
83+
options = _create_options(qtbot)
84+
line_section = options.get_widget().findChild(QWidget, "line_style_options")
85+
options.set_line_styles_visible(True, realizations=True)
86+
options.set_history_style_visible(False)
87+
options.set_observations_style_visible(False)
88+
assert not line_section.isHidden()
89+
90+
options.set_line_styles_visible(True, realizations=False)
91+
92+
assert line_section.isHidden()
93+
94+
95+
def test_that_everest_only_shows_the_line_style_section(qtbot):
96+
widget = _create_options(qtbot, is_everest=True).get_widget()
97+
98+
assert widget.findChild(QWidget, "line_style_options") is not None
99+
assert widget.findChild(QWidget, "statistics_style_options") is None
100+
assert widget.findChild(QWidget, "distribution_style_options") is None

0 commit comments

Comments
 (0)