Skip to content

Commit 3880aff

Browse files
authored
♻️SQLAlchemy migration: simcore-sdk (#7404)
1 parent ac9aa7c commit 3880aff

File tree

45 files changed

+372
-379
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+372
-379
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_user_preferences.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
from typing import Any
22

33
import sqlalchemy as sa
4-
from aiopg.sa.connection import SAConnection
54
from sqlalchemy.dialects.postgresql import insert as pg_insert
5+
from sqlalchemy.ext.asyncio import AsyncConnection
66

77
from .models.user_preferences import (
88
user_preferences_frontend,
99
user_preferences_user_service,
1010
)
1111

1212

13-
class CouldNotCreateOrUpdateUserPreferenceError(Exception):
14-
...
13+
class CouldNotCreateOrUpdateUserPreferenceError(Exception): ...
1514

1615

1716
class BasePreferencesRepo:
@@ -20,7 +19,7 @@ class BasePreferencesRepo:
2019
@classmethod
2120
async def save(
2221
cls,
23-
conn: SAConnection,
22+
conn: AsyncConnection,
2423
*,
2524
user_id: int,
2625
product_name: str,
@@ -49,7 +48,7 @@ async def save(
4948
@classmethod
5049
async def load(
5150
cls,
52-
conn: SAConnection,
51+
conn: AsyncConnection,
5352
*,
5453
user_id: int,
5554
product_name: str,

packages/postgres-database/tests/conftest.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
user_to_groups,
3939
users,
4040
)
41+
from sqlalchemy.engine.row import Row
4142
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
4243

4344
pytest_plugins = [
@@ -324,18 +325,26 @@ async def _creator(project_uuid: uuid.UUID) -> ProjectNode:
324325

325326

326327
@pytest.fixture
327-
def create_fake_product(
328-
connection: aiopg.sa.connection.SAConnection,
329-
) -> Callable[..., Awaitable[RowProxy]]:
330-
async def _creator(product_name: str) -> RowProxy:
331-
result = await connection.execute(
332-
sa.insert(products)
333-
.values(name=product_name, host_regex=".*")
334-
.returning(sa.literal_column("*"))
335-
)
336-
assert result
337-
row = await result.first()
338-
assert row
328+
async def create_fake_product(
329+
asyncpg_engine: AsyncEngine,
330+
) -> AsyncIterator[Callable[[str], Awaitable[Row]]]:
331+
created_product_names = set()
332+
333+
async def _creator(product_name: str) -> Row:
334+
async with asyncpg_engine.begin() as connection:
335+
result = await connection.execute(
336+
sa.insert(products)
337+
.values(name=product_name, host_regex=".*")
338+
.returning(sa.literal_column("*"))
339+
)
340+
assert result
341+
row = result.one()
342+
created_product_names.add(row.name)
339343
return row
340344

341-
return _creator
345+
yield _creator
346+
347+
async with asyncpg_engine.begin() as conn:
348+
await conn.execute(
349+
products.delete().where(products.c.name.in_(created_product_names))
350+
)

packages/postgres-database/tests/test_utils_groups_extra_properties.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
GroupExtraPropertiesRepo,
2222
)
2323
from sqlalchemy import literal_column
24+
from sqlalchemy.engine.row import Row
2425
from sqlalchemy.ext.asyncio import AsyncEngine
2526

2627

@@ -84,7 +85,7 @@ async def test_get(
8485
connection: aiopg.sa.connection.SAConnection,
8586
registered_user: RowProxy,
8687
product_name: str,
87-
create_fake_product: Callable[..., Awaitable[RowProxy]],
88+
create_fake_product: Callable[[str], Awaitable[Row]],
8889
create_fake_group_extra_properties: Callable[..., Awaitable[GroupExtraProperties]],
8990
):
9091
with pytest.raises(GroupExtraPropertiesNotFoundError):
@@ -106,7 +107,7 @@ async def test_get_v2(
106107
asyncpg_engine: AsyncEngine,
107108
registered_user: RowProxy,
108109
product_name: str,
109-
create_fake_product: Callable[..., Awaitable[RowProxy]],
110+
create_fake_product: Callable[[str], Awaitable[Row]],
110111
create_fake_group_extra_properties: Callable[..., Awaitable[GroupExtraProperties]],
111112
):
112113
with pytest.raises(GroupExtraPropertiesNotFoundError):
@@ -157,7 +158,7 @@ async def test_get_aggregated_properties_for_user_returns_properties_in_expected
157158
connection: aiopg.sa.connection.SAConnection,
158159
product_name: str,
159160
registered_user: RowProxy,
160-
create_fake_product: Callable[..., Awaitable[RowProxy]],
161+
create_fake_product: Callable[[str], Awaitable[Row]],
161162
create_fake_group: Callable[..., Awaitable[RowProxy]],
162163
create_fake_group_extra_properties: Callable[..., Awaitable[GroupExtraProperties]],
163164
everyone_group_id: int,
@@ -227,7 +228,7 @@ async def test_get_aggregated_properties_for_user_returns_properties_in_expected
227228
connection: aiopg.sa.connection.SAConnection,
228229
product_name: str,
229230
registered_user: RowProxy,
230-
create_fake_product: Callable[..., Awaitable[RowProxy]],
231+
create_fake_product: Callable[[str], Awaitable[Row]],
231232
create_fake_group: Callable[..., Awaitable[RowProxy]],
232233
create_fake_group_extra_properties: Callable[..., Awaitable[GroupExtraProperties]],
233234
everyone_group_id: int,
@@ -274,7 +275,7 @@ async def test_get_aggregated_properties_for_user_returns_property_values_as_tru
274275
connection: aiopg.sa.connection.SAConnection,
275276
product_name: str,
276277
registered_user: RowProxy,
277-
create_fake_product: Callable[..., Awaitable[RowProxy]],
278+
create_fake_product: Callable[[str], Awaitable[Row]],
278279
create_fake_group: Callable[..., Awaitable[RowProxy]],
279280
create_fake_group_extra_properties: Callable[..., Awaitable[GroupExtraProperties]],
280281
everyone_group_id: int,
@@ -385,7 +386,7 @@ async def test_get_aggregated_properties_for_user_returns_property_values_as_tru
385386
connection: aiopg.sa.connection.SAConnection,
386387
product_name: str,
387388
registered_user: RowProxy,
388-
create_fake_product: Callable[..., Awaitable[RowProxy]],
389+
create_fake_product: Callable[[str], Awaitable[Row]],
389390
create_fake_group: Callable[..., Awaitable[RowProxy]],
390391
create_fake_group_extra_properties: Callable[..., Awaitable[GroupExtraProperties]],
391392
everyone_group_id: int,

0 commit comments

Comments
 (0)