Skip to content

Commit

Permalink
HDXDSYS-1158 HAPI SQLAlchemy Schema - return views from prepare_hapi_…
Browse files Browse the repository at this point in the history
…views (#69)

* Return views from prepare_hapi_views

* Update CHANGELOG
  • Loading branch information
mcarans authored Oct 15, 2024
1 parent b6c854b commit 586c8c6
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .config/pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repos:
- id: end-of-file-fixer
- id: check-ast
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.5
rev: v0.6.9
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.2.31
rev: 0.4.18
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.1

### Added

- Return views from prepare_hapi_views call

## 0.9.0

### Added
Expand Down
24 changes: 12 additions & 12 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,49 @@
# uv pip compile pyproject.toml --resolver=backtracking --all-extras -o requirements.txt
cfgv==3.4.0
# via pre-commit
coverage==7.6.1
coverage==7.6.3
# via pytest-cov
distlib==0.3.8
distlib==0.3.9
# via virtualenv
filelock==3.15.4
filelock==3.16.1
# via virtualenv
greenlet==3.0.3
greenlet==3.1.1
# via sqlalchemy
hdx-python-database==1.3.3
# via hapi-schema (pyproject.toml)
identify==2.6.0
identify==2.6.1
# via pre-commit
iniconfig==2.0.0
# via pytest
nodeenv==1.9.1
# via pre-commit
packaging==24.1
# via pytest
platformdirs==4.2.2
platformdirs==4.3.6
# via virtualenv
pluggy==1.5.0
# via pytest
pre-commit==3.8.0
pre-commit==4.0.1
# via hapi-schema (pyproject.toml)
psycopg==3.2.1
psycopg==3.2.3
# via hapi-schema (pyproject.toml)
psycopg-binary==3.2.1
psycopg-binary==3.2.3
# via psycopg
pytest==8.3.2
pytest==8.3.3
# via
# hapi-schema (pyproject.toml)
# pytest-cov
pytest-cov==5.0.0
# via hapi-schema (pyproject.toml)
pyyaml==6.0.2
# via pre-commit
sqlalchemy==2.0.32
sqlalchemy==2.0.35
# via
# hapi-schema (pyproject.toml)
# hdx-python-database
typing-extensions==4.12.2
# via
# psycopg
# sqlalchemy
virtualenv==20.26.3
virtualenv==20.26.6
# via pre-commit
13 changes: 9 additions & 4 deletions src/hapi_schema/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import inspect
import os
from importlib import import_module
from typing import Dict

from sqlalchemy import TableClause
from sqlalchemy.sql.expression import union_all

from hapi_schema.utils.base import Base
Expand All @@ -14,14 +16,15 @@
pass


def prepare_hapi_views():
def prepare_hapi_views() -> Dict[str, TableClause]:
# Programmatically get views and prepare them for creation.
# Views must be in files with filename of form: db_{name}.py in the same
# directory. Views must be named like this: view_params_{name}.
global view_availability
path = inspect.getabsfile(prepare_hapi_views)
dirpath, _ = os.path.split(path)
availability_stmts = []
views = {}
for path in os.listdir(dirpath):
if os.path.isdir(path):
continue
Expand All @@ -32,7 +35,7 @@ def prepare_hapi_views():
table = filename[3:]
try:
view_params = getattr(module, f"view_params_{table}")
Database.prepare_view(view_params.__dict__)
views[table] = Database.prepare_view(view_params.__dict__)
availability_stmt = getattr(
module, f"availability_stmt_{table}"
)
Expand All @@ -48,5 +51,7 @@ def prepare_hapi_views():
selectable=union_all(*availability_stmts),
)

# Return this view (to simplify unit tests)
return Database.prepare_view(view_params_availability.__dict__)
views["data_availability"] = Database.prepare_view(
view_params_availability.__dict__
)
return views
2 changes: 1 addition & 1 deletion tests/test_conflict_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_conflict_event_view(run_view_test):


def test_conflict_event_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_food_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_food_price_view(run_view_test):


def test_food_price_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_food_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_food_security_view(run_view_test):


def test_food_security_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_funding.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_funding_view(run_view_test):


def test_funding_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_humanitarian_needs.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_humanitarian_needs_view(run_view_test):


def test_humanitarian_needs_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_idps.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_idps_view(run_view_test):


def test_idps_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_national_risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_national_risk_view(run_view_test):


def test_national_risk_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_operational_presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_operational_presence_view(run_view_test):


def test_operational_presence_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_population_view(run_view_test):


def test_population_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_poverty_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_poverty_rate_view(run_view_test):


def test_poverty_rate_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_refugees.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_refugees_view(run_view_test):


def test_refugees_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_returnees.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_returnees_view(run_view_test):


def test_returnees_availability(run_view_test):
view_availability = prepare_hapi_views()
view_availability = prepare_hapi_views()["data_availability"]
run_view_test(
view=view_availability,
whereclause=(
Expand Down

0 comments on commit 586c8c6

Please sign in to comment.