Repository: django/django
File: tests/gis_tests/geoapp/test_functions.py (Python)

Code before:
                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)


Review comment (suggestion):
TIL that we set vendor name as booleans on `BaseSpatialOperations` instead of using features per-backend instead (I was expecting this to raise `AttributeError` or non-MySQL based backends).

Code after:
                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),
            ("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):
            test_features.append(
                Feature(name="MultiPoint", geom=MultiPoint(Point(0, 0), Point(1, 1)))
            )
            expected_results.append(("MULTIPOINT", MultiPoint))
        for test_feature, (geom_type, geom_class) in zip(
            test_features, expected_results, strict=True
        ):
            with self.subTest(geom_type=geom_type, geom=test_feature.geom.wkt):
                test_feature.save()
                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)