Debian Codegraph @

tests/gis_tests/tests.py

raw
1 import unittest
2
3 from django.core.exceptions import ImproperlyConfigured
4 from django.db import ProgrammingError
5 from django.db.backends.base.base import NO_DB_ALIAS
6
7 try:
8 from django.contrib.gis.db.backends.postgis.operations import PostGISOperations
9
10 HAS_POSTGRES = True
11 except ImportError:
12 HAS_POSTGRES = False
13
14
15 if HAS_POSTGRES:
16
17 class FakeConnection:
18 def __init__(self):
19 self.settings_dict = {
20 "NAME": "test",
21 }
22
23 class FakePostGISOperations(PostGISOperations):
24 def __init__(self, version=None):
25 self.version = version
26 self.connection = FakeConnection()
27
28 def _get_postgis_func(self, func):
29 if func == "postgis_lib_version":
30 if self.version is None:
31 raise ProgrammingError
32 else:
33 return self.version
34 elif func == "version":
35 pass
36 else:
37 raise NotImplementedError("This function was not expected to be called")
38
39
40 @unittest.skipUnless(HAS_POSTGRES, "The psycopg driver is needed for these tests")
41 class TestPostGISVersionCheck(unittest.TestCase):
42 """
43 The PostGIS version check parses correctly the version numbers
44 """
45
46 def test_get_version(self):
47 expect = "1.0.0"
48 ops = FakePostGISOperations(expect)
49 actual = ops.postgis_lib_version()
50 self.assertEqual(expect, actual)
51
52 def test_version_classic_tuple(self):
53 expect = ("1.2.3", 1, 2, 3)
54 ops = FakePostGISOperations(expect[0])
55 actual = ops.postgis_version_tuple()
56 self.assertEqual(expect, actual)
57
58 def test_version_dev_tuple(self):
59 expect = ("1.2.3dev", 1, 2, 3)
60 ops = FakePostGISOperations(expect[0])
61 actual = ops.postgis_version_tuple()
62 self.assertEqual(expect, actual)
63
64 def test_version_loose_tuple(self):
65 expect = ("1.2.3b1.dev0", 1, 2, 3)
66 ops = FakePostGISOperations(expect[0])
67 actual = ops.postgis_version_tuple()
68 self.assertEqual(expect, actual)
69
70 def test_valid_version_numbers(self):
71 versions = [
72 ("1.3.0", 1, 3, 0),
73 ("2.1.1", 2, 1, 1),
74 ("2.2.0dev", 2, 2, 0),
75 ]
76
77 for version in versions:
78 with self.subTest(version=version):
79 ops = FakePostGISOperations(version[0])
80 actual = ops.spatial_version
81 self.assertEqual(version[1:], actual)
82
83 def test_no_version_number(self):
84 ops = FakePostGISOperations()
85 with self.assertRaises(ImproperlyConfigured):
86 ops.spatial_version
87
88
89 @unittest.skipUnless(HAS_POSTGRES, "PostGIS-specific tests.")
90 class TestPostGISBackend(unittest.TestCase):
91 def test_non_db_connection_classes(self):
92 from django.contrib.gis.db.backends.postgis.base import DatabaseWrapper
93 from django.db.backends.postgresql.features import DatabaseFeatures
94 from django.db.backends.postgresql.introspection import DatabaseIntrospection
95 from django.db.backends.postgresql.operations import DatabaseOperations
96
97 wrapper = DatabaseWrapper(settings_dict={}, alias=NO_DB_ALIAS)
98 # PostGIS-specific stuff is not initialized for non-db connections.
99 self.assertIs(wrapper.features_class, DatabaseFeatures)
100 self.assertIs(wrapper.ops_class, DatabaseOperations)
101 self.assertIs(wrapper.introspection_class, DatabaseIntrospection)