|
@@ -4,7 +4,9 @@ from django.contrib.gis.db.models.functions import (
|
|
|
from django.contrib.gis.geos import GEOSGeometry, LineString, Point
|
|
|
from django.contrib.gis.measure import D # alias for Distance
|
|
|
from django.db import NotSupportedError, connection
|
|
|
-from django.db.models import Exists, F, OuterRef, Q
|
|
|
+from django.db.models import (
|
|
|
+ Case, Count, Exists, F, IntegerField, OuterRef, Q, Value, When,
|
|
|
+)
|
|
|
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
|
|
|
|
|
|
from ..utils import FuncTestMixin
|
|
@@ -214,6 +216,24 @@ class DistanceTest(TestCase):
|
|
|
SouthTexasCity.objects.count(),
|
|
|
)
|
|
|
|
|
|
+ @skipUnlessDBFeature('supports_distances_lookups')
|
|
|
+ def test_distance_annotation_group_by(self):
|
|
|
+ stx_pnt = self.stx_pnt.transform(
|
|
|
+ SouthTexasCity._meta.get_field('point').srid,
|
|
|
+ clone=True,
|
|
|
+ )
|
|
|
+ qs = SouthTexasCity.objects.annotate(
|
|
|
+ relative_distance=Case(
|
|
|
+ When(point__distance_lte=(stx_pnt, D(km=20)), then=Value(20)),
|
|
|
+ default=Value(100),
|
|
|
+ output_field=IntegerField(),
|
|
|
+ ),
|
|
|
+ ).values('relative_distance').annotate(count=Count('pk'))
|
|
|
+ self.assertCountEqual(qs, [
|
|
|
+ {'relative_distance': 20, 'count': 5},
|
|
|
+ {'relative_distance': 100, 'count': 4},
|
|
|
+ ])
|
|
|
+
|
|
|
def test_mysql_geodetic_distance_error(self):
|
|
|
if not connection.ops.mysql:
|
|
|
self.skipTest('This is a MySQL-specific test.')
|