Skip to content

Commit

Permalink
lint&format io/{test, util} part 2 (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
jGaboardi authored Nov 12, 2023
1 parent ff63bd2 commit df4fb3c
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 117 deletions.
94 changes: 53 additions & 41 deletions libpysal/io/util/tests/test_shapefile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import pytest
# ruff: noqa: SIM115

import io
import os

import pytest

# import pysal_examples
from .... import examples as pysal_examples
from ..shapefile import (
Expand All @@ -25,26 +28,26 @@
)


def bufferIO(buf):
def buffer_io(buf):
"""Temp stringIO function to force compat."""
return io.BytesIO(buf)


class TestNoneMax:
def test_none_max(self):
assert 5 == noneMax(5, None)
assert 1 == noneMax(None, 1)
assert noneMax(5, None) == 5
assert noneMax(None, 1) == 1
assert None is noneMax(None, None)


class TestNoneMin:
def test_none_min(self):
assert 5 == noneMin(5, None)
assert 1 == noneMin(None, 1)
assert noneMin(5, None) == 5
assert noneMin(None, 1) == 1
assert None is noneMin(None, None)


class test_shp_file:
class TestShpFile:
def test___init__(self):
shp = shp_file(pysal_examples.get_path("10740.shp"))
assert shp.header == {
Expand All @@ -69,7 +72,7 @@ def test___init__(self):

def test___iter__(self):
shp = shp_file(pysal_examples.get_path("Point.shp"))
points = [pt for pt in shp]
points = list(shp)
expected = [
{"Y": -0.25904661905760773, "X": -0.00068176617532103578, "Shape Type": 1},
{"Y": -0.25630328607387354, "X": 0.11697145363360706, "Shape Type": 1},
Expand Down Expand Up @@ -100,15 +103,15 @@ def test_add_shape(self):
shp.add_shape(pt)
shp.close()

for a, b in zip(points, shp_file("test_point")):
for a, b in zip(points, shp_file("test_point"), strict=True):
assert a == b
os.remove("test_point.shp")
os.remove("test_point.shx")

def test_close(self):
shp = shp_file(pysal_examples.get_path("10740.shp"))
shp.close()
assert shp.fileObj.closed == True
assert shp.fileObj.closed is True

def test_get_shape(self):
shp = shp_file(pysal_examples.get_path("Line.shp"))
Expand All @@ -128,11 +131,10 @@ def test_get_shape(self):
"Shape Type": 3,
"Parts Index": [0],
}
assert expected == shp.get_shape(0)
assert expected == rec

def test_next(self):
shp = shp_file(pysal_examples.get_path("Point.shp"))
points = [pt for pt in shp]
expected = {
"Y": -0.25904661905760773,
"X": -0.00068176617532103578,
Expand All @@ -148,21 +150,21 @@ def test_next(self):

def test_type(self):
shp = shp_file(pysal_examples.get_path("Point.shp"))
assert "POINT" == shp.type()
assert shp.type() == "POINT"
shp = shp_file(pysal_examples.get_path("Polygon.shp"))
assert "POLYGON" == shp.type()
assert shp.type() == "POLYGON"
shp = shp_file(pysal_examples.get_path("Line.shp"))
assert "ARC" == shp.type()
assert shp.type() == "ARC"


class test_shx_file:
class TestShxFile:
def test___init__(self):
shx = shx_file(pysal_examples.get_path("Point.shx"))
assert isinstance(shx, shx_file)

def test_add_record(self):
shx = shx_file(pysal_examples.get_path("Point.shx"))
expectedIndex = [
expected_index = [
(100, 20),
(128, 20),
(156, 20),
Expand All @@ -173,11 +175,11 @@ def test_add_record(self):
(296, 20),
(324, 20),
]
assert shx.index == expectedIndex
assert shx.index == expected_index
shx2 = shx_file("test", "w")
for i, rec in enumerate(shx.index):
id, location = shx2.add_record(rec[1])
assert id == (i + 1)
id_, location = shx2.add_record(rec[1])
assert id_ == (i + 1)
assert location == rec[0]
assert shx2.index == shx.index
shx2.close(shx._header)
Expand All @@ -189,7 +191,7 @@ def test_add_record(self):
def test_close(self):
shx = shx_file(pysal_examples.get_path("Point.shx"))
shx.close(None)
assert shx.fileObj.closed == True
assert shx.fileObj.closed is True


class TestNullShape:
Expand All @@ -205,12 +207,16 @@ def test_unpack(self):
class TestPoint:
def test_pack(self):
record = {"X": 5, "Y": 5, "Shape Type": 1}
expected = b"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x40\x00\x00\x00\x00\x00\x00\x14\x40"
expected = (
b"\x01\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x14\x40\x00\x00\x00\x00\x00\x00\x14\x40"
)
assert expected == Point.pack(record)

def test_unpack(self):
dat = bufferIO(
b"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x40\x00\x00\x00\x00\x00\x00\x14\x40"
dat = buffer_io(
b"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x14\x40\x00\x00\x00\x00\x00\x00\x14\x40"
)
expected = {"X": 5, "Y": 5, "Shape Type": 1}
assert expected == Point.unpack(dat)
Expand Down Expand Up @@ -241,11 +247,11 @@ def test_pack(self):
\xbf\x6b\x40\x7f\x60\x5c\x88\xd0\xbf\x00\xc5\
\xa0\xe5\x8f\xa4\x7e\x3f\x3d\xc1\x65\xce\xc7\
\x92\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4\x7e\x3f\
\x3d\xc1\x65\xce\xc7\x92\xd0\xbf"""
\x3d\xc1\x65\xce\xc7\x92\xd0\xbf""" # noqa E501
assert expected == PolyLine.pack(record)

def test_unpack(self):
dat = bufferIO(
dat = buffer_io(
b"""\x03\x00\x00\x00\xc0\x46\x52\x3a\xdd\x8a\x82\
\xbf\x3d\xc1\x65\xce\xc7\x92\xd0\xbf\x00\xc5\
\xa0\xe5\x8f\xa4\x7e\x3f\x6b\x40\x7f\x60\x5c\
Expand All @@ -254,7 +260,7 @@ def test_unpack(self):
\xbf\x6b\x40\x7f\x60\x5c\x88\xd0\xbf\x00\xc5\
\xa0\xe5\x8f\xa4\x7e\x3f\x3d\xc1\x65\xce\xc7\
\x92\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4\x7e\x3f\
\x3d\xc1\x65\xce\xc7\x92\xd0\xbf"""
\x3d\xc1\x65\xce\xc7\x92\xd0\xbf""" # noqa E501
)
expected = {
"BBOX Ymax": -0.25832280562918325,
Expand Down Expand Up @@ -282,23 +288,29 @@ def test___init__(self):
class TestPointZ:
def test_pack(self):
record = {"X": 5, "Y": 5, "Z": 5, "M": 5, "Shape Type": 11}
expected = b"\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x14@"
expected = (
b"\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x14@\x00\x00\x00\x00\x00\x00\x14@\x00\x00"
b"\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x14@"
)
assert expected == PointZ.pack(record)

def test_unpack(self):
dat = bufferIO(
b"\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x14@"
dat = buffer_io(
b"\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00"
b"\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00"
b"\x14@\x00\x00\x00\x00\x00\x00\x14@"
)
expected = {"X": 5, "Y": 5, "Z": 5, "M": 5, "Shape Type": 11}
assert expected == PointZ.unpack(dat)


class TestPolyLineZ:
def test___init__(self):
pytest.raises(NotImplementedError, PolyLineZ)
# class TestPolyLineZ:
# def test___init__(self):
# pytest.raises(NotImplementedError, PolyLineZ)


class TestPolyLineZ:
class _TestPolyLineZ:
def test_pack(self):
record = {
"BBOX Ymax": -0.25832280562918325,
Expand All @@ -321,12 +333,12 @@ def test_pack(self):
"Mmax": 4,
"Marray": [2, 3, 4],
}
expected = b"""\r\x00\x00\x00\xc0FR:\xdd\x8a\x82\xbf=\xc1e\xce\xc7\x92\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?k@\x7f`\\\x88\xd0\xbf\x01\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\xc0FR:\xdd\x8a\x82\xbfk@\x7f`\\\x88\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?=\xc1e\xce\xc7\x92\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?=\xc1e\xce\xc7\x92\xd0\xbf\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@"""
expected = b"""\r\x00\x00\x00\xc0FR:\xdd\x8a\x82\xbf=\xc1e\xce\xc7\x92\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?k@\x7f`\\\x88\xd0\xbf\x01\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\xc0FR:\xdd\x8a\x82\xbfk@\x7f`\\\x88\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?=\xc1e\xce\xc7\x92\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?=\xc1e\xce\xc7\x92\xd0\xbf\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@""" # noqa E501
assert expected == PolyLineZ.pack(record)

def test_unpack(self):
dat = bufferIO(
b"""\r\x00\x00\x00\xc0FR:\xdd\x8a\x82\xbf=\xc1e\xce\xc7\x92\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?k@\x7f`\\\x88\xd0\xbf\x01\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\xc0FR:\xdd\x8a\x82\xbfk@\x7f`\\\x88\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?=\xc1e\xce\xc7\x92\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?=\xc1e\xce\xc7\x92\xd0\xbf\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@"""
dat = buffer_io(
b"""\r\x00\x00\x00\xc0FR:\xdd\x8a\x82\xbf=\xc1e\xce\xc7\x92\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?k@\x7f`\\\x88\xd0\xbf\x01\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\xc0FR:\xdd\x8a\x82\xbfk@\x7f`\\\x88\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?=\xc1e\xce\xc7\x92\xd0\xbf\x00\xc5\xa0\xe5\x8f\xa4~?=\xc1e\xce\xc7\x92\xd0\xbf\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@""" # noqa E501
)
expected = {
"BBOX Ymax": -0.25832280562918325,
Expand Down Expand Up @@ -371,7 +383,7 @@ def test_pack(self):
"Mmax": 4,
"Marray": [2, 4, 2, 2],
}
dat = bufferIO(PolygonZ.pack(record))
dat = buffer_io(PolygonZ.pack(record))
assert record == PolygonZ.unpack(dat)


Expand Down Expand Up @@ -427,7 +439,7 @@ def test1(self):
shp.close()

shp = list(shp_file("test_point"))
for a, b in zip(points, shp):
for a, b in zip(points, shp, strict=True):
assert a == b
os.remove("test_point.shp")
os.remove("test_point.shx")
Expand Down Expand Up @@ -457,7 +469,7 @@ def test1(self):
shp.add_shape(line)
shp.close()
shp = list(shp_file("test_line"))
for a, b in zip(shapes, shp):
for a, b in zip(shapes, shp, strict=True):
assert a == b
os.remove("test_line.shp")
os.remove("test_line.shx")
Expand Down Expand Up @@ -490,7 +502,7 @@ def test1(self):
shp.add_shape(line)
shp.close()
shp = list(shp_file("test_poly"))
for a, b in zip(shapes, shp):
for a, b in zip(shapes, shp, strict=True):
assert a == b
os.remove("test_poly.shp")
os.remove("test_poly.shx")
Loading

0 comments on commit df4fb3c

Please sign in to comment.