File: tests.py

package info (click to toggle)
python-django 1%3A1.11.29-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,428 kB
  • sloc: python: 220,776; javascript: 13,523; makefile: 209; xml: 201; sh: 64
file content (114 lines) | stat: -rw-r--r-- 4,177 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
import unittest

from django.core.exceptions import ImproperlyConfigured
from django.db import ProgrammingError
from django.utils import six

try:
    from django.contrib.gis.db.backends.postgis.operations import PostGISOperations
    HAS_POSTGRES = True
except ImportError:
    HAS_POSTGRES = False
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())


if HAS_POSTGRES:
    class FakeConnection(object):
        def __init__(self):
            self.settings_dict = {
                'NAME': 'test',
            }

    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
            elif func == 'version':
                pass
            else:
                raise NotImplementedError('This function was not expected to be called')


@unittest.skipUnless(HAS_POSTGRES, "The psycopg2 driver is needed for these tests")
class TestPostGISVersionCheck(unittest.TestCase):
    """
    The PostGIS version check parses correctly the version numbers
    """

    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)
            with self.assertRaises(Exception):
                ops.spatial_version

    def test_no_version_number(self):
        ops = FakePostGISOperations()
        with self.assertRaises(ImproperlyConfigured):
            ops.spatial_version

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