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

Code before:
                self.assertEqual(ref, geom)

        # Should only accept string objects.
        bad_input = (1, 5.23, None, False, memoryview(b"foo"))
        msg = "'wkt' must be bytes or str (got {} instead)."
        for bad_wkt in bad_input:
            with (
                self.subTest(bad_wkt=bad_wkt),
                self.assertRaisesMessage(TypeError, msg.format(bad_wkt)),
            ):
                wkt_r.read(bad_wkt)

    def test02_wktwriter(self):
        # Creating a WKTWriter instance, testing its ptr property.
        wkt_w = WKTWriter()
        msg = "Incompatible pointer type: "
        with self.assertRaisesMessage(TypeError, msg):
            wkt_w.ptr = WKTReader.ptr_type()

        ref = GEOSGeometry("POINT (5 23)")
        ref_wkt = "POINT (5.0000000000000000 23.0000000000000000)"
        self.assertEqual(ref_wkt, wkt_w.write(ref).decode())

    def test_wktwriter_constructor_arguments(self):
        wkt_w = WKTWriter(dim=3, trim=True, precision=3)
        ref = GEOSGeometry("POINT (5.34562 23 1.5)")
        if geos_version_tuple() > (3, 10):
            ref_wkt = "POINT Z (5.346 23 1.5)"
        else:
            ref_wkt = "POINT Z (5.35 23 1.5)"
        self.assertEqual(ref_wkt, wkt_w.write(ref).decode())

    def test03_wkbreader(self):
        # Creating a WKBReader instance
        wkb_r = WKBReader()

        hex_bin = b"000000000140140000000000004037000000000000"
        hex_str = "000000000140140000000000004037000000000000"
        wkb = memoryview(binascii.a2b_hex(hex_bin))
        ref = GEOSGeometry(hex_bin)

        # read() should return a GEOSGeometry on either a hex string or
        # a WKB buffer.
        g1 = wkb_r.read(wkb)
        g2 = wkb_r.read(hex_bin)
        g3 = wkb_r.read(hex_str)
        for geom in (g1, g2, g3):
            with self.subTest(geom=geom):
                self.assertEqual(ref, geom)

        bad_input = (1, 5.23, None, False)

Review comment (suggestion):
Thanks all. David proposed separately that we adjust the github matrix to stop testing dropped GEOS versions, but just noting that the worfklow "stable" using GEOS 3.9 still passed even with the test change.

Code after:

        # Should only accept string objects.
        bad_input = (1, 5.23, None, False, memoryview(b"foo"))
        msg = "'wkt' must be bytes or str (got {} instead)."
        for bad_wkt in bad_input:
            with (
                self.subTest(bad_wkt=bad_wkt),
                self.assertRaisesMessage(TypeError, msg.format(bad_wkt)),
            ):
                wkt_r.read(bad_wkt)

    def test02_wktwriter(self):
        # Creating a WKTWriter instance, testing its ptr property.
        wkt_w = WKTWriter()
        msg = "Incompatible pointer type: "
        with self.assertRaisesMessage(TypeError, msg):
            wkt_w.ptr = WKTReader.ptr_type()

        ref = GEOSGeometry("POINT (5 23)")
        ref_wkt = "POINT (5.0000000000000000 23.0000000000000000)"
        self.assertEqual(ref_wkt, wkt_w.write(ref).decode())

    def test_wktwriter_constructor_arguments(self):
        wkt_w = WKTWriter(dim=3, trim=True, precision=3)
        ref = GEOSGeometry("POINT (5.34562 23 1.5)")
        ref_wkt = "POINT Z (5.346 23 1.5)"
        self.assertEqual(ref_wkt, wkt_w.write(ref).decode())

    def test03_wkbreader(self):
        # Creating a WKBReader instance
        wkb_r = WKBReader()

        hex_bin = b"000000000140140000000000004037000000000000"
        hex_str = "000000000140140000000000004037000000000000"
        wkb = memoryview(binascii.a2b_hex(hex_bin))
        ref = GEOSGeometry(hex_bin)

        # read() should return a GEOSGeometry on either a hex string or
        # a WKB buffer.
        g1 = wkb_r.read(wkb)
        g2 = wkb_r.read(hex_bin)
        g3 = wkb_r.read(hex_str)
        for geom in (g1, g2, g3):
            with self.subTest(geom=geom):
                self.assertEqual(ref, geom)

        bad_input = (1, 5.23, None, False)
        msg = "'wkb' must be bytes, str or memoryview (got {} instead)."
        for bad_wkb in bad_input:
            with (
                self.subTest(bad_wkb=bad_wkb),