Code

Ticket #10594: fix_null_geometry_querysets_with_tests.diff

File fix_null_geometry_querysets_with_tests.diff, 3.0 KB (added by yourcelf, 5 years ago)

Updated diff that includes tests

Line 
1Index: django/contrib/gis/db/models/sql/query.py
2===================================================================
3--- django/contrib/gis/db/models/sql/query.py   (revision 10838)
4+++ django/contrib/gis/db/models/sql/query.py   (working copy)
5@@ -233,6 +233,9 @@
6         extra selection objects into Geometry and Distance objects.
7         TODO: Make converted objects 'lazy' for less overhead.
8         """
9+        if value == None:
10+            return value
11+
12         if SpatialBackend.oracle:
13             # Running through Oracle's first.
14             value = super(GeoQuery, self).convert_values(value, field or GeomField())
15Index: django/contrib/gis/tests/distapp/tests.py
16===================================================================
17--- django/contrib/gis/tests/distapp/tests.py   (revision 10838)
18+++ django/contrib/gis/tests/distapp/tests.py   (working copy)
19@@ -8,7 +8,7 @@
20 from django.contrib.gis.tests.utils import oracle, postgis, spatialite, no_oracle, no_spatialite
21 
22 from models import AustraliaCity, Interstate, SouthTexasInterstate, \
23-    SouthTexasCity, SouthTexasCityFt, CensusZipcode, SouthTexasZipcode
24+    SouthTexasCity, SouthTexasCityFt, CensusZipcode, SouthTexasZipcode, NullPointModel
25 from data import au_cities, interstates, stx_interstates, stx_cities, stx_zips
26 
27 class DistanceTest(unittest.TestCase):
28@@ -203,6 +203,21 @@
29             for i, z in enumerate(qs):
30                 self.assertAlmostEqual(z.distance.m, dists_m[i], 5)
31 
32+    def test03d_distance_method(self):
33+        "Testing the `distance` GeoQuerySet method with null geometries in models"
34+        # Ensure that null geometry fields in distance method lookups don't kill the
35+        # querysets (ticket #10594)
36+        null = NullPointModel.objects.create(name="Null")
37+        notnull = NullPointModel.objects.create(name="Not null", point=Point(0, 0))
38+        qs = NullPointModel.objects.distance(Point(0, 0))
39+        self.assertTrue(null in qs)
40+        self.assertTrue(notnull in qs)
41+        for obj in qs:
42+            if obj.name == "Null":
43+                self.assertEquals(obj.distance, None)
44+            elif obj.name == "Not null":
45+                self.assertEquals(obj.distance.m, 0)
46+
47     def test04_distance_lookups(self):
48         "Testing the `distance_lt`, `distance_gt`, `distance_lte`, and `distance_gte` lookup types."
49         # Retrieving the cities within a 20km 'donut' w/a 7km radius 'hole'
50Index: django/contrib/gis/tests/distapp/models.py
51===================================================================
52--- django/contrib/gis/tests/distapp/models.py  (revision 10838)
53+++ django/contrib/gis/tests/distapp/models.py  (working copy)
54@@ -47,3 +47,9 @@
55     path = models.LineStringField(srid=32140)
56     objects = models.GeoManager()
57     def __unicode__(self): return self.name
58+
59+class NullPointModel(models.Model):
60+    name = models.CharField(max_length=30)
61+    point = models.PointField(null=True)
62+    objects = models.GeoManager()
63+    def __unicode__(self): return self.name