gis: Fixed #8141; moved Python 2.3 incompatibilities out of the reach of the test suite.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8225 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2008-08-07 01:14:10 +00:00
parent b00e82fe2c
commit c396992c7d
3 changed files with 67 additions and 64 deletions

View File

@ -31,6 +31,8 @@
to a non-existant file location (e.g., `GDAL_LIBRARY_PATH='/null/path'`; to a non-existant file location (e.g., `GDAL_LIBRARY_PATH='/null/path'`;
setting to None/False/'' will not work as a string must be given). setting to None/False/'' will not work as a string must be given).
""" """
import sys
# Attempting to import objects that depend on the GDAL library. The # Attempting to import objects that depend on the GDAL library. The
# HAS_GDAL flag will be set to True if the library is present on # HAS_GDAL flag will be set to True if the library is present on
# the system. # the system.
@ -46,6 +48,8 @@ except:
# The envelope, error, and geomtype modules do not actually require the # The envelope, error, and geomtype modules do not actually require the
# GDAL library. # GDAL library.
from django.contrib.gis.gdal.envelope import Envelope PYTHON23 = sys.version_info[0] == 2 and sys.version_info[1] == 3
from django.contrib.gis.gdal.error import check_err, OGRException, OGRIndexError, SRSException if not PYTHON23:
from django.contrib.gis.gdal.geomtype import OGRGeomType from django.contrib.gis.gdal.envelope import Envelope
from django.contrib.gis.gdal.error import check_err, OGRException, OGRIndexError, SRSException
from django.contrib.gis.gdal.geomtype import OGRGeomType

View File

@ -6,7 +6,7 @@ import re
from django.conf import settings from django.conf import settings
# Checking for the presence of GDAL (needed for the SpatialReference object) # Checking for the presence of GDAL (needed for the SpatialReference object)
from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.gdal import HAS_GDAL, PYTHON23
if HAS_GDAL: if HAS_GDAL:
from django.contrib.gis.gdal import SpatialReference from django.contrib.gis.gdal import SpatialReference
@ -28,7 +28,6 @@ class SpatialRefSysMixin(object):
# distance queries. # distance queries.
units_regex = re.compile(r'.+UNIT ?\["(?P<unit_name>[\w \'\(\)]+)", ?(?P<unit>[\d\.]+)(,AUTHORITY\["(?P<unit_auth_name>[\w \'\(\)]+)","(?P<unit_auth_val>\d+)"\])?\]([\w ]+)?(,AUTHORITY\["(?P<auth_name>[\w \'\(\)]+)","(?P<auth_val>\d+)"\])?\]$') units_regex = re.compile(r'.+UNIT ?\["(?P<unit_name>[\w \'\(\)]+)", ?(?P<unit>[\d\.]+)(,AUTHORITY\["(?P<unit_auth_name>[\w \'\(\)]+)","(?P<unit_auth_val>\d+)"\])?\]([\w ]+)?(,AUTHORITY\["(?P<auth_name>[\w \'\(\)]+)","(?P<auth_val>\d+)"\])?\]$')
@property
def srs(self): def srs(self):
""" """
Returns a GDAL SpatialReference object, if GDAL is installed. Returns a GDAL SpatialReference object, if GDAL is installed.
@ -50,8 +49,8 @@ class SpatialRefSysMixin(object):
raise Exception('Could not get OSR SpatialReference from WKT: %s\nError:\n%s' % (self.wkt, msg)) raise Exception('Could not get OSR SpatialReference from WKT: %s\nError:\n%s' % (self.wkt, msg))
else: else:
raise Exception('GDAL is not installed.') raise Exception('GDAL is not installed.')
srs = property(srs)
@property
def ellipsoid(self): def ellipsoid(self):
""" """
Returns a tuple of the ellipsoid parameters: Returns a tuple of the ellipsoid parameters:
@ -63,47 +62,47 @@ class SpatialRefSysMixin(object):
m = self.spheroid_regex.match(self.wkt) m = self.spheroid_regex.match(self.wkt)
if m: return (float(m.group('major')), float(m.group('flattening'))) if m: return (float(m.group('major')), float(m.group('flattening')))
else: return None else: return None
ellipsoid = property(ellipsoid)
@property
def name(self): def name(self):
"Returns the projection name." "Returns the projection name."
return self.srs.name return self.srs.name
name = property(name)
@property
def spheroid(self): def spheroid(self):
"Returns the spheroid name for this spatial reference." "Returns the spheroid name for this spatial reference."
return self.srs['spheroid'] return self.srs['spheroid']
spheroid = property(spheroid)
@property
def datum(self): def datum(self):
"Returns the datum for this spatial reference." "Returns the datum for this spatial reference."
return self.srs['datum'] return self.srs['datum']
datum = property(datum)
@property
def projected(self): def projected(self):
"Is this Spatial Reference projected?" "Is this Spatial Reference projected?"
if HAS_GDAL: if HAS_GDAL:
return self.srs.projected return self.srs.projected
else: else:
return self.wkt.startswith('PROJCS') return self.wkt.startswith('PROJCS')
projected = property(projected)
@property
def local(self): def local(self):
"Is this Spatial Reference local?" "Is this Spatial Reference local?"
if HAS_GDAL: if HAS_GDAL:
return self.srs.local return self.srs.local
else: else:
return self.wkt.startswith('LOCAL_CS') return self.wkt.startswith('LOCAL_CS')
local = property(local)
@property
def geographic(self): def geographic(self):
"Is this Spatial Reference geographic?" "Is this Spatial Reference geographic?"
if HAS_GDAL: if HAS_GDAL:
return self.srs.geographic return self.srs.geographic
else: else:
return self.wkt.startswith('GEOGCS') return self.wkt.startswith('GEOGCS')
geographic = property(geographic)
@property
def linear_name(self): def linear_name(self):
"Returns the linear units name." "Returns the linear units name."
if HAS_GDAL: if HAS_GDAL:
@ -113,8 +112,8 @@ class SpatialRefSysMixin(object):
else: else:
m = self.units_regex.match(self.wkt) m = self.units_regex.match(self.wkt)
return m.group('unit_name') return m.group('unit_name')
linear_name = property(linear_name)
@property
def linear_units(self): def linear_units(self):
"Returns the linear units." "Returns the linear units."
if HAS_GDAL: if HAS_GDAL:
@ -124,8 +123,8 @@ class SpatialRefSysMixin(object):
else: else:
m = self.units_regex.match(self.wkt) m = self.units_regex.match(self.wkt)
return m.group('unit') return m.group('unit')
linear_units = property(linear_units)
@property
def angular_name(self): def angular_name(self):
"Returns the name of the angular units." "Returns the name of the angular units."
if HAS_GDAL: if HAS_GDAL:
@ -135,8 +134,8 @@ class SpatialRefSysMixin(object):
else: else:
m = self.units_regex.match(self.wkt) m = self.units_regex.match(self.wkt)
return m.group('unit_name') return m.group('unit_name')
angular_name = property(angular_name)
@property
def angular_units(self): def angular_units(self):
"Returns the angular units." "Returns the angular units."
if HAS_GDAL: if HAS_GDAL:
@ -146,8 +145,8 @@ class SpatialRefSysMixin(object):
else: else:
m = self.units_regex.match(self.wkt) m = self.units_regex.match(self.wkt)
return m.group('unit') return m.group('unit')
angular_units = property(angular_units)
@property
def units(self): def units(self):
"Returns a tuple of the units and the name." "Returns a tuple of the units and the name."
if self.projected or self.local: if self.projected or self.local:
@ -156,8 +155,8 @@ class SpatialRefSysMixin(object):
return (self.angular_units, self.angular_name) return (self.angular_units, self.angular_name)
else: else:
return (None, None) return (None, None)
units = property(units)
@classmethod
def get_units(cls, wkt): def get_units(cls, wkt):
""" """
Class method used by GeometryField on initialization to Class method used by GeometryField on initialization to
@ -169,8 +168,8 @@ class SpatialRefSysMixin(object):
else: else:
m = cls.units_regex.match(wkt) m = cls.units_regex.match(wkt)
return m.group('unit'), m.group('unit_name') return m.group('unit'), m.group('unit_name')
get_units = classmethod(get_units)
@classmethod
def get_spheroid(cls, wkt, string=True): def get_spheroid(cls, wkt, string=True):
""" """
Class method used by GeometryField on initialization to Class method used by GeometryField on initialization to
@ -197,6 +196,7 @@ class SpatialRefSysMixin(object):
else: else:
radius, flattening = sphere_params radius, flattening = sphere_params
return 'SPHEROID["%s",%s,%s]' % (sphere_name, radius, flattening) return 'SPHEROID["%s",%s,%s]' % (sphere_name, radius, flattening)
get_spheroid = classmethod(get_spheroid)
def __unicode__(self): def __unicode__(self):
""" """
@ -210,7 +210,7 @@ class SpatialRefSysMixin(object):
# The SpatialRefSys and GeometryColumns models # The SpatialRefSys and GeometryColumns models
_srid_info = True _srid_info = True
if settings.DATABASE_ENGINE == 'postgresql_psycopg2': if not PYTHON23 and settings.DATABASE_ENGINE == 'postgresql_psycopg2':
# Because the PostGIS version is checked when initializing the spatial # Because the PostGIS version is checked when initializing the spatial
# backend a `ProgrammingError` will be raised if the PostGIS tables # backend a `ProgrammingError` will be raised if the PostGIS tables
# and functions are not installed. We catch here so it won't be raised when # and functions are not installed. We catch here so it won't be raised when
@ -220,7 +220,7 @@ if settings.DATABASE_ENGINE == 'postgresql_psycopg2':
from django.contrib.gis.db.backend.postgis.models import GeometryColumns, SpatialRefSys from django.contrib.gis.db.backend.postgis.models import GeometryColumns, SpatialRefSys
except ProgrammingError: except ProgrammingError:
_srid_info = False _srid_info = False
elif settings.DATABASE_ENGINE == 'oracle': elif not PYTHON23 and settings.DATABASE_ENGINE == 'oracle':
# Same thing as above, except the GEOS library is attempted to be loaded for # Same thing as above, except the GEOS library is attempted to be loaded for
# `BaseSpatialBackend`, and an exception will be raised during the # `BaseSpatialBackend`, and an exception will be raised during the
# Django test suite if it doesn't exist. # Django test suite if it doesn't exist.

View File

@ -1,24 +1,30 @@
import sys import sys
from copy import copy from copy import copy
from unittest import TestSuite, TextTestRunner from unittest import TestSuite, TextTestRunner
from django.contrib.gis.gdal import HAS_GDAL
try:
from django.contrib.gis.tests.utils import mysql, oracle, postgis
except:
mysql, oracle, postgis = (False, False, False)
from django.contrib.gis.utils import HAS_GEOIP
from django.conf import settings from django.conf import settings
if not settings._target: settings.configure() if not settings._target: settings.configure()
# Tests that require use of a spatial database (e.g., creation of models) def geo_suite():
test_models = ['geoapp',] """
Builds a test suite for the GIS package. This is not named
`suite` so it will not interfere with the Django test suite (since
spatial database tables are required to execute these tests on
some backends).
"""
from django.contrib.gis.tests.utils import mysql, oracle, postgis
from django.contrib.gis.gdal import HAS_GDAL
from django.contrib.gis.utils import HAS_GEOIP
# Tests that do not require setting up and tearing down a spatial database. # Tests that require use of a spatial database (e.g., creation of models)
test_suite_names = [ test_models = ['geoapp',]
# Tests that do not require setting up and tearing down a spatial database.
test_suite_names = [
'test_geos', 'test_geos',
'test_measure', 'test_measure',
] ]
if HAS_GDAL: if HAS_GDAL:
if oracle: if oracle:
# TODO: There's a problem with `select_related` and GeoQuerySet on # TODO: There's a problem with `select_related` and GeoQuerySet on
# Oracle -- e.g., GeoModel.objects.distance(geom, field_name='fk__point') # Oracle -- e.g., GeoModel.objects.distance(geom, field_name='fk__point')
@ -37,25 +43,17 @@ if HAS_GDAL:
'test_gdal_srs', 'test_gdal_srs',
'test_spatialrefsys', 'test_spatialrefsys',
] ]
else: else:
print >>sys.stderr, "GDAL not available - no GDAL tests will be run." print >>sys.stderr, "GDAL not available - no GDAL tests will be run."
if HAS_GEOIP: if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'):
if hasattr(settings, 'GEOIP_PATH'):
test_suite_names.append('test_geoip') test_suite_names.append('test_geoip')
def geo_suite():
"""
Builds a test suite for the GIS package. This is not named
`suite` so it will not interfere with the Django test suite (since
spatial database tables are required to execute these tests on
some backends).
"""
s = TestSuite() s = TestSuite()
for test_suite in test_suite_names: for test_suite in test_suite_names:
tsuite = getattr(__import__('django.contrib.gis.tests', globals(), locals(), [test_suite]),test_suite) tsuite = getattr(__import__('django.contrib.gis.tests', globals(), locals(), [test_suite]),test_suite)
s.addTest(tsuite.suite()) s.addTest(tsuite.suite())
return s return s, test_models
def run(verbosity=1): def run(verbosity=1):
"Runs the tests that do not require geographic (GEOS, GDAL, etc.) models." "Runs the tests that do not require geographic (GEOS, GDAL, etc.) models."
@ -94,6 +92,7 @@ def run_tests(module_list, verbosity=1, interactive=True):
Finally, the tests may be run by invoking `./manage.py test`. Finally, the tests may be run by invoking `./manage.py test`.
""" """
from django.contrib.gis.db.backend import create_spatial_db from django.contrib.gis.db.backend import create_spatial_db
from django.contrib.gis.tests.utils import mysql
from django.db import connection from django.db import connection
from django.test.utils import destroy_test_db from django.test.utils import destroy_test_db
@ -110,7 +109,7 @@ def run_tests(module_list, verbosity=1, interactive=True):
# Creating the test suite, adding the test models to INSTALLED_APPS, and # Creating the test suite, adding the test models to INSTALLED_APPS, and
# adding the model test suites to our suite package. # adding the model test suites to our suite package.
test_suite = geo_suite() test_suite, test_models = geo_suite()
for test_model in test_models: for test_model in test_models:
module_name = 'django.contrib.gis.tests.%s' % test_model module_name = 'django.contrib.gis.tests.%s' % test_model
if mysql: if mysql: