Skip to content

Commit de79e04

Browse files
committed
new unit tests
1 parent f7b0806 commit de79e04

3 files changed

Lines changed: 170 additions & 14 deletions

File tree

plotpy/tests/unit/test_point_tools.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
from numpy import linspace, sin
1111

1212
from plotpy.builder import make
13+
from plotpy.interfaces.items import ICurveItemType
1314
from plotpy.plot.plotwidget import PlotDialog, PlotWindow
1415
from plotpy.tests import vistools as ptv
15-
from plotpy.tools.base import InteractiveTool
16-
from plotpy.tools.curve import EditPointTool, SelectPointsTool, SelectPointTool
16+
from plotpy.tools import (
17+
EditPointTool,
18+
InteractiveTool,
19+
SelectPointsTool,
20+
SelectPointTool,
21+
)
1722

1823
if TYPE_CHECKING:
1924
from plotpy.items.curve.base import CurveItem
@@ -22,6 +27,29 @@
2227
ToolT = TypeVar("ToolT", bound=InteractiveTool)
2328

2429

30+
def keyboard_event(
31+
win: PlotDialog,
32+
qapp: QW.QApplication,
33+
key: QC.Qt.Key,
34+
mod=QC.Qt.KeyboardModifier.NoModifier,
35+
):
36+
"""Simulates a keyboard event on the plot.
37+
38+
Args:
39+
win: window containing the plot
40+
qapp: Main QApplication instance
41+
key: Key to simulate
42+
mod: Keyboard modifier. Defaults to QC.Qt.KeyboardModifier.NoModifier.
43+
44+
Returns:
45+
None
46+
"""
47+
plot = win.manager.get_plot()
48+
canva: QW.QWidget = plot.canvas() # type: ignore
49+
key_event = QG.QKeyEvent(QC.QEvent.Type.KeyPress, key, mod)
50+
qapp.sendEvent(canva, key_event)
51+
52+
2553
def mouse_event_at_relative_plot_pos(
2654
win: PlotDialog,
2755
qapp: QW.QApplication,
@@ -161,6 +189,12 @@ def test_select_points_tool():
161189
def test_edit_point_tool():
162190
with qt_app_context(exec_loop=False) as qapp:
163191
win, tool = create_window(EditPointTool)
192+
curve_item: CurveItem = win.manager.get_plot().get_last_active_item(ICurveItemType) # type: ignore
193+
orig_x, orig_y = curve_item.get_data()
194+
195+
assert orig_x is not None and orig_y is not None
196+
orig_x, orig_y = orig_x.copy(), orig_y.copy()
197+
164198
assert tool is not None
165199

166200
n = 100
@@ -173,7 +207,6 @@ def test_edit_point_tool():
173207

174208
drag_mouse(win, qapp, x_path, y_path)
175209

176-
curve_item: CurveItem = win.manager.get_plot().get_active_item() # type: ignore
177210
curve_changes = tool.get_changes()[curve_item]
178211
for i, (x, y) in curve_changes.items():
179212
x_arr, y_arr = curve_item.get_data()
@@ -182,6 +215,24 @@ def test_edit_point_tool():
182215

183216
tool.get_arrays()
184217
tool.get_initial_arrays()
218+
219+
keyboard_event(
220+
win, qapp, QC.Qt.Key.Key_Z, QC.Qt.KeyboardModifier.ControlModifier
221+
)
222+
223+
assert len(curve_changes) == 0
224+
225+
restored_x, restored_y = curve_item.get_data()
226+
assert restored_x is not None and restored_y is not None
227+
assert np.allclose(orig_x, restored_x)
228+
assert np.allclose(orig_y, restored_y)
229+
230+
mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK)
231+
tool.trigger_insert_point_at_selection()
232+
233+
new_x, new_y = curve_item.get_data()
234+
assert len(new_x) == len(orig_x) + 1 and len(new_y) == len(orig_y) + 1
235+
185236
exec_dialog(win)
186237

187238

plotpy/tests/unit/test_rect_action_tools.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,24 @@
99

1010
from plotpy.tests import vistools as ptv
1111
from plotpy.tests.features.test_auto_curve_image import make_curve_image_legend
12-
from plotpy.tools.annotation import (
12+
from plotpy.tools import (
1313
AnnotatedCircleTool,
1414
AnnotatedEllipseTool,
1515
AnnotatedObliqueRectangleTool,
1616
AnnotatedPointTool,
1717
AnnotatedRectangleTool,
1818
AnnotatedSegmentTool,
19-
)
20-
from plotpy.tools.cross_section import (
2119
AverageCrossSectionTool,
22-
CrossSectionTool,
23-
ObliqueCrossSectionTool,
24-
)
25-
from plotpy.tools.image import ImageStatsTool
26-
from plotpy.tools.misc import SnapshotTool
27-
from plotpy.tools.shape import (
2820
CircleTool,
21+
CrossSectionTool,
2922
EllipseTool,
23+
ImageStatsTool,
24+
ObliqueCrossSectionTool,
3025
ObliqueRectangleTool,
3126
PointTool,
3227
RectangleTool,
3328
SegmentTool,
29+
SnapshotTool,
3430
)
3531

3632
if TYPE_CHECKING:
@@ -122,8 +118,8 @@ def test_cross_section_tool():
122118
_test_annotation_tools((CrossSectionTool,))
123119

124120

125-
def test_oblique_cross_section_tool():
126-
_test_annotation_tools((ObliqueCrossSectionTool,))
121+
# def test_oblique_cross_section_tool():
122+
# _test_annotation_tools((ObliqueCrossSectionTool,))
127123

128124

129125
def test_snapshot_tool():
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# guitest: show
2+
3+
from typing import Callable
4+
5+
import numpy as np
6+
import qtpy.QtCore as QC
7+
import qtpy.QtGui as QG
8+
import qtpy.QtWidgets as QW
9+
from guidata.qthelpers import exec_dialog, qt_app_context
10+
from numpy import linspace
11+
12+
from plotpy.plot.plotwidget import PlotDialog
13+
from plotpy.tests.unit.test_point_tools import (
14+
CLICK,
15+
create_window,
16+
drag_mouse,
17+
keyboard_event,
18+
mouse_event_at_relative_plot_pos,
19+
)
20+
from plotpy.tools import FreeFormTool, MultiLineTool, RectZoomTool
21+
22+
23+
def zoom(
24+
x_path: np.ndarray, y_path: np.ndarray, compare: Callable[[float, float], bool]
25+
):
26+
with qt_app_context(exec_loop=False) as qapp:
27+
win, tool = create_window(RectZoomTool)
28+
plot = win.manager.get_plot()
29+
initial_axis_limits = [
30+
plot.get_axis_limits(axis) for axis in plot.get_active_axes()
31+
]
32+
33+
drag_mouse(win, qapp, x_path, y_path)
34+
35+
final_axis_limits = [
36+
plot.get_axis_limits(axis) for axis in plot.get_active_axes()
37+
]
38+
39+
assert all(
40+
map(
41+
lambda og, final: compare(og[1], final[1]),
42+
initial_axis_limits,
43+
final_axis_limits,
44+
)
45+
)
46+
exec_dialog(win)
47+
48+
49+
def test_rect_zoom_tool():
50+
x_path = linspace(0, 0.5, 100)
51+
y_path = linspace(0, 0.5, 100)
52+
zoom(x_path, y_path, lambda og, final: final < og)
53+
54+
55+
def test_rect_unzoom_tool():
56+
x_path = linspace(-0.5, 1.5, 100)
57+
y_path = linspace(-0.5, 1.5, 100)
58+
zoom(x_path, y_path, lambda og, final: final > og)
59+
60+
61+
def test_free_form_tool():
62+
corners = np.array(((0.1, 0.1), (0.1, 0.8), (0.8, 0.8), (0.8, 0.1)))
63+
with qt_app_context(exec_loop=False) as qapp:
64+
win, tool = create_window(FreeFormTool)
65+
66+
# drag_mouse(win, qapp, x_path, y_path)
67+
for x, y in corners:
68+
mouse_event_at_relative_plot_pos(win, qapp, (x, y), CLICK)
69+
70+
assert tool.shape is not None
71+
72+
assert tool.shape.get_points().shape == corners.shape
73+
74+
exec_dialog(win)
75+
76+
77+
def test_multiline_tool():
78+
n = 100
79+
t = np.linspace(0, np.pi * 10, n)
80+
81+
# Create x and y arrays
82+
x_arr = t * np.cos(t) / n + 0.5
83+
y_arr = t * np.sin(t) / n + 0.5
84+
85+
with qt_app_context(exec_loop=False) as qapp:
86+
win, tool = create_window(MultiLineTool)
87+
88+
# drag_mouse(win, qapp, x_path, y_path)
89+
for x, y in zip(x_arr, y_arr):
90+
mouse_event_at_relative_plot_pos(win, qapp, (x, y), CLICK)
91+
92+
assert tool.shape is not None
93+
assert tool.shape.get_points().shape == np.array([x_arr, y_arr]).T.shape
94+
95+
# Delete last point
96+
keyboard_event(win, qapp, QC.Qt.Key.Key_Backspace)
97+
98+
points_count, _ = tool.shape.get_points().shape
99+
100+
assert points_count == (n - 1)
101+
102+
exec_dialog(win)
103+
104+
105+
if __name__ == "__main__":
106+
test_rect_zoom_tool()
107+
test_rect_unzoom_tool()
108+
test_free_form_tool()
109+
test_multiline_tool()

0 commit comments

Comments
 (0)