Skip to content

Commit

Permalink
test: update test and fixtures
Browse files Browse the repository at this point in the history
- separate the test files into mapping family data and meta data
- add more fixtures to conftest to test diff outcomes, i.e where we have
  missing metadata information
  • Loading branch information
Osneil Drakes authored and Osneil Drakes committed Sep 10, 2024
1 parent 6948037 commit d2ee138
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 71 deletions.
129 changes: 128 additions & 1 deletion tests/unit_tests/parsers/family/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


@pytest.fixture()
def test_family_doc_df():
def mock_family_doc_df():
yield pd.DataFrame(
[
{
Expand Down Expand Up @@ -52,6 +52,133 @@ def test_family_doc_df():
)


@pytest.fixture()
def mock_family_row_ds():
yield pd.Series(
{
"ProjectsID": 1,
"ApprovedRef": "FP004",
"ProjectName": "Enhancing resilience of marine ecosystems",
"Theme": "Adaptation",
"Sector": "Private",
"ProjectURL": "https://www.climateaction.fund/project/FP004",
"Summary": "The Summary of the Project",
"Countries": [
{
"CountryName": "Haiti",
"ISO3": "HTI",
"Region": "Latin America and the Caribbean",
},
],
"Entities": [
{
"Name": "Climate Action Innovations",
}
],
"Funding": [
{
"Source": "GCF",
"Budget": 82000,
"BudgetUSDeq": 82000,
},
{
"ProjectBudgetID": 412,
"Source": "Co-Financing",
"Budget": 620000,
"BudgetUSDeq": 620000,
},
],
"ResultAreas": [
{
"Area": "The Area for the Result Area",
"Type": "The Type for the Result Area",
},
],
}
)


@pytest.fixture()
def mock_family_row_no_result_areas():
yield pd.Series(
{
"ProjectsID": 2,
"ApprovedRef": "FP004",
"ProjectName": "Enhancing resilience of marine ecosystems",
"Theme": "Adaptation",
"Sector": "Private",
"ProjectURL": "https://www.climateaction.fund/project/FP004",
"Summary": "The Summary of the Project",
"Countries": [
{
"CountryName": "Haiti",
"ISO3": "HTI",
"Region": "Latin America and the Caribbean",
},
],
"Entities": [
{
"Name": "Climate Action Innovations",
}
],
"Funding": [
{
"Source": "GCF",
"Budget": 82000,
"BudgetUSDeq": 82000,
},
{
"ProjectBudgetID": 412,
"Source": "Co-Financing",
"Budget": 620000,
"BudgetUSDeq": 620000,
},
],
"ResultAreas": [
{"Area": "", "Type": ""},
],
}
)


@pytest.fixture()
def mock_family_row_no_entities_no_regions():
yield pd.Series(
{
"ProjectsID": 3,
"ApprovedRef": "FP004",
"ProjectName": "Enhancing resilience of marine ecosystems",
"Theme": "Adaptation",
"Sector": "Private",
"ProjectURL": "https://www.climateaction.fund/project/FP004",
"Summary": "The Summary of the Project",
"Countries": [
{"Region": ""},
],
"Entities": [{"Name": ""}],
"Funding": [
{
"Source": "GCF",
"Budget": 82000,
"BudgetUSDeq": 82000,
},
{
"ProjectBudgetID": 412,
"Source": "Co-Financing",
"Budget": 620000,
"BudgetUSDeq": 620000,
},
],
"ResultAreas": [
{
"Area": "The Area for the Result Area",
"Type": "The Type for the Result Area",
},
],
}
)


@pytest.fixture()
def required_family_columns():
required_columns = [column.value for column in FamilyColumnsNames]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pandas as pd
import pytest

from gcf_data_mapper.parsers.family import family, get_budgets, process_row
from gcf_data_mapper.parsers.family import family, map_family_data, process_row


@pytest.fixture
Expand Down Expand Up @@ -31,14 +31,25 @@ def parsed_family_data():


def test_returns_expected_family_data_structure(
test_family_doc_df: pd.DataFrame, parsed_family_data: list[dict]
mock_family_doc_df: pd.DataFrame, parsed_family_data: list[dict]
):
family_data = family(test_family_doc_df, debug=True)
family_data = family(mock_family_doc_df, debug=True)
assert family_data != []
assert len(family_data) == len(parsed_family_data)
assert family_data == parsed_family_data


def test_returns_expected_import_id_for_family_data(
mock_family_row_ds: pd.Series,
):
approved_ref = mock_family_row_ds.ApprovedRef
projects_id = mock_family_row_ds.ProjectsID

family_data = map_family_data(mock_family_row_ds)
assert family_data is not None
assert family_data["import_id"] == f"GCF.family.{approved_ref}.{projects_id}"


def test_raises_error_on_validating_row_for_missing_columns():
test_data_frame = pd.DataFrame(
[
Expand All @@ -61,71 +72,6 @@ def test_raises_error_on_validating_row_for_missing_columns():
assert expected_error_message == str(e.value)


@pytest.mark.parametrize(
("funding_list, source, expected_value"),
[
(
[
{
"Source": "GCF",
"Budget": 1000,
"BudgetUSDeq": 2000,
},
{
"Source": "Co-Financing",
"Budget": 1000,
"BudgetUSDeq": 2000,
},
],
"GCF",
[2000],
),
(
[
{
"Source": "GCF",
"Budget": 1000,
"BudgetUSDeq": 2000,
},
{
"Source": "Co-Financing",
"Budget": 1000,
"BudgetUSDeq": 2000,
},
{
"Source": "Co-Financing",
"Budget": 2000,
"BudgetUSDeq": 4000,
},
],
"Co-Financing",
[2000, 4000],
),
(
[
{
"Source": "Co-Financing",
"Budget": 1000,
"BudgetUSDeq": 2000,
},
{
"Source": "Co-Financing",
"Budget": 2000,
"BudgetUSDeq": 4000,
},
],
"GCF",
[0],
),
],
)
def test_returns_expected_value_when_parsing_budget_data(
funding_list: list[dict], source: str, expected_value: list[int]
):
budgets = get_budgets(funding_list, source)
assert budgets == expected_value


@pytest.mark.parametrize(
("test_ds,expected_return,error_message"),
[
Expand Down Expand Up @@ -194,7 +140,14 @@ def test_skips_processing_row_if_row_contains_empty_values(
projects_id = test_ds.ProjectsID

columns, _ = required_family_columns
expected_return = process_row(test_ds, projects_id, columns)
assert expected_return is None
family_data = process_row(test_ds, projects_id, columns)
assert expected_return == family_data
captured = capsys.readouterr()
assert error_message == captured.out.strip()


def test_skips_processing_row_if_family_metadata_has_missing_data(
mock_family_row_no_result_areas, capsys
):
family_data = map_family_data(mock_family_row_no_result_areas)
assert family_data is None
119 changes: 119 additions & 0 deletions tests/unit_tests/parsers/family/test_map_family_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import pandas as pd
import pytest

from gcf_data_mapper.parsers.family import get_budgets, map_family_metadata


@pytest.fixture()
def parsed_family_metadata():
return {
"approved_ref": ["FP004"],
"implementing_agencies": ["Climate Action Innovations"],
"project_id": [1],
"project_url": ["https://www.climateaction.fund/project/FP004"],
"project_value_co_financing": [620000],
"project_value_fund_spend": [82000],
"regions": ["Latin America and the Caribbean"],
"result_areas": ["The Area for the Result Area"],
"result_types": ["The Type for the Result Area"],
"sector": ["Private"],
"theme": ["Adaptation"],
}


def test_returns_expected_metadata_structure(
mock_family_row_ds: pd.Series, parsed_family_metadata: dict
):
family_metadata = map_family_metadata(mock_family_row_ds)
assert family_metadata is not None
assert family_metadata == parsed_family_metadata


@pytest.mark.parametrize(
("mock_family_row, expected_return, output_message"),
[
(
"mock_family_row_no_entities_no_regions",
None,
"🛑 The following lists contain empty values: Implementing Agencies, Regions. Projects ID 3",
),
(
"mock_family_row_no_result_areas",
None,
"🛑 The following lists contain empty values: Result Areas, Result Types. Projects ID 2",
),
],
)
def test_returns_none_if_nested_values_in_family_metadata_row_contains_empty_values(
mock_family_row: pd.Series, expected_return, output_message: str, request, capsys
):
family_metadata = map_family_metadata(request.getfixturevalue(mock_family_row))

assert family_metadata == expected_return
captured = capsys.readouterr()
assert output_message == captured.out.strip()


@pytest.mark.parametrize(
("funding_list, source, expected_value"),
[
(
[
{
"Source": "GCF",
"Budget": 1000,
"BudgetUSDeq": 2000,
},
{
"Source": "Co-Financing",
"Budget": 1000,
"BudgetUSDeq": 2000,
},
],
"GCF",
[2000],
),
(
[
{
"Source": "GCF",
"Budget": 1000,
"BudgetUSDeq": 2000,
},
{
"Source": "Co-Financing",
"Budget": 1000,
"BudgetUSDeq": 2000,
},
{
"Source": "Co-Financing",
"Budget": 2000,
"BudgetUSDeq": 4000,
},
],
"Co-Financing",
[2000, 4000],
),
(
[
{
"Source": "Co-Financing",
"Budget": 1000,
"BudgetUSDeq": 2000,
},
{
"Source": "Co-Financing",
"Budget": 2000,
"BudgetUSDeq": 4000,
},
],
"GCF",
[0],
),
],
)
def test_returns_expected_value_when_parsing_budget_data(
funding_list: list[dict], source: str, expected_value: list[int]
):
budgets = get_budgets(funding_list, source)
assert budgets == expected_value

0 comments on commit d2ee138

Please sign in to comment.