diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 65393da..4eb0dc3 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -36,6 +36,12 @@ jobs: with: submodules: recursive + - name: Install Postgresql 15 for unit tests + id: install-postgres-15 + run: | + sudo apt-get update -qy && \ + sudo apt install -qy postgresql + - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: @@ -62,7 +68,7 @@ jobs: run: poetry run pre-commit run - name: Run pytest - run: poetry run pytest --cov=openeo_processes_dask --cov-report=xml + run: poetry run pytest --cov=openeo-fastapi --cov-report=xml - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 diff --git a/.gitignore b/.gitignore index 70dd045..bd76ed9 100644 --- a/.gitignore +++ b/.gitignore @@ -50,6 +50,7 @@ coverage.xml .hypothesis/ .pytest_cache/ cover/ +tests/__pycache__ # Translations *.mo @@ -163,4 +164,4 @@ cython_debug/ notebooks/ .notebooks/ -poetry.lock \ No newline at end of file +poetry.lock diff --git a/README.md b/README.md index f31b059..f8a7e92 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,36 @@ A FastAPI implementation of the OpenEO Api specification. +## Install + +Install using pip +``` +``` + +### Use + +The openeo fastapi repo has been set up to work with alembic. When you use this package to to prepare your api, you will need to create an alembic directory. In this directory, you can optionally add a models.py file and extend and of the models from openeo_fastapi.client.models. + +The env.py file in the alembic directory, needs to be edited in the following way. +``` +from openeo_fastapi.settings import BASE + +target_metadata = BASE.metadata +``` + +You can now create auto revisions for a psql database using the alembic python commands. + +``` +alembic_cfg = Config("alembic.ini") + +command.revision(alembic_cfg, f"openeo-fastapi-{__version__}", autogenerate=True) +command.upgrade(alembic_cfg, "head") + +engine = get_engine() +``` + +Check how it is configured for the tests to see more. + ## Contribute Included is a vscode dev container which is intended to be used as the development environment for this package. A virtual environment needs to be set up inside the dev container, this is managed by poetry. diff --git a/openeo_fastapi/client/psql/__init__.py b/openeo_fastapi/client/psql/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/openeo_fastapi/client/psql/engine.py b/openeo_fastapi/client/psql/engine.py new file mode 100644 index 0000000..5f3267c --- /dev/null +++ b/openeo_fastapi/client/psql/engine.py @@ -0,0 +1,19 @@ +from sqlalchemy import create_engine + +from openeo_fastapi.client.psql.settings import BASE, DataBaseSettings + + +def get_engine(): + """Return default session using config from pydantic settings.""" + db_settings = DataBaseSettings() + + engine = create_engine( + url="postgresql://{}:{}@{}:{}/{}".format( + db_settings.POSTGRES_USER._secret_value, + db_settings.POSTGRES_PASSWORD._secret_value, + db_settings.POSTGRESQL_HOST._secret_value, + db_settings.POSTGRESQL_PORT._secret_value, + db_settings.POSTGRES_DB._secret_value, + ) + ) + return engine diff --git a/openeo_fastapi/client/psql/models.py b/openeo_fastapi/client/psql/models.py new file mode 100644 index 0000000..c039355 --- /dev/null +++ b/openeo_fastapi/client/psql/models.py @@ -0,0 +1,17 @@ +import datetime + +from sqlalchemy import VARCHAR, Column, DateTime +from sqlalchemy.dialects.postgresql import UUID + +from openeo_fastapi.client.psql.settings import BASE + + +class User(BASE): + """ORM for the user table.""" + + __tablename__ = "users" + __table_args__ = {"extend_existing": True} + + user_id = Column(UUID(as_uuid=True), primary_key=True, unique=True) + oidc_sub = Column(VARCHAR, unique=True) + created_at = Column(DateTime, default=datetime.datetime.utcnow(), nullable=False) diff --git a/openeo_fastapi/client/psql/settings.py b/openeo_fastapi/client/psql/settings.py new file mode 100644 index 0000000..2ba72f7 --- /dev/null +++ b/openeo_fastapi/client/psql/settings.py @@ -0,0 +1,16 @@ +from pathlib import Path + +from pydantic import BaseSettings, SecretStr +from sqlalchemy.orm import declarative_base + +BASE = declarative_base() + + +class DataBaseSettings(BaseSettings): + POSTGRES_USER: SecretStr + POSTGRES_PASSWORD: SecretStr + POSTGRESQL_HOST: SecretStr + POSTGRESQL_PORT: SecretStr + POSTGRES_DB: SecretStr + + ALEMBIC_DIR: Path diff --git a/pyproject.toml b/pyproject.toml index d4818c9..b6ae4f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,8 +24,10 @@ planetary_computer = ">=1.0.0" stackstac = ">=0.5.0" stac-validator = ">=3.3.2" pip = "^23.3.2" -ipykernel = "^6.28.0" requests = "^2.31.0" +SQLAlchemy = "^2.0.27" +psycopg2-binary = "^2.9.5" +alembic = "^1.13.1" [tool.poetry.group.dev.dependencies] pytest = "^7.2.0" @@ -34,6 +36,7 @@ pre-commit = "^2.20.0" pytest-cov = "^4.0.0" pytest-asyncio = "^0.23.0" aioresponses = "^0.7.5" +pytest-postgresql = ">=4.1.1" [build-system] requires = ["poetry-core"] diff --git a/tests/alembic/README.md b/tests/alembic/README.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/alembic/alembic.ini b/tests/alembic/alembic.ini new file mode 100644 index 0000000..c10d4ca --- /dev/null +++ b/tests/alembic/alembic.ini @@ -0,0 +1,116 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = alembic + +# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s +# Uncomment the line below if you want the files to be prepended with date and time +# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file +# for all available tokens +# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python>=3.9 or backports.zoneinfo library. +# Any required deps can installed by adding `alembic[tz]` to the pip requirements +# string value is passed to ZoneInfo() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to alembic/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# set to 'true' to search source files recursively +# in each "version_locations" directory +# new in Alembic version 1.10 +# recursive_version_locations = false + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +sqlalchemy.url = driver://user:pass@localhost/dbname + + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# lint with attempts to fix using "ruff" - use the exec runner, execute a binary +# hooks = ruff +# ruff.type = exec +# ruff.executable = %(here)s/.venv/bin/ruff +# ruff.options = --fix REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/tests/alembic/alembic/README b/tests/alembic/alembic/README new file mode 100644 index 0000000..2500aa1 --- /dev/null +++ b/tests/alembic/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. diff --git a/tests/alembic/alembic/env.py b/tests/alembic/alembic/env.py new file mode 100644 index 0000000..21a1e2c --- /dev/null +++ b/tests/alembic/alembic/env.py @@ -0,0 +1,84 @@ +from logging.config import fileConfig +from os import environ + +from alembic import context +from sqlalchemy import engine_from_config, pool + +from tests.alembic.models import BASE + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config +config.set_main_option( + "sqlalchemy.url", + f"postgresql://{environ.get('POSTGRES_USER')}:{environ.get('POSTGRES_PASSWORD')}" + f"@{environ.get('POSTGRESQL_HOST')}:{environ.get('POSTGRESQL_PORT')}" + f"/{environ.get('POSTGRES_DB')}", +) + + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +target_metadata = BASE.metadata +# target_metadata = None + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure(connection=connection, target_metadata=target_metadata) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/tests/alembic/alembic/script.py.mako b/tests/alembic/alembic/script.py.mako new file mode 100644 index 0000000..fbc4b07 --- /dev/null +++ b/tests/alembic/alembic/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/tests/alembic/models.py b/tests/alembic/models.py new file mode 100644 index 0000000..3793424 --- /dev/null +++ b/tests/alembic/models.py @@ -0,0 +1,3 @@ +from openeo_fastapi.client.psql.settings import BASE + +metadata = BASE.metadata diff --git a/tests/client/test_psql.py b/tests/client/test_psql.py new file mode 100644 index 0000000..ed61b93 --- /dev/null +++ b/tests/client/test_psql.py @@ -0,0 +1,73 @@ +import pytest +from sqlalchemy import BOOLEAN, Column, select +from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm import sessionmaker + +from openeo_fastapi.client.psql.models import User + + +def test_db_setup(mock_engine): + """A test to validate the basic structure of our ORMs and get_engine functions.""" + import uuid + + user_uid = uuid.uuid4() + user = User(user_id=user_uid, oidc_sub="someone@egi.eu") + + session = sessionmaker(mock_engine) + with session.begin() as sesh: + sesh.add(user) + + with session.begin() as sesh: + right_user = select(User).filter_by(user_id=user_uid) + wrong_user = select(User).filter_by(user_id=uuid.uuid4()) + + assert sesh.scalars(right_user).first() + assert not sesh.scalars(wrong_user).first() + + +def test_db_models_extendable(mock_engine): + """Test the existing models can be extended and used to revise the database.""" + + import uuid + + user_uid = uuid.uuid4() + + # Extend the user class + class ExtendedUser(User): + """ORM for the user table.""" + + new_value = Column(BOOLEAN, nullable=False) + + # Try to revise the database using the extended User + import os + from pathlib import Path + + from alembic import command + from alembic.config import Config + + from tests.conftest import ALEMBIC_DIR + + os.chdir(Path(ALEMBIC_DIR)) + alembic_cfg = Config("alembic.ini") + + command.revision(alembic_cfg, f"openeo-fastapi-extended", autogenerate=True) + command.upgrade(alembic_cfg, "head") + + # See if the revision has been extended + a_user = ExtendedUser(user_id=user_uid, oidc_sub="someone@egi.eu", new_value=True) + + session = sessionmaker(mock_engine) + with session.begin() as sesh: + sesh.add(a_user) + + with session.begin() as sesh: + extended_user = select(ExtendedUser).filter_by(user_id=user_uid) + + assert sesh.scalars(extended_user).first().new_value == True + + # Using the non-extended model should now raise an error + old_user = User(user_id=uuid.uuid4(), oidc_sub="someone@egi.eu") + + with pytest.raises(IntegrityError): + with session.begin() as sesh: + sesh.add(old_user) diff --git a/tests/conftest.py b/tests/conftest.py index 0b054a5..5e382ac 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,11 @@ +import importlib.metadata import json import os +from pathlib import Path from unittest import mock from unittest.mock import patch +import fsspec import pytest from fastapi import FastAPI from requests import Response @@ -15,6 +18,14 @@ path_to_current_file = os.path.realpath(__file__) current_directory = os.path.split(path_to_current_file)[0] +# Have the version ready for using as an autorevision name +__version__ = importlib.metadata.version("openeo_fastapi") + +# A user needs to have an alembic directory for the auto generated revisions to be added to. +ALEMBIC_DIR = Path(__file__).parent.parent / "tests/alembic/" + +fs = fsspec.filesystem(protocol="file") + @pytest.fixture(autouse=True) def mock_settings_env_vars(): @@ -26,6 +37,7 @@ def mock_settings_env_vars(): "API_TITLE": "Test Api", "API_DESCRIPTION": "My Test Api", "STAC_API_URL": "http://test-stac-api.mock.com/api/", + "ALEMBIC_DIR": str(ALEMBIC_DIR), }, ): yield @@ -69,7 +81,7 @@ def collections_core(): @pytest.fixture() def collections(): - with open(os.path.join(current_directory, "collections.json")) as f_in: + with open(os.path.join(current_directory, "data/collections.json")) as f_in: return json.load(f_in) @@ -140,3 +152,63 @@ def mocked_issuer(): organisation="mycloud", roles=["admin", "user"], ) + + +@pytest.fixture() +def mock_engine(postgresql): + """Postgresql engine for SQLAlchemy.""" + import os + from pathlib import Path + + from alembic import command + from alembic.config import Config + + from openeo_fastapi.client.psql.engine import get_engine + + os.chdir(Path(ALEMBIC_DIR)) + + # Set the env vars that alembic will use for DB connection and run alembic engine from CLI! + os.environ["POSTGRES_USER"] = postgresql.info.user + os.environ["POSTGRES_PASSWORD"] = "postgres" + os.environ["POSTGRESQL_HOST"] = postgresql.info.host + os.environ["POSTGRESQL_PORT"] = str(postgresql.info.port) + os.environ["POSTGRES_DB"] = postgresql.info.dbname + + alembic_cfg = Config("alembic.ini") + + command.revision(alembic_cfg, f"openeo-fastapi-{__version__}", autogenerate=True) + command.upgrade(alembic_cfg, "head") + + engine = get_engine() + + return engine + + +@pytest.fixture(scope="function", autouse=True) +def cleanup_out_folder(): + # Path to test alembic versions folder + alembic_version_dir = str(ALEMBIC_DIR / "alembic/versions") + alembic_pycache = str(ALEMBIC_DIR / "__pycache__") + alembic_cache_dir = str(ALEMBIC_DIR / "alembic/__pycache__") + alembic_ver_cache_dir = str(ALEMBIC_DIR / "alembic/versions/__pycache__") + + if not fs.exists(alembic_version_dir): + fs.mkdir(alembic_version_dir) + + yield # Yield to the running tests + + # Teardown: Delete the output folder, + if fs.exists(alembic_version_dir): + for file in fs.ls(alembic_version_dir): + fs.rm(file, recursive=True) + fs.rmdir(alembic_version_dir) + + # Remove alembic pycaches + if fs.exists(alembic_pycache): + fs.rm(alembic_pycache, recursive=True) + + if fs.exists(alembic_ver_cache_dir): + fs.rm(alembic_ver_cache_dir, recursive=True) + + if fs.exists(alembic_cache_dir): + fs.rm(alembic_cache_dir, recursive=True) diff --git a/tests/collections.json b/tests/data/collections.json similarity index 100% rename from tests/collections.json rename to tests/data/collections.json