Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 9b02c30
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 20 deletions.
40 changes: 36 additions & 4 deletions python/analysis/auto_generated/network/qgsgraph.sip.in
Expand Up @@ -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;
Expand All @@ -163,6 +193,8 @@ Find vertex by associated point
:return: vertex index
%End

protected:

};

/************************************************************************
Expand Down
50 changes: 48 additions & 2 deletions src/analysis/network/qgsgraph.h
Expand Up @@ -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
Expand Down
46 changes: 32 additions & 14 deletions tests/src/python/test_qgsgraph.py
Expand Up @@ -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()
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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.