|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +# pylint: disable=unused-argument |
| 3 | +# pylint: disable=unused-variable |
| 4 | +# pylint: disable=too-many-arguments |
| 5 | +import uuid |
| 6 | +from collections.abc import Awaitable, Callable |
| 7 | +from datetime import datetime |
| 8 | +from typing import Any, AsyncIterator |
| 9 | + |
| 10 | +import pytest |
| 11 | +import sqlalchemy |
| 12 | +from aiopg.sa.connection import SAConnection |
| 13 | +from aiopg.sa.result import RowProxy |
| 14 | +from faker import Faker |
| 15 | +from simcore_postgres_database.models.projects import projects |
| 16 | +from simcore_postgres_database.utils_projects import ( |
| 17 | + DBProjectNotFoundError, |
| 18 | + ProjectsRepo, |
| 19 | +) |
| 20 | +from sqlalchemy.ext.asyncio import AsyncEngine |
| 21 | + |
| 22 | + |
| 23 | +async def _delete_project(connection: SAConnection, project_uuid: uuid.UUID) -> None: |
| 24 | + result = await connection.execute( |
| 25 | + sqlalchemy.delete(projects).where(projects.c.uuid == f"{project_uuid}") |
| 26 | + ) |
| 27 | + assert result.rowcount == 1 |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +async def registered_user( |
| 32 | + connection: SAConnection, |
| 33 | + create_fake_user: Callable[..., Awaitable[RowProxy]], |
| 34 | +) -> RowProxy: |
| 35 | + user = await create_fake_user(connection) |
| 36 | + assert user |
| 37 | + return user |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +async def registered_project( |
| 42 | + connection: SAConnection, |
| 43 | + registered_user: RowProxy, |
| 44 | + create_fake_project: Callable[..., Awaitable[RowProxy]], |
| 45 | +) -> AsyncIterator[dict[str, Any]]: |
| 46 | + project = await create_fake_project(connection, registered_user) |
| 47 | + assert project |
| 48 | + |
| 49 | + yield dict(project) |
| 50 | + |
| 51 | + await _delete_project(connection, project["uuid"]) |
| 52 | + |
| 53 | + |
| 54 | +async def test_get_project_last_change_date( |
| 55 | + asyncpg_engine: AsyncEngine, registered_project: dict, faker: Faker |
| 56 | +): |
| 57 | + projects_repo = ProjectsRepo(asyncpg_engine) |
| 58 | + |
| 59 | + project_last_change_date = await projects_repo.get_project_last_change_date( |
| 60 | + project_uuid=registered_project["uuid"] |
| 61 | + ) |
| 62 | + assert isinstance(project_last_change_date, datetime) |
| 63 | + |
| 64 | + with pytest.raises(DBProjectNotFoundError): |
| 65 | + await projects_repo.get_project_last_change_date( |
| 66 | + project_uuid=faker.uuid4() # <-- Non existing uuid in DB |
| 67 | + ) |
0 commit comments