Increased test coverage for django.contrib.gis.gdal.layer.Layer.

This commit is contained in:
Mariusz Felisiak 2022-01-26 17:47:03 +01:00 committed by GitHub
parent f97401d1b1
commit f38c3cbadc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -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')

View File

@ -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)