hapi-schema 0.8.3__tar.gz → 0.8.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/PKG-INFO +1 -1
  2. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/changelog.md +22 -0
  3. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/_version.py +2 -2
  4. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_operational_presence.py +8 -1
  5. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_poverty_rate.py +29 -9
  6. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_views_as_tables.py +1 -0
  7. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/utils/constraints.py +5 -0
  8. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/utils/enums.py +0 -7
  9. hapi_schema-0.8.5/tests/sample_data/data_poverty_rate.py +16 -0
  10. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_operational_presence.py +2 -0
  11. hapi_schema-0.8.5/tests/test_poverty_rate.py +112 -0
  12. hapi_schema-0.8.3/tests/sample_data/data_poverty_rate.py +0 -12
  13. hapi_schema-0.8.3/tests/test_poverty_rate.py +0 -63
  14. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/.config/coveragerc +0 -0
  15. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/.config/pre-commit-config.yaml +0 -0
  16. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/.config/pytest.ini +0 -0
  17. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/.config/ruff.toml +0 -0
  18. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/.github/workflows/publish.yaml +0 -0
  19. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/.github/workflows/run-python-tests.yaml +0 -0
  20. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/.gitignore +0 -0
  21. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/LICENSE +0 -0
  22. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/README.md +0 -0
  23. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/contributing.md +0 -0
  24. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/docker/docker-compose.yml +0 -0
  25. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/initialize_test_db.sh +0 -0
  26. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/pyproject.toml +0 -0
  27. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/requirements.txt +0 -0
  28. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/__init__.py +0 -0
  29. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_admin1.py +0 -0
  30. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_admin2.py +0 -0
  31. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_conflict_event.py +0 -0
  32. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_currency.py +0 -0
  33. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_dataset.py +0 -0
  34. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_food_price.py +0 -0
  35. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_food_security.py +0 -0
  36. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_funding.py +0 -0
  37. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_humanitarian_needs.py +0 -0
  38. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_location.py +0 -0
  39. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_national_risk.py +0 -0
  40. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_org.py +0 -0
  41. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_org_type.py +0 -0
  42. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_patch.py +0 -0
  43. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_population.py +0 -0
  44. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_refugees.py +0 -0
  45. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_resource.py +0 -0
  46. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_sector.py +0 -0
  47. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_wfp_commodity.py +0 -0
  48. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/db_wfp_market.py +0 -0
  49. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/utils/__init__.py +0 -0
  50. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/utils/base.py +0 -0
  51. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/utils/hapi_views_code_generator.py +0 -0
  52. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/utils/view_as_table_code_generator.py +0 -0
  53. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/utils/view_as_table_definitions.toml +0 -0
  54. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/utils/view_params.py +0 -0
  55. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/src/hapi_schema/views.py +0 -0
  56. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/conftest.py +0 -0
  57. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/__init__.py +0 -0
  58. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_admin1.py +0 -0
  59. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_admin2.py +0 -0
  60. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_conflict_event.py +0 -0
  61. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_currency.py +0 -0
  62. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_dataset.py +0 -0
  63. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_food_price.py +0 -0
  64. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_food_security.py +0 -0
  65. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_funding.py +0 -0
  66. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_humanitarian_needs.py +0 -0
  67. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_location.py +0 -0
  68. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_national_risk.py +0 -0
  69. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_operational_presence.py +0 -0
  70. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_org.py +0 -0
  71. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_org_type.py +0 -0
  72. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_patch.py +0 -0
  73. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_population.py +0 -0
  74. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_refugees.py +0 -0
  75. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_resource.py +0 -0
  76. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_sector.py +0 -0
  77. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_wfp_commodity.py +0 -0
  78. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/sample_data/data_wfp_market.py +0 -0
  79. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_admin1.py +0 -0
  80. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_admin2.py +0 -0
  81. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_conflict_event.py +0 -0
  82. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_currency.py +0 -0
  83. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_dataset.py +0 -0
  84. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_food_price.py +0 -0
  85. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_food_security.py +0 -0
  86. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_funding.py +0 -0
  87. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_humanitarian_needs.py +0 -0
  88. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_location.py +0 -0
  89. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_national_risk.py +0 -0
  90. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_org.py +0 -0
  91. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_org_type.py +0 -0
  92. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_patch.py +0 -0
  93. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_population.py +0 -0
  94. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_refugees.py +0 -0
  95. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_resource.py +0 -0
  96. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_sector.py +0 -0
  97. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_views_as_tables.py +0 -0
  98. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_wfp_commodity.py +0 -0
  99. {hapi_schema-0.8.3 → hapi_schema-0.8.5}/tests/test_wfp_market.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: hapi-schema
3
- Version: 0.8.3
3
+ Version: 0.8.5
4
4
  Summary: HAPI database schema specified in SQLAlchemy
5
5
  Project-URL: Homepage, https://github.com/OCHA-DAP/hapi-schemas
6
6
  Author-email: Simon Johnson <simon.johnson@un.org>
@@ -5,10 +5,32 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## 0.8.5
9
+
10
+ ## Changes
11
+ - refactor DBPovertyRate to wide instead of narrow
12
+ - added org\_type\_description to operational\_presence\_view
13
+
14
+ ## Removed
15
+ - rate\_constraint and `PovertyClassification` enum
16
+
17
+ ## 0.8.4
18
+
19
+ ### Added
20
+
21
+ - New constraints: rate\_constraint
22
+
23
+ ### CHanged
24
+
25
+ - refactor DBPovertyRate to track rates rather than population, and link to
26
+ location (with admin1\_name as a text field)
27
+
8
28
  ## 0.8.3
9
29
 
10
30
  ### Changed
31
+
11
32
  - fixed WFP commodity enum
33
+ - remove ALL ("*") from PovertyClassification enum
12
34
  - change FLOAT to DECIMAL in funding, national\_risk, and food\_security
13
35
 
14
36
  ## 0.8.2
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '0.8.3'
16
- __version_tuple__ = version_tuple = (0, 8, 3)
15
+ __version__ = version = '0.8.5'
16
+ __version_tuple__ = version_tuple = (0, 8, 5)
@@ -15,7 +15,7 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
15
15
  from hapi_schema.db_admin1 import DBAdmin1
16
16
  from hapi_schema.db_admin2 import DBAdmin2
17
17
  from hapi_schema.db_location import DBLocation
18
- from hapi_schema.db_org import DBOrg
18
+ from hapi_schema.db_org import DBOrg, DBOrgType
19
19
  from hapi_schema.db_sector import DBSector
20
20
  from hapi_schema.utils.base import Base
21
21
  from hapi_schema.utils.constraints import reference_period_constraint
@@ -66,6 +66,7 @@ view_params_operational_presence = ViewParams(
66
66
  selectable=select(
67
67
  *DBOperationalPresence.__table__.columns,
68
68
  DBOrg.org_type_code.label("org_type_code"),
69
+ DBOrgType.description.label("org_type_description"),
69
70
  DBSector.name.label("sector_name"),
70
71
  DBLocation.code.label("location_code"),
71
72
  DBLocation.name.label("location_name"),
@@ -101,6 +102,12 @@ view_params_operational_presence = ViewParams(
101
102
  & (DBOperationalPresence.org_acronym == DBOrg.acronym),
102
103
  isouter=True,
103
104
  )
105
+ # Join org to org_type
106
+ .join(
107
+ DBOrgType.__table__,
108
+ (DBOrg.org_type_code == DBOrgType.code),
109
+ isouter=True,
110
+ )
104
111
  # Join op to sector
105
112
  .join(
106
113
  DBSector.__table__,
@@ -4,22 +4,22 @@ from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
6
  DateTime,
7
- Enum,
7
+ Float,
8
8
  ForeignKey,
9
- Integer,
9
+ String,
10
10
  select,
11
11
  )
12
12
  from sqlalchemy.orm import Mapped, mapped_column, relationship
13
+ from sqlalchemy.schema import CheckConstraint
13
14
 
14
15
  from hapi_schema.db_admin1 import DBAdmin1
15
16
  from hapi_schema.db_location import DBLocation
16
17
  from hapi_schema.db_resource import DBResource
17
18
  from hapi_schema.utils.base import Base
18
19
  from hapi_schema.utils.constraints import (
19
- population_constraint,
20
+ percentage_constraint,
20
21
  reference_period_constraint,
21
22
  )
22
- from hapi_schema.utils.enums import PovertyClassification
23
23
  from hapi_schema.utils.view_params import ViewParams
24
24
 
25
25
 
@@ -27,8 +27,15 @@ from hapi_schema.utils.view_params import ViewParams
27
27
  class DBPovertyRate(Base):
28
28
  __tablename__ = "poverty_rate"
29
29
  __table_args__ = (
30
- population_constraint(),
30
+ percentage_constraint(var_name="headcount_ratio"),
31
+ percentage_constraint(var_name="intensity_of_deprivation"),
32
+ percentage_constraint(var_name="vulnerable_to_poverty"),
33
+ percentage_constraint(var_name="in_severe_poverty"),
31
34
  reference_period_constraint(),
35
+ CheckConstraint(
36
+ sqltext="ABS(headcount_ratio / 100 * intensity_of_deprivation / 100 - mpi) < 0.00001",
37
+ name="mpi_product",
38
+ ),
32
39
  )
33
40
 
34
41
  resource_hdx_id: Mapped[str] = mapped_column(
@@ -40,10 +47,23 @@ class DBPovertyRate(Base):
40
47
  nullable=False,
41
48
  primary_key=True,
42
49
  )
43
- classification: Mapped[PovertyClassification] = mapped_column(
44
- Enum(PovertyClassification), nullable=False, primary_key=True
50
+ # TODO temporary -- will remove in future release and use name from admin1 table
51
+ admin1_name: Mapped[str] = mapped_column(
52
+ String(512), nullable=False, index=True, primary_key=True
53
+ )
54
+ mpi: Mapped[float] = mapped_column(Float, nullable=False, index=False)
55
+ headcount_ratio: Mapped[float] = mapped_column(
56
+ Float, nullable=False, index=False
57
+ )
58
+ intensity_of_deprivation: Mapped[float] = mapped_column(
59
+ Float, nullable=False, index=False
60
+ )
61
+ vulnerable_to_poverty: Mapped[float] = mapped_column(
62
+ Float, nullable=False, index=False
63
+ )
64
+ in_severe_poverty: Mapped[float] = mapped_column(
65
+ Float, nullable=False, index=False
45
66
  )
46
- population: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
47
67
  reference_period_start: Mapped[datetime] = mapped_column(
48
68
  DateTime, primary_key=True
49
69
  )
@@ -64,10 +84,10 @@ view_params_poverty_rate = ViewParams(
64
84
  DBLocation.code.label("location_code"),
65
85
  DBLocation.name.label("location_name"),
66
86
  DBAdmin1.code.label("admin1_code"),
67
- DBAdmin1.name.label("admin1_name"),
68
87
  DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
69
88
  DBAdmin1.location_ref.label("location_ref"),
70
89
  ).select_from(
90
+ # Join PR to admin1 to loc
71
91
  DBPovertyRate.__table__.join(
72
92
  DBAdmin1.__table__,
73
93
  DBPovertyRate.admin1_ref == DBAdmin1.id,
@@ -197,6 +197,7 @@ class DBOperationalPresenceVAT(Base):
197
197
  DateTime, index=True
198
198
  )
199
199
  org_type_code: Mapped[str] = mapped_column(String(32))
200
+ org_type_description: Mapped[str] = mapped_column(String(512))
200
201
  sector_name: Mapped[str] = mapped_column(String(512))
201
202
  location_code: Mapped[str] = mapped_column(String(128), index=True)
202
203
  location_name: Mapped[str] = mapped_column(String(512), index=True)
@@ -43,6 +43,11 @@ def non_negative_constraint(
43
43
  return CheckConstraint(sqltext=sqltext, name=f"{var_name}")
44
44
 
45
45
 
46
+ def percentage_constraint(var_name: str) -> CheckConstraint:
47
+ sqltext = f"{var_name} >= 0. AND {var_name} <= 100."
48
+ return CheckConstraint(sqltext=sqltext, name=f"{var_name}")
49
+
50
+
46
51
  def reference_period_constraint() -> CheckConstraint:
47
52
  """reference_period_end should be greater than reference_period_start"""
48
53
  sqltext = "reference_period_end >= reference_period_start "
@@ -62,13 +62,6 @@ class PriceType(str, enum.Enum):
62
62
  WHOLESALE = "Wholesale"
63
63
 
64
64
 
65
- class PovertyClassification(str, enum.Enum):
66
- POOR = "poor"
67
- VULNERABLE = "vulnerable"
68
- EXTREMELY_POOR = "extremely_poor"
69
- ALL = "*"
70
-
71
-
72
65
  class IPCPhase(str, enum.Enum):
73
66
  PHASE_1 = "1"
74
67
  PHASE_2 = "2"
@@ -0,0 +1,16 @@
1
+ from datetime import datetime
2
+
3
+ data_poverty_rate = [
4
+ dict(
5
+ resource_hdx_id="90deb235-1bf5-4bae-b231-3393222c2d01",
6
+ admin1_ref=1,
7
+ admin1_name="Province 01", # not p-coded/normalised yet
8
+ mpi=0.617442,
9
+ headcount_ratio=85.4,
10
+ intensity_of_deprivation=72.3,
11
+ vulnerable_to_poverty=10.5,
12
+ in_severe_poverty=52.1,
13
+ reference_period_start=datetime(2024, 1, 1),
14
+ reference_period_end=datetime(2024, 12, 31),
15
+ ),
16
+ ]
@@ -23,6 +23,8 @@ def test_operational_presence_view(run_view_test):
23
23
  view_operational_presence.c.location_code == "FOO",
24
24
  view_operational_presence.c.org_acronym == "ORG02",
25
25
  view_operational_presence.c.org_type_code == "437",
26
+ view_operational_presence.c.org_type_description
27
+ == "International NGO",
26
28
  view_operational_presence.c.sector_name
27
29
  == "Water Sanitation Hygiene",
28
30
  ),
@@ -0,0 +1,112 @@
1
+ from datetime import datetime
2
+
3
+ from hdx.database import Database
4
+
5
+ from hapi_schema.db_poverty_rate import (
6
+ DBPovertyRate,
7
+ view_params_poverty_rate,
8
+ )
9
+
10
+
11
+ def test_poverty_rate_view(run_view_test):
12
+ """Check that poverty_rate references other tables."""
13
+ view_poverty_rate = Database.prepare_view(
14
+ view_params_poverty_rate.__dict__
15
+ )
16
+ run_view_test(
17
+ view=view_poverty_rate,
18
+ whereclause=(
19
+ view_poverty_rate.c.resource_hdx_id
20
+ == "90deb235-1bf5-4bae-b231-3393222c2d01",
21
+ view_poverty_rate.c.location_name == "Foolandia",
22
+ view_poverty_rate.c.admin1_name == "Province 01",
23
+ ),
24
+ )
25
+
26
+
27
+ def test_headcount_ratio_constraint(run_constraints_test):
28
+ """Check that headcount ratio is between 0 and 100"""
29
+ data = _sample_data()
30
+ data["headcount_ratio"] = 101
31
+ run_constraints_test(
32
+ new_rows=[
33
+ DBPovertyRate(**data),
34
+ ],
35
+ expected_constraint="headcount_ratio",
36
+ )
37
+
38
+
39
+ def test_intensity_of_deprivation_constraint(run_constraints_test):
40
+ """Check that intensity of deprivation is between 0 and 100"""
41
+ data = _sample_data()
42
+ data["intensity_of_deprivation"] = 101
43
+ run_constraints_test(
44
+ new_rows=[
45
+ DBPovertyRate(**data),
46
+ ],
47
+ expected_constraint="intensity_of_deprivation",
48
+ )
49
+
50
+
51
+ def test_vulnerable_to_poverty_constraint(run_constraints_test):
52
+ """Check that vulnerable_to_poverty is between 0 and 100"""
53
+ data = _sample_data()
54
+ data["vulnerable_to_poverty"] = 101
55
+ run_constraints_test(
56
+ new_rows=[
57
+ DBPovertyRate(**data),
58
+ ],
59
+ expected_constraint="vulnerable_to_poverty",
60
+ )
61
+
62
+
63
+ def test_in_severe_poverty_constraint(run_constraints_test):
64
+ """Check that in_severe_poverty is between 0 and 100"""
65
+ data = _sample_data()
66
+ data["in_severe_poverty"] = 101
67
+ run_constraints_test(
68
+ new_rows=[
69
+ DBPovertyRate(**data),
70
+ ],
71
+ expected_constraint="in_severe_poverty",
72
+ )
73
+
74
+
75
+ def test_mpi_product_constraint(run_constraints_test):
76
+ """Check that MPI is between 0.0 and 1.0"""
77
+ data = _sample_data()
78
+ data["mpi"] = 1.1
79
+ run_constraints_test(
80
+ new_rows=[
81
+ DBPovertyRate(**data),
82
+ ],
83
+ expected_constraint="mpi_product",
84
+ )
85
+
86
+
87
+ def test_reference_period_constraint(run_constraints_test):
88
+ """Check that reference_period_end cannot be less than start"""
89
+ data = _sample_data()
90
+ data["reference_period_start"] = datetime(2025, 1, 1)
91
+ run_constraints_test(
92
+ new_rows=[
93
+ DBPovertyRate(**data),
94
+ ],
95
+ expected_constraint="reference_period",
96
+ )
97
+
98
+
99
+ def _sample_data():
100
+ # return the whole record, then tests can change as needed
101
+ return dict(
102
+ resource_hdx_id="90deb235-1bf5-4bae-b231-3393222c2d01",
103
+ admin1_ref=1,
104
+ admin1_name="Province 02",
105
+ mpi=0.617442,
106
+ headcount_ratio=85.4,
107
+ intensity_of_deprivation=72.3,
108
+ vulnerable_to_poverty=10.5,
109
+ in_severe_poverty=52.1,
110
+ reference_period_start=datetime(2024, 1, 1),
111
+ reference_period_end=datetime(2024, 12, 31),
112
+ )
@@ -1,12 +0,0 @@
1
- from datetime import datetime
2
-
3
- data_poverty_rate = [
4
- dict(
5
- resource_hdx_id="90deb235-1bf5-4bae-b231-3393222c2d01",
6
- admin1_ref=2,
7
- classification="vulnerable",
8
- population=1000000,
9
- reference_period_start=datetime(2024, 1, 1),
10
- reference_period_end=datetime(2024, 12, 31),
11
- ),
12
- ]
@@ -1,63 +0,0 @@
1
- from datetime import datetime
2
-
3
- from hdx.database import Database
4
-
5
- from hapi_schema.db_poverty_rate import (
6
- DBPovertyRate,
7
- view_params_poverty_rate,
8
- )
9
-
10
-
11
- def test_poverty_rate_view(run_view_test):
12
- """Check that poverty_rate references other tables."""
13
- view_poverty_rate = Database.prepare_view(
14
- view_params_poverty_rate.__dict__
15
- )
16
- run_view_test(
17
- view=view_poverty_rate,
18
- whereclause=(
19
- view_poverty_rate.c.resource_hdx_id
20
- == "90deb235-1bf5-4bae-b231-3393222c2d01",
21
- view_poverty_rate.c.location_name == "Foolandia",
22
- view_poverty_rate.c.admin1_name == "Province 01",
23
- ),
24
- )
25
-
26
-
27
- def test_population_constraint(run_constraints_test):
28
- """Check that population cannot be negative"""
29
- data = _sample_data()
30
- data["population"] = -1
31
- run_constraints_test(
32
- new_rows=[
33
- DBPovertyRate(**data),
34
- ],
35
- expected_constraint="population",
36
- )
37
-
38
-
39
- def test_reference_period_constraint(run_constraints_test):
40
- """Check that reference_period_end cannot be less than start"""
41
- data = _sample_data()
42
- data["reference_period_start"] = datetime(2025, 1, 1)
43
- run_constraints_test(
44
- new_rows=[
45
- DBPovertyRate(**data),
46
- ],
47
- expected_constraint="reference_period",
48
- )
49
-
50
-
51
- # Util functions
52
-
53
-
54
- def _sample_data():
55
- # return the whole record, then tests can change as needed
56
- return dict(
57
- resource_hdx_id="90deb235-1bf5-4bae-b231-3393222c2d01",
58
- admin1_ref=1,
59
- classification="vulnerable",
60
- population=1000000,
61
- reference_period_start=datetime(2024, 1, 1),
62
- reference_period_end=datetime(2024, 12, 31),
63
- )
File without changes
File without changes
File without changes
File without changes
File without changes