Skip to content

Commit

Permalink
Feature/pdct 1453 update budget type checker to allow floats as well …
Browse files Browse the repository at this point in the history
…as ints (#17)

* fix: add float to typing check for funding figures

* style : make outputs a bit more clearer

* Bump version to 0.1.12

---------

Co-authored-by: Osneil Drakes <[email protected]>
  • Loading branch information
odrakes-cpr and Osneil Drakes authored Sep 11, 2024
1 parent b3348a2 commit aa37326
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
16 changes: 10 additions & 6 deletions gcf_data_mapper/parsers/family.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Iterable, Optional
from typing import Any, Iterable, Optional, Union

import click
import pandas as pd
Expand Down Expand Up @@ -57,13 +57,15 @@ def calculate_status(row: pd.Series) -> Optional[str]:
return None


def get_budgets(funding_list: list[dict], source: str) -> Optional[list[int]]:
def get_budgets(
funding_list: list[dict], source: str
) -> Optional[list[Union[int, float]]]:
"""Get the budget amount from the row based on the funding source.
:param list[dict] row: A list of all the funding information, represented in dictionaries
:param str source: The funding source to retrieve the budget from.
:return Optional[list[int]]: A list of budget amounts corresponding to the source,
:return Optional[list[Union[int, float]]: A list of budget amounts corresponding to the source,
or [0] if the source is not found.
"""

Expand All @@ -75,7 +77,7 @@ def get_budgets(funding_list: list[dict], source: str) -> Optional[list[int]]:
]

# Check for any invalid values
if any(not isinstance(budget, (int)) for budget in budgets):
if any(not isinstance(budget, (int, float)) for budget in budgets):
click.echo("🛑 Funding entries does not have valid int budget values")
return None

Expand Down Expand Up @@ -164,16 +166,18 @@ def map_family_data(
"""

family_metadata = map_family_metadata(row)
projects_id = row.at[FamilyColumnsNames.PROJECTS_ID.value]

# When processing the family metadata if there are any empty/falsy values we return None
# and skip the row. Therefore we don't want to process the rest of the family data so we
# return None in this conditional.
if family_metadata is None:
click.echo("🛑 Skipping row as family metadata has missing information")
click.echo(
f"🛑 Skipping row as family metadata has missing information, ProjectsID : {projects_id}"
)
return None

approved_ref = row.at[FamilyColumnsNames.APPROVED_REF.value]
projects_id = row.at[FamilyColumnsNames.PROJECTS_ID.value]
summary = row.at[FamilyColumnsNames.SUMMARY.value]
title = row.at[FamilyColumnsNames.TITLE.value]

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "gcf-data-mapper"
version = "0.1.11"
version = "0.1.12"
description = "A CLI tool to wrangle GCF data into format recognised by the bulk-import tool."
authors = ["CPR-dev-team <[email protected]>"]
license = "Apache-2.0"
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/parsers/family/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def mock_family_row_no_entities_no_regions():


@pytest.fixture()
def mock_family_row_with_non_int_budget_values():
def mock_family_row_with_non_int_non_float_budget_values():
yield pd.Series(
{
"ProjectsID": 3,
Expand All @@ -214,8 +214,8 @@ def mock_family_row_with_non_int_budget_values():
},
{
"Source": "Co-Financing",
"Budget": 620000.20,
"BudgetUSDeq": 620000.50,
"Budget": "620000.20",
"BudgetUSDeq": "620000.50",
},
],
"ResultAreas": [
Expand Down
5 changes: 3 additions & 2 deletions tests/unit_tests/parsers/family/test_map_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,16 @@ def test_skips_processing_row_if_row_contains_empty_values(


def test_skips_processing_row_if_family_metadata_has_missing_data(
mock_family_row_no_result_areas, capsys
mock_family_row_no_result_areas: pd.Series, capsys
):
projects_id = mock_family_row_no_result_areas.ProjectsID
family_data = map_family_data(mock_family_row_no_result_areas)
assert family_data is None
captured = capsys.readouterr()
# We have two outputs, one from map_family_metadata pointing to the missing data and the second
# from map_family_data informing that the row is being skipped
map_family_data_output = captured.out.strip().split("\n")
assert (
"🛑 Skipping row as family metadata has missing information"
f"🛑 Skipping row as family metadata has missing information, ProjectsID : {projects_id}"
== map_family_data_output[1]
)
4 changes: 2 additions & 2 deletions tests/unit_tests/parsers/family/test_map_family_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ def test_returns_expected_value_when_parsing_budget_data(


def test_map_family_metadata_returns_none_if_budget_does_not_contain_valid_int_types(
mock_family_row_with_non_int_budget_values: pd.Series,
mock_family_row_with_non_int_non_float_budget_values: pd.Series,
):
result = map_family_metadata(mock_family_row_with_non_int_budget_values)
result = map_family_metadata(mock_family_row_with_non_int_non_float_budget_values)
assert result is None


Expand Down

0 comments on commit aa37326

Please sign in to comment.