2014-05-17 20:59:06 +08:00
|
|
|
import sys
|
2014-03-25 23:24:47 +08:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.db import ProgrammingError
|
2014-05-17 20:59:06 +08:00
|
|
|
from django.utils import six
|
2014-03-25 23:24:47 +08:00
|
|
|
|
2014-03-30 02:24:14 +08:00
|
|
|
try:
|
|
|
|
from django.contrib.gis.db.backends.postgis.operations import PostGISOperations
|
|
|
|
HAS_POSTGRES = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_POSTGRES = False
|
2014-05-17 20:59:06 +08:00
|
|
|
except ImproperlyConfigured as e:
|
|
|
|
# If psycopg is installed but not geos, the import path hits
|
|
|
|
# django.contrib.gis.geometry.backend which will "helpfully" convert
|
|
|
|
# an ImportError into an ImproperlyConfigured.
|
|
|
|
# Here, we make sure we're only catching this specific case and not another
|
|
|
|
# ImproperlyConfigured one.
|
|
|
|
if e.args and e.args[0].startswith('Could not import user-defined GEOMETRY_BACKEND'):
|
|
|
|
HAS_POSTGRES = False
|
|
|
|
else:
|
|
|
|
six.reraise(*sys.exc_info())
|
2014-03-25 23:24:47 +08:00
|
|
|
|
|
|
|
|
2014-03-30 02:24:14 +08:00
|
|
|
if HAS_POSTGRES:
|
2017-01-19 15:39:46 +08:00
|
|
|
class FakeConnection:
|
2014-03-30 02:24:14 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.settings_dict = {
|
|
|
|
'NAME': 'test',
|
|
|
|
}
|
2014-03-25 23:24:47 +08:00
|
|
|
|
2014-03-30 02:24:14 +08:00
|
|
|
class FakePostGISOperations(PostGISOperations):
|
|
|
|
def __init__(self, version=None):
|
|
|
|
self.version = version
|
|
|
|
self.connection = FakeConnection()
|
|
|
|
|
|
|
|
def _get_postgis_func(self, func):
|
|
|
|
if func == 'postgis_lib_version':
|
|
|
|
if self.version is None:
|
|
|
|
raise ProgrammingError
|
|
|
|
else:
|
|
|
|
return self.version
|
2015-06-06 16:25:42 +08:00
|
|
|
elif func == 'version':
|
|
|
|
pass
|
2014-03-25 23:24:47 +08:00
|
|
|
else:
|
2014-03-30 02:24:14 +08:00
|
|
|
raise NotImplementedError('This function was not expected to be called')
|
2014-03-25 23:24:47 +08:00
|
|
|
|
|
|
|
|
2014-03-30 02:24:14 +08:00
|
|
|
@unittest.skipUnless(HAS_POSTGRES, "The psycopg2 driver is needed for these tests")
|
2016-10-27 15:53:39 +08:00
|
|
|
class TestPostGISVersionCheck(unittest.TestCase):
|
2014-03-25 23:24:47 +08:00
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The PostGIS version check parses correctly the version numbers
|
2014-03-25 23:24:47 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_get_version(self):
|
|
|
|
expect = '1.0.0'
|
|
|
|
ops = FakePostGISOperations(expect)
|
|
|
|
actual = ops.postgis_lib_version()
|
|
|
|
self.assertEqual(expect, actual)
|
|
|
|
|
|
|
|
def test_version_classic_tuple(self):
|
|
|
|
expect = ('1.2.3', 1, 2, 3)
|
|
|
|
ops = FakePostGISOperations(expect[0])
|
|
|
|
actual = ops.postgis_version_tuple()
|
|
|
|
self.assertEqual(expect, actual)
|
|
|
|
|
|
|
|
def test_version_dev_tuple(self):
|
|
|
|
expect = ('1.2.3dev', 1, 2, 3)
|
|
|
|
ops = FakePostGISOperations(expect[0])
|
|
|
|
actual = ops.postgis_version_tuple()
|
|
|
|
self.assertEqual(expect, actual)
|
|
|
|
|
|
|
|
def test_valid_version_numbers(self):
|
|
|
|
versions = [
|
|
|
|
('1.3.0', 1, 3, 0),
|
|
|
|
('2.1.1', 2, 1, 1),
|
|
|
|
('2.2.0dev', 2, 2, 0),
|
|
|
|
]
|
|
|
|
|
|
|
|
for version in versions:
|
|
|
|
ops = FakePostGISOperations(version[0])
|
|
|
|
actual = ops.spatial_version
|
|
|
|
self.assertEqual(version[1:], actual)
|
|
|
|
|
|
|
|
def test_invalid_version_numbers(self):
|
|
|
|
versions = ['nope', '123']
|
|
|
|
|
|
|
|
for version in versions:
|
|
|
|
ops = FakePostGISOperations(version)
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(Exception):
|
|
|
|
ops.spatial_version
|
2014-03-25 23:24:47 +08:00
|
|
|
|
|
|
|
def test_no_version_number(self):
|
|
|
|
ops = FakePostGISOperations()
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
ops.spatial_version
|
2016-11-13 05:18:22 +08:00
|
|
|
|
|
|
|
def test_version_dependent_funcs(self):
|
|
|
|
"""
|
|
|
|
Resolve names of functions renamed and deprecated in PostGIS 2.2.0
|
|
|
|
depending on PostGIS version.
|
|
|
|
Remove when dropping support for PostGIS 2.1.
|
|
|
|
"""
|
|
|
|
ops = FakePostGISOperations('2.2.0')
|
|
|
|
self.assertEqual(ops.spatial_function_name('DistanceSphere'), 'ST_DistanceSphere')
|
|
|
|
self.assertEqual(ops.spatial_function_name('DistanceSpheroid'), 'ST_DistanceSpheroid')
|
|
|
|
self.assertEqual(ops.spatial_function_name('LengthSpheroid'), 'ST_LengthSpheroid')
|
|
|
|
self.assertEqual(ops.spatial_function_name('MemSize'), 'ST_MemSize')
|
|
|
|
|
|
|
|
ops = FakePostGISOperations('2.1.0')
|
|
|
|
self.assertEqual(ops.spatial_function_name('DistanceSphere'), 'ST_distance_sphere')
|
|
|
|
self.assertEqual(ops.spatial_function_name('DistanceSpheroid'), 'ST_distance_spheroid')
|
|
|
|
self.assertEqual(ops.spatial_function_name('LengthSpheroid'), 'ST_length_spheroid')
|
|
|
|
self.assertEqual(ops.spatial_function_name('MemSize'), 'ST_mem_size')
|