|
4 | 4 | # pylint: disable=unused-argument |
5 | 5 | # pylint: disable=unused-variable |
6 | 6 |
|
| 7 | +from collections.abc import AsyncIterable |
7 | 8 | from typing import Any |
8 | 9 |
|
9 | 10 | import pytest |
10 | | -from aiopg.sa.connection import SAConnection |
11 | 11 | from faker import Faker |
12 | | -from pytest_simcore.helpers.faker_factories import random_user |
| 12 | +from pytest_simcore.helpers.faker_factories import ( |
| 13 | + random_user, |
| 14 | +) |
| 15 | +from pytest_simcore.helpers.postgres_tools import ( |
| 16 | + insert_and_get_row_lifespan, |
| 17 | +) |
13 | 18 | from simcore_postgres_database.models.users import UserRole, users |
| 19 | +from simcore_postgres_database.utils_repos import ( |
| 20 | + pass_or_acquire_connection, |
| 21 | +) |
14 | 22 | from simcore_postgres_database.utils_users import UserNotFoundInRepoError, UsersRepo |
15 | | -from sqlalchemy.ext.asyncio import AsyncConnection |
| 23 | +from sqlalchemy.ext.asyncio import AsyncEngine |
16 | 24 |
|
17 | 25 |
|
18 | 26 | @pytest.fixture |
19 | | -async def user(connection: SAConnection, faker: Faker) -> dict[str, Any]: |
20 | | - data = random_user(role=faker.random_element(elements=UserRole)) |
21 | | - user_id = await connection.scalar( |
22 | | - users.insert().values(**data).returning(users.c.id) |
23 | | - ) |
24 | | - assert user_id |
25 | | - data["id"] = user_id |
26 | | - return data |
27 | | - |
28 | | - |
29 | | -async def test_users_repo_get( |
30 | | - connection_factory: SAConnection | AsyncConnection, user: dict[str, Any] |
31 | | -): |
| 27 | +async def user( |
| 28 | + faker: Faker, |
| 29 | + asyncpg_engine: AsyncEngine, |
| 30 | +) -> AsyncIterable[dict[str, Any]]: |
| 31 | + async with insert_and_get_row_lifespan( # pylint:disable=contextmanager-generator-missing-cleanup |
| 32 | + asyncpg_engine, |
| 33 | + table=users, |
| 34 | + values=random_user( |
| 35 | + faker, |
| 36 | + role=faker.random_element(elements=UserRole), |
| 37 | + ), |
| 38 | + pk_col=users.c.id, |
| 39 | + ) as row: |
| 40 | + yield row |
| 41 | + |
| 42 | + |
| 43 | +async def test_users_repo_get(asyncpg_engine: AsyncEngine, user: dict[str, Any]): |
32 | 44 | repo = UsersRepo() |
33 | | - # NOTE: Temporary usage of connection_factory until asyncpg is used |
34 | | - assert await repo.get_email(connection_factory, user_id=user["id"]) == user["email"] |
35 | | - assert await repo.get_role(connection_factory, user_id=user["id"]) == user["role"] |
36 | 45 |
|
37 | | - with pytest.raises(UserNotFoundInRepoError): |
38 | | - await repo.get_role(connection_factory, user_id=55) |
| 46 | + async with pass_or_acquire_connection(asyncpg_engine) as connection: |
| 47 | + assert await repo.get_email(connection, user_id=user["id"]) == user["email"] |
| 48 | + assert await repo.get_role(connection, user_id=user["id"]) == user["role"] |
| 49 | + |
| 50 | + with pytest.raises(UserNotFoundInRepoError): |
| 51 | + await repo.get_role(connection, user_id=55) |
0 commit comments