From e158ec0ba0b43cbe9b509d4ed5587fa3b2ae3cc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20No=C3=A9?= Date: Wed, 11 May 2016 11:29:16 +0200 Subject: [PATCH] Fixed #26333 -- Made GIS Geometry classes deconstructible. --- django/contrib/gis/geos/geometry.py | 2 + tests/gis_tests/geos_tests/test_geos.py | 65 +++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/django/contrib/gis/geos/geometry.py b/django/contrib/gis/geos/geometry.py index 99e680e492..c48e99cd5b 100644 --- a/django/contrib/gis/geos/geometry.py +++ b/django/contrib/gis/geos/geometry.py @@ -21,10 +21,12 @@ from django.contrib.gis.geos.prototypes.io import ( ewkb_w, wkb_r, wkb_w, wkt_r, wkt_w, ) from django.utils import six +from django.utils.deconstruct import deconstructible from django.utils.deprecation import RemovedInDjango20Warning from django.utils.encoding import force_bytes, force_text +@deconstructible class GEOSGeometry(GEOSBase, ListMixin): "A class that, generally, encapsulates a GEOS geometry." diff --git a/tests/gis_tests/geos_tests/test_geos.py b/tests/gis_tests/geos_tests/test_geos.py index 6c32835237..02943baf0d 100644 --- a/tests/gis_tests/geos_tests/test_geos.py +++ b/tests/gis_tests/geos_tests/test_geos.py @@ -1246,6 +1246,71 @@ class GEOSTest(SimpleTestCase, TestDataMixin): self.assertEqual(mls.interpolate(9), Point(0, 9)) self.assertEqual(mls.interpolate(17), Point(10, 7)) + def test_deconstructible(self): + """ + Geometry classes should be deconstructible. + """ + point = Point(4.337844, 50.827537, srid=4326) + path, args, kwargs = point.deconstruct() + self.assertEqual(path, 'django.contrib.gis.geos.point.Point') + self.assertEqual(args, (4.337844, 50.827537)) + self.assertEqual(kwargs, {'srid': 4326}) + + ls = LineString(((0, 0), (1, 1))) + path, args, kwargs = ls.deconstruct() + self.assertEqual(path, 'django.contrib.gis.geos.linestring.LineString') + self.assertEqual(args, (((0, 0), (1, 1)))) + self.assertEqual(kwargs, {}) + + ls2 = LineString([Point(0, 0), Point(1, 1)], srid=4326) + path, args, kwargs = ls2.deconstruct() + self.assertEqual(path, 'django.contrib.gis.geos.linestring.LineString') + self.assertEqual(args, ([Point(0, 0), Point(1, 1)])) + self.assertEqual(kwargs, {'srid': 4326}) + + ext_coords = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)) + int_coords = ((0.4, 0.4), (0.4, 0.6), (0.6, 0.6), (0.6, 0.4), (0.4, 0.4)) + poly = Polygon(ext_coords, int_coords) + path, args, kwargs = poly.deconstruct() + self.assertEqual(path, 'django.contrib.gis.geos.polygon.Polygon') + self.assertEqual(args, (ext_coords, int_coords)) + self.assertEqual(kwargs, {}) + + lr = LinearRing((0, 0), (0, 1), (1, 1), (0, 0)) + path, args, kwargs = lr.deconstruct() + self.assertEqual(path, 'django.contrib.gis.geos.linestring.LinearRing') + self.assertEqual(args, ((0, 0), (0, 1), (1, 1), (0, 0))) + self.assertEqual(kwargs, {}) + + mp = MultiPoint(Point(0, 0), Point(1, 1)) + path, args, kwargs = mp.deconstruct() + self.assertEqual(path, 'django.contrib.gis.geos.collections.MultiPoint') + self.assertEqual(args, (Point(0, 0), Point(1, 1))) + self.assertEqual(kwargs, {}) + + ls1 = LineString((0, 0), (1, 1)) + ls2 = LineString((2, 2), (3, 3)) + mls = MultiLineString(ls1, ls2) + path, args, kwargs = mls.deconstruct() + self.assertEqual(path, 'django.contrib.gis.geos.collections.MultiLineString') + self.assertEqual(args, (ls1, ls2)) + self.assertEqual(kwargs, {}) + + p1 = Polygon(((0, 0), (0, 1), (1, 1), (0, 0))) + p2 = Polygon(((1, 1), (1, 2), (2, 2), (1, 1))) + mp = MultiPolygon(p1, p2) + path, args, kwargs = mp.deconstruct() + self.assertEqual(path, 'django.contrib.gis.geos.collections.MultiPolygon') + self.assertEqual(args, (p1, p2, )) + self.assertEqual(kwargs, {}) + + poly = Polygon(((0, 0), (0, 1), (1, 1), (0, 0))) + gc = GeometryCollection(Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly) + path, args, kwargs = gc.deconstruct() + self.assertEqual(path, 'django.contrib.gis.geos.collections.GeometryCollection') + self.assertEqual(args, (Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly)) + self.assertEqual(kwargs, {}) + def test_geos_version(self): """Testing the GEOS version regular expression.""" from django.contrib.gis.geos.libgeos import version_regex