Skip to content

Commit

Permalink
Refactored with old checks in place
Browse files Browse the repository at this point in the history
  • Loading branch information
mdales committed Nov 12, 2024
1 parent f42bfaa commit fdf2931
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion prepare_species/extract_species_psql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
50 changes: 42 additions & 8 deletions tests/test_species_filter.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"])

Expand All @@ -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"])
Expand Down Expand Up @@ -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"),
Expand All @@ -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]

0 comments on commit fdf2931

Please sign in to comment.