|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +# pylint: disable=unused-argument |
| 3 | +# pylint: disable=unused-variable |
| 4 | +# pylint: disable=too-many-arguments |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from collections.abc import AsyncIterable, Callable |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +import pytest |
| 11 | +import sqlalchemy as sa |
| 12 | +from aiohttp import web |
| 13 | +from aiohttp.test_utils import TestServer |
| 14 | +from faker import Faker |
| 15 | +from pytest_simcore.helpers.typing_env import EnvVarsDict |
| 16 | +from servicelib.aiohttp.application import create_safe_application |
| 17 | +from simcore_service_webserver.application_settings import setup_settings |
| 18 | +from simcore_service_webserver.db.plugin import get_asyncpg_engine, setup_db |
| 19 | +from sqlalchemy.ext.asyncio import AsyncEngine |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def web_server( |
| 24 | + event_loop: asyncio.AbstractEventLoop, |
| 25 | + app_environment: EnvVarsDict, # configs |
| 26 | + postgres_db: sa.engine.Engine, # db-ready |
| 27 | + webserver_test_server_port: int, |
| 28 | + aiohttp_server: Callable, |
| 29 | +) -> TestServer: |
| 30 | + """Creates an app that setups the database only""" |
| 31 | + app = web.Application() |
| 32 | + |
| 33 | + app = create_safe_application() |
| 34 | + setup_settings(app) |
| 35 | + setup_db(app) |
| 36 | + |
| 37 | + return event_loop.run_until_complete( |
| 38 | + aiohttp_server(app, port=webserver_test_server_port) |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def app(web_server: TestServer) -> web.Application: |
| 44 | + """Setup and started app with db""" |
| 45 | + _app = web_server.app |
| 46 | + assert get_asyncpg_engine(_app) |
| 47 | + return _app |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def asyncpg_engine( |
| 52 | + app: web.Application, |
| 53 | +) -> AsyncEngine: |
| 54 | + """Returns the asyncpg engine ready to be used against postgres""" |
| 55 | + return get_asyncpg_engine(app) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +async def product_owner_user( |
| 60 | + faker: Faker, |
| 61 | + asyncpg_engine: AsyncEngine, |
| 62 | +) -> AsyncIterable[dict[str, Any]]: |
| 63 | + """A PO user in the database""" |
| 64 | + |
| 65 | + from pytest_simcore.helpers.faker_factories import ( |
| 66 | + random_user, |
| 67 | + ) |
| 68 | + from pytest_simcore.helpers.postgres_tools import ( |
| 69 | + insert_and_get_row_lifespan, |
| 70 | + ) |
| 71 | + from simcore_postgres_database.models.users import UserRole, users |
| 72 | + |
| 73 | + async with insert_and_get_row_lifespan( # pylint:disable=contextmanager-generator-missing-cleanup |
| 74 | + asyncpg_engine, |
| 75 | + table=users, |
| 76 | + values=random_user( |
| 77 | + faker, |
| 78 | + |
| 79 | + name="po-user-fixture", |
| 80 | + role=UserRole.PRODUCT_OWNER, |
| 81 | + ), |
| 82 | + pk_col=users.c.id, |
| 83 | + ) as record: |
| 84 | + yield record |
0 commit comments