|
| 1 | +# pylint:disable=contextmanager-generator-missing-cleanup |
| 2 | +# pylint:disable=redefined-outer-name |
| 3 | +# pylint:disable=unused-argument |
| 4 | + |
| 5 | +from collections.abc import AsyncIterator |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import pytest |
| 9 | +import sqlalchemy as sa |
| 10 | +from fastapi import FastAPI |
| 11 | +from models_library.projects import ProjectID |
| 12 | +from models_library.projects_networks import NetworksWithAliases |
| 13 | +from models_library.users import UserID |
| 14 | +from pydantic import TypeAdapter |
| 15 | +from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict |
| 16 | +from pytest_simcore.helpers.postgres_tools import ( |
| 17 | + PostgresTestConfig, |
| 18 | + insert_and_get_row_lifespan, |
| 19 | +) |
| 20 | +from pytest_simcore.helpers.typing_env import EnvVarsDict |
| 21 | +from simcore_postgres_database.models.projects import projects |
| 22 | +from simcore_postgres_database.models.users import users |
| 23 | +from simcore_service_dynamic_scheduler.repository.events import ( |
| 24 | + get_project_networks_repo, |
| 25 | +) |
| 26 | +from simcore_service_dynamic_scheduler.repository.project_networks import ( |
| 27 | + ProjectNetworkNotFoundError, |
| 28 | + ProjectNetworksRepo, |
| 29 | +) |
| 30 | +from sqlalchemy.ext.asyncio import AsyncEngine |
| 31 | + |
| 32 | +pytest_simcore_core_services_selection = [ |
| 33 | + "postgres", |
| 34 | +] |
| 35 | +pytest_simcore_ops_services_selection = [ |
| 36 | + "adminer", |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def app_environment( |
| 42 | + app_environment: EnvVarsDict, |
| 43 | + postgres_db: sa.engine.Engine, |
| 44 | + postgres_host_config: PostgresTestConfig, |
| 45 | + disable_rabbitmq_lifespan: None, |
| 46 | + disable_redis_lifespan: None, |
| 47 | + disable_service_tracker_lifespan: None, |
| 48 | + disable_deferred_manager_lifespan: None, |
| 49 | + disable_notifier_lifespan: None, |
| 50 | + disable_status_monitor_lifespan: None, |
| 51 | + monkeypatch: pytest.MonkeyPatch, |
| 52 | +) -> EnvVarsDict: |
| 53 | + setenvs_from_dict( |
| 54 | + monkeypatch, |
| 55 | + { |
| 56 | + "POSTGRES_CLIENT_NAME": "test_postgres_client", |
| 57 | + }, |
| 58 | + ) |
| 59 | + return app_environment |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def engine(app: FastAPI) -> AsyncEngine: |
| 64 | + assert isinstance(app.state.engine, AsyncEngine) |
| 65 | + return app.state.engine |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture |
| 69 | +def user_id() -> UserID: |
| 70 | + return 1 |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture |
| 74 | +async def user_in_db( |
| 75 | + engine: AsyncEngine, |
| 76 | + user: dict[str, Any], |
| 77 | + user_id: UserID, |
| 78 | +) -> AsyncIterator[dict[str, Any]]: |
| 79 | + """ |
| 80 | + injects a user in db |
| 81 | + """ |
| 82 | + assert user_id == user["id"] |
| 83 | + async with insert_and_get_row_lifespan( |
| 84 | + engine, |
| 85 | + table=users, |
| 86 | + values=user, |
| 87 | + pk_col=users.c.id, |
| 88 | + pk_value=user["id"], |
| 89 | + ) as row: |
| 90 | + yield row |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture |
| 94 | +async def project_in_db( |
| 95 | + engine: AsyncEngine, |
| 96 | + project_id: ProjectID, |
| 97 | + project_data: dict[str, Any], |
| 98 | + user_in_db: UserID, |
| 99 | +) -> AsyncIterator[dict[str, Any]]: |
| 100 | + assert f"{project_id}" == project_data["uuid"] |
| 101 | + async with insert_and_get_row_lifespan( |
| 102 | + engine, |
| 103 | + table=projects, |
| 104 | + values=project_data, |
| 105 | + pk_col=projects.c.uuid, |
| 106 | + pk_value=project_data["uuid"], |
| 107 | + ) as row: |
| 108 | + yield row |
| 109 | + |
| 110 | + |
| 111 | +@pytest.fixture() |
| 112 | +def project_networks_repo(app: FastAPI) -> ProjectNetworksRepo: |
| 113 | + return get_project_networks_repo(app) |
| 114 | + |
| 115 | + |
| 116 | +@pytest.fixture |
| 117 | +def networks_with_aliases() -> NetworksWithAliases: |
| 118 | + return TypeAdapter(NetworksWithAliases).validate_python( |
| 119 | + NetworksWithAliases.model_json_schema()["examples"][0] |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +async def test_no_project_networks_for_project( |
| 124 | + project_networks_repo: ProjectNetworksRepo, |
| 125 | + project_in_db: dict[str, Any], |
| 126 | + project_id: ProjectID, |
| 127 | +): |
| 128 | + with pytest.raises(ProjectNetworkNotFoundError): |
| 129 | + await project_networks_repo.get_projects_networks(project_id=project_id) |
| 130 | + |
| 131 | + |
| 132 | +async def test_upsert_projects_networks( |
| 133 | + project_networks_repo: ProjectNetworksRepo, |
| 134 | + project_in_db: dict[str, Any], |
| 135 | + project_id: ProjectID, |
| 136 | + networks_with_aliases: NetworksWithAliases, |
| 137 | +): |
| 138 | + |
| 139 | + # allows ot test the upsert capabilities |
| 140 | + for _ in range(2): |
| 141 | + await project_networks_repo.upsert_projects_networks( |
| 142 | + project_id=project_id, networks_with_aliases=networks_with_aliases |
| 143 | + ) |
| 144 | + |
| 145 | + project_networks = await project_networks_repo.get_projects_networks( |
| 146 | + project_id=project_id |
| 147 | + ) |
| 148 | + assert project_networks.project_uuid == project_id |
| 149 | + assert project_networks.networks_with_aliases == networks_with_aliases |
0 commit comments