Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#13670 closed Bug (fixed)

postgis adapter fails to compare against empty string

Reported by: milosu Owned by: nobody
Component: GIS Version: 1.2
Severity: Normal Keywords:
Cc: milosu, john@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

following test case fails:

from django.db.models import Q
from django.contrib.gis.db import models

class Area(models.Model):
    poly = models.PolygonField(srid=4326, verbose_name=u"area repr")
    objects = models.GeoManager()

class District(models.Model):
    poly = models.PolygonField(srid=4326, verbose_name=u"district repr")
    objects = models.GeoManager()

class Factory(models.Model):
    area = models.ForeignKey(Area)
    district = models.ForeignKey(District)

__test__ = {'API_TESTS':"""

>>> from django.contrib.gis.geos import fromstr
>>> location = fromstr('POINT (5 7)')
>>> a, created = Area.objects.get_or_create(poly=fromstr('POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))'))
>>> d, created = District.objects.get_or_create(poly=fromstr('POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))'))
>>> f, created = Factory.objects.get_or_create(area=a, district=d)
>>> areas = Area.objects.filter(poly__intersects = location)
>>> districts = District.objects.filter(poly__intersects = location)
>>> qobj = Q(area__isnull=True)&Q(district__isnull=True)
>>> qobj |= (Q(area__in = areas)|Q(district__in = districts))
>>> match = Factory.objects.filter(qobj)
>>> match2 = Factory.objects.filter(id__in = match)
>>> match2.count()
        1
"""}

the last line will raise an exception in django.contrib.gis.db.backends.postgis.adapter eq due to the comparision against empty string

the following patch solves the problem:

Index: adapter.py
===================================================================
--- adapter.py  (revision 3756)
+++ adapter.py  (working copy)
@@ -21,6 +21,8 @@
             raise Exception('Error implementing psycopg2 protocol. Is psycopg2 installed?')
 
     def __eq__(self, other):
+        if not isinstance(other, PostGISAdapter):
+           return False
         return (self.ewkb == other.ewkb) and (self.srid == other.srid)
 
     def __str__(self):

Attachments (1)

13670.1.patch (2.3 KB) - added by jpaulett 4 years ago.
Fix for other backends, move to unittest, & distill test to core example using existing models.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 5 years ago by milosu

  • Cc milosu added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 4 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:4 Changed 4 years ago by julien

  • Patch needs improvement set

Could you provide a single diff patch including the fix and tests? Could you also rewrite your tests using unittests since this is now the preferred way for django's test suite.

Changed 4 years ago by jpaulett

Fix for other backends, move to unittest, & distill test to core example using existing models.

comment:5 Changed 4 years ago by jpaulett

  • Cc john@… added
  • Easy pickings unset
  • Patch needs improvement unset
  • UI/UX unset

comment:6 Changed 4 years ago by jbronn

  • Resolution set to fixed
  • Status changed from new to closed

In [16757]:

Fixed #13670 -- Comparisons with the spatial adapter won't blow up in some corner cases. Thanks, milosu for the bug report and jpaulett for the patch.

Note: See TracTickets for help on using tickets.
Back to Top