diff --git a/python/grass/pygrass/vector/geometry.py b/python/grass/pygrass/vector/geometry.py index 63154984b34..968629776f2 100644 --- a/python/grass/pygrass/vector/geometry.py +++ b/python/grass/pygrass/vector/geometry.py @@ -1888,7 +1888,8 @@ def c_read_next_line(c_mapinfo, c_points, c_cats): if ftype == -2: raise StopIteration if ftype == -1: - raise + msg = "Cannot read the next line" + raise GrassError(msg) return ftype, v_id, c_points, c_cats diff --git a/python/grass/pygrass/vector/testsuite/test_geometry.py b/python/grass/pygrass/vector/testsuite/test_geometry.py index 6e164ca7779..a31eca7f7c3 100644 --- a/python/grass/pygrass/vector/testsuite/test_geometry.py +++ b/python/grass/pygrass/vector/testsuite/test_geometry.py @@ -5,15 +5,17 @@ """ import unittest +from unittest.mock import MagicMock, patch import numpy as np import grass.lib.vector as libvect +from grass.exceptions import GrassError from grass.gunittest.case import TestCase from grass.gunittest.main import test from grass.pygrass.vector import VectorTopo from grass.pygrass.vector.basic import Bbox -from grass.pygrass.vector.geometry import Area, Line, Node, Point +from grass.pygrass.vector.geometry import Area, Line, Node, Point, c_read_next_line class PointTestCase(TestCase): @@ -390,5 +392,33 @@ def test_isles_2(self): self.assertEqual(str(isle.bbox()), "Bbox(3.0, 1.0, 3.0, 1.0)") +class ReadNextLineTestCase(TestCase): + def test_c_read_next_line_error(self): + """Test c_read_next_line raises GrassError when Vect_read_next_line returns -1""" + mock_mapinfo = MagicMock() + mock_mapinfo.contents.next_line = 1 + with ( + patch( + "grass.pygrass.vector.geometry.libvect.Vect_read_next_line", + return_value=-1, + ), + self.assertRaises(GrassError), + ): + c_read_next_line(mock_mapinfo, None, None) + + def test_c_read_next_line_stop_iteration(self): + """Test c_read_next_line raises StopIteration when Vect_read_next_line returns -2""" + mock_mapinfo = MagicMock() + mock_mapinfo.contents.next_line = 1 + with ( + patch( + "grass.pygrass.vector.geometry.libvect.Vect_read_next_line", + return_value=-2, + ), + self.assertRaises(StopIteration), + ): + c_read_next_line(mock_mapinfo, None, None) + + if __name__ == "__main__": test()