From f38c3cbadcb2b1bf77057cdbe89b06ca7bb71016 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 26 Jan 2022 17:47:03 +0100 Subject: [PATCH] Increased test coverage for django.contrib.gis.gdal.layer.Layer. --- tests/gis_tests/gdal_tests/test_ds.py | 20 ++++++++++++++++++-- tests/gis_tests/test_data.py | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/gis_tests/gdal_tests/test_ds.py b/tests/gis_tests/gdal_tests/test_ds.py index f2a6ddf101..2632e817b4 100644 --- a/tests/gis_tests/gdal_tests/test_ds.py +++ b/tests/gis_tests/gdal_tests/test_ds.py @@ -9,6 +9,7 @@ from django.contrib.gis.gdal import ( from django.contrib.gis.gdal.field import ( OFTDateTime, OFTInteger, OFTReal, OFTString, ) +from django.contrib.gis.geos import GEOSGeometry from django.test import SimpleTestCase from ..test_data import TEST_DATA, TestDS, get_ds_file @@ -139,6 +140,8 @@ class DataSourceTest(SimpleTestCase): # Incrementing through each layer, this tests DataSource.__iter__ for layer in ds: + self.assertEqual(layer.name, source.name) + self.assertEqual(str(layer), source.name) # Making sure we get the number of features we expect self.assertEqual(len(layer), source.nfeat) @@ -254,9 +257,15 @@ class DataSourceTest(SimpleTestCase): # Incrementing through each layer and feature. for layer in ds: - for feat in layer: + geoms = layer.get_geoms() + geos_geoms = layer.get_geoms(geos=True) + self.assertEqual(len(geoms), len(geos_geoms)) + self.assertEqual(len(geoms), len(layer)) + for feat, geom, geos_geom in zip(layer, geoms, geos_geoms): g = feat.geom - + self.assertEqual(geom, g) + self.assertIsInstance(geos_geom, GEOSGeometry) + self.assertEqual(g, geos_geom.ogr) # Making sure we get the right Geometry name & type self.assertEqual(source.geom, g.geom_name) self.assertEqual(source.gtype, g.geom_type) @@ -314,3 +323,10 @@ class DataSourceTest(SimpleTestCase): feat = ds[0][0] # Reference value obtained using `ogrinfo`. self.assertEqual(676586997978, feat.get('ALAND10')) + + def test_nonexistent_field(self): + source = ds_list[0] + ds = DataSource(source.ds) + msg = 'invalid field name: nonexistent' + with self.assertRaisesMessage(GDALException, msg): + ds[0].get_fields('nonexistent') diff --git a/tests/gis_tests/test_data.py b/tests/gis_tests/test_data.py index dca847e8cb..ab6e13f558 100644 --- a/tests/gis_tests/test_data.py +++ b/tests/gis_tests/test_data.py @@ -45,6 +45,7 @@ class TestDS(TestObj): """ def __init__(self, name, *, ext='shp', **kwargs): # Shapefile is default extension, unless specified otherwise. + self.name = name self.ds = get_ds_file(name, ext) super().__init__(**kwargs)