From 66657eb01f36081f33d847390e4f7034ff3e9f52 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Sat, 2 Sep 2017 08:38:17 +0100 Subject: Improved messages in IndexErrors raised by GDAL objects. --- tests/gis_tests/gdal_tests/test_ds.py | 24 ++++++++++++++++-------- tests/gis_tests/gdal_tests/test_geom.py | 16 +++++++++++----- 2 files changed, 27 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/tests/gis_tests/gdal_tests/test_ds.py b/tests/gis_tests/gdal_tests/test_ds.py index 0d936ee036..34c3953fd5 100644 --- a/tests/gis_tests/gdal_tests/test_ds.py +++ b/tests/gis_tests/gdal_tests/test_ds.py @@ -1,11 +1,11 @@ import os import re -import unittest from django.contrib.gis.gdal import ( GDAL_VERSION, DataSource, Envelope, GDALException, OGRGeometry, ) from django.contrib.gis.gdal.field import OFTInteger, OFTReal, OFTString +from django.test import SimpleTestCase from ..test_data import TEST_DATA, TestDS, get_ds_file @@ -64,7 +64,7 @@ ds_list = ( bad_ds = (TestDS('foo'),) -class DataSourceTest(unittest.TestCase): +class DataSourceTest(SimpleTestCase): def test01_valid_shp(self): "Testing valid SHP Data Source files." @@ -83,11 +83,12 @@ class DataSourceTest(unittest.TestCase): self.assertEqual(source.driver, str(ds.driver)) # Making sure indexing works - with self.assertRaises(IndexError): - ds[len(ds)] + msg = 'Index out of range when accessing layers in a datasource: %s.' + with self.assertRaisesMessage(IndexError, msg % len(ds)): + ds.__getitem__(len(ds)) - with self.assertRaises(IndexError): - ds['invalid'] + with self.assertRaisesMessage(IndexError, 'Invalid OGR layer name given: invalid.'): + ds.__getitem__('invalid') def test02_invalid_shp(self): "Testing invalid SHP files for the Data Source." @@ -122,9 +123,9 @@ class DataSourceTest(unittest.TestCase): self.assertIn(f, source.fields) # Negative FIDs are not allowed. - with self.assertRaises(IndexError): + with self.assertRaisesMessage(IndexError, 'Negative indices are not allowed on OGR Layers.'): layer.__getitem__(-1) - with self.assertRaises(IndexError): + with self.assertRaisesMessage(IndexError, 'Invalid feature id: 50000.'): layer.__getitem__(50000) if hasattr(source, 'field_values'): @@ -141,6 +142,13 @@ class DataSourceTest(unittest.TestCase): for fld_name, fld_value in source.field_values.items(): self.assertEqual(fld_value[i], feat.get(fld_name)) + msg = 'Index out of range when accessing field in a feature: %s.' + with self.assertRaisesMessage(IndexError, msg % len(feat)): + feat.__getitem__(len(feat)) + + with self.assertRaisesMessage(IndexError, 'Invalid OFT field name given: invalid.'): + feat.__getitem__('invalid') + def test03b_layer_slice(self): "Test indexing and slicing on Layers." # Using the first data-source because the same slice diff --git a/tests/gis_tests/gdal_tests/test_geom.py b/tests/gis_tests/gdal_tests/test_geom.py index aa1b371751..c7a0872de8 100644 --- a/tests/gis_tests/gdal_tests/test_geom.py +++ b/tests/gis_tests/gdal_tests/test_geom.py @@ -1,6 +1,5 @@ import json import pickle -import unittest from binascii import b2a_hex from django.contrib.gis.gdal import ( @@ -8,11 +7,12 @@ from django.contrib.gis.gdal import ( ) from django.template import Context from django.template.engine import Engine +from django.test import SimpleTestCase from ..test_data import TestDataMixin -class OGRGeomTest(unittest.TestCase, TestDataMixin): +class OGRGeomTest(SimpleTestCase, TestDataMixin): "This tests the OGR Geometry." def test_geomtype(self): @@ -158,7 +158,8 @@ class OGRGeomTest(unittest.TestCase, TestDataMixin): self.assertEqual(ls.coords, linestr.tuple) self.assertEqual(linestr, OGRGeometry(ls.wkt)) self.assertNotEqual(linestr, prev) - with self.assertRaises(IndexError): + msg = 'Index out of range when accessing points of a line string: %s.' + with self.assertRaisesMessage(IndexError, msg % len(linestr)): linestr.__getitem__(len(linestr)) prev = linestr @@ -183,7 +184,8 @@ class OGRGeomTest(unittest.TestCase, TestDataMixin): for ls in mlinestr: self.assertEqual(2, ls.geom_type) self.assertEqual('LINESTRING', ls.geom_name) - with self.assertRaises(IndexError): + msg = 'Index out of range when accessing geometry in a collection: %s.' + with self.assertRaisesMessage(IndexError, msg % len(mlinestr)): mlinestr.__getitem__(len(mlinestr)) def test_linearring(self): @@ -213,6 +215,9 @@ class OGRGeomTest(unittest.TestCase, TestDataMixin): self.assertEqual('POLYGON', poly.geom_name) self.assertEqual(p.n_p, poly.point_count) self.assertEqual(p.n_i + 1, len(poly)) + msg = 'Index out of range when accessing rings of a polygon: %s.' + with self.assertRaisesMessage(IndexError, msg % len(poly)): + poly.__getitem__(len(poly)) # Testing area & centroid. self.assertAlmostEqual(p.area, poly.area, 9) @@ -263,7 +268,8 @@ class OGRGeomTest(unittest.TestCase, TestDataMixin): if mp.valid: self.assertEqual(mp.n_p, mpoly.point_count) self.assertEqual(mp.num_geom, len(mpoly)) - with self.assertRaises(IndexError): + msg = 'Index out of range when accessing geometry in a collection: %s.' + with self.assertRaisesMessage(IndexError, msg % len(mpoly)): mpoly.__getitem__(len(mpoly)) for p in mpoly: self.assertEqual('POLYGON', p.geom_name) -- cgit v1.3