Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/app 181 tidy up gcf result areas that are being displayed in the UI #35

Merged
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
1 change: 1 addition & 0 deletions gcf_data_mapper/enums/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class FamilyNestedColumnNames(Enum):
REGION = "Region"
SOURCE = "Source"
TYPE = "Type"
VALUE = "Value"


class GCFProjectBudgetSource(Enum):
Expand Down
20 changes: 18 additions & 2 deletions gcf_data_mapper/parsers/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ def get_budgets(funding_list: list[dict], source: str) -> Optional[list[str]]:
return budgets if budgets else ["0"]


def get_related_result_areas(result_areas: list[dict]) -> list[str]:
"""Get the related result areas from the row.

:param list[dict] result_areas: A list of all the result areas information, represented in dictionaries.
:return list[str]: A list of result areas where the 'Value' is greater than or equal to 0%.
"""

area_key = FamilyNestedColumnNames.AREA.value
value_key = FamilyNestedColumnNames.VALUE.value

return [
str(result[area_key])
for result in result_areas
if result[value_key] and float(result[value_key].replace("%", "")) > 0
]


def map_family_metadata(row: pd.Series) -> Optional[dict]:
"""Map the metadata of a family based on the provided row.

Expand All @@ -101,7 +118,6 @@ def map_family_metadata(row: pd.Series) -> Optional[dict]:
funding_sources = row.at[FamilyColumnsNames.FUNDING.value]
result_areas = row.at[FamilyColumnsNames.RESULT_AREAS.value]

area_key = FamilyNestedColumnNames.AREA.value
name_key = FamilyNestedColumnNames.NAME.value
region_key = FamilyNestedColumnNames.REGION.value
type_key = FamilyNestedColumnNames.TYPE.value
Expand All @@ -116,7 +132,7 @@ def map_family_metadata(row: pd.Series) -> Optional[dict]:

implementing_agencies = [str(entity[name_key]) for entity in entities]
regions = [str(country[region_key]) for country in countries]
areas = [str(result[area_key]) for result in result_areas]
areas = get_related_result_areas(result_areas)
types = [str(result[type_key]) for result in result_areas]

# As we are filtering the budget information by source for gcf and co financing, we
Expand Down
4 changes: 3 additions & 1 deletion gcf_data_mapper/parsers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def arrays_contain_empty_values(list_values: list[tuple], id: str) -> bool:
:return bool: True if any list contains empty values, False otherwise.
"""
arrays_with_empty_values = [
name for name, array in list_values if any(not value for value in array)
name
for name, array in list_values
if not array or any(not value for value in array)
]

if arrays_with_empty_values:
Expand Down
7 changes: 6 additions & 1 deletion tests/unit_tests/parsers/family/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def mock_family_doc_df():
{
"Area": "Coastal protection and restoration",
"Type": "Adaptation",
"Value": "100%",
},
],
"ApprovalDate": "2016-06-30T00:00:00.000Z",
Expand Down Expand Up @@ -95,6 +96,7 @@ def mock_family_row_ds():
{
"Area": "The Area for the Result Area",
"Type": "The Type for the Result Area",
"Value": "100%",
},
],
"ApprovalDate": "2016-06-30T00:00:00.000Z",
Expand Down Expand Up @@ -141,7 +143,7 @@ def mock_family_row_no_result_areas():
},
],
"ResultAreas": [
{"Area": "", "Type": ""},
{"Area": "", "Type": "", "Value": ""},
],
"ApprovalDate": "2016-06-30T00:00:00.000Z",
"StartDate": "2024-06-28T00:00:00.000Z",
Expand Down Expand Up @@ -182,6 +184,7 @@ def mock_family_row_no_entities_no_regions():
{
"Area": "The Area for the Result Area",
"Type": "The Type for the Result Area",
"Value": "100%",
},
],
"ApprovalDate": "2016-06-30T00:00:00.000Z",
Expand Down Expand Up @@ -222,6 +225,7 @@ def mock_family_row_with_non_int_non_float_budget_values():
{
"Area": "The Area for the Result Area",
"Type": "The Type for the Result Area",
"Value": "100%",
},
],
"ApprovalDate": "2016-06-30T00:00:00.000Z",
Expand Down Expand Up @@ -271,6 +275,7 @@ def mock_family_doc_with_whitespace():
{
"Area": " Coastal protection and restoration ",
"Type": " Adaptation ",
"Value": "100%",
},
],
"ApprovalDate": " 2016-06-30T00:00:00.000Z ",
Expand Down
28 changes: 28 additions & 0 deletions tests/unit_tests/parsers/family/test_map_family_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,31 @@ def test_all_metadata_values_are_list_of_strings(mock_family_row_ds: pd.Series):
for value in family_metadata.values():
assert isinstance(value, list)
assert all(isinstance(item, str) for item in value)


def test_maps_result_areas_with_value_greater_than_zero(mock_family_row_ds: pd.Series):
mock_family_row_ds["ResultAreas"] = [
{
"Area": "The Area for the Result Area 1",
"Type": "The Type for the Result Area 1",
"Value": "50.00%",
},
{
"Area": "The Area for the Result Area 2",
"Type": "The Type for the Result Area 2",
"Value": "50.00%",
},
{
"Area": "The Area for the Result Area 3",
"Type": "The Type for the Result Area 3",
"Value": "0.00%",
},
]

family_metadata = map_family_metadata(mock_family_row_ds)
assert family_metadata is not None
assert family_metadata["result_area"] == [
"The Area for the Result Area 1",
"The Area for the Result Area 2",
]
assert len(family_metadata["result_area"]) == 2
Loading