Skip to content

Commit 57238f3

Browse files
committed
feat: tests for load vertices button
1 parent 5261aab commit 57238f3

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-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: 53 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

@@ -22,6 +30,11 @@
2230

2331
@pytest.mark.filterwarnings("ignore:.*imp module is deprecated.*")
2432
class TestGui(unittest.TestCase):
33+
def tearDown(self):
34+
"""Run after each test"""
35+
# Clean up layers
36+
QgsProject.instance().removeAllMapLayers()
37+
2538
def test_without_live_preview(self):
2639
from ORStools.gui.ORStoolsDialog import ORStoolsDialog
2740
from ORStools.gui.ORStoolsDialogConfig import ORStoolsDialogConfigMain
@@ -358,3 +371,42 @@ def test_ORStoolsDialogConfig_url(self):
358371
"POINT(8.67251100000000008 49.39887900000000087)",
359372
next(layer.getFeatures()).geometry().asPolyline()[0].asWkt(),
360373
)
374+
375+
def test_load_valid_point_layer(self):
376+
"""Test loading vertices from valid point layer."""
377+
from ORStools.gui.ORStoolsDialog import ORStoolsDialogMain
378+
379+
dialog_main = ORStoolsDialogMain(IFACE)
380+
dialog_main._init_gui_control()
381+
382+
# Create test layers
383+
self.point_layer = QgsVectorLayer("Point?crs=EPSG:4326", "test_points", "memory")
384+
self.line_layer = QgsVectorLayer("LineString?crs=EPSG:4326", "test_lines", "memory")
385+
386+
# Add 3 features to point layer
387+
for coords in [(1, 2), (3, 4), (5, 6)]:
388+
feat = QgsFeature()
389+
feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(*coords)))
390+
self.point_layer.dataProvider().addFeature(feat)
391+
392+
QgsProject.instance().addMapLayers([self.point_layer, self.line_layer])
393+
394+
# Run test
395+
dialog_main.dlg.load_vertices_from_layer("ok")
396+
397+
# Verify
398+
self.assertTrue(dialog_main.dlg.line_tool is not None)
399+
self.assertEqual(dialog_main.dlg.routing_fromline_list.count(), 3)
400+
self.assertIsInstance(dialog_main.dlg.rubber_band, QgsRubberBand)
401+
402+
def test_user_cancels_operation(self):
403+
"""Test when user cancels the dialog."""
404+
from ORStools.gui.ORStoolsDialog import ORStoolsDialogMain
405+
406+
dialog_main = ORStoolsDialogMain(IFACE)
407+
dialog_main._init_gui_control()
408+
dialog_main.dlg.load_vertices_from_layer("not_ok")
409+
410+
self.assertTrue(dialog_main.dlg.line_tool is not None)
411+
self.assertEqual(dialog_main.dlg.routing_fromline_list.count(), 0)
412+
self.assertNotIsInstance(dialog_main.dlg.rubber_band, QgsRubberBand)

0 commit comments

Comments
 (0)