Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added hypothesis tests to test_feature.py #222 #251

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion tests/test_feature.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
"""Test Feature and FeatureCollection."""
# """Test Feature and FeatureCollection."""

import unittest

import pytest
from hypothesis import given
from hypothesis.strategies import composite
from hypothesis.strategies import dictionaries
from hypothesis.strategies import floats
from hypothesis.strategies import lists
from hypothesis.strategies import none
from hypothesis.strategies import one_of
from hypothesis.strategies import text
from hypothesis.strategies import tuples

from pygeoif import feature
from pygeoif import geometry


@composite
def polygons(draw):
# """Generate a polygon geometry"""
# """The polygon is closed"""
coords = draw(coordinates())
coords.append(coords[0])
return geometry.Polygon(coords)


@composite
def coordinates(draw):
# """Generate a list of coordinates for geometries"""
return draw(
lists(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Consider testing coordinates outside the valid range

The current implementation limits latitude and longitude to their maximum valid ranges. It might be useful to test how the Feature class handles invalid coordinates (e.g., latitudes > 90 or < -90, longitudes > 180 or < -180) to ensure proper error handling or validation.

@composite
def coordinates(draw):
    """Generate a list of coordinates for geometries, including invalid ones"""
    return draw(
        lists(
            tuples(
                floats(min_value=-200, max_value=200), floats(min_value=-100, max_value=100),
            ),
            min_size=3,
            max_size=10,
        ),
    )

tuples(
floats(min_value=180, max_value=180),
floats(min_value=90, max_value=90),
),
min_size=3,
max_size=10,
),
)


@composite
def properties(draw):
# """Generate random properties"""
return draw(dictionaries(text(), one_of(text(), floats(), none()), max_size=5))


class TestFeature:
def setup_method(self) -> None:
self.a = geometry.Polygon(
Expand Down Expand Up @@ -100,6 +139,21 @@ def test_feature_repr_eval(self) -> None:
== self.f2.__geo_interface__
)

@given(polygon=polygons(), props=properties())
def test_feature_with_random_data(self, polygon, props) -> None:
f = feature.Feature(polygon, props)
assert isinstance(f, feature.Feature)
assert f.geometry == polygon
assert f.properties == props

Comment on lines +142 to +147
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Add assertions for Feature's geo_interface

The test_feature_with_random_data function could benefit from additional assertions to verify the correctness of the Feature's geo_interface attribute. This would ensure that the GeoJSON representation of the Feature is correctly generated for various input combinations.

@given(polygon=polygons(), props=properties())
def test_feature_with_random_data(self, polygon, props) -> None:
    f = feature.Feature(polygon, props)
    assert isinstance(f, feature.Feature)
    assert f.geometry == polygon
    assert f.properties == props
    assert f.__geo_interface__ == {
        "type": "Feature",
        "geometry": polygon.__geo_interface__,
        "properties": props
    }

@given(polygon=polygons())
def test_feature_collection_with_random_features(self, polygon) -> None:
f1 = feature.Feature(polygon)
f2 = feature.Feature(polygon)
fc = feature.FeatureCollection([f1, f2])
assert isinstance(fc, feature.FeatureCollection)
assert len(fc) == 2

Comment on lines +149 to +155
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Extend FeatureCollection test with varying number of features

The current test for FeatureCollection always uses two features. Consider parameterizing the test to create FeatureCollections with a varying number of features, including edge cases like empty collections and large collections.

Suggested change
@given(polygon=polygons())
def test_feature_collection_with_random_features(self, polygon) -> None:
f1 = feature.Feature(polygon)
f2 = feature.Feature(polygon)
fc = feature.FeatureCollection([f1, f2])
assert isinstance(fc, feature.FeatureCollection)
assert len(fc) == 2
@given(polygon=polygons(), num_features=st.integers(min_value=0, max_value=100))
def test_feature_collection_with_random_features(self, polygon, num_features) -> None:
features = [feature.Feature(polygon) for _ in range(num_features)]
fc = feature.FeatureCollection(features)
assert isinstance(fc, feature.FeatureCollection)
assert len(fc) == num_features

def test_featurecollection(self) -> None:
pytest.raises(TypeError, feature.FeatureCollection)
pytest.raises(TypeError, feature.FeatureCollection, None)
Expand Down
Loading