diff --git a/prepare_species/extract_species_psql.py b/prepare_species/extract_species_psql.py index 5b6d59b..49bbd6f 100644 --- a/prepare_species/extract_species_psql.py +++ b/prepare_species/extract_species_psql.py @@ -177,7 +177,7 @@ def process_habitats( return habitats -def process_geometries(geometries_data: List) -> Dict: +def process_geometries(geometries_data: List[Tuple[int,shapely.Geometry]]) -> Dict[int,shapely.Geometry]: if len(geometries_data) == 0: raise ValueError("No geometries") diff --git a/tests/test_species_filter.py b/tests/test_species_filter.py index a934882..cadf3cf 100644 --- a/tests/test_species_filter.py +++ b/tests/test_species_filter.py @@ -1,6 +1,7 @@ import pytest +import postgis -from prepare_species.extract_species_psql import process_habitats +from prepare_species.extract_species_psql import process_habitats, process_geometries @pytest.mark.parametrize("label", [ "resident", @@ -9,14 +10,13 @@ "Seasonal Occurrence Unknown", None ]) -def test_simple_resident_species_filter(label): +def test_simple_resident_species_habitat_filter(label): habitat_data = [ - ("resident", "Yes", "4.1|4.2", "Terrestrial"), - ("resident", "No", "4.3", "Terrestrial"), + (label, "Yes", "4.1|4.2", "Terrestrial"), + (label, "No", "4.3", "Terrestrial"), ] res = process_habitats(habitat_data) - # Just resident assert list(res.keys()) == [1] assert res[1] == set(["4.1", "4.2", "4.3"]) @@ -26,14 +26,13 @@ def test_simple_resident_species_filter(label): ("breeding", "Non-Breeding Season"), ("Breeding Season", "Non-Breeding Season"), ]) -def test_simple_migratory_species_filter(breeding_label, non_breeding_label): +def test_simple_migratory_species_habitat_filter(breeding_label, non_breeding_label): habitat_data = [ (breeding_label, "Yes", "4.1|4.2", "Terrestrial"), (non_breeding_label, "No", "4.3", "Terrestrial"), ] res = process_habitats(habitat_data) - # Just resident assert list(res.keys()) == [2, 3] assert res[2] == set(["4.1", "4.2"]) assert res[3] == set(["4.3"]) @@ -69,7 +68,7 @@ def test_do_not_reject_if_caves_in_minor_habitat(): "passage", "Passage", ]) -def test_passage_ignored(label): +def test_passage_habitat_ignored(label): habitat_data = [ ("resident", "Yes", "4.1|4.2", "Terrestrial"), (label, "No", "4.3", "Terrestrial"), @@ -91,3 +90,38 @@ def test_fail_no_habitats_after_filter(): ] with pytest.raises(ValueError): _ = process_habitats(habitat_data) + +def test_fail_if_unrecognised_season_for_habitat(): + habitat_data = [ + ("resident", "Yes", "4.1|4.2", "Terrestrial"), + ("zarquon", "Yes", "4.3", "Terrestrial"), + ] + with pytest.raises(ValueError): + _ = process_habitats(habitat_data) + +def test_empty_geometry_list(): + geoemetries_data = [] + with pytest.raises(ValueError): + _ = process_geometries(geoemetries_data) + + +@pytest.mark.parametrize("label", [ + 1, + 5 +]) +def test_simple_resident_species_geometry_filter(label): + habitat_data = [ + (label, postgis.Geometry.from_ewkb("000000000140000000000000004010000000000000")), + ] + res = process_geometries(habitat_data) + + assert list(res.keys()) == [1] + +def test_simple_migratory_species_geometry_filter(): + habitat_data = [ + (2, postgis.Geometry.from_ewkb("000000000140000000000000004010000000000000")), + (3, postgis.Geometry.from_ewkb("000000000140000000000000004010000000000000")), + ] + res = process_geometries(habitat_data) + + assert list(res.keys()) == [2, 3]