Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion geojson/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ def __init__(self, features, **extra):
]

def errors(self):
return self.check_list_errors(lambda x: x.errors(), self.features)
return self.check_list_errors(_feature_errors, self.features)

def __getitem__(self, key):
try:
return self.get("features", ())[key]
except (KeyError, TypeError, IndexError):
return super(GeoJSON, self).__getitem__(key)


def _feature_errors(feature):
if not isinstance(feature, Feature):
return "FeatureCollection features must be Features"
return feature.errors()
28 changes: 28 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,31 @@ def test_valid_geometrycollection(self):
geometries=[point, poly]
)
self.assertTrue(geom_collection.is_valid)


class TestValidationFeatureCollection(unittest.TestCase):

def test_featurecollection_rejects_geometry_items(self):
collection = geojson.FeatureCollection([geojson.Point((0, 0))])

self.assertFalse(collection.is_valid)
self.assertIn("FeatureCollection features must be Features",
collection.errors())

def test_featurecollection_rejects_nested_featurecollections(self):
collection = geojson.FeatureCollection([
geojson.FeatureCollection([
geojson.Feature(geometry=geojson.Point((0, 0)))
])
])

self.assertFalse(collection.is_valid)
self.assertIn("FeatureCollection features must be Features",
collection.errors())

def test_valid_featurecollection(self):
collection = geojson.FeatureCollection([
geojson.Feature(geometry=geojson.Point((0, 0)))
])

self.assertTrue(collection.is_valid)