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

Code before:
            [empty],
        )
        self.assertSequenceEqual(City.objects.filter(point__isempty=True), [empty])

    @skipUnlessDBFeature("has_IsEmpty_function")
    def test_isempty_geometry_null(self):
        nowhere = State.objects.create(name="Nowhere", poly=None)
        self.assertSequenceEqual(
            State.objects.annotate(isempty=functions.IsEmpty("poly")).filter(
                isempty=None
            ),
            [nowhere],
        )
        self.assertSequenceEqual(
            State.objects.annotate(isempty=functions.IsEmpty("poly"))
            .filter(isempty=False)
            .order_by("name")
            .values_list("name", flat=True),
            ["Colorado", "Kansas"],
        )
        self.assertSequenceEqual(
            State.objects.annotate(isempty=functions.IsEmpty("poly")).filter(
                isempty=True
            ),
            [],
        )
        self.assertSequenceEqual(State.objects.filter(poly__isempty=True), [])

    @skipUnlessDBFeature("has_IsValid_function")
    def test_isvalid(self):
        valid_geom = fromstr("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))")
        invalid_geom = fromstr("POLYGON((0 0, 0 1, 1 1, 1 0, 1 1, 1 0, 0 0))")
        State.objects.create(name="valid", poly=valid_geom)
        State.objects.create(name="invalid", poly=invalid_geom)
        valid = (
            State.objects.filter(name="valid")
            .annotate(isvalid=functions.IsValid("poly"))
            .first()
        )
        invalid = (
            State.objects.filter(name="invalid")
            .annotate(isvalid=functions.IsValid("poly"))
            .first()
        )
        self.assertIs(valid.isvalid, True)
        self.assertIs(invalid.isvalid, False)

    @skipUnlessDBFeature("has_Area_function")
    def test_area_with_regular_aggregate(self):
        # Create projected country objects, for this test to work on all
        # backends.

Review comment (suggestion):
We could move the queryset to a separate variable, e.g.
```suggestion
        qs = State.objects.annotate(isempty=functions.IsEmpty("poly"))
        self.assertSequenceEqual(qs.filter(isempty=None), [nowhere])
        self.assertSequenceEqual(
            qs.filter(isempty=False).order_by("name").values_list("name", flat=True),
            ["Colorado", "Kansas"],
        )
        self.assertSequenceEqual(qs.filter(isempty=True), [])
```

Code after:
            [empty],
        )
        self.assertSequenceEqual(City.objects.filter(point__isempty=True), [empty])

    @skipUnlessDBFeature("has_IsEmpty_function")
    def test_isempty_geometry_null(self):
        nowhere = State.objects.create(name="Nowhere", poly=None)
        qs = State.objects.annotate(isempty=functions.IsEmpty("poly"))
        self.assertSequenceEqual(qs.filter(isempty=None), [nowhere])
        self.assertSequenceEqual(
            qs.filter(isempty=False).order_by("name").values_list("name", flat=True),
            ["Colorado", "Kansas"],
        )
        self.assertSequenceEqual(qs.filter(isempty=True), [])
        self.assertSequenceEqual(State.objects.filter(poly__isempty=True), [])

    @skipUnlessDBFeature("has_IsValid_function")
    def test_isvalid(self):
        valid_geom = fromstr("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))")
        invalid_geom = fromstr("POLYGON((0 0, 0 1, 1 1, 1 0, 1 1, 1 0, 0 0))")
        State.objects.create(name="valid", poly=valid_geom)
        State.objects.create(name="invalid", poly=invalid_geom)
        valid = (
            State.objects.filter(name="valid")
            .annotate(isvalid=functions.IsValid("poly"))
            .first()
        )
        invalid = (
            State.objects.filter(name="invalid")
            .annotate(isvalid=functions.IsValid("poly"))
            .first()
        )
        self.assertIs(valid.isvalid, True)
        self.assertIs(invalid.isvalid, False)

    @skipUnlessDBFeature("has_Area_function")
    def test_area_with_regular_aggregate(self):
        # Create projected country objects, for this test to work on all
        # backends.
        for c in Country.objects.all():
            CountryWebMercator.objects.create(
                name=c.name, mpoly=c.mpoly.transform(3857, clone=True)
            )
        # Test in projected coordinate system
        qs = CountryWebMercator.objects.annotate(area_sum=Sum(functions.Area("mpoly")))
        # Some backends (e.g. Oracle) cannot group by multipolygon values, so
        # defer such fields in the aggregation query.
        for c in qs.defer("mpoly"):
            result = c.area_sum
            # If the result is a measure object, get value.
            if isinstance(result, Area):