Code

Ticket #19366: 19366-3.diff

File 19366-3.diff, 4.1 KB (added by cdestigter, 17 months ago)

19366-2.diff plus fix lt when geometries are different lengths

Line 
1diff --git a/django/contrib/gis/geos/mutable_list.py b/django/contrib/gis/geos/mutable_list.py
2index 820cdfa..b41a83a 100644
3--- a/django/contrib/gis/geos/mutable_list.py
4+++ b/django/contrib/gis/geos/mutable_list.py
5@@ -149,27 +149,28 @@ class ListMixin(object):
6         return self
7 
8     def __eq__(self, other):
9-        for i in range(len(self)):
10+        olen = len(other)
11+        for i in range(olen):
12             try:
13                 c = self[i] == other[i]
14-            except IndexError:
15-                # must be other is shorter
16+            except self._IndexError:
17+                # self must be shorter
18                 return False
19             if not c:
20                 return False
21-        return True
22+        return len(self) == olen
23 
24     def __lt__(self, other):
25-        slen = len(self)
26-        for i in range(slen):
27+        olen = len(other)
28+        for i in range(olen):
29             try:
30                 c = self[i] < other[i]
31-            except IndexError:
32-                # must be other is shorter
33-                return False
34+            except self._IndexError:
35+                # self must be shorter
36+                return True
37             if c:
38                 return c
39-        return slen < len(other)
40+        return len(self) < olen
41 
42     ### Public list interface Methods ###
43     ## Non-mutating ##
44diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py
45index 283daa4..ec320f9 100644
46--- a/django/contrib/gis/geos/tests/test_geos.py
47+++ b/django/contrib/gis/geos/tests/test_geos.py
48@@ -451,6 +451,21 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
49             self.assertEqual(poly.wkt, Polygon(*tuple(r for r in poly)).wkt)
50             self.assertEqual(poly.wkt, Polygon(*tuple(LinearRing(r.tuple) for r in poly)).wkt)
51 
52+    def test_polygon_comparison(self):
53+        p1 = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
54+        p2 = Polygon(((0, 0), (0, 1), (1, 0), (0, 0)))
55+        self.assertTrue(p1 > p2)
56+        self.assertFalse(p1 < p2)
57+        self.assertFalse(p2 > p1)
58+        self.assertTrue(p2 < p1)
59+
60+        p3 = Polygon(((0, 0), (0, 1), (1, 1), (2, 0), (0, 0)))
61+        p4 = Polygon(((0, 0), (0, 1), (2, 2), (1, 0), (0, 0)))
62+        self.assertFalse(p4 < p3)
63+        self.assertTrue(p3 < p4)
64+        self.assertTrue(p4 > p3)
65+        self.assertFalse(p3 > p4)
66+
67     def test_multipolygons(self):
68         "Testing MultiPolygon objects."
69         prev = fromstr('POINT (0 0)')
70diff --git a/django/contrib/gis/geos/tests/test_mutable_list.py b/django/contrib/gis/geos/tests/test_mutable_list.py
71index 675505f..988d841 100644
72--- a/django/contrib/gis/geos/tests/test_mutable_list.py
73+++ b/django/contrib/gis/geos/tests/test_mutable_list.py
74@@ -363,6 +363,7 @@ class ListMixinTest(unittest.TestCase):
75 
76         pl, ul = self.lists_of_len()
77         self.assertEqual(pl, ul, 'cmp for equal')
78+        self.assertFalse(ul == pl + [2], 'cmp for not equal')
79         self.assertTrue(pl >= ul, 'cmp for gte self')
80         self.assertTrue(pl <= ul, 'cmp for lte self')
81         self.assertTrue(ul >= pl, 'cmp for self gte')
82@@ -377,6 +378,14 @@ class ListMixinTest(unittest.TestCase):
83         self.assertTrue(ul < pl + [2], 'cmp')
84         self.assertTrue(ul <= pl + [2], 'cmp')
85 
86+        # Also works with a custom IndexError
87+        ul_longer = ul + [2]
88+        ul_longer._IndexError = TypeError
89+        ul._IndexError = TypeError
90+        self.assertFalse(ul_longer == pl)
91+        self.assertFalse(ul == ul_longer)
92+        self.assertTrue(ul_longer > ul)
93+
94         pl[1] = 20
95         self.assertTrue(pl > ul, 'cmp for gt self')
96         self.assertTrue(ul < pl, 'cmp for self lt')
97diff --git a/django/contrib/gis/tests/__init__.py b/django/contrib/gis/tests/__init__.py
98index 765c030..54c945d 100644
99--- a/django/contrib/gis/tests/__init__.py
100+++ b/django/contrib/gis/tests/__init__.py
101@@ -121,3 +121,5 @@ class GeoDjangoTestSuiteRunner(DjangoTestSuiteRunner):
102 
103     def build_suite(self, test_labels, extra_tests=None, **kwargs):
104         return geodjango_suite()
105+
106+from django.contrib.gis.geos.tests.test_geos import *