From 8f1de59e6ab6cfe980dd61614f984da6b1ff96ab Mon Sep 17 00:00:00 2001 From: uchiha_vivek Date: Fri, 27 Sep 2024 22:45:05 +0530 Subject: [PATCH 1/3] added hypothesis tests to test_feature.py --- tests/test_feature.py | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/test_feature.py b/tests/test_feature.py index 641dcae..e092417 100644 --- a/tests/test_feature.py +++ b/tests/test_feature.py @@ -3,11 +3,49 @@ 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( + 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( @@ -100,6 +138,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 + + @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 + def test_featurecollection(self) -> None: pytest.raises(TypeError, feature.FeatureCollection) pytest.raises(TypeError, feature.FeatureCollection, None) From e2df2ca6bdc0f6650be39dd3c2446f56648ab4fa Mon Sep 17 00:00:00 2001 From: uchiha_vivek Date: Fri, 27 Sep 2024 23:01:02 +0530 Subject: [PATCH 2/3] added hypothesis tests to test_feature.py --- tests/test_feature.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_feature.py b/tests/test_feature.py index e092417..f4dba2d 100644 --- a/tests/test_feature.py +++ b/tests/test_feature.py @@ -1,4 +1,4 @@ -"""Test Feature and FeatureCollection.""" +# """Test Feature and FeatureCollection.""" import unittest @@ -19,8 +19,8 @@ @composite def polygons(draw): - """Generate a polygon geometry""" - """The polygon is closed""" + # """Generate a polygon geometry""" + # """The polygon is closed""" coords = draw(coordinates()) coords.append(coords[0]) return geometry.Polygon(coords) @@ -28,7 +28,7 @@ def polygons(draw): @composite def coordinates(draw): - """Generate a list of coordinates for geometries""" + # """Generate a list of coordinates for geometries""" return draw( lists( tuples( @@ -42,7 +42,7 @@ def coordinates(draw): @composite def properties(draw): - """Generate random properties""" + # """Generate random properties""" return draw(dictionaries(text(), one_of(text(), floats(), none()), max_size=5)) From d6226635648b5be54f946154f89462e16345b4d0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 17:31:28 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_feature.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_feature.py b/tests/test_feature.py index f4dba2d..a23dfee 100644 --- a/tests/test_feature.py +++ b/tests/test_feature.py @@ -32,7 +32,8 @@ def coordinates(draw): return draw( lists( tuples( - floats(min_value=180, max_value=180), floats(min_value=90, max_value=90), + floats(min_value=180, max_value=180), + floats(min_value=90, max_value=90), ), min_size=3, max_size=10,