Skip to content

Commit 23ee1f1

Browse files
committed
unified undre postgres_tools
1 parent 0b02c1e commit 23ee1f1

File tree

7 files changed

+40
-39
lines changed

7 files changed

+40
-39
lines changed

packages/postgres-database/tests/conftest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from aiopg.sa.engine import Engine
1919
from aiopg.sa.result import ResultProxy, RowProxy
2020
from faker import Faker
21+
from pytest_simcore.helpers import postgres_tools
2122
from pytest_simcore.helpers.faker_factories import (
2223
random_group,
2324
random_project,
@@ -165,10 +166,7 @@ def pg_sa_engine(
165166

166167
yield sync_engine
167168

168-
# NOTE: ALL is deleted after
169-
with sync_engine.begin() as conn:
170-
conn.execute(sa.DDL("DROP TABLE IF EXISTS alembic_version"))
171-
db_metadata.drop_all(sync_engine)
169+
postgres_tools.drop_all_tables(sync_engine)
172170
sync_engine.dispose()
173171

174172

packages/postgres-database/tests/test_models_groups.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# pylint: disable=redefined-outer-name
2-
# pylint: disable=unused-argument
3-
# pylint: disable=unused-variable
2+
# pylint: disable=unused-argument# pylint: disable=unused-variable
43
# pylint: disable=too-many-arguments
54

65

@@ -13,6 +12,7 @@
1312
from aiopg.sa.engine import Engine
1413
from aiopg.sa.result import ResultProxy, RowProxy
1514
from psycopg2.errors import ForeignKeyViolation, RaiseException, UniqueViolation
15+
from pytest_simcore.helpers import postgres_tools
1616
from pytest_simcore.helpers.faker_factories import random_user
1717
from simcore_postgres_database.models.base import metadata
1818
from simcore_postgres_database.webserver_models import (
@@ -36,7 +36,7 @@ async def connection(
3636
async with engine.acquire() as conn:
3737
yield conn
3838

39-
metadata.drop_all(sync_engine)
39+
postgres_tools.drop_all_tables(sync_engine)
4040

4141

4242
async def test_user_group_uniqueness(

packages/postgres-database/tests/test_uniqueness_in_comp_tasks.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
# pylint:disable=redefined-outer-name
55

66
import json
7+
from collections.abc import Awaitable, Callable
78

9+
import aiopg.sa.engine
10+
import aiopg.sa.exc
811
import pytest
912
import sqlalchemy as sa
1013
from psycopg2.errors import UniqueViolation # pylint: disable=no-name-in-module
14+
from pytest_simcore.helpers import postgres_tools
1115
from pytest_simcore.helpers.faker_factories import fake_pipeline, fake_task_factory
1216
from simcore_postgres_database.models.base import metadata
1317
from simcore_postgres_database.webserver_models import comp_pipeline, comp_tasks
@@ -16,9 +20,11 @@
1620

1721

1822
@pytest.fixture
19-
async def engine(make_engine):
20-
engine = await make_engine()
21-
sync_engine = make_engine(is_async=False)
23+
async def engine(
24+
make_engine: Callable[[bool], Awaitable[aiopg.sa.engine.Engine] | sa.engine.Engine]
25+
):
26+
engine: aiopg.sa.engine.Engine = await make_engine()
27+
sync_engine: sa.engine.Engine = make_engine(is_async=False)
2228
metadata.drop_all(sync_engine)
2329
metadata.create_all(sync_engine)
2430

@@ -32,6 +38,7 @@ async def engine(make_engine):
3238

3339
yield engine
3440

41+
postgres_tools.drop_all_tables(sync_engine)
3542
engine.close()
3643
await engine.wait_closed()
3744

packages/pytest-simcore/src/pytest_simcore/helpers/postgres_tools.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ class PostgresTestConfig(TypedDict):
1717
port: str
1818

1919

20+
def drop_all_tables(sa_sync_engine: sa.engine.Engine):
21+
with sa_sync_engine.begin() as conn:
22+
conn.execute(sa.DDL("DROP TABLE IF EXISTS alembic_version"))
23+
conn.execute(
24+
# NOTE: terminates all open transactions before droping all tables
25+
# This solves https://github.com/ITISFoundation/osparc-simcore/issues/7008
26+
sa.DDL(
27+
"SELECT pg_terminate_backend(pid) "
28+
"FROM pg_stat_activity "
29+
"WHERE state = 'idle in transaction';"
30+
)
31+
)
32+
# SEE https://github.com/ITISFoundation/osparc-simcore/issues/1776
33+
metadata.drop_all(bind=conn)
34+
35+
2036
@contextmanager
2137
def migrated_pg_tables_context(
2238
postgres_config: PostgresTestConfig,
@@ -50,18 +66,8 @@ def migrated_pg_tables_context(
5066
simcore_postgres_database.cli.clean.callback() # just cleans discover cache
5167

5268
postgres_engine = sa.create_engine(dsn)
53-
with postgres_engine.begin() as conn:
54-
conn.execute(
55-
# NOTE: terminates all open transactions before droping all tables
56-
# This solves https://github.com/ITISFoundation/osparc-simcore/issues/7008
57-
sa.DDL(
58-
"SELECT pg_terminate_backend(pid) "
59-
"FROM pg_stat_activity "
60-
"WHERE state = 'idle in transaction';"
61-
)
62-
)
63-
# SEE https://github.com/ITISFoundation/osparc-simcore/issues/1776
64-
metadata.drop_all(bind=postgres_engine)
69+
drop_all_tables(postgres_engine)
70+
postgres_engine.dispose()
6571

6672

6773
def is_postgres_responsive(url) -> bool:

packages/service-library/tests/aiohttp/with_postgres/test_aiopg_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pytest
1414
import sqlalchemy as sa
1515
import sqlalchemy.exc as sa_exceptions
16+
from pytest_simcore.helpers import postgres_tools
1617
from servicelib.aiohttp.aiopg_utils import init_pg_tables, is_pg_responsive
1718
from servicelib.common_aiopg_utils import DataSourceName, create_pg_engine
1819

@@ -64,7 +65,7 @@ def test_dsn_uri_with_query(postgres_service_with_fake_data: DataSourceName):
6465

6566
# if url is wrong, these will fail
6667
metadata.create_all(sa_engine)
67-
metadata.drop_all(sa_engine)
68+
postgres_tools.drop_all_tables(sa_engine)
6869

6970
except sa_exceptions.SQLAlchemyError as ee:
7071
pytest.fail(f"Cannot connect with {uri}: {ee}")

services/api-server/tests/unit/_with_db/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from models_library.api_schemas_api_server.api_keys import ApiKeyInDB
2525
from pydantic import PositiveInt
2626
from pytest_mock import MockerFixture
27+
from pytest_simcore.helpers import postgres_tools
2728
from pytest_simcore.helpers.faker_factories import (
2829
random_api_key,
2930
random_product,
@@ -32,7 +33,6 @@
3233
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
3334
from pytest_simcore.helpers.typing_env import EnvVarsDict
3435
from simcore_postgres_database.models.api_keys import api_keys
35-
from simcore_postgres_database.models.base import metadata
3636
from simcore_postgres_database.models.products import products
3737
from simcore_postgres_database.models.users import users
3838
from simcore_service_api_server.core.application import init_app
@@ -141,8 +141,8 @@ def migrated_db(postgres_service: dict, make_engine: Callable):
141141
pg_cli.downgrade.callback("base")
142142
pg_cli.clean.callback()
143143
# FIXME: deletes all because downgrade is not reliable!
144-
engine = make_engine(is_async=False)
145-
metadata.drop_all(engine)
144+
sync_engine = make_engine(is_async=False)
145+
postgres_tools.drop_all_tables(sync_engine)
146146

147147

148148
@pytest.fixture

services/web/server/tests/unit/with_dbs/conftest.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from pydantic import ByteSize, TypeAdapter
4545
from pytest_docker.plugin import Services
4646
from pytest_mock import MockerFixture
47+
from pytest_simcore.helpers import postgres_tools
4748
from pytest_simcore.helpers.faker_factories import random_product
4849
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
4950
from pytest_simcore.helpers.typing_env import EnvVarsDict
@@ -524,19 +525,7 @@ def postgres_db(
524525
# NOTE: we directly drop the table, that is faster
525526
# testing the upgrade/downgrade is already done in postgres-database.
526527
# there is no need to it here.
527-
with engine.begin() as conn:
528-
conn.execute(sa.DDL("DROP TABLE IF EXISTS alembic_version"))
529-
530-
conn.execute(
531-
# NOTE: terminates all open transactions before droping all tables
532-
# This solves https://github.com/ITISFoundation/osparc-simcore/issues/7008
533-
sa.DDL(
534-
"SELECT pg_terminate_backend(pid) "
535-
"FROM pg_stat_activity "
536-
"WHERE state = 'idle in transaction';"
537-
)
538-
)
539-
orm.metadata.drop_all(bind=conn)
528+
postgres_tools.drop_all_tables(engine)
540529

541530
engine.dispose()
542531

0 commit comments

Comments
 (0)