Skip to content

Commit

Permalink
HDXDSYS-1387 Add to all HAPI views a generated column: admin_level (#73)
Browse files Browse the repository at this point in the history
* admin_level column in views
* Update pre-commit
* Avoid repetition of cases
* Add tests for admin_level column
  • Loading branch information
mcarans authored Nov 27, 2024
1 parent f451440 commit 27e7676
Show file tree
Hide file tree
Showing 26 changed files with 111 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .config/pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ default_language_version:
python: python3.12
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-ast
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
rev: v0.7.0
hooks:
# Run the linter.
- id: ruff
Expand All @@ -17,7 +17,7 @@ repos:
- id: ruff-format
args: [--config, .config/ruff.toml]
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.4.18
rev: 0.4.24
hooks:
# Run the pip compile
- id: pip-compile
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.9.4

### Added

- Admin_level to views

## 0.9.3

### Fixed
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ run = """
--cov-report=lcov --cov-report=term-missing
"""

[tool.hatch.envs.hatch-static-analysis]
dependencies = ["ruff==0.7.0"]

[tool.hatch.envs.hatch-static-analysis.scripts]
format-check = ["ruff format --config .config/ruff.toml --check --diff {args:.}",]
format-fix = ["ruff format --config .config/ruff.toml {args:.}",]
Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# uv pip compile pyproject.toml --resolver=backtracking --all-extras -o requirements.txt
cfgv==3.4.0
# via pre-commit
coverage==7.6.4
coverage==7.6.8
# via pytest-cov
distlib==0.3.9
# via virtualenv
Expand All @@ -12,13 +12,13 @@ greenlet==3.1.1
# via sqlalchemy
hdx-python-database==1.3.4
# via hapi-schema (pyproject.toml)
identify==2.6.1
identify==2.6.3
# via pre-commit
iniconfig==2.0.0
# via pytest
nodeenv==1.9.1
# via pre-commit
packaging==24.1
packaging==24.2
# via pytest
platformdirs==4.3.6
# via virtualenv
Expand All @@ -34,7 +34,7 @@ pytest==8.3.3
# via
# hapi-schema (pyproject.toml)
# pytest-cov
pytest-cov==5.0.0
pytest-cov==6.0.0
# via hapi-schema (pyproject.toml)
pyyaml==6.0.2
# via pre-commit
Expand All @@ -46,5 +46,5 @@ typing-extensions==4.12.2
# via
# psycopg
# sqlalchemy
virtualenv==20.27.0
virtualenv==20.28.0
# via pre-commit
3 changes: 3 additions & 0 deletions src/hapi_schema/db_conflict_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from hapi_schema.utils.enums import EventType, build_enum_using_values
from hapi_schema.utils.view_params import ViewParams
from hapi_schema.views import get_admin2_case


# normalised table
Expand Down Expand Up @@ -77,6 +78,7 @@ class DBConflictEvent(Base):
DBAdmin2.name.label("admin2_name"),
DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
DBAdmin2.admin1_ref.label("admin1_ref"),
get_admin2_case(DBConflictEvent),
).select_from(
# Join risk to admin2
DBConflictEvent.__table__.join(
Expand Down Expand Up @@ -108,6 +110,7 @@ class DBConflictEvent(Base):
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
get_admin2_case(DBConflictEvent),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
3 changes: 3 additions & 0 deletions src/hapi_schema/db_food_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
build_enum_using_values,
)
from hapi_schema.utils.view_params import ViewParams
from hapi_schema.views import get_admin2_case

# normalised table

Expand Down Expand Up @@ -100,6 +101,7 @@ class DBFoodPrice(Base):
DBAdmin2.code.label("admin2_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
get_admin2_case(DBWFPMarket),
).select_from(
# the admin2 comes from wfp_market
DBFoodPrice.__table__.join(
Expand Down Expand Up @@ -142,6 +144,7 @@ class DBFoodPrice(Base):
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
get_admin2_case(DBWFPMarket),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
3 changes: 3 additions & 0 deletions src/hapi_schema/db_food_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from hapi_schema.utils.enums import IPCPhase, IPCType, build_enum_using_values
from hapi_schema.utils.view_params import ViewParams
from hapi_schema.views import get_admin2_case


class DBFoodSecurity(Base):
Expand Down Expand Up @@ -80,6 +81,7 @@ class DBFoodSecurity(Base):
DBAdmin2.name.label("admin2_name"),
DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
DBAdmin2.admin1_ref.label("admin1_ref"),
get_admin2_case(DBFoodSecurity),
).select_from(
# Join pop to admin2 to admin1 to loc
DBFoodSecurity.__table__.join(
Expand Down Expand Up @@ -112,6 +114,7 @@ class DBFoodSecurity(Base):
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
get_admin2_case(DBFoodSecurity),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
1 change: 1 addition & 0 deletions src/hapi_schema/db_funding.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class DBFunding(Base):
literal("").label("admin1_code"),
literal("").label("admin2_name"),
literal("").label("admin2_code"),
literal(0).label("admin_level"),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
3 changes: 3 additions & 0 deletions src/hapi_schema/db_humanitarian_needs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
build_enum_using_values,
)
from hapi_schema.utils.view_params import ViewParams
from hapi_schema.views import get_admin2_case


class DBHumanitarianNeeds(Base):
Expand Down Expand Up @@ -85,6 +86,7 @@ class DBHumanitarianNeeds(Base):
DBAdmin2.name.label("admin2_name"),
DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
DBAdmin2.admin1_ref.label("admin1_ref"),
get_admin2_case(DBHumanitarianNeeds),
).select_from(
# Join pop to admin2 to admin1 to loc
DBHumanitarianNeeds.__table__.join(
Expand Down Expand Up @@ -122,6 +124,7 @@ class DBHumanitarianNeeds(Base):
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
get_admin2_case(DBHumanitarianNeeds),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
3 changes: 3 additions & 0 deletions src/hapi_schema/db_idps.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from hapi_schema.utils.enums import DTMAssessmentType, build_enum_using_values
from hapi_schema.utils.view_params import ViewParams
from hapi_schema.views import get_admin2_case


class DBIDPs(Base):
Expand Down Expand Up @@ -82,6 +83,7 @@ class DBIDPs(Base):
DBAdmin2.name.label("admin2_name"),
DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
DBAdmin2.admin1_ref.label("admin1_ref"),
get_admin2_case(DBIDPs),
).select_from(
DBIDPs.__table__.join(
DBAdmin2.__table__,
Expand Down Expand Up @@ -112,6 +114,7 @@ class DBIDPs(Base):
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
get_admin2_case(DBIDPs),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
1 change: 1 addition & 0 deletions src/hapi_schema/db_national_risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class DBNationalRisk(Base):
literal("").label("admin1_code"),
literal("").label("admin2_name"),
literal("").label("admin2_code"),
literal(0).label("admin_level"),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
3 changes: 3 additions & 0 deletions src/hapi_schema/db_operational_presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from hapi_schema.utils.base import Base
from hapi_schema.utils.constraints import reference_period_constraint
from hapi_schema.utils.view_params import ViewParams
from hapi_schema.views import get_admin2_case


class DBOperationalPresence(Base):
Expand Down Expand Up @@ -81,6 +82,7 @@ class DBOperationalPresence(Base):
DBAdmin2.name.label("admin2_name"),
DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
DBAdmin2.admin1_ref.label("admin1_ref"),
get_admin2_case(DBOperationalPresence),
).select_from(
# Join op to admin2 to admin1 to loc
DBOperationalPresence.__table__.join(
Expand Down Expand Up @@ -131,6 +133,7 @@ class DBOperationalPresence(Base):
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
get_admin2_case(DBOperationalPresence),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
3 changes: 3 additions & 0 deletions src/hapi_schema/db_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from hapi_schema.utils.enums import Gender, build_enum_using_values
from hapi_schema.utils.view_params import ViewParams
from hapi_schema.views import get_admin2_case


class DBPopulation(Base):
Expand Down Expand Up @@ -82,6 +83,7 @@ class DBPopulation(Base):
DBAdmin2.name.label("admin2_name"),
DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
DBAdmin2.admin1_ref.label("admin1_ref"),
get_admin2_case(DBPopulation),
).select_from(
# Join pop to admin2 to admin1 to loc
DBPopulation.__table__.join(
Expand Down Expand Up @@ -113,6 +115,7 @@ class DBPopulation(Base):
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
get_admin2_case(DBPopulation),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
3 changes: 3 additions & 0 deletions src/hapi_schema/db_poverty_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
reference_period_constraint,
)
from hapi_schema.utils.view_params import ViewParams
from hapi_schema.views import get_admin1_case


# normalised table
Expand Down Expand Up @@ -86,6 +87,7 @@ class DBPovertyRate(Base):
DBAdmin1.code.label("admin1_code"),
DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
DBAdmin1.location_ref.label("location_ref"),
get_admin1_case(DBPovertyRate),
).select_from(
# Join PR to admin1 to loc
DBPovertyRate.__table__.join(
Expand All @@ -111,6 +113,7 @@ class DBPovertyRate(Base):
DBAdmin1.code.label("admin1_code"),
literal("").label("admin2_name"),
literal("").label("admin2_code"),
get_admin1_case(DBPovertyRate),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
1 change: 1 addition & 0 deletions src/hapi_schema/db_refugees.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class DBRefugees(Base):
literal("").label("admin1_code"),
literal("").label("admin2_name"),
literal("").label("admin2_code"),
literal(0).label("admin_level"),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
1 change: 1 addition & 0 deletions src/hapi_schema/db_returnees.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class DBReturnees(Base):
literal("").label("admin1_code"),
literal("").label("admin2_name"),
literal("").label("admin2_code"),
literal(0).label("admin_level"),
DBResource.hapi_updated_date,
)
.select_from(
Expand Down
9 changes: 9 additions & 0 deletions src/hapi_schema/db_views_as_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class DBConflictEventVAT(Base):
admin2_name: Mapped[str] = mapped_column(String(512), index=True)
admin2_is_unspecified: Mapped[bool] = mapped_column(Boolean)
admin1_ref: Mapped[int] = mapped_column(Integer)
admin_level: Mapped[int] = mapped_column(Integer)


class DBCurrencyVAT(Base):
Expand Down Expand Up @@ -174,6 +175,7 @@ class DBFoodPriceVAT(Base):
admin2_code: Mapped[str] = mapped_column(String(128), index=True)
admin2_name: Mapped[str] = mapped_column(String(512), index=True)
admin2_is_unspecified: Mapped[bool] = mapped_column(Boolean)
admin_level: Mapped[int] = mapped_column(Integer)


class DBFoodSecurityVAT(Base):
Expand Down Expand Up @@ -214,6 +216,7 @@ class DBFoodSecurityVAT(Base):
admin2_name: Mapped[str] = mapped_column(String(512), index=True)
admin2_is_unspecified: Mapped[bool] = mapped_column(Boolean)
admin1_ref: Mapped[int] = mapped_column(Integer)
admin_level: Mapped[int] = mapped_column(Integer)


class DBFundingVAT(Base):
Expand Down Expand Up @@ -276,6 +279,7 @@ class DBHumanitarianNeedsVAT(Base):
admin2_name: Mapped[str] = mapped_column(String(512), index=True)
admin2_is_unspecified: Mapped[bool] = mapped_column(Boolean)
admin1_ref: Mapped[int] = mapped_column(Integer)
admin_level: Mapped[int] = mapped_column(Integer)


class DBIDPsVAT(Base):
Expand Down Expand Up @@ -312,6 +316,7 @@ class DBIDPsVAT(Base):
admin2_name: Mapped[str] = mapped_column(String(512), index=True)
admin2_is_unspecified: Mapped[bool] = mapped_column(Boolean)
admin1_ref: Mapped[int] = mapped_column(Integer)
admin_level: Mapped[int] = mapped_column(Integer)


class DBLocationVAT(Base):
Expand Down Expand Up @@ -396,6 +401,7 @@ class DBOperationalPresenceVAT(Base):
admin2_name: Mapped[str] = mapped_column(String(512), index=True)
admin2_is_unspecified: Mapped[bool] = mapped_column(Boolean)
admin1_ref: Mapped[int] = mapped_column(Integer)
admin_level: Mapped[int] = mapped_column(Integer)


class DBOrgTypeVAT(Base):
Expand Down Expand Up @@ -451,6 +457,7 @@ class DBPopulationVAT(Base):
admin2_name: Mapped[str] = mapped_column(String(512), index=True)
admin2_is_unspecified: Mapped[bool] = mapped_column(Boolean)
admin1_ref: Mapped[int] = mapped_column(Integer)
admin_level: Mapped[int] = mapped_column(Integer)


class DBPovertyRateVAT(Base):
Expand Down Expand Up @@ -482,6 +489,7 @@ class DBPovertyRateVAT(Base):
admin1_code: Mapped[str] = mapped_column(String(128))
admin1_is_unspecified: Mapped[bool] = mapped_column(Boolean)
location_ref: Mapped[int] = mapped_column(Integer, index=True)
admin_level: Mapped[int] = mapped_column(Integer)


class DBRefugeesVAT(Base):
Expand Down Expand Up @@ -622,4 +630,5 @@ class DBAvailabilityVAT(Base):
admin1_code: Mapped[str] = mapped_column(String(128), primary_key=True)
admin2_name: Mapped[str] = mapped_column(String(512), primary_key=True)
admin2_code: Mapped[str] = mapped_column(String(128), primary_key=True)
admin_level: Mapped[int] = mapped_column(Integer)
hapi_updated_date: Mapped[datetime] = mapped_column(DateTime, index=True)
Loading

0 comments on commit 27e7676

Please sign in to comment.