Repository: django/django
File: tests/gis_tests/geos_tests/test_coordseq.py (Python)

Code before:
import math
from unittest import skipIf
from unittest.mock import patch

from django.contrib.gis.geos import GEOSGeometry, LineString
from django.contrib.gis.geos import prototypes as capi
from django.contrib.gis.geos.coordseq import GEOSCoordSeq
from django.contrib.gis.geos.libgeos import geos_version_tuple
from django.test import SimpleTestCase


class GEOSCoordSeqTest(SimpleTestCase):
    def test_getitem(self):
        coord_seq = LineString([(x, x) for x in range(2)]).coord_seq
        for i in (0, 1):
            with self.subTest(i):
                self.assertEqual(coord_seq[i], (i, i))
        for i in (-3, 10):
            msg = f"Invalid GEOS Geometry index: {i}"
            with self.subTest(i):
                with self.assertRaisesMessage(IndexError, msg):
                    coord_seq[i]

    @skipIf(geos_version_tuple() <= (3, 13), "GEOS M support requires 3.14+")
    def test_has_m(self):
        geom = GEOSGeometry("POINT ZM (1 2 3 4)")
        coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
        self.assertIs(coord_seq.hasm, True)

        geom = GEOSGeometry("POINT Z (1 2 3)")
        coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
        self.assertIs(coord_seq.hasm, False)

        geom = GEOSGeometry("POINT M (1 2 3)")
        coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
        self.assertIs(coord_seq.hasm, True)

    @skipIf(geos_version_tuple() <= (3, 13), "GEOS M support requires 3.14+")
    def test_get_set_m(self):
        geom = GEOSGeometry("POINT ZM (1 2 3 4)")
        coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
        self.assertEqual(coord_seq.tuple, (1, 2, 3, 4))
        self.assertEqual(coord_seq.getM(0), 4)
        coord_seq.setM(0, 10)
        self.assertEqual(coord_seq.tuple, (1, 2, 3, 10))
        self.assertEqual(coord_seq.getM(0), 10)

        geom = GEOSGeometry("POINT M (1 2 4)")
        coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)

Review comment (suggestion):
I think, like the other recent PR, this should be strictly less than (3, 14) to account for patch updates.

Code after:
import math
from unittest import skipIf
from unittest.mock import patch

from django.contrib.gis.geos import GEOSGeometry, LineString
from django.contrib.gis.geos import prototypes as capi
from django.contrib.gis.geos.coordseq import GEOSCoordSeq
from django.contrib.gis.geos.libgeos import geos_version_tuple
from django.test import SimpleTestCase


class GEOSCoordSeqTest(SimpleTestCase):
    def test_getitem(self):
        coord_seq = LineString([(x, x) for x in range(2)]).coord_seq
        for i in (0, 1):
            with self.subTest(i):
                self.assertEqual(coord_seq[i], (i, i))
        for i in (-3, 10):
            msg = f"Invalid GEOS Geometry index: {i}"
            with self.subTest(i):
                with self.assertRaisesMessage(IndexError, msg):
                    coord_seq[i]

    @skipIf(geos_version_tuple() < (3, 14), "GEOS M support requires 3.14+")
    def test_has_m(self):
        geom = GEOSGeometry("POINT ZM (1 2 3 4)")
        coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
        self.assertIs(coord_seq.hasm, True)

        geom = GEOSGeometry("POINT Z (1 2 3)")
        coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
        self.assertIs(coord_seq.hasm, False)

        geom = GEOSGeometry("POINT M (1 2 3)")
        coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)
        self.assertIs(coord_seq.hasm, True)

    @skipIf(geos_version_tuple() < (3, 14), "GEOS M support requires 3.14+")
    def test_get_set_m(self):
        geom = GEOSGeometry("POINT ZM (1 2 3 4)")
        coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=True)
        self.assertEqual(coord_seq.tuple, (1, 2, 3, 4))
        self.assertEqual(coord_seq.getM(0), 4)
        coord_seq.setM(0, 10)
        self.assertEqual(coord_seq.tuple, (1, 2, 3, 10))
        self.assertEqual(coord_seq.getM(0), 10)

        geom = GEOSGeometry("POINT M (1 2 4)")
        coord_seq = GEOSCoordSeq(capi.get_cs(geom.ptr), z=False)