Skip to content

Commit fda5251

Browse files
committed
new tests and imrpovements
1 parent 276c11f commit fda5251

11 files changed

Lines changed: 436 additions & 92 deletions

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"python.testing.unittestEnabled": false,
2323
"python.testing.pytestEnabled": true,
2424
"python.testing.pytestPath": "pytest",
25-
"python.testing.pytestArgs": [],
25+
"python.testing.pytestArgs": [
26+
"plotpy"
27+
],
2628
"[python]": {
2729
"editor.defaultFormatter": "charliermarsh.ruff"
2830
},

plotpy/plot/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,9 @@ def disable_unused_axes(self):
11071107
self.enableAxis(self.colormap_axis)
11081108

11091109
def get_items(
1110-
self, z_sorted: bool = False, item_type: itf.IBasePlotItem | None = None
1110+
self,
1111+
z_sorted: bool = False,
1112+
item_type: type[itf.IItemType | itf.IBasePlotItem] | None = None,
11111113
) -> list[itf.IBasePlotItem]:
11121114
"""Return widget's item list
11131115
(items are based on IBasePlotItem's interface)

plotpy/tests/items/test_curves.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from plotpy.builder import make
1414
from plotpy.tests import vistools as ptv
15+
from plotpy.tools import CurveStatsTool
1516

1617

1718
def test_plot():
@@ -43,6 +44,8 @@ def test_plot():
4344
_win = ptv.show_items(
4445
items, wintitle=test_plot.__doc__, title="Curves", plot_type="curve"
4546
)
47+
stats_tool = _win.get_plot().manager.get_tool(CurveStatsTool)
48+
_win.get_plot().manager.set_active_tool(stats_tool)
4649

4750

4851
if __name__ == "__main__":

plotpy/tests/items/test_image_masked.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def build_items(self) -> list:
3636
xdata=[0, 20],
3737
ydata=[0, 25],
3838
)
39+
image.add_masked_area("rectangular", 0, 0, 10, 10, True)
40+
image.apply_masked_areas()
3941
return [image]
4042

4143

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import numpy as np
2+
import pytest
3+
import qtpy.QtCore as QC
4+
from guidata.qthelpers import exec_dialog, qt_app_context
5+
from qwt import QwtPlotItem
6+
7+
from plotpy.builder import make
8+
from plotpy.interfaces.items import (
9+
ICurveItemType,
10+
IDecoratorItemType,
11+
IShapeItemType,
12+
ITrackableItemType,
13+
)
14+
from plotpy.items import CurveItem, DataInfoLabel, Marker, TrImageItem, XRangeSelection
15+
from plotpy.plot.base import BasePlot
16+
from plotpy.plot.plotwidget import PlotWindow
17+
from plotpy.tests.data import gen_image4
18+
from plotpy.tests.unit.utils import (
19+
create_window,
20+
drag_mouse,
21+
keyboard_event,
22+
)
23+
from plotpy.tools import (
24+
CurveStatsTool,
25+
HCursorTool,
26+
HRangeTool,
27+
VCursorTool,
28+
XCursorTool,
29+
)
30+
from plotpy.tools.cursor import BaseCursorTool
31+
32+
# guitest: show
33+
34+
35+
def test_curve_stat_tool():
36+
with qt_app_context(exec_loop=False) as qapp:
37+
win, tool = create_window(CurveStatsTool)
38+
plot = win.manager.get_plot()
39+
40+
active_tool = win.manager.get_active_tool()
41+
assert isinstance(active_tool, CurveStatsTool)
42+
43+
og_stat_items = [
44+
item
45+
for item in plot.get_items()
46+
if isinstance(item, (DataInfoLabel, XRangeSelection))
47+
]
48+
49+
drag_mouse(win, qapp, np.array([0.5, 0.6, 0.7]), np.array([0.5, 0.6, 0.7]))
50+
51+
new_stat_items = [
52+
item
53+
for item in plot.get_items()
54+
if isinstance(item, (DataInfoLabel, XRangeSelection))
55+
]
56+
57+
# There should be one more new DataInfoLabel and one new XRangeSelection
58+
# compared to the original plot items (before mouse drag)
59+
assert len(new_stat_items) == len(og_stat_items) + 2
60+
61+
exec_dialog(win)
62+
63+
64+
@pytest.mark.parametrize(
65+
"cursor_tool", [HCursorTool, VCursorTool, XCursorTool, HRangeTool]
66+
)
67+
def test_cursor_tool(cursor_tool: type[BaseCursorTool]):
68+
with qt_app_context(exec_loop=False) as qapp:
69+
win, tool = create_window(cursor_tool)
70+
plot = win.manager.get_plot()
71+
72+
active_tool = win.manager.get_active_tool()
73+
assert isinstance(active_tool, cursor_tool)
74+
tool_shape_type = type(active_tool.create_shape())
75+
assert tool_shape_type not in (type(item) for item in plot.get_items())
76+
77+
drag_mouse(win, qapp, np.array([0.5, 0.6, 0.7]), np.array([0.5, 0.6, 0.7]))
78+
79+
assert tool_shape_type in (type(item) for item in plot.get_items())
80+
81+
exec_dialog(win)
82+
83+
84+
if __name__ == "__main__":
85+
test_curve_stat_tool()
86+
test_cursor_tool(HCursorTool)
87+
test_cursor_tool(VCursorTool)
88+
test_cursor_tool(XCursorTool)
89+
test_cursor_tool(HRangeTool)

plotpy/tests/unit/test_display_coords_tool.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import numpy as np
4+
import qtpy.QtCore as QC
45
from guidata.qthelpers import qt_app_context
56

67
from plotpy.interfaces.items import IImageItemType
@@ -14,6 +15,22 @@ def test_display_coords_on_curve():
1415
win, tool = create_window(DisplayCoordsTool)
1516
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=False)
1617
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=True)
18+
drag_mouse(
19+
win,
20+
qapp,
21+
np.array([0.5]),
22+
np.array([0.5]),
23+
click=True,
24+
mod=QC.Qt.KeyboardModifier.AltModifier,
25+
)
26+
drag_mouse(
27+
win,
28+
qapp,
29+
np.array([0.5]),
30+
np.array([0.5]),
31+
click=True,
32+
mod=QC.Qt.KeyboardModifier.ControlModifier,
33+
)
1734

1835

1936
def test_display_coords_on_image():
@@ -22,6 +39,22 @@ def test_display_coords_on_image():
2239
win, tool = create_window(DisplayCoordsTool, active_item_type=IImageItemType)
2340
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=False)
2441
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=True)
42+
drag_mouse(
43+
win,
44+
qapp,
45+
np.array([0.5]),
46+
np.array([0.5]),
47+
click=True,
48+
mod=QC.Qt.KeyboardModifier.AltModifier,
49+
)
50+
drag_mouse(
51+
win,
52+
qapp,
53+
np.array([0.5]),
54+
np.array([0.5]),
55+
click=True,
56+
mod=QC.Qt.KeyboardModifier.ControlModifier,
57+
)
2558

2659

2760
if __name__ == "__main__":

plotpy/tests/unit/test_multiline_tools.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
from plotpy.tests.unit.utils import (
88
CLICK,
99
create_window,
10+
drag_mouse,
1011
keyboard_event,
1112
mouse_event_at_relative_plot_pos,
1213
)
1314
from plotpy.tools import FreeFormTool, MultiLineTool
1415

16+
# guitest: show
17+
1518

1619
def test_free_form_tool():
1720
"""Test the free form tool."""
@@ -24,9 +27,25 @@ def test_free_form_tool():
2427
mouse_event_at_relative_plot_pos(win, qapp, (x, y), CLICK)
2528

2629
assert tool.shape is not None
27-
2830
assert tool.shape.get_points().shape == corners.shape
2931

32+
# Delete last point
33+
keyboard_event(win, qapp, QC.Qt.Key.Key_Backspace)
34+
35+
points_count, _ = tool.shape.get_points().shape
36+
37+
assert points_count == (len(corners) - 1)
38+
39+
# add last point by dragging mouse
40+
drag_mouse(win, qapp, corners[-2:, 0], corners[-2:, 1])
41+
42+
points_count, _ = tool.shape.get_points().shape
43+
assert points_count == len(corners)
44+
45+
keyboard_event(win, qapp, QC.Qt.Key.Key_Enter)
46+
47+
assert tool.shape is None
48+
3049
exec_dialog(win)
3150

3251

@@ -56,6 +75,16 @@ def test_multiline_tool():
5675

5776
assert points_count == (n - 1)
5877

78+
# add last point by dragging mouse
79+
drag_mouse(win, qapp, x_arr[-2:], y_arr[-2:])
80+
81+
points_count, _ = tool.shape.get_points().shape
82+
assert points_count == n
83+
84+
keyboard_event(win, qapp, QC.Qt.Key.Key_Enter)
85+
86+
assert tool.shape is None
87+
5988
exec_dialog(win)
6089

6190

0 commit comments

Comments
 (0)