From 4217f64c207c4750ac7bffd759c768f0daefd01d Mon Sep 17 00:00:00 2001 From: Sofia Paganin Date: Wed, 15 Jun 2022 21:19:39 -0700 Subject: [PATCH 1/2] add invalid type validation and test --- cligj/features.py | 2 ++ tests/test_features.py | 8 ++++++++ tests/twopoints_invalid_type.txt | 2 ++ 3 files changed, 12 insertions(+) create mode 100644 tests/twopoints_invalid_type.txt diff --git a/cligj/features.py b/cligj/features.py index a7ecc06..b97fdcc 100644 --- a/cligj/features.py +++ b/cligj/features.py @@ -125,6 +125,8 @@ def iter_features(geojsonfile, func=None): newfeat = func(to_feature(json.loads(line))) if newfeat: yield newfeat + else: + raise TypeError("Type must be Feature or FeatureCollection") # Indented or pretty-printed GeoJSON features or feature # collections will fail out of the try clause above since diff --git a/tests/test_features.py b/tests/test_features.py index 43eea90..d972cf1 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -132,6 +132,13 @@ def test_geometrypretty(expected_features): features = normalize_feature_inputs(None, 'features', ["tests/point_pretty_geom.txt"]) assert _geoms(features)[0] == _geoms(expected_features)[0] + +def test_invalid_type(): + features = normalize_feature_inputs(None, 'features', ["tests/twopoints_invalid_type.txt"]) + with pytest.raises(TypeError): + _geoms(features) + + class MockGeo(object): def __init__(self, feature): self.__geo_interface__ = feature @@ -149,6 +156,7 @@ def test_normalize_feature_objects_bad(expected_features): with pytest.raises(ValueError): list(normalize_feature_objects(objs)) + def test_to_feature(expected_features): geom = expected_features[0]['geometry'] feat = {'type': 'Feature', 'properties': {}, 'geometry': geom} diff --git a/tests/twopoints_invalid_type.txt b/tests/twopoints_invalid_type.txt new file mode 100644 index 0000000..5b2c1b0 --- /dev/null +++ b/tests/twopoints_invalid_type.txt @@ -0,0 +1,2 @@ +{"type": "MultiPoint", "geometry": [[134.519974,34.084171],[134.546188,34.081322],[134.546219,34.078766],[134.542374,34.081287]]} +{"type": "MultiPoint", "geometry": [[130.405380,33.571598],[130.383682,33.567059],[130.486740,33.652233],[130.455490,33.597004],[130.437195,33.601784],[130.447998,33.654606],[130.409592,33.606941]]} \ No newline at end of file From dfd5fa366d60d1f44ea923faae748472b4e4b6c4 Mon Sep 17 00:00:00 2001 From: Sofia Paganin Date: Tue, 21 Jun 2022 19:02:37 -0700 Subject: [PATCH 2/2] update error message --- cligj/features.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cligj/features.py b/cligj/features.py index b97fdcc..9b2b904 100644 --- a/cligj/features.py +++ b/cligj/features.py @@ -126,7 +126,7 @@ def iter_features(geojsonfile, func=None): if newfeat: yield newfeat else: - raise TypeError("Type must be Feature or FeatureCollection") + raise TypeError("Invalid type") # Indented or pretty-printed GeoJSON features or feature # collections will fail out of the try clause above since