diff --git a/backend/db/crud_living_income_benchmark.py b/backend/db/crud_living_income_benchmark.py index 19bb9907..3f28c1ab 100644 --- a/backend/db/crud_living_income_benchmark.py +++ b/backend/db/crud_living_income_benchmark.py @@ -6,7 +6,8 @@ LivingIncomeBenchmarkDict, ) from models.cpi import Cpi -from fastapi import HTTPException, status + +# from fastapi import HTTPException, status def get_all_lib(session: Session) -> List[LivingIncomeBenchmarkDict]: @@ -54,7 +55,7 @@ def get_by_country_region_year( last_year_cpi = cpi.order_by(Cpi.year.desc()).first() # add CPI value # INFLATION RATE HERE - if case_year_cpi: + if case_year_cpi and last_year_cpi and last_year_cpi.value: # Calculate CPI factor # CPI Factor logic case_year_cpi_value = case_year_cpi.value if case_year_cpi else 0 @@ -69,15 +70,28 @@ def get_by_country_region_year( lib["case_year_cpi"] = case_year_cpi_value lib["last_year_cpi"] = last_year_cpi_value lib["cpi_factor"] = cpi_factor + lib["message"] = None return lib else: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail=f"Benchmark not available for the year {year}.", + lib = lib.serialize + lib["case_year_cpi"] = None + lib["last_year_cpi"] = None + lib["cpi_factor"] = None + lib["message"] = ( + f"This is the benchmark value for {lib['year']}, which is the " + "most recent available. If you wish to adjust it for " + "inflation and update the value manually, you can use the " + "'Set the target yourself' option." ) + return lib + # raise HTTPException( + # status_code=status.HTTP_404_NOT_FOUND, + # detail=f"Benchmark not available for the year {year}.", + # ) else: lib = lib.serialize lib["case_year_cpi"] = None lib["last_year_cpi"] = None lib["cpi_factor"] = None + lib["message"] = None return lib diff --git a/backend/models/living_income_benchmark.py b/backend/models/living_income_benchmark.py index 06d8aac5..13d5efdf 100644 --- a/backend/models/living_income_benchmark.py +++ b/backend/models/living_income_benchmark.py @@ -27,6 +27,7 @@ class LivingIncomeBenchmarkDict(TypedDict): case_year_cpi: Optional[float] last_year_cpi: Optional[float] cpi_factor: Optional[float] + message: Optional[str] = None class LivingIncomeBenchmark(Base): @@ -95,6 +96,8 @@ def serialize(self) -> LivingIncomeBenchmarkDict: }, "case_year_cpi": None, "last_year_cpi": None, + "cpi_factor": None, + "message": None, } diff --git a/backend/tests/test_000_main.py b/backend/tests/test_000_main.py index e2dc7099..7347b4b2 100644 --- a/backend/tests/test_000_main.py +++ b/backend/tests/test_000_main.py @@ -72,7 +72,11 @@ async def test_add_commodity_categories_and_commodities_master_data( payload = [ { "name": "Grains", - "children": [{"name": "Wheat"}, {"name": "Rice"}, {"name": "Corn"}], + "children": [ + {"name": "Wheat"}, + {"name": "Rice"}, + {"name": "Corn"}, + ], }, { "name": "Nuts", @@ -122,8 +126,14 @@ async def test_add_country_master_data( self, app: FastAPI, session: Session, client: AsyncClient ) -> None: payload = [ - {"name": "Indonesia", "children": [{"name": "Bali"}, {"name": "Lombok"}]}, - {"name": "India", "children": [{"name": "Delhi"}, {"name": "Mumbai"}]}, + { + "name": "Indonesia", + "children": [{"name": "Bali"}, {"name": "Lombok"}], + }, + { + "name": "India", + "children": [{"name": "Delhi"}, {"name": "Mumbai"}], + }, ] for val in payload: country = Country(name=val["name"]) @@ -134,7 +144,9 @@ async def test_add_country_master_data( session.commit() session.flush() session.refresh(country) - countries = session.query(Country).filter(Country.parent.is_(None)).all() + countries = ( + session.query(Country).filter(Country.parent.is_(None)).all() + ) countries = [c.serialize for c in countries] assert countries == [ { @@ -273,6 +285,8 @@ async def test_add_benchmark_n_region_master_data( "value": {"lcu": 1200.5, "usd": 2200.5, "eur": 3200.5}, "case_year_cpi": None, "last_year_cpi": None, + "cpi_factor": None, + "message": None, }, { "id": 2, @@ -287,6 +301,8 @@ async def test_add_benchmark_n_region_master_data( "value": {"lcu": 1000.0, "usd": 2000.0, "eur": 3000.0}, "case_year_cpi": None, "last_year_cpi": None, + "cpi_factor": None, + "message": None, }, ] # cpi diff --git a/backend/tests/test_070_benchmark.py b/backend/tests/test_070_benchmark.py index 9c153362..583a9d41 100644 --- a/backend/tests/test_070_benchmark.py +++ b/backend/tests/test_070_benchmark.py @@ -39,6 +39,7 @@ async def test_get_benchmark_by_country_region_year_1( "case_year_cpi": None, "last_year_cpi": None, "cpi_factor": None, + "message": None, "household_equiv": None, "links": None, "nr_adults": None, @@ -71,6 +72,7 @@ async def test_get_benchmark_by_country_region_year_2( "value": {"lcu": 1200.5, "usd": 2200.5, "eur": 3200.5}, "case_year_cpi": 6000.0, "last_year_cpi": 7000.0, + "message": None, "cpi_factor": -0.14285714285714285, "household_equiv": None, "links": None, @@ -95,9 +97,7 @@ async def test_get_benchmark_by_country_region_year_3( ) assert res.status_code == 404 res = res.json() - assert res == { - "detail": "Benchmark value not found." - } + assert res == {"detail": "Benchmark value not found."} @pytest.mark.asyncio async def test_get_benchmark_by_country_region_year_4( @@ -114,8 +114,26 @@ async def test_get_benchmark_by_country_region_year_4( "year": 2023, }, ) - assert res.status_code == 404 + assert res.status_code == 200 res = res.json() assert res == { - "detail": "Benchmark not available for the year 2023." + "id": 1, + "country": 1, + "region": 1, + "household_size": 4.0, + "year": 2020, + "nr_adults": None, + "household_equiv": None, + "source": "www.akvo.org", + "links": None, + "value": {"lcu": 1200.5, "usd": 2200.5, "eur": 3200.5}, + "case_year_cpi": None, + "last_year_cpi": None, + "cpi_factor": None, + "message": ( + "This is the benchmark value for 2020, which is the most " + "recent available. If you wish to adjust it for inflation " + "and update the value manually, you can use the 'Set the " + "target yourself' option." + ), } diff --git a/frontend/src/pages/cases/components/IncomeDriverTarget.js b/frontend/src/pages/cases/components/IncomeDriverTarget.js index c6e6c28c..cdbc58ab 100644 --- a/frontend/src/pages/cases/components/IncomeDriverTarget.js +++ b/frontend/src/pages/cases/components/IncomeDriverTarget.js @@ -448,6 +448,7 @@ const IncomeDriverTarget = ({ borderBottom: "1px solid #e8e8e8", marginBottom: "12px", color: "red", + paddingBottom: "10px", }} >