Skip to content

Commit

Permalink
fix imports
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed May 8, 2023
1 parent 188f54c commit 2599037
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from contextlib import contextmanager
from copy import deepcopy
from typing import Any, Iterator
from typing import Iterator, TypedDict

import simcore_postgres_database.cli
import sqlalchemy as sa
Expand All @@ -11,20 +10,25 @@
log = logging.getLogger(__name__)


class PostgresTestConfig(TypedDict):
user: str
password: str
database: str
host: str
port: str


@contextmanager
def migrated_pg_tables_context(
postgres_config: dict[str, str]
) -> Iterator[dict[str, Any]]:
postgres_config: PostgresTestConfig,
) -> Iterator[PostgresTestConfig]:
"""
Within the context, tables are created and dropped
using migration upgrade/downgrade routines
"""

cfg = deepcopy(postgres_config)
cfg.update(
dsn="postgresql://{user}:{password}@{host}:{port}/{database}".format(
**postgres_config
)
dsn = "postgresql://{user}:{password}@{host}:{port}/{database}".format(
**postgres_config
)

assert simcore_postgres_database.cli.discover.callback
Expand All @@ -33,7 +37,7 @@ def migrated_pg_tables_context(
simcore_postgres_database.cli.discover.callback(**postgres_config)
simcore_postgres_database.cli.upgrade.callback("head")

yield cfg
yield postgres_config

# downgrades database to zero ---
#
Expand All @@ -49,7 +53,7 @@ def migrated_pg_tables_context(
# FIXME: migration downgrade fails to remove User types
# SEE https://github.com/ITISFoundation/osparc-simcore/issues/1776
# Added drop_all as tmp fix
postgres_engine = sa.create_engine(cfg["dsn"])
postgres_engine = sa.create_engine(dsn)
metadata.drop_all(bind=postgres_engine)


Expand Down
14 changes: 3 additions & 11 deletions packages/pytest-simcore/src/pytest_simcore/postgres_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# pylint: disable=unused-argument
# pylint: disable=unused-variable

from typing import AsyncIterator, Final, Iterator, TypedDict
from typing import AsyncIterator, Final, Iterator

import docker
import pytest
Expand All @@ -14,19 +14,11 @@
from tenacity.wait import wait_fixed

from .helpers.utils_docker import get_localhost_ip, get_service_published_port
from .helpers.utils_postgres import migrated_pg_tables_context
from .helpers.utils_postgres import PostgresTestConfig, migrated_pg_tables_context

TEMPLATE_DB_TO_RESTORE = "template_simcore_db"


class PostgresTestConfig(TypedDict):
user: str
password: str
database: str
host: str
port: str


def execute_queries(
postgres_engine: sa.engine.Engine,
sql_statements: list[str],
Expand Down Expand Up @@ -193,7 +185,7 @@ def postgres_db(
) -> Iterator[sa.engine.Engine]:
"""An postgres database init with empty tables and an sqlalchemy engine connected to it"""

with migrated_pg_tables_context(docker_client, postgres_dsn.copy()):
with migrated_pg_tables_context(postgres_dsn.copy()):
yield postgres_engine


Expand Down

0 comments on commit 2599037

Please sign in to comment.