Skip to content

Commit

Permalink
test: initial commit adding hypothesis property testing library
Browse files Browse the repository at this point in the history
Adding the Hypothesis cache dir to .gitignore
Grammar fixes in geometry.py
Hypothesis dep in pyproject.toml without version peg
Initial strategies in conftest.py. This is the only file name I could use that didn't cause an issue with the name-tests-test pre-commit hook. 'strategies.py' would be better because strictly speaking these aren't fixtures.
An example test for encode/decode invariance.
  • Loading branch information
bpshaver committed Nov 5, 2023
1 parent c24953e commit dc6c700
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ venv/
__pycache__/
*.stderr*
docs/_*
.hypothesis/
6 changes: 3 additions & 3 deletions pygeoif/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def convex_hull(self) -> Optional[Union["Point", "LineString", "Polygon"]]:
Returns a representation of the smallest convex Polygon containing
all the points in the object unless the number of points in the object
is less than three.
is fewer than three.
For two points, the convex hull collapses to a LineString;
for 1, to a Point.
"""
Expand Down Expand Up @@ -271,7 +271,7 @@ def is_empty(self) -> bool:
"""
Return if this geometry is empty.
A Point is considered empty when it has less than 2 coordinates.
A Point is considered empty when it has fewer than 2 coordinates.
"""
return len(self._geoms) < 2 # noqa: PLR2004

Expand Down Expand Up @@ -382,7 +382,7 @@ def is_empty(self) -> bool:
"""
Return if this geometry is empty.
A Linestring is considered empty when it has less than 2 points.
A Linestring is considered empty when it has fewer than 2 points.
"""
return len(self._geoms) < 2 # noqa: PLR2004

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ linting = [
"yamllint",
]
tests = [
"hypothesis",
"pytest",
"pytest-cov",
]
Expand Down
52 changes: 52 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Data-generating strategies for property-based testing."""
import hypothesis.strategies as st

# Incomplete list of allowed spatial reference systems to generate data
# - EPSG 4326
# - EPSG 3857
# ...?


# EPSG:4326 primitives
latitudes = st.floats(
min_value=-90.0,
max_value=90.0,
allow_nan=False,
allow_infinity=False,
)
longitudes = st.floats(
min_value=-180.0,
max_value=180.0,
allow_nan=False,
allow_infinity=False,
)
elevations = st.floats(allow_nan=False, allow_infinity=False)


# Point2D
@st.composite
def points_2d(draw, srs="EPSG:4326"):
if srs == "EPSG:4326":
return draw(st.tuples(latitudes, longitudes))
raise NotImplementedError

Check warning on line 31 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L31

Added line #L31 was not covered by tests


# Point3D
@st.composite
def points_3d(draw, srs="EPSG:4326"):
if srs == "EPSG:4326":
return draw(st.tuples(latitudes, longitudes, elevations))
raise NotImplementedError

Check warning on line 39 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L39

Added line #L39 was not covered by tests


# PointType
@st.composite
def points(draw, srs="EPSG:4326"):
if srs == "EPSG:4326":
return draw(st.one_of(points_2d(), points_3d()))
raise NotImplementedError

Check warning on line 47 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L47

Added line #L47 was not covered by tests


# LineType

# Geometries
9 changes: 9 additions & 0 deletions tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from unittest import mock

import pytest
from hypothesis import given

from pygeoif import geometry
from pygeoif.exceptions import DimensionError
from tests.conftest import points


def test_empty() -> None:
Expand Down Expand Up @@ -250,3 +252,10 @@ def test_hash_empty() -> None:
point = geometry.Point(None, None)

assert hash(point) == hash(())


@given(points("EPSG:4326"))
def test_repr_eval_hypothesis_epsg_4326(point) -> None:
point = geometry.Point(*point)

assert eval(repr(point), {}, {"Point": geometry.Point}) == point

0 comments on commit dc6c700

Please sign in to comment.