Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/gis_tests/geoapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,15 @@ class ManyPointModel(NamedModel):
point1 = models.PointField()
point2 = models.PointField()
point3 = models.PointField(srid=3857)


class Points(models.Model):
geom = models.MultiPointField()


class Lines(models.Model):
geom = models.MultiLineStringField()


class GeometryCollectionModel(models.Model):
geom = models.GeometryCollectionField()
10 changes: 2 additions & 8 deletions tests/gis_tests/geoapp/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
Polygon,
fromstr,
)
from django.contrib.gis.geos.libgeos import geos_version_tuple
from django.contrib.gis.measure import Area
from django.db import NotSupportedError, connection
from django.db.models import F, IntegerField, Sum, Value
from django.test import TestCase, skipUnlessDBFeature

from ..utils import FuncTestMixin
from ..utils import FuncTestMixin, can_save_multipoint
from .models import (
City,
Country,
Expand Down Expand Up @@ -965,12 +964,7 @@ def test_geometry_type(self):
("MULTILINESTRING", MultiLineString),
("MULTIPOLYGON", MultiPolygon),
]
# GEOSWKTWriter_write() behavior was changed in GEOS 3.12+ to include
# parentheses for sub-members. MariaDB doesn't accept WKT
# representations with additional parentheses for MultiPoint. This is
# an accepted bug (MDEV-36166) in MariaDB that should be fixed in the
# future.
if not connection.ops.mariadb or geos_version_tuple() < (3, 12):
if can_save_multipoint:
test_features.append(
Feature(name="MultiPoint", geom=MultiPoint(Point(0, 0), Point(1, 1)))
)
Expand Down
48 changes: 47 additions & 1 deletion tests/gis_tests/geoapp/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from io import StringIO
from unittest import skipIf

from django.contrib.gis import gdal
from django.contrib.gis.db.models import Extent, MakeLine, Union, functions
Expand All @@ -21,15 +22,18 @@
from django.test import TestCase, skipUnlessDBFeature
from django.test.utils import CaptureQueriesContext

from ..utils import skipUnlessGISLookup
from ..utils import cannot_save_multipoint, skipUnlessGISLookup
from .models import (
City,
Country,
Feature,
GeometryCollectionModel,
Lines,
MinusOneSRID,
MultiFields,
NonConcreteModel,
PennsylvaniaCity,
Points,
State,
ThreeDimensionalFeature,
Track,
Expand Down Expand Up @@ -269,6 +273,48 @@ def test_empty_geometries(self):
self.assertEqual(feature.geom.srid, g.srid)


class SaveLoadTests(TestCase):
def test_multilinestringfield(self):
geom = MultiLineString(
LineString((0, 0), (1, 1), (5, 5)),
LineString((0, 0), (0, 5), (5, 5), (5, 0), (0, 0)),
)
obj = Lines.objects.create(geom=geom)
obj.refresh_from_db()
self.assertEqual(obj.geom.tuple, geom.tuple)

def test_multilinestring_with_linearring(self):
geom = MultiLineString(
LineString((0, 0), (1, 1), (5, 5)),
LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0)),
)
obj = Lines.objects.create(geom=geom)
obj.refresh_from_db()
self.assertEqual(obj.geom.tuple, geom.tuple)
self.assertEqual(obj.geom[1].__class__.__name__, "LineString")
self.assertEqual(obj.geom[0].tuple, geom[0].tuple)
# LinearRings are transformed to LineString.
self.assertEqual(obj.geom[1].__class__.__name__, "LineString")
self.assertEqual(obj.geom[1].tuple, geom[1].tuple)

@skipIf(cannot_save_multipoint, "MariaDB cannot save MultiPoint due to a bug.")
def test_multipointfield(self):
geom = MultiPoint(Point(1, 1), Point(0, 0))
obj = Points.objects.create(geom=geom)
obj.refresh_from_db()
self.assertEqual(obj.geom, geom)

def test_geometrycollectionfield(self):
geom = GeometryCollection(
Point(2, 2),
LineString((0, 0), (2, 2)),
Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))),
)
obj = GeometryCollectionModel.objects.create(geom=geom)
obj.refresh_from_db()
self.assertIs(obj.geom.equals(geom), True)


class GeoLookupTest(TestCase):
fixtures = ["initial"]

Expand Down
7 changes: 7 additions & 0 deletions tests/gis_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest import mock

from django.conf import settings
from django.contrib.gis.geos.libgeos import geos_version_tuple
from django.db import DEFAULT_DB_ALIAS, connection
from django.db.models import Func

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


class FuncTestMixin:
Expand Down