Opened 14 years ago

Closed 13 years ago

Last modified 13 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 John Paulett 13 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 by milosu, 14 years ago

Cc: milosu added

comment:2 by Russell Keith-Magee, 14 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Julien Phalip, 14 years ago

Severity: Normal
Type: Bug

comment:4 by Julien Phalip, 14 years ago

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.

by John Paulett, 13 years ago

Attachment: 13670.1.patch added

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

comment:5 by John Paulett, 13 years ago

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

comment:6 by jbronn, 13 years ago

Resolution: fixed
Status: newclosed

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