summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Pope <nick.pope@flightdataservices.com>2017-09-02 08:38:17 +0100
committerTim Graham <timograham@gmail.com>2017-09-04 19:00:32 -0400
commit66657eb01f36081f33d847390e4f7034ff3e9f52 (patch)
treee02a4d4ed4705516fd18ae5429cff0839c7f5654
parent0d9e1163e897d8e43a3e018938a58131efcbc135 (diff)
downloaddjango-66657eb01f36081f33d847390e4f7034ff3e9f52.tar.gz
Improved messages in IndexErrors raised by GDAL objects.
-rw-r--r--django/contrib/gis/gdal/datasource.py4
-rw-r--r--django/contrib/gis/gdal/feature.py4
-rw-r--r--django/contrib/gis/gdal/geometries.py6
-rw-r--r--tests/gis_tests/gdal_tests/test_ds.py24
-rw-r--r--tests/gis_tests/gdal_tests/test_geom.py16
5 files changed, 34 insertions, 20 deletions
diff --git a/django/contrib/gis/gdal/datasource.py b/django/contrib/gis/gdal/datasource.py
index 9890a5ab51..d3009c234f 100644
--- a/django/contrib/gis/gdal/datasource.py
+++ b/django/contrib/gis/gdal/datasource.py
@@ -89,12 +89,12 @@ class DataSource(GDALBase):
if isinstance(index, str):
layer = capi.get_layer_by_name(self.ptr, force_bytes(index))
if not layer:
- raise IndexError('invalid OGR Layer name given: "%s"' % index)
+ raise IndexError('Invalid OGR layer name given: %s.' % index)
elif isinstance(index, int):
if 0 <= index < self.layer_count:
layer = capi.get_layer(self._ptr, index)
else:
- raise IndexError('index out of range')
+ raise IndexError('Index out of range when accessing layers in a datasource: %s.' % index)
else:
raise TypeError('Invalid index type: %s' % type(index))
return Layer(layer, self)
diff --git a/django/contrib/gis/gdal/feature.py b/django/contrib/gis/gdal/feature.py
index 9a398803fe..db8c981312 100644
--- a/django/contrib/gis/gdal/feature.py
+++ b/django/contrib/gis/gdal/feature.py
@@ -38,7 +38,7 @@ class Feature(GDALBase):
elif 0 <= index < self.num_fields:
i = index
else:
- raise IndexError('index out of range')
+ raise IndexError('Index out of range when accessing field in a feature: %s.' % index)
return Field(self, i)
def __len__(self):
@@ -111,5 +111,5 @@ class Feature(GDALBase):
"Return the index of the given field name."
i = capi.get_field_index(self.ptr, force_bytes(field_name))
if i < 0:
- raise IndexError('invalid OFT field name given: "%s"' % field_name)
+ raise IndexError('Invalid OFT field name given: %s.' % field_name)
return i
diff --git a/django/contrib/gis/gdal/geometries.py b/django/contrib/gis/gdal/geometries.py
index f2ca1345ed..7b304d9af0 100644
--- a/django/contrib/gis/gdal/geometries.py
+++ b/django/contrib/gis/gdal/geometries.py
@@ -564,7 +564,7 @@ class LineString(OGRGeometry):
elif dim == 3:
return (x.value, y.value, z.value)
else:
- raise IndexError('index out of range: %s' % index)
+ raise IndexError('Index out of range when accessing points of a line string: %s.' % index)
def __len__(self):
"Return the number of points in the LineString."
@@ -616,7 +616,7 @@ class Polygon(OGRGeometry):
if 0 <= index < self.geom_count:
return OGRGeometry(capi.clone_geom(capi.get_geom_ref(self.ptr, index)), self.srs)
else:
- raise IndexError('index out of range: %s' % index)
+ raise IndexError('Index out of range when accessing rings of a polygon: %s.' % index)
# Polygon Properties
@property
@@ -655,7 +655,7 @@ class GeometryCollection(OGRGeometry):
if 0 <= index < self.geom_count:
return OGRGeometry(capi.clone_geom(capi.get_geom_ref(self.ptr, index)), self.srs)
else:
- raise IndexError('index out of range: %s' % index)
+ raise IndexError('Index out of range when accessing geometry in a collection: %s.' % index)
def __len__(self):
"Return the number of geometries in this Geometry Collection."
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)