|
| 1 | +import datetime |
| 2 | + |
| 3 | +import pytest |
| 4 | +import sqlalchemy as sa |
| 5 | +from simcore_postgres_database.models.comp_runs import comp_runs |
| 6 | +from simcore_postgres_database.utils_comp_runs import get_latest_run_id_for_project |
| 7 | +from sqlalchemy.ext.asyncio import AsyncEngine |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +async def sample_comp_runs(asyncpg_engine: AsyncEngine): |
| 12 | + async with asyncpg_engine.begin() as conn: |
| 13 | + await conn.execute(sa.text("SET session_replication_role = replica;")) |
| 14 | + await conn.execute(sa.delete(comp_runs)) |
| 15 | + await conn.execute( |
| 16 | + comp_runs.insert(), |
| 17 | + [ |
| 18 | + { |
| 19 | + "run_id": 1, |
| 20 | + "project_uuid": "project-1", |
| 21 | + "user_id": 10, |
| 22 | + "iteration": 1, |
| 23 | + "result": "NOT_STARTED", |
| 24 | + "created": datetime.datetime( |
| 25 | + 2024, 1, 1, 10, 0, 0, tzinfo=datetime.UTC |
| 26 | + ), |
| 27 | + "modified": datetime.datetime( |
| 28 | + 2024, 1, 1, 10, 0, 0, tzinfo=datetime.UTC |
| 29 | + ), |
| 30 | + "started": None, |
| 31 | + "ended": None, |
| 32 | + "cancelled": None, |
| 33 | + "scheduled": None, |
| 34 | + "processed": None, |
| 35 | + "metadata": None, |
| 36 | + "use_on_demand_clusters": False, |
| 37 | + "dag_adjacency_list": None, |
| 38 | + }, |
| 39 | + { |
| 40 | + "run_id": 2, |
| 41 | + "project_uuid": "project-1", |
| 42 | + "user_id": 10, |
| 43 | + "iteration": 2, |
| 44 | + "result": "NOT_STARTED", |
| 45 | + "created": datetime.datetime( |
| 46 | + 2024, 1, 1, 11, 0, 0, tzinfo=datetime.UTC |
| 47 | + ), |
| 48 | + "modified": datetime.datetime( |
| 49 | + 2024, 1, 1, 11, 0, 0, tzinfo=datetime.UTC |
| 50 | + ), |
| 51 | + "started": None, |
| 52 | + "ended": None, |
| 53 | + "cancelled": None, |
| 54 | + "scheduled": None, |
| 55 | + "processed": None, |
| 56 | + "metadata": None, |
| 57 | + "use_on_demand_clusters": False, |
| 58 | + "dag_adjacency_list": None, |
| 59 | + }, |
| 60 | + { |
| 61 | + "run_id": 3, |
| 62 | + "project_uuid": "project-1", |
| 63 | + "user_id": 20, |
| 64 | + "iteration": 1, |
| 65 | + "result": "NOT_STARTED", |
| 66 | + "created": datetime.datetime( |
| 67 | + 2024, 1, 1, 12, 0, 0, tzinfo=datetime.UTC |
| 68 | + ), |
| 69 | + "modified": datetime.datetime( |
| 70 | + 2024, 1, 1, 12, 0, 0, tzinfo=datetime.UTC |
| 71 | + ), |
| 72 | + "started": None, |
| 73 | + "ended": None, |
| 74 | + "cancelled": None, |
| 75 | + "scheduled": None, |
| 76 | + "processed": None, |
| 77 | + "metadata": None, |
| 78 | + "use_on_demand_clusters": False, |
| 79 | + "dag_adjacency_list": None, |
| 80 | + }, |
| 81 | + { |
| 82 | + "run_id": 4, |
| 83 | + "project_uuid": "project-2", |
| 84 | + "user_id": 30, |
| 85 | + "iteration": 1, |
| 86 | + "result": "NOT_STARTED", |
| 87 | + "created": datetime.datetime( |
| 88 | + 2024, 1, 1, 13, 0, 0, tzinfo=datetime.UTC |
| 89 | + ), |
| 90 | + "modified": datetime.datetime( |
| 91 | + 2024, 1, 1, 13, 0, 0, tzinfo=datetime.UTC |
| 92 | + ), |
| 93 | + "started": None, |
| 94 | + "ended": None, |
| 95 | + "cancelled": None, |
| 96 | + "scheduled": None, |
| 97 | + "processed": None, |
| 98 | + "metadata": None, |
| 99 | + "use_on_demand_clusters": False, |
| 100 | + "dag_adjacency_list": None, |
| 101 | + }, |
| 102 | + ], |
| 103 | + ) |
| 104 | + await conn.execute(sa.text("SET session_replication_role = DEFAULT;")) |
| 105 | + yield |
| 106 | + async with asyncpg_engine.begin() as conn: |
| 107 | + await conn.execute(sa.text("SET session_replication_role = replica;")) |
| 108 | + await conn.execute(sa.delete(comp_runs)) |
| 109 | + await conn.execute(sa.text("SET session_replication_role = DEFAULT;")) |
| 110 | + |
| 111 | + |
| 112 | +async def test_get_latest_run_id_for_project( |
| 113 | + asyncpg_engine: AsyncEngine, sample_comp_runs |
| 114 | +): |
| 115 | + run_id = await get_latest_run_id_for_project(asyncpg_engine, project_id="project-1") |
| 116 | + assert run_id == 3 |
| 117 | + |
| 118 | + run_id2 = await get_latest_run_id_for_project( |
| 119 | + asyncpg_engine, project_id="project-2" |
| 120 | + ) |
| 121 | + assert run_id2 == 4 |
| 122 | + |
| 123 | + |
| 124 | +async def test_get_latest_run_id_for_project_no_runs( |
| 125 | + asyncpg_engine: AsyncEngine, sample_comp_runs |
| 126 | +): |
| 127 | + import uuid |
| 128 | + |
| 129 | + with pytest.raises(ValueError, match="did not return any row") as exc: |
| 130 | + await get_latest_run_id_for_project( |
| 131 | + asyncpg_engine, project_id=str(uuid.uuid4()) |
| 132 | + ) |
| 133 | + assert "did not return any row" in str(exc.value) |
0 commit comments