|
4 | 4 |
|
5 | 5 |
|
6 | 6 | import pytest |
| 7 | +import sqlalchemy as sa |
7 | 8 | from aiohttp.test_utils import TestClient |
8 | 9 | from faker import Faker |
9 | 10 | from models_library.api_schemas_directorv2.comp_runs import ( |
| 11 | + ComputationCollectionRunRpcGet, |
| 12 | + ComputationCollectionRunRpcGetPage, |
| 13 | + ComputationCollectionRunTaskRpcGet, |
| 14 | + ComputationCollectionRunTaskRpcGetPage, |
10 | 15 | ComputationRunRpcGet, |
11 | 16 | ComputationRunRpcGetPage, |
12 | 17 | ComputationTaskRpcGet, |
13 | 18 | ComputationTaskRpcGetPage, |
14 | 19 | ) |
15 | 20 | from models_library.api_schemas_webserver.computations import ( |
| 21 | + ComputationCollectionRunRestGet, |
| 22 | + ComputationCollectionRunTaskRestGet, |
16 | 23 | ComputationRunRestGet, |
17 | 24 | ComputationTaskRestGet, |
18 | 25 | ) |
|
26 | 33 | ) |
27 | 34 | from pytest_simcore.services_api_mocks_for_aiohttp_clients import AioResponsesMock |
28 | 35 | from servicelib.aiohttp import status |
| 36 | +from simcore_postgres_database.models.comp_run_collections import comp_run_collections |
| 37 | +from simcore_postgres_database.models.projects_metadata import projects_metadata |
29 | 38 | from simcore_service_webserver.db.models import UserRole |
30 | 39 | from simcore_service_webserver.projects.models import ProjectDict |
31 | 40 |
|
@@ -287,3 +296,190 @@ async def test_list_computations_latest_iteration( |
287 | 296 | ) |
288 | 297 | if user_role != UserRole.ANONYMOUS: |
289 | 298 | assert ComputationTaskRestGet.model_validate(data[0]) |
| 299 | + |
| 300 | + |
| 301 | +### NEW: |
| 302 | + |
| 303 | + |
| 304 | +@pytest.fixture |
| 305 | +def mock_rpc_list_computation_collection_runs_page( |
| 306 | + mocker: MockerFixture, |
| 307 | + user_project: ProjectDict, |
| 308 | +) -> ComputationCollectionRunRpcGetPage: |
| 309 | + project_uuid = user_project["uuid"] |
| 310 | + example = ComputationCollectionRunRpcGet.model_config["json_schema_extra"][ |
| 311 | + "examples" |
| 312 | + ][0] |
| 313 | + example["project_ids"] = [project_uuid] |
| 314 | + example["info"]["project_metadata"]["root_parent_project_id"] = project_uuid |
| 315 | + |
| 316 | + return mocker.patch( |
| 317 | + "simcore_service_webserver.director_v2._computations_service.computations.list_computation_collection_runs_page", |
| 318 | + spec=True, |
| 319 | + return_value=ComputationCollectionRunRpcGetPage( |
| 320 | + items=[ComputationCollectionRunRpcGet.model_validate(example)], |
| 321 | + total=1, |
| 322 | + ), |
| 323 | + ) |
| 324 | + |
| 325 | + |
| 326 | +@pytest.fixture |
| 327 | +def mock_rpc_list_computation_collection_run_tasks_page( |
| 328 | + mocker: MockerFixture, |
| 329 | + user_project: ProjectDict, |
| 330 | +) -> str: |
| 331 | + project_uuid = user_project["uuid"] |
| 332 | + workbench_ids = list(user_project["workbench"].keys()) |
| 333 | + example = ComputationCollectionRunTaskRpcGet.model_config["json_schema_extra"][ |
| 334 | + "examples" |
| 335 | + ][0] |
| 336 | + example["node_id"] = workbench_ids[0] |
| 337 | + example["project_uuid"] = project_uuid |
| 338 | + |
| 339 | + mocker.patch( |
| 340 | + "simcore_service_webserver.director_v2._computations_service.computations.list_computation_collection_run_tasks_page", |
| 341 | + spec=True, |
| 342 | + return_value=ComputationCollectionRunTaskRpcGetPage( |
| 343 | + items=[ComputationCollectionRunTaskRpcGet.model_validate(example)], |
| 344 | + total=1, |
| 345 | + ), |
| 346 | + ) |
| 347 | + |
| 348 | + return workbench_ids[0] |
| 349 | + |
| 350 | + |
| 351 | +@pytest.mark.parametrize(*standard_role_response(), ids=str) |
| 352 | +async def test_list_computation_collection_runs_and_tasks( |
| 353 | + director_v2_service_mock: AioResponsesMock, |
| 354 | + user_project: ProjectDict, |
| 355 | + client: TestClient, |
| 356 | + logged_user: LoggedUser, |
| 357 | + user_role: UserRole, |
| 358 | + expected: ExpectedResponse, |
| 359 | + mock_rpc_list_computation_collection_runs_page: None, |
| 360 | + mock_rpc_list_computation_collection_run_tasks_page: str, |
| 361 | + faker: Faker, |
| 362 | +): |
| 363 | + assert client.app |
| 364 | + url = client.app.router["list_computation_collection_runs"].url_for() |
| 365 | + resp = await client.get(f"{url}") |
| 366 | + data, _ = await assert_status( |
| 367 | + resp, status.HTTP_200_OK if user_role == UserRole.GUEST else expected.ok |
| 368 | + ) |
| 369 | + if user_role != UserRole.ANONYMOUS: |
| 370 | + assert ComputationCollectionRunRestGet.model_validate(data[0]) |
| 371 | + assert data[0]["name"] == user_project["name"] |
| 372 | + |
| 373 | + url = client.app.router["list_computation_collection_run_tasks"].url_for( |
| 374 | + collection_run_id=faker.uuid4() |
| 375 | + ) |
| 376 | + resp = await client.get(f"{url}") |
| 377 | + data, _ = await assert_status( |
| 378 | + resp, status.HTTP_200_OK if user_role == UserRole.GUEST else expected.ok |
| 379 | + ) |
| 380 | + if user_role != UserRole.ANONYMOUS: |
| 381 | + assert ComputationCollectionRunTaskRestGet.model_validate(data[0]) |
| 382 | + assert len(data) == 1 |
| 383 | + assert ( |
| 384 | + data[0]["name"] |
| 385 | + == user_project["workbench"][ |
| 386 | + mock_rpc_list_computation_collection_run_tasks_page |
| 387 | + ]["label"] |
| 388 | + ) |
| 389 | + |
| 390 | + |
| 391 | +@pytest.fixture |
| 392 | +async def populated_comp_run_collection( |
| 393 | + client: TestClient, |
| 394 | + postgres_db: sa.engine.Engine, |
| 395 | +): |
| 396 | + assert client.app |
| 397 | + example = ComputationCollectionRunRpcGet.model_config["json_schema_extra"][ |
| 398 | + "examples" |
| 399 | + ][0] |
| 400 | + collection_run_id = example["collection_run_id"] |
| 401 | + |
| 402 | + with postgres_db.connect() as con: |
| 403 | + con.execute( |
| 404 | + comp_run_collections.insert() |
| 405 | + .values( |
| 406 | + collection_run_id=collection_run_id, |
| 407 | + client_or_system_generated_id=collection_run_id, |
| 408 | + client_or_system_generated_display_name="My Collection Run", |
| 409 | + generated_by_system=False, |
| 410 | + created=sa.func.now(), |
| 411 | + modified=sa.func.now(), |
| 412 | + ) |
| 413 | + .returning(comp_run_collections.c.collection_run_id) |
| 414 | + ) |
| 415 | + yield |
| 416 | + con.execute(comp_run_collections.delete()) |
| 417 | + |
| 418 | + |
| 419 | +@pytest.mark.parametrize(*standard_role_response(), ids=str) |
| 420 | +async def test_list_computation_collection_runs_with_client_defined_name( |
| 421 | + director_v2_service_mock: AioResponsesMock, |
| 422 | + user_project: ProjectDict, |
| 423 | + client: TestClient, |
| 424 | + logged_user: LoggedUser, |
| 425 | + user_role: UserRole, |
| 426 | + expected: ExpectedResponse, |
| 427 | + populated_comp_run_collection: None, |
| 428 | + mock_rpc_list_computation_collection_runs_page: None, |
| 429 | +): |
| 430 | + assert client.app |
| 431 | + url = client.app.router["list_computation_collection_runs"].url_for() |
| 432 | + resp = await client.get(f"{url}") |
| 433 | + data, _ = await assert_status( |
| 434 | + resp, status.HTTP_200_OK if user_role == UserRole.GUEST else expected.ok |
| 435 | + ) |
| 436 | + if user_role != UserRole.ANONYMOUS: |
| 437 | + assert ComputationCollectionRunRestGet.model_validate(data[0]) |
| 438 | + assert data[0]["name"] == "My Collection Run" |
| 439 | + |
| 440 | + |
| 441 | +@pytest.fixture |
| 442 | +async def populated_project_metadata( |
| 443 | + client: TestClient, |
| 444 | + logged_user: LoggedUser, |
| 445 | + user_project: ProjectDict, |
| 446 | + postgres_db: sa.engine.Engine, |
| 447 | +): |
| 448 | + assert client.app |
| 449 | + project_uuid = user_project["uuid"] |
| 450 | + with postgres_db.connect() as con: |
| 451 | + con.execute( |
| 452 | + projects_metadata.insert().values( |
| 453 | + **{ |
| 454 | + "project_uuid": project_uuid, |
| 455 | + "custom": {"job_name": "My Job Name"}, |
| 456 | + } |
| 457 | + ) |
| 458 | + ) |
| 459 | + yield |
| 460 | + con.execute(projects_metadata.delete()) |
| 461 | + |
| 462 | + |
| 463 | +@pytest.mark.parametrize(*standard_role_response(), ids=str) |
| 464 | +async def test_list_computation_collection_runs_and_tasks_with_different_names( |
| 465 | + director_v2_service_mock: AioResponsesMock, |
| 466 | + user_project: ProjectDict, |
| 467 | + client: TestClient, |
| 468 | + logged_user: LoggedUser, |
| 469 | + user_role: UserRole, |
| 470 | + expected: ExpectedResponse, |
| 471 | + populated_project_metadata: None, |
| 472 | + mock_rpc_list_computation_collection_run_tasks_page: str, |
| 473 | + faker: Faker, |
| 474 | +): |
| 475 | + assert client.app |
| 476 | + url = client.app.router["list_computation_collection_run_tasks"].url_for( |
| 477 | + collection_run_id=faker.uuid4() |
| 478 | + ) |
| 479 | + resp = await client.get(f"{url}") |
| 480 | + data, _ = await assert_status( |
| 481 | + resp, status.HTTP_200_OK if user_role == UserRole.GUEST else expected.ok |
| 482 | + ) |
| 483 | + if user_role != UserRole.ANONYMOUS: |
| 484 | + assert ComputationCollectionRunTaskRestGet.model_validate(data[0]) |
| 485 | + assert data[0]["name"] == "My Job Name" |
0 commit comments