Skip to content

Commit

Permalink
[#359] Incorporate new benchmark message for slide 2 & 4
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Aug 12, 2024
1 parent 6b6a7f1 commit d28a732
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 14 deletions.
24 changes: 19 additions & 5 deletions backend/db/crud_living_income_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Expand All @@ -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
3 changes: 3 additions & 0 deletions backend/models/living_income_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -95,6 +96,8 @@ def serialize(self) -> LivingIncomeBenchmarkDict:
},
"case_year_cpi": None,
"last_year_cpi": None,
"cpi_factor": None,
"message": None,
}


Expand Down
24 changes: 20 additions & 4 deletions backend/tests/test_000_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"])
Expand All @@ -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 == [
{
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
28 changes: 23 additions & 5 deletions backend/tests/test_070_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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."
),
}
15 changes: 15 additions & 0 deletions frontend/src/pages/cases/components/IncomeDriverTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ const IncomeDriverTarget = ({
borderBottom: "1px solid #e8e8e8",
marginBottom: "12px",
color: "red",
paddingBottom: "10px",
}}
>
<Col span={24}>
Expand All @@ -456,6 +457,20 @@ const IncomeDriverTarget = ({
</Col>
</Row>
)}
{/* Benchmark message if no CPI */}
{benchmark?.message && (
<Row
style={{
borderBottom: "1px solid #e8e8e8",
marginBottom: "12px",
color: "red",
paddingBottom: "10px",
}}
>
<Col span={24}>{benchmark.message}</Col>
</Row>
)}
{/* EOL Benchmark message if no CPI */}
<Row gutter={[8, 8]} style={{ display: !disableTarget ? "none" : "" }}>
<Col span={8}>
<Form.Item label="Region" name="region">
Expand Down

0 comments on commit d28a732

Please sign in to comment.