Skip to content

Commit be02212

Browse files
committed
Builder unit tests: use qt_app_context
1 parent 2f002a1 commit be02212

6 files changed

+163
-128
lines changed

plotpy/tests/unit/test_builder_annotation.py

+27-24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import numpy as np
99
import pytest
10+
from guidata.qthelpers import qt_app_context
1011

1112
from plotpy.builder import make
1213
from plotpy.tests.unit.test_builder_curve import show_items_qtbot
@@ -61,28 +62,30 @@ def _make_annotation(
6162
],
6263
)
6364
def test_builder_annotation_params(method):
64-
items = []
65-
for show_label in [True, False]:
66-
items.append(
67-
_make_annotation(
68-
method,
69-
title="title",
70-
subtitle="subtitle",
71-
show_label=show_label,
65+
"""Test PlotBuilder annotation factory method parameters"""
66+
with qt_app_context(exec_loop=False):
67+
items = []
68+
for show_label in [True, False]:
69+
items.append(
70+
_make_annotation(
71+
method,
72+
title="title",
73+
subtitle="subtitle",
74+
show_label=show_label,
75+
)
7276
)
73-
)
74-
for show_computations in [True, False]:
75-
items.append(_make_annotation(method, show_computations=show_computations))
76-
for show_subtitle in [True, False]:
77-
items.append(_make_annotation(method, show_subtitle=show_subtitle))
78-
for format in ["%f", "%e"]:
79-
items.append(_make_annotation(method, format=format))
80-
for uncertainty in [0.0, 1.0]:
81-
items.append(_make_annotation(method, uncertainty=uncertainty))
82-
for transform_matrix in [None, np.identity(3)]:
83-
items.append(_make_annotation(method, transform_matrix=transform_matrix))
84-
for readonly in [True, False]:
85-
items.append(_make_annotation(method, readonly=readonly))
86-
for private in [True, False]:
87-
items.append(_make_annotation(method, private=private))
88-
show_items_qtbot(items)
77+
for show_computations in [True, False]:
78+
items.append(_make_annotation(method, show_computations=show_computations))
79+
for show_subtitle in [True, False]:
80+
items.append(_make_annotation(method, show_subtitle=show_subtitle))
81+
for format in ["%f", "%e"]:
82+
items.append(_make_annotation(method, format=format))
83+
for uncertainty in [0.0, 1.0]:
84+
items.append(_make_annotation(method, uncertainty=uncertainty))
85+
for transform_matrix in [None, np.identity(3)]:
86+
items.append(_make_annotation(method, transform_matrix=transform_matrix))
87+
for readonly in [True, False]:
88+
items.append(_make_annotation(method, readonly=readonly))
89+
for private in [True, False]:
90+
items.append(_make_annotation(method, private=private))
91+
show_items_qtbot(items)

plotpy/tests/unit/test_builder_curve.py

+25-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99
import pytest
10-
from guidata.qthelpers import exec_dialog
10+
from guidata.qthelpers import exec_dialog, qt_app_context
1111
from qtpy.QtCore import Qt
1212
from qwt import QwtPlotCurve
1313

@@ -43,17 +43,19 @@ def _make_curve_style(shade, curvestyle, baseline):
4343
@pytest.mark.parametrize("baseline", [0.0])
4444
def test_builder_curve_curve_style(shade, curvestyle, baseline):
4545
"""Test curve parameters of curve() method"""
46-
curve = _make_curve_style(shade, curvestyle, baseline)
47-
show_items_qtbot([curve], "curve")
46+
with qt_app_context(exec_loop=False):
47+
curve = _make_curve_style(shade, curvestyle, baseline)
48+
show_items_qtbot([curve], "curve")
4849

4950

5051
@pytest.mark.parametrize("shade", [0, 0.4, 1.0])
5152
@pytest.mark.parametrize("curvestyle", ["Lines"])
5253
@pytest.mark.parametrize("baseline", [0.0, 1.0])
5354
def test_builder_curve_curve_shade_baseline(shade, curvestyle, baseline):
5455
"""Test curve parameters of curve() method"""
55-
curve = _make_curve_style(shade, curvestyle, baseline)
56-
show_items_qtbot([curve], "curve")
56+
with qt_app_context(exec_loop=False):
57+
curve = _make_curve_style(shade, curvestyle, baseline)
58+
show_items_qtbot([curve], "curve")
5759

5860

5961
def _make_curve_dsamp(dsamp_factor, use_dsamp):
@@ -70,16 +72,18 @@ def _make_curve_dsamp(dsamp_factor, use_dsamp):
7072
@pytest.mark.parametrize("use_dsamp", [True])
7173
def test_builder_curve_dsamp_on(dsamp_factor, use_dsamp):
7274
"""Test downsampling parameters of curve() method: use_dsamp=True"""
73-
curve = _make_curve_dsamp(dsamp_factor, use_dsamp)
74-
show_items_qtbot([curve], "curve")
75+
with qt_app_context(exec_loop=False):
76+
curve = _make_curve_dsamp(dsamp_factor, use_dsamp)
77+
show_items_qtbot([curve], "curve")
7578

7679

7780
@pytest.mark.parametrize("dsamp_factor", [1, 2])
7881
@pytest.mark.parametrize("use_dsamp", [False])
7982
def test_builder_curve_dsamp_off(dsamp_factor, use_dsamp):
8083
"""Test downsampling parameters of curve() method: use_dsamp=False"""
81-
curve = _make_curve_dsamp(dsamp_factor, use_dsamp)
82-
show_items_qtbot([curve], "curve")
84+
with qt_app_context(exec_loop=False):
85+
curve = _make_curve_dsamp(dsamp_factor, use_dsamp)
86+
show_items_qtbot([curve], "curve")
8387

8488

8589
def _make_curve_linestyle(color, linestyle, linewidth):
@@ -102,17 +106,19 @@ def _make_curve_linestyle(color, linestyle, linewidth):
102106
@pytest.mark.parametrize("linewidth", [1, 2])
103107
def test_builder_curve_line_style(color, linestyle, linewidth):
104108
"""Test line parameters of curve() method"""
105-
curve = _make_curve_linestyle(color, linestyle, linewidth)
106-
show_items_qtbot([curve], "curve")
109+
with qt_app_context(exec_loop=False):
110+
curve = _make_curve_linestyle(color, linestyle, linewidth)
111+
show_items_qtbot([curve], "curve")
107112

108113

109114
@pytest.mark.parametrize("color", ["red", "blue"])
110115
@pytest.mark.parametrize("linestyle", ["SolidLine"])
111116
@pytest.mark.parametrize("linewidth", [1, 2])
112117
def test_builder_curve_line_color(color, linestyle, linewidth):
113118
"""Test line parameters of curve() method"""
114-
curve = _make_curve_linestyle(color, linestyle, linewidth)
115-
show_items_qtbot([curve], "curve")
119+
with qt_app_context(exec_loop=False):
120+
curve = _make_curve_linestyle(color, linestyle, linewidth)
121+
show_items_qtbot([curve], "curve")
116122

117123

118124
def _make_curve_marker(marker, markersize, markerfacecolor, markeredgecolor):
@@ -160,8 +166,9 @@ def test_builder_curve_marker_params_symbol(
160166
marker, markersize, markerfacecolor, markeredgecolor
161167
):
162168
"""Test marker parameters of curve() methodg"""
163-
curve = _make_curve_marker(marker, markersize, markerfacecolor, markeredgecolor)
164-
show_items_qtbot([curve], "curve")
169+
with qt_app_context(exec_loop=False):
170+
curve = _make_curve_marker(marker, markersize, markerfacecolor, markeredgecolor)
171+
show_items_qtbot([curve], "curve")
165172

166173

167174
@pytest.mark.parametrize("marker", ["Cross"])
@@ -172,5 +179,6 @@ def test_builder_curve_marker_size_color(
172179
marker, markersize, markerfacecolor, markeredgecolor
173180
):
174181
"""Test marker parameters of curve() methodg"""
175-
curve = _make_curve_marker(marker, markersize, markerfacecolor, markeredgecolor)
176-
show_items_qtbot([curve], "curve")
182+
with qt_app_context(exec_loop=False):
183+
curve = _make_curve_marker(marker, markersize, markerfacecolor, markeredgecolor)
184+
show_items_qtbot([curve], "curve")

plotpy/tests/unit/test_builder_image.py

+28-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import numpy as np
99
import pytest
10+
from guidata.qthelpers import qt_app_context
1011

1112
from plotpy.builder import make
1213
from plotpy.constants import LUTAlpha
@@ -61,55 +62,64 @@ def _make_image(
6162
],
6263
)
6364
def test_builder_image_alpha_function(alpha_function):
64-
item = _make_image(alpha_function=alpha_function)
65-
show_items_qtbot([item])
65+
with qt_app_context(exec_loop=False):
66+
item = _make_image(alpha_function=alpha_function)
67+
show_items_qtbot([item])
6668

6769

6870
@pytest.mark.parametrize(
6971
"xdata,ydata", [[[None, None], [None, None]], [[-10, 10], [-10, 10]]]
7072
)
7173
def test_builder_image_xdata_ydata(xdata, ydata):
72-
item = _make_image(xdata=xdata, ydata=ydata)
73-
show_items_qtbot([item])
74+
with qt_app_context(exec_loop=False):
75+
item = _make_image(xdata=xdata, ydata=ydata)
76+
show_items_qtbot([item])
7477

7578

7679
@pytest.mark.parametrize("pixel_size", [None, 1.0, (1.0, 2.0)])
7780
def test_builder_image_pixel_size(pixel_size):
78-
item = _make_image(pixel_size=pixel_size)
79-
show_items_qtbot([item])
81+
with qt_app_context(exec_loop=False):
82+
item = _make_image(pixel_size=pixel_size)
83+
show_items_qtbot([item])
8084

8185

8286
@pytest.mark.parametrize("center_on", [None, [1.0, 3.0]])
8387
def test_builder_image_center_on(center_on):
84-
item = _make_image(center_on=center_on, pixel_size=(1.0, 1.0))
85-
show_items_qtbot([item])
88+
with qt_app_context(exec_loop=False):
89+
item = _make_image(center_on=center_on, pixel_size=(1.0, 1.0))
90+
show_items_qtbot([item])
8691

8792

8893
@pytest.mark.parametrize("interpolation", ["nearest", "linear", "antialiasing"])
8994
def test_builder_image_interpolation(interpolation):
90-
item = _make_image(interpolation=interpolation)
91-
show_items_qtbot([item])
95+
with qt_app_context(exec_loop=False):
96+
item = _make_image(interpolation=interpolation)
97+
show_items_qtbot([item])
9298

9399

94100
@pytest.mark.parametrize("background_color", [None, "red"])
95101
def test_builder_image_background_color(background_color):
96-
item = _make_image(background_color=background_color)
97-
show_items_qtbot([item])
102+
with qt_app_context(exec_loop=False):
103+
item = _make_image(background_color=background_color)
104+
show_items_qtbot([item])
98105

99106

100107
@pytest.mark.parametrize("eliminate_outliers", [None, 3.0])
101108
def test_builder_image_eliminate_outliers(eliminate_outliers):
102-
item = _make_image(eliminate_outliers=eliminate_outliers)
103-
show_items_qtbot([item])
109+
with qt_app_context(exec_loop=False):
110+
item = _make_image(eliminate_outliers=eliminate_outliers)
111+
show_items_qtbot([item])
104112

105113

106114
@pytest.mark.parametrize("lut_range", [None, [0.0, 100.0]])
107115
def test_builder_image_lut_range(lut_range):
108-
item = _make_image(lut_range=lut_range)
109-
show_items_qtbot([item])
116+
with qt_app_context(exec_loop=False):
117+
item = _make_image(lut_range=lut_range)
118+
show_items_qtbot([item])
110119

111120

112121
@pytest.mark.parametrize("lock_position", [None, True, False])
113122
def test_builder_image_lock_position(lock_position):
114-
item = _make_image(lock_position=lock_position)
115-
show_items_qtbot([item])
123+
with qt_app_context(exec_loop=False):
124+
item = _make_image(lock_position=lock_position)
125+
show_items_qtbot([item])

plotpy/tests/unit/test_builder_shape.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import numpy as np
99
import pytest
10+
from guidata.qthelpers import qt_app_context
1011

1112
from plotpy.builder import make
1213
from plotpy.tests import get_path
@@ -37,28 +38,31 @@ def _make_standard_shape(
3738
[make.segment, make.rectangle, make.circle, make.ellipse],
3839
)
3940
def test_builder_standard_shape(method):
40-
items = []
41-
items.append(_make_standard_shape(method, title="title"))
42-
show_items_qtbot(items)
41+
with qt_app_context(exec_loop=False):
42+
items = []
43+
items.append(_make_standard_shape(method, title="title"))
44+
show_items_qtbot(items)
4345

4446

4547
def test_builder_polygon():
4648
items = []
4749
x = np.linspace(0, 1, 10)
4850
y = x**2
49-
for closed in [True, False]:
50-
items.append(make.polygon(x, y, closed=closed, title="title"))
51-
show_items_qtbot(items)
51+
with qt_app_context(exec_loop=False):
52+
for closed in [True, False]:
53+
items.append(make.polygon(x, y, closed=closed, title="title"))
54+
show_items_qtbot(items)
5255

5356

5457
def test_builder_svgshape():
5558
items = []
5659
svg_path = get_path("svg_target.svg")
5760
with open(svg_path, "rb") as f:
5861
svg_data = f.read()
59-
for shape_str in ("circle", "rectangle", "square"):
60-
for data_or_path in (svg_data, svg_path):
61-
items.append(
62-
make.svg(shape_str, data_or_path, 0.0, 0.0, 1.0, 1.0, title="title")
63-
)
64-
show_items_qtbot(items)
62+
with qt_app_context(exec_loop=False):
63+
for shape_str in ("circle", "rectangle", "square"):
64+
for data_or_path in (svg_data, svg_path):
65+
items.append(
66+
make.svg(shape_str, data_or_path, 0.0, 0.0, 1.0, 1.0, title="title")
67+
)
68+
show_items_qtbot(items)

0 commit comments

Comments
 (0)