Skip to content
Permalink
Browse files
Add some unit tests for QgsGraph
  • Loading branch information
nyalldawson committed Nov 9, 2021
1 parent fe075c5 commit 4181ad4ea721e50fd585fc651f8ef1923af08dce
Showing with 109 additions and 0 deletions.
  1. +1 −0 tests/src/python/CMakeLists.txt
  2. +108 −0 tests/src/python/test_qgsgraph.py
@@ -117,6 +117,7 @@ ADD_PYTHON_TEST(PyQgsNominatimGeocoder test_qgsnominatimgeocoder.py)
ADD_PYTHON_TEST(PyQgsGoogleMapsGeocoder test_qgsgooglemapsgeocoder.py)
ADD_PYTHON_TEST(PyQgsGpxProvider test_provider_gpx.py)
ADD_PYTHON_TEST(PyQgsGraduatedSymbolRenderer test_qgsgraduatedsymbolrenderer.py)
ADD_PYTHON_TEST(PyQgsGraph test_qgsgraph.py)
ADD_PYTHON_TEST(PyQgsHashLineSymbolLayer test_qgshashlinesymbollayer.py)
ADD_PYTHON_TEST(PyQgsHighlight test_qgshighlight.py)
ADD_PYTHON_TEST(PyQgsImageCache test_qgsimagecache.py)
@@ -0,0 +1,108 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsGraph.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '08/11/2021'
__copyright__ = 'Copyright 2021, The QGIS Project'

import qgis # NOQA

from qgis.PyQt.QtTest import QSignalSpy
from qgis.core import (
QgsGeocoderInterface,
QgsWkbTypes,
QgsGeocoderResult,
QgsGeometry,
QgsPointXY,
QgsCoordinateReferenceSystem,
QgsLocatorContext,
QgsFeedback,
QgsGeocoderContext,
QgsRectangle
)
from qgis.analysis import (
QgsGraph
)
from qgis.testing import start_app, unittest

start_app()


class TestQgsGraph(unittest.TestCase):

def test_empty_graph(self):
graph = QgsGraph()
self.assertEqual(graph.vertexCount(), 0)
self.assertEqual(graph.edgeCount(), 0)

def test_graph_vertices(self):
graph = QgsGraph()

self.assertEqual(graph.findVertex(QgsPointXY(10, 11)), -1)

vertex_1 = graph.addVertex(QgsPointXY(1, 2))
self.assertEqual(graph.vertexCount(), 1)
self.assertEqual(graph.vertex(vertex_1).point(), QgsPointXY(1, 2))
self.assertFalse(graph.vertex(vertex_1).incomingEdges())
self.assertFalse(graph.vertex(vertex_1).outgoingEdges())

self.assertEqual(graph.findVertex(QgsPointXY(10, 11)), -1)
self.assertEqual(graph.findVertex(QgsPointXY(1, 2)), vertex_1)

vertex_2 = graph.addVertex(QgsPointXY(3, 4))
self.assertEqual(graph.vertexCount(), 2)
self.assertEqual(graph.vertex(vertex_1).point(), QgsPointXY(1, 2))
self.assertFalse(graph.vertex(vertex_1).incomingEdges())
self.assertFalse(graph.vertex(vertex_1).outgoingEdges())
self.assertEqual(graph.vertex(vertex_2).point(), QgsPointXY(3, 4))
self.assertFalse(graph.vertex(vertex_2).incomingEdges())
self.assertFalse(graph.vertex(vertex_2).outgoingEdges())

self.assertEqual(graph.findVertex(QgsPointXY(10, 11)), -1)
self.assertEqual(graph.findVertex(QgsPointXY(1, 2)), vertex_1)
self.assertEqual(graph.findVertex(QgsPointXY(3, 4)), vertex_2)

def test_graph_edges(self):
graph = QgsGraph()
vertex_1 = graph.addVertex(QgsPointXY(1, 2))
vertex_2 = graph.addVertex(QgsPointXY(3, 4))
vertex_3 = graph.addVertex(QgsPointXY(5, 6))

edge_1 = graph.addEdge(vertex_1, vertex_2, [1, 2])
self.assertEqual(graph.edgeCount(), 1)
self.assertEqual(graph.edge(edge_1).cost(0), 1)
self.assertEqual(graph.edge(edge_1).cost(1), 2)
self.assertEqual(graph.edge(edge_1).strategies(), [1, 2])
self.assertEqual(graph.edge(edge_1).fromVertex(), vertex_1)
self.assertEqual(graph.edge(edge_1).toVertex(), vertex_2)

edge_2 = graph.addEdge(vertex_2, vertex_1, [1, 2])
self.assertEqual(graph.edgeCount(), 2)
self.assertEqual(graph.edge(edge_1).fromVertex(), vertex_1)
self.assertEqual(graph.edge(edge_1).toVertex(), vertex_2)
self.assertEqual(graph.edge(edge_2).cost(0), 1)
self.assertEqual(graph.edge(edge_2).cost(1), 2)
self.assertEqual(graph.edge(edge_2).strategies(), [1, 2])
self.assertEqual(graph.edge(edge_2).fromVertex(), vertex_2)
self.assertEqual(graph.edge(edge_2).toVertex(), vertex_1)

edge_3 = graph.addEdge(vertex_3, vertex_1, [11, 12])
self.assertEqual(graph.edgeCount(), 3)
self.assertEqual(graph.edge(edge_1).fromVertex(), vertex_1)
self.assertEqual(graph.edge(edge_1).toVertex(), vertex_2)
self.assertEqual(graph.edge(edge_2).fromVertex(), vertex_2)
self.assertEqual(graph.edge(edge_2).toVertex(), vertex_1)
self.assertEqual(graph.edge(edge_3).cost(0), 11)
self.assertEqual(graph.edge(edge_3).cost(1), 12)
self.assertEqual(graph.edge(edge_3).strategies(), [11, 12])
self.assertEqual(graph.edge(edge_3).fromVertex(), vertex_3)
self.assertEqual(graph.edge(edge_3).toVertex(), vertex_1)


if __name__ == '__main__':
unittest.main()

0 comments on commit 4181ad4

Please sign in to comment.