@@ -215,70 +215,91 @@ def test_geojson_tooltip():
215215 assert issubclass (w [- 1 ].category , UserWarning ), 'GeoJsonTooltip GeometryCollection test failed.'
216216
217217
218- def test_geojson_search_properties ():
219- features = [{'properties' : None } for _ in range (3 )]
220- assert GeoJson ._search_properties (features , 'properties' ) is None
221- features = [{'properties' : {'hi' : 'there' }},
222- {'properties' : {'hi' : 'what' }}]
223- assert GeoJson ._search_properties (features , 'properties' ) == 'feature.properties.hi'
224- features = [{'properties' : {'hi' : {'more' : 'some value' }}},
225- {'properties' : {'hi' : {'more' : 'another value' }}}]
226- assert (GeoJson ._search_properties (features , 'properties' )
227- == 'feature.properties.hi.more' )
228- features = [{'properties' : {'hi' : 'there' }},
229- {'properties' : {'hi' : 'there' }}]
230- assert GeoJson ._search_properties (features , 'properties' ) is None
231- features = [{'properties' : {'hi' : 'there' }},
232- {'properties' : {'hi' : None }}]
233- assert GeoJson ._search_properties (features , 'properties' ) is None
234- features = [{'properties' : {'hi' : 'there' }},
235- {'properties' : 42 }]
236- assert GeoJson ._search_properties (features , 'properties' ) is None
237- features = [{'properties' : [42 , 43 ]},
238- {'properties' : [1 , 2 ]}]
239- assert GeoJson ._search_properties (features , 'properties' ) is None
240-
241-
242218def test_geojson_find_identifier ():
243219
244- def _create (properties ):
220+ def _create (* properties ):
245221 return {"type" : "FeatureCollection" , "features" : [
246- {"type" : "Feature" ,
247- "properties" : properties }
248- ]}
222+ {"type" : "Feature" , "properties" : item }
223+ for item in properties ]}
249224
250- data_bare = _create (None )
251- data_with_id = _create (None )
225+ def _assert_id_got_added (data ):
226+ _geojson = GeoJson (data )
227+ assert _geojson .find_identifier () == 'feature.id'
228+ assert _geojson .data ['features' ][0 ]['id' ] == '0'
229+
230+ data_with_id = _create (None , None )
252231 data_with_id ['features' ][0 ]['id' ] = 'this-is-an-id'
253- data_with_unique_property = _create ({
254- 'property-key' : 'some-value' ,
255- })
232+ data_with_id ['features' ][1 ]['id' ] = 'this-is-another-id'
233+ geojson = GeoJson (data_with_id )
234+ assert geojson .find_identifier () == 'feature.id'
235+ assert geojson .data ['features' ][0 ]['id' ] == 'this-is-an-id'
236+
237+ data_with_unique_properties = _create (
238+ {'property-key' : 'some-value' },
239+ {'property-key' : 'another-value' },
240+ )
241+ geojson = GeoJson (data_with_unique_properties )
242+ assert geojson .find_identifier () == 'feature.properties.property-key'
243+
244+ data_with_unique_properties = _create (
245+ {'property-key' : 42 },
246+ {'property-key' : 43 },
247+ {'property-key' : 'or a string' },
248+ )
249+ geojson = GeoJson (data_with_unique_properties )
250+ assert geojson .find_identifier () == 'feature.properties.property-key'
251+
252+ # The test cases below have no id field or unique property,
253+ # so an id will be added to the data.
254+
255+ data_with_identical_ids = _create (None , None )
256+ data_with_identical_ids ['features' ][0 ]['id' ] = 'identical-ids'
257+ data_with_identical_ids ['features' ][1 ]['id' ] = 'identical-ids'
258+ _assert_id_got_added (data_with_identical_ids )
259+
260+ data_with_some_missing_ids = _create (None , None )
261+ data_with_some_missing_ids ['features' ][0 ]['id' ] = 'this-is-an-id'
262+ # the second feature doesn't have an id
263+ _assert_id_got_added (data_with_some_missing_ids )
264+
265+ data_with_identical_properties = _create (
266+ {'property-key' : 'identical-value' },
267+ {'property-key' : 'identical-value' },
268+ )
269+ _assert_id_got_added (data_with_identical_properties )
270+
271+ data_bare = _create (None )
272+ _assert_id_got_added (data_bare )
273+
274+ data_empty_dict = _create ({})
275+ _assert_id_got_added (data_empty_dict )
276+
277+ data_without_properties = _create (None )
278+ del data_without_properties ['features' ][0 ]['properties' ]
279+ _assert_id_got_added (data_without_properties )
280+
281+ data_some_without_properties = _create ({'key' : 'value' }, 'will be deleted' )
282+ # the first feature has properties, but the second doesn't
283+ del data_some_without_properties ['features' ][1 ]['properties' ]
284+ _assert_id_got_added (data_some_without_properties )
285+
256286 data_with_nested_properties = _create ({
257287 "summary" : {"distance" : 343.2 },
258288 "way_points" : [3 , 5 ],
259289 })
290+ _assert_id_got_added (data_with_nested_properties )
291+
260292 data_with_incompatible_properties = _create ({
261293 "summary" : {"distances" : [0 , 6 ], "durations" : None },
262294 "way_points" : [3 , 5 ],
263295 })
264-
265- geojson = GeoJson (data_with_id )
266- assert geojson .find_identifier () == 'feature.id'
267- geojson = GeoJson (data_bare )
268- assert geojson .find_identifier () == 'feature.id'
269- assert geojson .data ['features' ][0 ]['id' ] == '0' # the id got added
270- geojson = GeoJson (data_with_unique_property )
271- assert geojson .find_identifier () == 'feature.properties.property-key'
272- geojson = GeoJson (data_with_nested_properties )
273- assert geojson .find_identifier () == 'feature.properties.summary.distance'
274- geojson = GeoJson (data_with_incompatible_properties )
275- assert geojson .find_identifier () == 'feature.id'
276- assert geojson .data ['features' ][0 ]['id' ] == '0' # the id got added
296+ _assert_id_got_added (data_with_incompatible_properties )
277297
278298 data_loose_geometry = {"type" : "LineString" , "coordinates" : [
279299 [3.961389 , 43.583333 ], [3.968056 , 43.580833 ], [3.974722 , 43.578333 ],
280300 [3.986389 , 43.575278 ], [3.998333 , 43.5725 ], [4.163333 , 43.530556 ],
281301 ]}
282302 geojson = GeoJson (data_loose_geometry )
283303 geojson .convert_to_feature_collection ()
284- assert geojson .find_identifier () == 'feature.id' # id got added
304+ assert geojson .find_identifier () == 'feature.id'
305+ assert geojson .data ['features' ][0 ]['id' ] == '0'
0 commit comments