Removed hardcoded paths to SpatialRefSys models in tests.

This commit is contained in:
Tim Graham 2020-09-14 01:58:05 -04:00 committed by GitHub
parent a7a4ff1026
commit 71ae1ab012
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 23 deletions

View File

@ -1,8 +1,10 @@
import re import re
from django.db import connection
from django.test import TestCase, skipUnlessDBFeature from django.test import TestCase, skipUnlessDBFeature
from django.utils.functional import cached_property
from .utils import SpatialRefSys, oracle, postgis, spatialite from .utils import oracle, postgis, spatialite
test_srs = ({ test_srs = ({
'srid': 4326, 'srid': 4326,
@ -51,9 +53,13 @@ test_srs = ({
@skipUnlessDBFeature("has_spatialrefsys_table") @skipUnlessDBFeature("has_spatialrefsys_table")
class SpatialRefSysTest(TestCase): class SpatialRefSysTest(TestCase):
@cached_property
def SpatialRefSys(self):
return connection.ops.connection.ops.spatial_ref_sys()
def test_get_units(self): def test_get_units(self):
epsg_4326 = next(f for f in test_srs if f['srid'] == 4326) epsg_4326 = next(f for f in test_srs if f['srid'] == 4326)
unit, unit_name = SpatialRefSys().get_units(epsg_4326['wkt']) unit, unit_name = self.SpatialRefSys().get_units(epsg_4326['wkt'])
self.assertEqual(unit_name, 'degree') self.assertEqual(unit_name, 'degree')
self.assertAlmostEqual(unit, 0.01745329251994328) self.assertAlmostEqual(unit, 0.01745329251994328)
@ -62,7 +68,7 @@ class SpatialRefSysTest(TestCase):
Test retrieval of SpatialRefSys model objects. Test retrieval of SpatialRefSys model objects.
""" """
for sd in test_srs: for sd in test_srs:
srs = SpatialRefSys.objects.get(srid=sd['srid']) srs = self.SpatialRefSys.objects.get(srid=sd['srid'])
self.assertEqual(sd['srid'], srs.srid) self.assertEqual(sd['srid'], srs.srid)
# Some of the authority names are borked on Oracle, e.g., SRID=32140. # Some of the authority names are borked on Oracle, e.g., SRID=32140.
@ -84,7 +90,7 @@ class SpatialRefSysTest(TestCase):
Test getting OSR objects from SpatialRefSys model objects. Test getting OSR objects from SpatialRefSys model objects.
""" """
for sd in test_srs: for sd in test_srs:
sr = SpatialRefSys.objects.get(srid=sd['srid']) sr = self.SpatialRefSys.objects.get(srid=sd['srid'])
self.assertTrue(sr.spheroid.startswith(sd['spheroid'])) self.assertTrue(sr.spheroid.startswith(sd['spheroid']))
self.assertEqual(sd['geographic'], sr.geographic) self.assertEqual(sd['geographic'], sr.geographic)
self.assertEqual(sd['projected'], sr.projected) self.assertEqual(sd['projected'], sr.projected)
@ -110,7 +116,7 @@ class SpatialRefSysTest(TestCase):
prec = sd['eprec'] prec = sd['eprec']
# Getting our spatial reference and its ellipsoid # Getting our spatial reference and its ellipsoid
srs = SpatialRefSys.objects.get(srid=sd['srid']) srs = self.SpatialRefSys.objects.get(srid=sd['srid'])
ellps2 = srs.ellipsoid ellps2 = srs.ellipsoid
for i in range(3): for i in range(3):
@ -126,9 +132,9 @@ class SpatialRefSysTest(TestCase):
add_srs_entry(3857) add_srs_entry(3857)
self.assertTrue( self.assertTrue(
SpatialRefSys.objects.filter(srid=3857).exists() self.SpatialRefSys.objects.filter(srid=3857).exists()
) )
srs = SpatialRefSys.objects.get(srid=3857) srs = self.SpatialRefSys.objects.get(srid=3857)
self.assertTrue( self.assertTrue(
SpatialRefSys.get_spheroid(srs.wkt).startswith('SPHEROID[') self.SpatialRefSys.get_spheroid(srs.wkt).startswith('SPHEROID[')
) )

View File

@ -52,21 +52,6 @@ spatialite = _default_db == 'spatialite'
# MySQL spatial indices can't handle NULL geometries. # MySQL spatial indices can't handle NULL geometries.
gisfield_may_be_null = not mysql gisfield_may_be_null = not mysql
if oracle and 'gis' in settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE']:
from django.contrib.gis.db.backends.oracle.models import (
OracleSpatialRefSys as SpatialRefSys,
)
elif postgis:
from django.contrib.gis.db.backends.postgis.models import (
PostGISSpatialRefSys as SpatialRefSys,
)
elif spatialite:
from django.contrib.gis.db.backends.spatialite.models import (
SpatialiteSpatialRefSys as SpatialRefSys,
)
else:
SpatialRefSys = None
class FuncTestMixin: class FuncTestMixin:
"""Assert that Func expressions aren't mutated during their as_sql().""" """Assert that Func expressions aren't mutated during their as_sql()."""