Skip to content

Commit bbabbac

Browse files
timgrahamjacobtylerwalls
authored andcommitted
Added tests for MultiPointField, MultiLineStringField, and GeometryCollectionField.
1 parent 3ccef1d commit bbabbac

4 files changed

Lines changed: 68 additions & 9 deletions

File tree

tests/gis_tests/geoapp/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,15 @@ class ManyPointModel(NamedModel):
102102
point1 = models.PointField()
103103
point2 = models.PointField()
104104
point3 = models.PointField(srid=3857)
105+
106+
107+
class Points(models.Model):
108+
geom = models.MultiPointField()
109+
110+
111+
class Lines(models.Model):
112+
geom = models.MultiLineStringField()
113+
114+
115+
class GeometryCollectionModel(models.Model):
116+
geom = models.GeometryCollectionField()

tests/gis_tests/geoapp/test_functions.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
Polygon,
1515
fromstr,
1616
)
17-
from django.contrib.gis.geos.libgeos import geos_version_tuple
1817
from django.contrib.gis.measure import Area
1918
from django.db import NotSupportedError, connection
2019
from django.db.models import F, IntegerField, Sum, Value
2120
from django.test import TestCase, skipUnlessDBFeature
2221

23-
from ..utils import FuncTestMixin
22+
from ..utils import FuncTestMixin, can_save_multipoint
2423
from .models import (
2524
City,
2625
Country,
@@ -965,12 +964,7 @@ def test_geometry_type(self):
965964
("MULTILINESTRING", MultiLineString),
966965
("MULTIPOLYGON", MultiPolygon),
967966
]
968-
# GEOSWKTWriter_write() behavior was changed in GEOS 3.12+ to include
969-
# parentheses for sub-members. MariaDB doesn't accept WKT
970-
# representations with additional parentheses for MultiPoint. This is
971-
# an accepted bug (MDEV-36166) in MariaDB that should be fixed in the
972-
# future.
973-
if not connection.ops.mariadb or geos_version_tuple() < (3, 12):
967+
if can_save_multipoint:
974968
test_features.append(
975969
Feature(name="MultiPoint", geom=MultiPoint(Point(0, 0), Point(1, 1)))
976970
)

tests/gis_tests/geoapp/tests.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from io import StringIO
2+
from unittest import skipIf
23

34
from django.contrib.gis import gdal
45
from django.contrib.gis.db.models import Extent, MakeLine, Union, functions
@@ -21,15 +22,18 @@
2122
from django.test import TestCase, skipUnlessDBFeature
2223
from django.test.utils import CaptureQueriesContext
2324

24-
from ..utils import skipUnlessGISLookup
25+
from ..utils import cannot_save_multipoint, skipUnlessGISLookup
2526
from .models import (
2627
City,
2728
Country,
2829
Feature,
30+
GeometryCollectionModel,
31+
Lines,
2932
MinusOneSRID,
3033
MultiFields,
3134
NonConcreteModel,
3235
PennsylvaniaCity,
36+
Points,
3337
State,
3438
ThreeDimensionalFeature,
3539
Track,
@@ -269,6 +273,48 @@ def test_empty_geometries(self):
269273
self.assertEqual(feature.geom.srid, g.srid)
270274

271275

276+
class SaveLoadTests(TestCase):
277+
def test_multilinestringfield(self):
278+
geom = MultiLineString(
279+
LineString((0, 0), (1, 1), (5, 5)),
280+
LineString((0, 0), (0, 5), (5, 5), (5, 0), (0, 0)),
281+
)
282+
obj = Lines.objects.create(geom=geom)
283+
obj.refresh_from_db()
284+
self.assertEqual(obj.geom.tuple, geom.tuple)
285+
286+
def test_multilinestring_with_linearring(self):
287+
geom = MultiLineString(
288+
LineString((0, 0), (1, 1), (5, 5)),
289+
LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0)),
290+
)
291+
obj = Lines.objects.create(geom=geom)
292+
obj.refresh_from_db()
293+
self.assertEqual(obj.geom.tuple, geom.tuple)
294+
self.assertEqual(obj.geom[1].__class__.__name__, "LineString")
295+
self.assertEqual(obj.geom[0].tuple, geom[0].tuple)
296+
# LinearRings are transformed to LineString.
297+
self.assertEqual(obj.geom[1].__class__.__name__, "LineString")
298+
self.assertEqual(obj.geom[1].tuple, geom[1].tuple)
299+
300+
@skipIf(cannot_save_multipoint, "MariaDB cannot save MultiPoint due to a bug.")
301+
def test_multipointfield(self):
302+
geom = MultiPoint(Point(1, 1), Point(0, 0))
303+
obj = Points.objects.create(geom=geom)
304+
obj.refresh_from_db()
305+
self.assertEqual(obj.geom, geom)
306+
307+
def test_geometrycollectionfield(self):
308+
geom = GeometryCollection(
309+
Point(2, 2),
310+
LineString((0, 0), (2, 2)),
311+
Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))),
312+
)
313+
obj = GeometryCollectionModel.objects.create(geom=geom)
314+
obj.refresh_from_db()
315+
self.assertIs(obj.geom.equals(geom), True)
316+
317+
272318
class GeoLookupTest(TestCase):
273319
fixtures = ["initial"]
274320

tests/gis_tests/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unittest import mock
55

66
from django.conf import settings
7+
from django.contrib.gis.geos.libgeos import geos_version_tuple
78
from django.db import DEFAULT_DB_ALIAS, connection
89
from django.db.models import Func
910

@@ -31,6 +32,12 @@ def skip_wrapper(*args, **kwargs):
3132
_default_db = settings.DATABASES[DEFAULT_DB_ALIAS]["ENGINE"].rsplit(".")[-1]
3233
# MySQL spatial indices can't handle NULL geometries.
3334
gisfield_may_be_null = _default_db != "mysql"
35+
# GEOSWKTWriter_write() behavior was changed in GEOS 3.12+ to include
36+
# parentheses for sub-members. MariaDB doesn't accept WKT representations with
37+
# additional parentheses for MultiPoint. This is an accepted bug (MDEV-36166)
38+
# in MariaDB that should be fixed in the future.
39+
cannot_save_multipoint = connection.ops.mariadb and geos_version_tuple() >= (3, 12)
40+
can_save_multipoint = not cannot_save_multipoint
3441

3542

3643
class FuncTestMixin:

0 commit comments

Comments
 (0)