From a5b0a618c3ff16f9f52a9abdc89c05899ef112b4 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Thu, 17 Jul 2025 09:50:39 +0300 Subject: Fixed #28696 -- Added GeometryType GIS database function and __geom_type lookup. Co-Authored-By: Mariusz Felisiak --- tests/gis_tests/geoapp/test_functions.py | 66 +++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/gis_tests/geoapp/test_functions.py b/tests/gis_tests/geoapp/test_functions.py index 9844905200..989f6069cd 100644 --- a/tests/gis_tests/geoapp/test_functions.py +++ b/tests/gis_tests/geoapp/test_functions.py @@ -4,14 +4,31 @@ import re from decimal import Decimal from django.contrib.gis.db.models import GeometryField, PolygonField, functions -from django.contrib.gis.geos import GEOSGeometry, LineString, Point, Polygon, fromstr +from django.contrib.gis.geos import ( + GEOSGeometry, + LineString, + MultiLineString, + MultiPoint, + MultiPolygon, + Point, + Polygon, + fromstr, +) from django.contrib.gis.measure import Area from django.db import NotSupportedError, connection from django.db.models import IntegerField, Sum, Value from django.test import TestCase, skipUnlessDBFeature from ..utils import FuncTestMixin -from .models import City, Country, CountryWebMercator, ManyPointModel, State, Track +from .models import ( + City, + Country, + CountryWebMercator, + Feature, + ManyPointModel, + State, + Track, +) class GISFunctionsTests(FuncTestMixin, TestCase): @@ -880,3 +897,48 @@ class GISFunctionsTests(FuncTestMixin, TestCase): City.objects.annotate(union=functions.GeoFunc(1, "point")).get( name="Dallas" ) + + @skipUnlessDBFeature("has_GeometryType_function") + def test_geometry_type(self): + Feature.objects.bulk_create( + [ + Feature(name="Point", geom=Point(0, 0)), + Feature(name="LineString", geom=LineString((0, 0), (1, 1))), + Feature(name="Polygon", geom=Polygon(((0, 0), (1, 0), (1, 1), (0, 0)))), + Feature(name="MultiPoint", geom=MultiPoint(Point(0, 0), Point(1, 1))), + Feature( + name="MultiLineString", + geom=MultiLineString( + LineString((0, 0), (1, 1)), LineString((1, 1), (2, 2)) + ), + ), + Feature( + name="MultiPolygon", + geom=MultiPolygon( + Polygon(((0, 0), (1, 0), (1, 1), (0, 0))), + Polygon(((1, 1), (2, 1), (2, 2), (1, 1))), + ), + ), + ] + ) + + expected_results = { + ("POINT", Point), + ("LINESTRING", LineString), + ("POLYGON", Polygon), + ("MULTIPOINT", MultiPoint), + ("MULTILINESTRING", MultiLineString), + ("MULTIPOLYGON", MultiPolygon), + } + + for geom_type, geom_class in expected_results: + with self.subTest(geom_type=geom_type): + obj = ( + Feature.objects.annotate( + geometry_type=functions.GeometryType("geom") + ) + .filter(geom__geom_type=geom_type) + .get() + ) + self.assertIsInstance(obj.geom, geom_class) + self.assertEqual(obj.geometry_type, geom_type) -- cgit v1.3