Skip to content

Commit cf1a80f

Browse files
authored
Added validation to BaseSpatialFeatures.has_<Func>_function.
1 parent f0a8789 commit cf1a80f

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

django/contrib/gis/db/backends/base/features.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from django.contrib.gis.db import models
44

5+
from .operations import BaseSpatialOperations
6+
57

68
class BaseSpatialFeatures:
79
gis_enabled = True
@@ -107,5 +109,11 @@ def __getattr__(self, name):
107109
m = re.match(r"has_(\w*)_function$", name)
108110
if m:
109111
func_name = m[1]
112+
if func_name not in BaseSpatialOperations.unsupported_functions:
113+
raise ValueError(
114+
f"DatabaseFeatures.has_{func_name}_function isn't valid. "
115+
f'Is "{func_name}" missing from '
116+
"BaseSpatialOperations.unsupported_functions?"
117+
)
110118
return func_name not in self.connection.ops.unsupported_functions
111119
raise AttributeError

django/contrib/gis/db/backends/base/operations.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ def select_extent(self):
3939
"AsGML",
4040
"AsKML",
4141
"AsSVG",
42+
"AsWKB",
43+
"AsWKT",
4244
"Azimuth",
4345
"BoundingCircle",
4446
"Centroid",
4547
"ClosestPoint",
4648
"Difference",
4749
"Distance",
50+
"DistanceSpheroid",
4851
"Envelope",
52+
"ForcePolygonCW",
4953
"FromWKB",
5054
"FromWKT",
5155
"GeoHash",

tests/gis_tests/geoapp/test_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def test_memsize(self):
559559
# Exact value depends on database and version.
560560
self.assertTrue(20 <= ptown.size <= 105)
561561

562-
@skipUnlessDBFeature("has_NumGeom_function")
562+
@skipUnlessDBFeature("has_NumGeometries_function")
563563
def test_num_geom(self):
564564
# Both 'countries' only have two geometries.
565565
for c in Country.objects.annotate(num_geom=functions.NumGeometries("mpoly")):
@@ -576,7 +576,7 @@ def test_num_geom(self):
576576
else:
577577
self.assertEqual(1, city.num_geom)
578578

579-
@skipUnlessDBFeature("has_NumPoint_function")
579+
@skipUnlessDBFeature("has_NumPoints_function")
580580
def test_num_points(self):
581581
coords = [(-95.363151, 29.763374), (-95.448601, 29.713803)]
582582
Track.objects.create(name="Foo", line=LineString(coords))

tests/gis_tests/tests.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import unittest
22

33
from django.core.exceptions import ImproperlyConfigured
4-
from django.db import ProgrammingError
4+
from django.db import ProgrammingError, connection
55
from django.db.backends.base.base import NO_DB_ALIAS
6+
from django.test import TestCase
67

78
try:
89
from django.contrib.gis.db.backends.postgis.operations import PostGISOperations
@@ -12,6 +13,16 @@
1213
HAS_POSTGRES = False
1314

1415

16+
class BaseSpatialFeaturesTests(TestCase):
17+
def test_invalid_has_func_function(self):
18+
msg = (
19+
'DatabaseFeatures.has_Invalid_function isn\'t valid. Is "Invalid" '
20+
"missing from BaseSpatialOperations.unsupported_functions?"
21+
)
22+
with self.assertRaisesMessage(ValueError, msg):
23+
connection.features.has_Invalid_function
24+
25+
1526
if HAS_POSTGRES:
1627

1728
class FakeConnection:

0 commit comments

Comments
 (0)