Skip to content

Commit 086984c

Browse files
committed
fixed tests
1 parent f054cb8 commit 086984c

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
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/test_utils_user_preferences.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# pylint: disable=inconsistent-return-statements
22
# pylint: disable=redefined-outer-name
33

4-
from collections.abc import Awaitable, Callable
4+
from collections.abc import AsyncIterator, Awaitable, Callable
55
from typing import Any
66

77
import pytest
8-
from aiopg.sa.connection import SAConnection
8+
from aiopg.sa.connection import AsyncConnection
99
from aiopg.sa.result import RowProxy
1010
from faker import Faker
1111
from pytest_simcore.helpers.faker_factories import random_user
@@ -15,6 +15,7 @@
1515
FrontendUserPreferencesRepo,
1616
UserServicesUserPreferencesRepo,
1717
)
18+
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine
1819

1920

2021
@pytest.fixture
@@ -38,8 +39,16 @@ def preference_repo(request: pytest.FixtureRequest) -> type[BasePreferencesRepo]
3839
return request.param
3940

4041

42+
@pytest.fixture
43+
async def connection(
44+
asyncpg_engine: AsyncEngine,
45+
) -> AsyncIterator[AsyncConnection]:
46+
async with asyncpg_engine.begin() as connection:
47+
yield connection
48+
49+
4150
async def _assert_save_get_preference(
42-
connection: SAConnection,
51+
connection: AsyncConnection,
4352
preference_repo: type[BasePreferencesRepo],
4453
*,
4554
user_id: int,
@@ -65,7 +74,7 @@ async def _assert_save_get_preference(
6574

6675

6776
async def _assert_preference_not_saved(
68-
connection: SAConnection,
77+
connection: AsyncConnection,
6978
preference_repo: type[BasePreferencesRepo],
7079
*,
7180
user_id: int,
@@ -92,7 +101,7 @@ def _get_random_payload(
92101
pytest.fail(f"Did not define a casa for {preference_repo=}. Please add one.")
93102

94103

95-
async def _get_user_id(connection: SAConnection, faker: Faker) -> int:
104+
async def _get_user_id(connection: AsyncConnection, faker: Faker) -> int:
96105
data = random_user(role=faker.random_element(elements=UserRole))
97106
user_id = await connection.scalar(
98107
users.insert().values(**data).returning(users.c.id)
@@ -102,7 +111,7 @@ async def _get_user_id(connection: SAConnection, faker: Faker) -> int:
102111

103112

104113
async def test_user_preference_repo_workflow(
105-
connection: SAConnection,
114+
connection: AsyncConnection,
106115
preference_repo: type[BasePreferencesRepo],
107116
preference_one: str,
108117
product_name: str,
@@ -144,7 +153,7 @@ async def test_user_preference_repo_workflow(
144153

145154

146155
async def test__same_preference_name_product_name__different_users(
147-
connection: SAConnection,
156+
connection: AsyncConnection,
148157
preference_repo: type[BasePreferencesRepo],
149158
preference_one: str,
150159
product_name: str,
@@ -193,7 +202,7 @@ async def test__same_preference_name_product_name__different_users(
193202

194203

195204
async def test__same_user_preference_name__different_product_name(
196-
connection: SAConnection,
205+
connection: AsyncConnection,
197206
create_fake_product: Callable[..., Awaitable[RowProxy]],
198207
preference_repo: type[BasePreferencesRepo],
199208
preference_one: str,
@@ -244,7 +253,7 @@ async def test__same_user_preference_name__different_product_name(
244253

245254

246255
async def test__same_product_name_user__different_preference_name(
247-
connection: SAConnection,
256+
connection: AsyncConnection,
248257
preference_repo: type[BasePreferencesRepo],
249258
preference_one: str,
250259
preference_two: str,

services/web/server/src/simcore_service_webserver/users/_preferences_repository.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from models_library.users import UserID
55
from simcore_postgres_database.utils_user_preferences import FrontendUserPreferencesRepo
66

7-
from ..db.plugin import get_database_engine
7+
from ..db.plugin import get_asyncpg_engine
88

99

1010
def _get_user_preference_name(user_id: UserID, preference_name: PreferenceName) -> str:
@@ -18,7 +18,7 @@ async def get_user_preference(
1818
product_name: ProductName,
1919
preference_class: type[FrontendUserPreference],
2020
) -> FrontendUserPreference | None:
21-
async with get_database_engine(app).acquire() as conn:
21+
async with get_asyncpg_engine(app).connect() as conn:
2222
preference_payload: dict | None = await FrontendUserPreferencesRepo.load(
2323
conn,
2424
user_id=user_id,
@@ -42,7 +42,7 @@ async def set_user_preference(
4242
product_name: ProductName,
4343
preference: FrontendUserPreference,
4444
) -> None:
45-
async with get_database_engine(app).acquire() as conn:
45+
async with get_asyncpg_engine(app).begin() as conn:
4646
await FrontendUserPreferencesRepo.save(
4747
conn,
4848
user_id=user_id,

0 commit comments

Comments
 (0)