Skip to content

Commit

Permalink
HDXDSYS-753 switch * with all and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
turnerm committed May 24, 2024
1 parent 0aa2067 commit 1ec7c7a
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 103 deletions.
5 changes: 2 additions & 3 deletions src/hapi_schema/db_conflict_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from sqlalchemy import (
DateTime,
Enum,
ForeignKey,
Integer,
select,
Expand All @@ -18,7 +17,7 @@
population_constraint,
reference_period_constraint,
)
from hapi_schema.utils.enums import EventType
from hapi_schema.utils.enums import EventType, build_enum_using_values
from hapi_schema.utils.view_params import ViewParams


Expand All @@ -43,7 +42,7 @@ class DBConflictEvent(Base):
primary_key=True,
)
event_type: Mapped[EventType] = mapped_column(
Enum(EventType), nullable=False, primary_key=True
build_enum_using_values(EventType), nullable=False, primary_key=True
)
events: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
fatalities: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
Expand Down
6 changes: 3 additions & 3 deletions src/hapi_schema/db_funding.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class DBFunding(Base):
__table_args__ = (
CheckConstraint(
"requirements_usd >= 0.0",
name="requirements_usd",
name="requirements_usd_constraint",
),
CheckConstraint(
"funding_usd >= 0.0",
name="funding_usd",
name="funding_usd_constraint",
),
CheckConstraint(
"funding_pct >= 0.0",
name="funding_pct",
name="funding_pct_constraint",
),
reference_period_constraint(),
)
Expand Down
3 changes: 2 additions & 1 deletion src/hapi_schema/db_national_risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class DBNationalRisk(Base):
general_risk_constraint("vulnerability"),
general_risk_constraint("coping_capacity"),
CheckConstraint(
"(global_rank >= 1) AND (global_rank <= 250)", name="global_rank"
"(global_rank >= 1) AND (global_rank <= 250)",
name="global_rank_constraint",
),
CheckConstraint(
"meta_avg_recentness_years >= 0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/hapi_schema/db_poverty_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DBPovertyRate(Base):
reference_period_constraint(),
CheckConstraint(
sqltext="ABS(headcount_ratio / 100 * intensity_of_deprivation / 100 - mpi) < 0.00001",
name="mpi_product",
name="mpi_product_constraint",
),
)

Expand Down
22 changes: 13 additions & 9 deletions src/hapi_schema/utils/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def min_age_constraint() -> CheckConstraint:
Otherwise, it can be NULL for unknown, or should be an
integer of 0 or greater."""
sqltext = (
"(age_range = '*' AND min_age IS NULL) OR "
"(age_range != '*' AND (min_age >= 0 OR min_age is NULL))"
"(age_range = 'all' AND min_age IS NULL) OR "
"(age_range != 'all' AND min_age >= 0)"
)
return CheckConstraint(sqltext=sqltext, name="min_age_constraint")

Expand All @@ -32,31 +32,35 @@ def population_constraint(
) -> CheckConstraint:
"""Population must not be a negative number."""
sqltext = f"{population_var_name} >= 0"
return CheckConstraint(sqltext=sqltext, name=f"{population_var_name}")
return CheckConstraint(
sqltext=sqltext, name=f"{population_var_name}_constraint"
)


def non_negative_constraint(
var_name: str,
) -> CheckConstraint:
"""Require a column to be non-negative."""
sqltext = f"{var_name} >= 0"
return CheckConstraint(sqltext=sqltext, name=f"{var_name}")
return CheckConstraint(sqltext=sqltext, name=f"{var_name}_constraint")


def percentage_constraint(var_name: str) -> CheckConstraint:
sqltext = f"{var_name} >= 0. AND {var_name} <= 100."
return CheckConstraint(sqltext=sqltext, name=f"{var_name}")
return CheckConstraint(sqltext=sqltext, name=f"{var_name}_constraint")


def reference_period_constraint() -> CheckConstraint:
"""reference_period_end should be greater than reference_period_start"""
sqltext = "reference_period_end >= reference_period_start "
return CheckConstraint(sqltext=sqltext, name="reference_period")
return CheckConstraint(sqltext=sqltext, name="reference_period_constraint")


def general_risk_constraint(risk_name: str) -> CheckConstraint:
sqltext = f"({risk_name}_risk >= 0) AND ({risk_name}_risk <= 10)"
return CheckConstraint(sqltext=sqltext, name=f"{risk_name}_risk")
return CheckConstraint(
sqltext=sqltext, name=f"{risk_name}_risk_constraint"
)


def code_and_reference_period_unique_constraint(
Expand All @@ -65,11 +69,11 @@ def code_and_reference_period_unique_constraint(
return UniqueConstraint(
"code",
"reference_period_start",
name=f"{admin_level}_code_and_reference_period_unique",
name=f"{admin_level}_code_and_reference_period_unique_constraint",
)


def latlon_constraint() -> CheckConstraint:
"""Latitude and longitude must be valid"""
sqltext = "lat <= 90.0 AND lat >= -90.0 AND lon <= 180.0 AND lon >= -180.0"
return CheckConstraint(sqltext=sqltext, name="latlon")
return CheckConstraint(sqltext=sqltext, name="latlon_constraint")
47 changes: 30 additions & 17 deletions src/hapi_schema/utils/enums.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import enum
from enum import Enum as PythonEnum
from typing import List

from sqlalchemy import Enum as SQLAlchemyEnum

class Gender(str, enum.Enum):
"""The two functions below create enums using the values rather than the keys"""


def _get_list_of_values(enum_class: PythonEnum) -> List[str]:
return [e.value for e in enum_class]


def build_enum_using_values(enum_class: PythonEnum) -> SQLAlchemyEnum:
return SQLAlchemyEnum(enum_class, values_callable=_get_list_of_values)


class Gender(str, PythonEnum):
FEMALE = "f"
MALE = "m"
NONBINARY = "x"
UNSPECIFIED = "u"
OTHER = "o"
ALL = "*"
ALL = "all"


class DisabledMarker(str, enum.Enum):
class DisabledMarker(str, PythonEnum):
YES = "y"
NO = "n"
ALL = "*"
ALL = "all"


class EventType(str, enum.Enum):
class EventType(str, PythonEnum):
CIVILIAN_TARGETING = "civilian_targeting"
DEMONSTRATION = "demonstration"
POLITICAL_VIOLENCE = "political_violence"


class PopulationGroup(str, enum.Enum):
class PopulationGroup(str, PythonEnum):
REFUGEES = "REF"
ROC = "ROC"
ASYLUM_SEEKERS = "ASY"
Expand All @@ -38,55 +51,55 @@ class PopulationGroup(str, enum.Enum):
POC = "POC"
RDP = "RDP"
RRI = "RRI"
ALL = "*"
ALL = "all"


class PopulationStatus(str, enum.Enum):
class PopulationStatus(str, PythonEnum):
POPULATION = "POP"
AFFECTED = "AFF"
INNEED = "INN"
TARGETED = "TGT"
REACHED = "REA"
ALL = "*"
ALL = "all"


class PriceFlag(str, enum.Enum):
class PriceFlag(str, PythonEnum):
ACTUAL = "actual"
AGGREGATE = "aggregate"
ACTUAL_AGGREGATE = "actual,aggregate"


class PriceType(str, enum.Enum):
class PriceType(str, PythonEnum):
FARM_GATE = "Farm Gate"
RETAIL = "Retail"
WHOLESALE = "Wholesale"


class IPCPhase(str, enum.Enum):
class IPCPhase(str, PythonEnum):
PHASE_1 = "1"
PHASE_2 = "2"
PHASE_3 = "3"
PHASE_4 = "4"
PHASE_5 = "5"
PHASE_3_PLUS = "3+"
ALL = "*"
ALL = "all"


class IPCType(str, enum.Enum):
class IPCType(str, PythonEnum):
CURRENT = "current"
FIRST_PROJECTION = "first projection"
SECOND_PROJECTION = "second projection"


class RiskClass(str, enum.Enum):
class RiskClass(str, PythonEnum):
VERY_LOW = "1"
LOW = "2"
MEDIUM = "3"
HIGH = "4"
VERY_HIGH = "5"


class CommodityCategory(str, enum.Enum):
class CommodityCategory(str, PythonEnum):
CEREALS_TUBERS = "cereals and tubers"
MEAT_FISH_EGGS = "meat, fish and eggs"
MILK_DAIRY = "milk and dairy"
Expand Down
14 changes: 7 additions & 7 deletions tests/sample_data/data_humanitarian_needs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
dict(
resource_hdx_id="90deb235-1bf5-4bae-b231-3393222c2d01",
admin2_ref=1,
gender="*",
age_range="*",
disabled_marker="*",
sector_code="*",
population_group="*",
gender="all",
age_range="all",
disabled_marker="all",
sector_code="intersectoral",
population_group="all",
population_status="AFF",
population=1_000_000,
reference_period_start=datetime(2023, 1, 1),
Expand All @@ -20,7 +20,7 @@
resource_hdx_id="90deb235-1bf5-4bae-b231-3393222c2d01",
admin2_ref=1,
gender="f",
age_range="*",
age_range="all",
disabled_marker="n",
sector_code="SHL",
population_group="REF",
Expand Down Expand Up @@ -49,7 +49,7 @@
dict(
resource_hdx_id="90deb235-1bf5-4bae-b231-3393222c2d01",
admin2_ref=4,
gender="*",
gender="all",
age_range="80+",
min_age=80,
disabled_marker="y",
Expand Down
8 changes: 4 additions & 4 deletions tests/sample_data/data_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
dict(
resource_hdx_id="90deb235-1bf5-4bae-b231-3393222c2d01",
admin2_ref=1,
gender="*",
age_range="*",
gender="all",
age_range="all",
population=1_000_000,
reference_period_start=datetime(2023, 1, 1),
reference_period_end=datetime(2023, 6, 30),
Expand All @@ -16,7 +16,7 @@
resource_hdx_id="90deb235-1bf5-4bae-b231-3393222c2d01",
admin2_ref=1,
gender="f",
age_range="*",
age_range="all",
population=500_000,
reference_period_start=datetime(2023, 1, 1),
reference_period_end=datetime(2023, 6, 30),
Expand All @@ -37,7 +37,7 @@
dict(
resource_hdx_id="90deb235-1bf5-4bae-b231-3393222c2d01",
admin2_ref=4,
gender="*",
gender="all",
age_range="80+",
min_age=80,
population=500,
Expand Down
4 changes: 2 additions & 2 deletions tests/sample_data/data_sector.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
name="Health",
),
dict(
code="*",
name="All",
code="intersectoral",
name="Intersectoral",
),
]
4 changes: 2 additions & 2 deletions tests/test_admin1.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_reference_period_constraint(run_constraints_test):
reference_period_end=datetime(2023, 1, 1),
)
],
expected_constraint="reference_period",
expected_constraint="reference_period_constraint",
)


Expand All @@ -65,5 +65,5 @@ def test_code_date_unique(run_constraints_test):
reference_period_start=datetime(2023, 1, 1),
),
],
expected_constraint="admin1_code_and_reference_period_unique",
expected_constraint="admin1_code_and_reference_period_unique_constraint",
)
4 changes: 2 additions & 2 deletions tests/test_admin2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_reference_period_constraint(run_constraints_test):
reference_period_end=datetime(2023, 1, 1),
)
],
expected_constraint="reference_period",
expected_constraint="reference_period_constraint",
)


Expand All @@ -66,5 +66,5 @@ def test_code_date_unique(run_constraints_test):
reference_period_start=datetime(2023, 1, 1),
),
],
expected_constraint="admin2_code_and_reference_period_unique",
expected_constraint="admin2_code_and_reference_period_unique_constraint",
)
6 changes: 3 additions & 3 deletions tests/test_conflict_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_events_constraint(run_constraints_test):
new_rows=[
DBConflictEvent(**data),
],
expected_constraint="events",
expected_constraint="events_constraint",
)


Expand All @@ -58,7 +58,7 @@ def test_fatalities_constraint(run_constraints_test):
new_rows=[
DBConflictEvent(**data),
],
expected_constraint="fatalities",
expected_constraint="fatalities_constraint",
)


Expand All @@ -70,5 +70,5 @@ def test_reference_period_constraint(run_constraints_test):
new_rows=[
DBConflictEvent(**data),
],
expected_constraint="reference_period",
expected_constraint="reference_period_constraint",
)
4 changes: 2 additions & 2 deletions tests/test_food_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_price_not_negative_constraint(run_constraints_test):
new_rows=[
DBFoodPrice(**data),
],
expected_constraint="price",
expected_constraint="price_constraint",
)


Expand All @@ -55,7 +55,7 @@ def test_reference_period_constraint(run_constraints_test):
new_rows=[
DBFoodPrice(**data),
],
expected_constraint="reference_period",
expected_constraint="reference_period_constraint",
)


Expand Down
Loading

0 comments on commit 1ec7c7a

Please sign in to comment.