|
| 1 | +import io |
| 2 | + |
| 3 | +import pytest |
| 4 | +from hypothesis import given |
| 5 | +from hypothesis.strategies import ( |
| 6 | + builds, |
| 7 | + floats, |
| 8 | + integers, |
| 9 | + none, |
| 10 | + one_of, |
| 11 | +) |
| 12 | + |
| 13 | +import shapefile as shp |
| 14 | + |
| 15 | +float_nums = floats(allow_nan=False, allow_infinity=False) |
| 16 | + |
| 17 | +points_2D = builds(shp.Point, float_nums, float_nums, one_of(none(), integers())) |
| 18 | +pointMs = builds( |
| 19 | + shp.PointM, |
| 20 | + float_nums, |
| 21 | + float_nums, |
| 22 | + one_of(none(), float_nums), |
| 23 | + one_of(none(), integers()), |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.hypothesis |
| 28 | +@given(expected=points_2D, i=integers(min_value=1)) |
| 29 | +def test_Point_2D_roundtrips( |
| 30 | + expected: shp.Point, |
| 31 | + i: int, |
| 32 | +) -> None: |
| 33 | + stream = io.BytesIO() |
| 34 | + n = shp.Point.write_to_byte_stream(b_io=stream, s=expected, i=i) |
| 35 | + assert n == stream.tell() |
| 36 | + stream.seek(0) |
| 37 | + actual = shp.Point.from_byte_stream( |
| 38 | + shapeType=shp.POINT, |
| 39 | + b_io=stream, |
| 40 | + next_shape_pos=n, |
| 41 | + oid=expected.oid, |
| 42 | + bbox=None, |
| 43 | + ) |
| 44 | + assert isinstance(actual, shp.Point) |
| 45 | + assert actual.points == expected.points |
| 46 | + assert actual.oid == expected.oid |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.hypothesis |
| 50 | +@given(expected=pointMs, i=integers(min_value=1)) |
| 51 | +def test_Point_M_roundtrips( |
| 52 | + expected: shp.Point, |
| 53 | + i: int, |
| 54 | +) -> None: |
| 55 | + stream = io.BytesIO() |
| 56 | + n = shp.PointM.write_to_byte_stream(b_io=stream, s=expected, i=i) |
| 57 | + assert n == stream.tell() |
| 58 | + stream.seek(0) |
| 59 | + actual = shp.PointM.from_byte_stream( |
| 60 | + shapeType=shp.POINTM, |
| 61 | + b_io=stream, |
| 62 | + next_shape_pos=n, |
| 63 | + oid=expected.oid, |
| 64 | + bbox=None, |
| 65 | + ) |
| 66 | + assert isinstance(actual, shp.PointM) |
| 67 | + assert actual.points == expected.points |
| 68 | + assert actual.m == expected.m |
| 69 | + assert actual.oid == expected.oid |
0 commit comments