2014-03-25 15:24:47 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.db import ProgrammingError
|
2023-02-21 07:42:28 +00:00
|
|
|
from django.db.backends.base.base import NO_DB_ALIAS
|
2014-03-25 15:24:47 +00:00
|
|
|
|
2014-03-29 18:24:14 +00:00
|
|
|
try:
|
2020-07-24 06:25:47 +00:00
|
|
|
from django.contrib.gis.db.backends.postgis.operations import PostGISOperations
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2014-03-29 18:24:14 +00:00
|
|
|
HAS_POSTGRES = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_POSTGRES = False
|
2014-03-25 15:24:47 +00:00
|
|
|
|
|
|
|
|
2014-03-29 18:24:14 +00:00
|
|
|
if HAS_POSTGRES:
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class FakeConnection:
|
2014-03-29 18:24:14 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.settings_dict = {
|
|
|
|
"NAME": "test",
|
|
|
|
}
|
2014-03-25 15:24:47 +00:00
|
|
|
|
2014-03-29 18:24:14 +00: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 08:25:42 +00:00
|
|
|
elif func == "version":
|
|
|
|
pass
|
2014-03-25 15:24:47 +00:00
|
|
|
else:
|
2014-03-29 18:24:14 +00:00
|
|
|
raise NotImplementedError("This function was not expected to be called")
|
2014-03-25 15:24:47 +00:00
|
|
|
|
|
|
|
|
2022-12-01 19:23:43 +00:00
|
|
|
@unittest.skipUnless(HAS_POSTGRES, "The psycopg driver is needed for these tests")
|
2016-10-27 07:53:39 +00:00
|
|
|
class TestPostGISVersionCheck(unittest.TestCase):
|
2014-03-25 15:24:47 +00:00
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
The PostGIS version check parses correctly the version numbers
|
2014-03-25 15:24:47 +00: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)
|
|
|
|
|
2017-02-15 16:14:21 +00:00
|
|
|
def test_version_loose_tuple(self):
|
|
|
|
expect = ("1.2.3b1.dev0", 1, 2, 3)
|
|
|
|
ops = FakePostGISOperations(expect[0])
|
|
|
|
actual = ops.postgis_version_tuple()
|
|
|
|
self.assertEqual(expect, actual)
|
|
|
|
|
2014-03-25 15:24:47 +00:00
|
|
|
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:
|
2017-03-07 21:00:43 +00:00
|
|
|
with self.subTest(version=version):
|
|
|
|
ops = FakePostGISOperations(version[0])
|
|
|
|
actual = ops.spatial_version
|
|
|
|
self.assertEqual(version[1:], actual)
|
2014-03-25 15:24:47 +00:00
|
|
|
|
|
|
|
def test_no_version_number(self):
|
|
|
|
ops = FakePostGISOperations()
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
ops.spatial_version
|
2023-02-21 07:42:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(HAS_POSTGRES, "PostGIS-specific tests.")
|
|
|
|
class TestPostGISBackend(unittest.TestCase):
|
|
|
|
def test_non_db_connection_classes(self):
|
|
|
|
from django.contrib.gis.db.backends.postgis.base import DatabaseWrapper
|
|
|
|
from django.db.backends.postgresql.features import DatabaseFeatures
|
|
|
|
from django.db.backends.postgresql.introspection import DatabaseIntrospection
|
|
|
|
from django.db.backends.postgresql.operations import DatabaseOperations
|
|
|
|
|
|
|
|
wrapper = DatabaseWrapper(settings_dict={}, alias=NO_DB_ALIAS)
|
|
|
|
# PostGIS-specific stuff is not initialized for non-db connections.
|
|
|
|
self.assertIs(wrapper.features_class, DatabaseFeatures)
|
|
|
|
self.assertIs(wrapper.ops_class, DatabaseOperations)
|
|
|
|
self.assertIs(wrapper.introspection_class, DatabaseIntrospection)
|