Skip to content

Commit 7c01578

Browse files
committed
feat: tests for load vertices button
1 parent 5261aab commit 7c01578

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

ORStools/gui/ORStoolsDialog.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,20 @@ def reload_rubber_band(self) -> None:
546546
if self.line_tool is not None:
547547
self.line_tool.create_rubber_band()
548548

549-
def load_vertices_from_layer(self) -> None:
549+
def load_vertices_from_layer(self, testing: str = "") -> None:
550550
if not self.line_tool:
551551
self.line_tool = maptools.LineTool(self)
552+
552553
box = LayerMessageBox()
553-
res = box.exec_()
554-
if res == QMessageBox.Ok:
554+
555+
if testing == "ok":
556+
result = QMessageBox.Ok
557+
elif testing == "not_ok":
558+
result = QMessageBox.Cancel
559+
else:
560+
result = box.exec_()
561+
562+
if result == QMessageBox.Ok:
555563
layer = box.selectedLayer()
556564
try:
557565
self.routing_fromline_list.clear()

tests/test_gui.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
12
from qgis.PyQt.QtWidgets import QLineEdit
2-
from qgis._core import QgsSettings
3+
from qgis._core import (
4+
QgsSettings,
5+
QgsVectorLayer,
6+
QgsFeature,
7+
QgsGeometry,
8+
QgsProject,
9+
QgsPointXY,
10+
)
311
from qgis.gui import QgsCollapsibleGroupBox
412
from qgis.testing import unittest
513

@@ -358,3 +366,42 @@ def test_ORStoolsDialogConfig_url(self):
358366
"POINT(8.67251100000000008 49.39887900000000087)",
359367
next(layer.getFeatures()).geometry().asPolyline()[0].asWkt(),
360368
)
369+
370+
def test_load_valid_point_layer(self):
371+
"""Test loading vertices from valid point layer."""
372+
from ORStools.gui.ORStoolsDialog import ORStoolsDialogMain
373+
374+
dialog_main = ORStoolsDialogMain(IFACE)
375+
dialog_main._init_gui_control()
376+
377+
# Create test layers
378+
self.point_layer = QgsVectorLayer("Point?crs=EPSG:4326", "test_points", "memory")
379+
self.line_layer = QgsVectorLayer("LineString?crs=EPSG:4326", "test_lines", "memory")
380+
381+
# Add 3 features to point layer
382+
for coords in [(1, 2), (3, 4), (5, 6)]:
383+
feat = QgsFeature()
384+
feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(*coords)))
385+
self.point_layer.dataProvider().addFeature(feat)
386+
387+
QgsProject.instance().addMapLayers([self.point_layer, self.line_layer])
388+
389+
# Run test
390+
dialog_main.dlg.load_vertices_from_layer("ok")
391+
392+
# Verify
393+
self.assertTrue(dialog_main.dlg.line_tool is not None)
394+
self.assertEqual(dialog_main.dlg.routing_fromline_list.count(), 3)
395+
self.assertIsInstance(dialog_main.dlg.rubber_band, QgsRubberBand)
396+
397+
def test_user_cancels_operation(self):
398+
"""Test when user cancels the dialog."""
399+
from ORStools.gui.ORStoolsDialog import ORStoolsDialogMain
400+
401+
dialog_main = ORStoolsDialogMain(IFACE)
402+
dialog_main._init_gui_control()
403+
dialog_main.dlg.load_vertices_from_layer("not_ok")
404+
405+
self.assertTrue(dialog_main.dlg.line_tool is not None)
406+
self.assertEqual(dialog_main.dlg.routing_fromline_list.count(), 0)
407+
self.assertNotIsInstance(dialog_main.dlg.rubber_band, QgsRubberBand)

0 commit comments

Comments
 (0)