Skip to content
Permalink
Browse files
Raise IndexError when requesting invalid edge or vertex indices
from QgsGraph, instead of crashing
  • Loading branch information
nyalldawson committed Nov 9, 2021
1 parent d2c997c commit 9b02c301a50875e8d1a3cbc647f723d8af8a9c88
Showing with 116 additions and 20 deletions.
  1. +36 −4 python/analysis/auto_generated/network/qgsgraph.sip.in
  2. +48 −2 src/analysis/network/qgsgraph.h
  3. +32 −14 tests/src/python/test_qgsgraph.py
@@ -141,19 +141,49 @@ to ``toVertexIdx``.
Returns number of graph vertices
%End

const QgsGraphVertex &vertex( int idx ) const;

QgsGraphVertex vertex( int idx ) const;
%Docstring
Returns vertex at given index
Returns the vertex at the given index.

:raises IndexError: if the vertex is not found.
%End
%MethodCode
auto it = sipCpp->mGraphVertices.constFind( a0 );
if ( it != sipCpp->mGraphVertices.constEnd() )
{
return sipConvertFromNewType( new QgsGraphVertex( ( it ).value() ), sipType_QgsGraphVertex, Py_None );
}
else
{
PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
sipIsErr = 1;
}
%End

int edgeCount() const;
%Docstring
Returns number of graph edges
%End

const QgsGraphEdge &edge( int idx ) const;

QgsGraphEdge edge( int idx ) const;
%Docstring
Returns edge at given index
Returns the edge at the given index.

:raises IndexError: if the edge is not found.
%End
%MethodCode
auto it = sipCpp->mGraphEdges.constFind( a0 );
if ( it != sipCpp->mGraphEdges.constEnd() )
{
return sipConvertFromNewType( new QgsGraphEdge( ( it ).value() ), sipType_QgsGraphEdge, Py_None );
}
else
{
PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
sipIsErr = 1;
}
%End

int findVertex( const QgsPointXY &pt ) const;
@@ -163,6 +193,8 @@ Find vertex by associated point
:return: vertex index
%End

protected:

};

/************************************************************************
@@ -165,20 +165,66 @@ class ANALYSIS_EXPORT QgsGraph
*/
int vertexCount() const;

#ifndef SIP_RUN

/**
* Returns vertex at given index
* Returns the vertex at the given index.
*/
const QgsGraphVertex &vertex( int idx ) const;
#else

/**
* Returns the vertex at the given index.
*
* \throws IndexError if the vertex is not found.
*/
QgsGraphVertex vertex( int idx ) const;
% MethodCode
auto it = sipCpp->mGraphVertices.constFind( a0 );
if ( it != sipCpp->mGraphVertices.constEnd() )
{
return sipConvertFromNewType( new QgsGraphVertex( ( it ).value() ), sipType_QgsGraphVertex, Py_None );
}
else
{
PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
sipIsErr = 1;
}
% End
#endif

/**
* Returns number of graph edges
*/
int edgeCount() const;

#ifndef SIP_RUN

/**
* Returns edge at given index
* Returns the edge at the given index.
*/
const QgsGraphEdge &edge( int idx ) const;
#else

/**
* Returns the edge at the given index.
*
* \throws IndexError if the edge is not found.
*/
QgsGraphEdge edge( int idx ) const;
% MethodCode
auto it = sipCpp->mGraphEdges.constFind( a0 );
if ( it != sipCpp->mGraphEdges.constEnd() )
{
return sipConvertFromNewType( new QgsGraphEdge( ( it ).value() ), sipType_QgsGraphEdge, Py_None );
}
else
{
PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
sipIsErr = 1;
}
% End
#endif

/**
* Find vertex by associated point
@@ -11,23 +11,12 @@
__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.core import (
QgsPointXY
)
from qgis.testing import start_app, unittest

start_app()
@@ -45,6 +34,11 @@ def test_graph_vertices(self):

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

with self.assertRaises(IndexError):
graph.vertex(-1)
with self.assertRaises(IndexError):
graph.vertex(0)

vertex_1 = graph.addVertex(QgsPointXY(1, 2))
self.assertEqual(graph.vertexCount(), 1)
self.assertEqual(graph.vertex(vertex_1).point(), QgsPointXY(1, 2))
@@ -54,6 +48,11 @@ def test_graph_vertices(self):
self.assertEqual(graph.findVertex(QgsPointXY(10, 11)), -1)
self.assertEqual(graph.findVertex(QgsPointXY(1, 2)), vertex_1)

with self.assertRaises(IndexError):
graph.vertex(-1)
with self.assertRaises(IndexError):
graph.vertex(2)

vertex_2 = graph.addVertex(QgsPointXY(3, 4))
self.assertEqual(graph.vertexCount(), 2)
self.assertEqual(graph.vertex(vertex_1).point(), QgsPointXY(1, 2))
@@ -63,12 +62,20 @@ def test_graph_vertices(self):
self.assertFalse(graph.vertex(vertex_2).incomingEdges())
self.assertFalse(graph.vertex(vertex_2).outgoingEdges())

with self.assertRaises(IndexError):
graph.vertex(-1)
with self.assertRaises(IndexError):
graph.vertex(3)

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()
with self.assertRaises(IndexError):
graph.edge(0)

vertex_1 = graph.addVertex(QgsPointXY(1, 2))
vertex_2 = graph.addVertex(QgsPointXY(3, 4))
vertex_3 = graph.addVertex(QgsPointXY(5, 6))
@@ -81,6 +88,11 @@ def test_graph_edges(self):
self.assertEqual(graph.edge(edge_1).fromVertex(), vertex_1)
self.assertEqual(graph.edge(edge_1).toVertex(), vertex_2)

with self.assertRaises(IndexError):
graph.edge(-1)
with self.assertRaises(IndexError):
graph.edge(1)

edge_2 = graph.addEdge(vertex_2, vertex_1, [1, 2])
self.assertEqual(graph.edgeCount(), 2)
self.assertEqual(graph.edge(edge_1).fromVertex(), vertex_1)
@@ -91,6 +103,9 @@ def test_graph_edges(self):
self.assertEqual(graph.edge(edge_2).fromVertex(), vertex_2)
self.assertEqual(graph.edge(edge_2).toVertex(), vertex_1)

with self.assertRaises(IndexError):
graph.edge(2)

edge_3 = graph.addEdge(vertex_3, vertex_1, [11, 12])
self.assertEqual(graph.edgeCount(), 3)
self.assertEqual(graph.edge(edge_1).fromVertex(), vertex_1)
@@ -103,6 +118,9 @@ def test_graph_edges(self):
self.assertEqual(graph.edge(edge_3).fromVertex(), vertex_3)
self.assertEqual(graph.edge(edge_3).toVertex(), vertex_1)

with self.assertRaises(IndexError):
graph.edge(3)


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

0 comments on commit 9b02c30

Please sign in to comment.