|
9 | 9 | from urllib.parse import quote |
10 | 10 |
|
11 | 11 | import pytest |
12 | | -import simcore_service_webserver.tasks._controller._rest |
13 | 12 | from aiohttp.test_utils import TestClient |
14 | 13 | from faker import Faker |
15 | 14 | from models_library.api_schemas_long_running_tasks.tasks import ( |
|
25 | 24 | AsyncJobStatus, |
26 | 25 | ) |
27 | 26 | from models_library.api_schemas_rpc_async_jobs.exceptions import ( |
28 | | - JobAbortedError, |
29 | | - JobError, |
30 | 27 | JobMissingError, |
31 | | - JobNotDoneError, |
32 | 28 | JobSchedulerError, |
33 | 29 | ) |
34 | 30 | from models_library.api_schemas_storage.export_data_async_jobs import ( |
@@ -425,23 +421,6 @@ async def test_upload_file( |
425 | 421 | assert not data |
426 | 422 |
|
427 | 423 |
|
428 | | -@pytest.fixture |
429 | | -def create_backend_mock( |
430 | | - mocker: MockerFixture, |
431 | | -) -> Callable[[str, str, Any], None]: |
432 | | - def _(module: str, method: str, result_or_exception: Any): |
433 | | - def side_effect(*args, **kwargs): |
434 | | - if isinstance(result_or_exception, Exception): |
435 | | - raise result_or_exception |
436 | | - |
437 | | - return result_or_exception |
438 | | - |
439 | | - for fct in (f"{module}.{method}",): |
440 | | - mocker.patch(fct, side_effect=side_effect) |
441 | | - |
442 | | - return _ |
443 | | - |
444 | | - |
445 | 424 | @pytest.mark.parametrize("user_role", _user_roles) |
446 | 425 | @pytest.mark.parametrize( |
447 | 426 | "backend_result_or_exception, expected_status", |
@@ -572,76 +551,6 @@ async def test_cancel_async_jobs( |
572 | 551 | assert response.status == expected_status |
573 | 552 |
|
574 | 553 |
|
575 | | -@pytest.mark.parametrize("user_role", _user_roles) |
576 | | -@pytest.mark.parametrize( |
577 | | - "backend_result_or_exception, expected_status", |
578 | | - [ |
579 | | - (JobNotDoneError(job_id=_faker.uuid4()), status.HTTP_404_NOT_FOUND), |
580 | | - (AsyncJobResult(result=None), status.HTTP_200_OK), |
581 | | - (JobError(job_id=_faker.uuid4()), status.HTTP_500_INTERNAL_SERVER_ERROR), |
582 | | - (JobAbortedError(job_id=_faker.uuid4()), status.HTTP_410_GONE), |
583 | | - (JobSchedulerError(exc=_faker.text()), status.HTTP_500_INTERNAL_SERVER_ERROR), |
584 | | - (JobMissingError(job_id=_faker.uuid4()), status.HTTP_404_NOT_FOUND), |
585 | | - ], |
586 | | - ids=lambda x: type(x).__name__, |
587 | | -) |
588 | | -async def test_get_async_job_result( |
589 | | - user_role: UserRole, |
590 | | - logged_user: UserInfoDict, |
591 | | - client: TestClient, |
592 | | - create_backend_mock: Callable[[str, str, Any], None], |
593 | | - faker: Faker, |
594 | | - backend_result_or_exception: Any, |
595 | | - expected_status: int, |
596 | | -): |
597 | | - _job_id = AsyncJobId(faker.uuid4()) |
598 | | - create_backend_mock( |
599 | | - tasks_rest.__name__, |
600 | | - f"_tasks_service.{_tasks_service.get_task_result.__name__}", |
601 | | - backend_result_or_exception, |
602 | | - ) |
603 | | - |
604 | | - response = await client.get(f"/{API_VERSION}/tasks/{_job_id}/result") |
605 | | - assert response.status == expected_status |
606 | | - |
607 | | - |
608 | | -@pytest.mark.parametrize("user_role", _user_roles) |
609 | | -@pytest.mark.parametrize( |
610 | | - "backend_result_or_exception, expected_status", |
611 | | - [ |
612 | | - ( |
613 | | - [ |
614 | | - AsyncJobGet( |
615 | | - job_id=AsyncJobId(_faker.uuid4()), |
616 | | - job_name="task_name", |
617 | | - ) |
618 | | - ], |
619 | | - status.HTTP_200_OK, |
620 | | - ), |
621 | | - (JobSchedulerError(exc=_faker.text()), status.HTTP_500_INTERNAL_SERVER_ERROR), |
622 | | - ], |
623 | | - ids=lambda x: type(x).__name__, |
624 | | -) |
625 | | -async def test_get_user_async_jobs( |
626 | | - user_role: UserRole, |
627 | | - logged_user: UserInfoDict, |
628 | | - client: TestClient, |
629 | | - create_backend_mock: Callable[[str, str, Any], None], |
630 | | - backend_result_or_exception: Any, |
631 | | - expected_status: int, |
632 | | -): |
633 | | - create_backend_mock( |
634 | | - tasks_rest.__name__, |
635 | | - f"_tasks_service.{_tasks_service.list_tasks.__name__}", |
636 | | - backend_result_or_exception, |
637 | | - ) |
638 | | - |
639 | | - response = await client.get(f"/{API_VERSION}/tasks") |
640 | | - assert response.status == expected_status |
641 | | - if response.status == status.HTTP_200_OK: |
642 | | - Envelope[list[TaskGet]].model_validate(await response.json()) |
643 | | - |
644 | | - |
645 | 554 | @pytest.mark.parametrize("user_role", _user_roles) |
646 | 555 | @pytest.mark.parametrize( |
647 | 556 | "http_method, href, backend_method, backend_object, return_status, return_schema", |
@@ -723,79 +632,3 @@ async def test_get_async_job_links( |
723 | 632 | assert response.status == return_status |
724 | 633 | if return_schema: |
725 | 634 | Envelope[return_schema].model_validate(await response.json()) |
726 | | - |
727 | | - |
728 | | -@pytest.fixture |
729 | | -def create_consume_events_mock( |
730 | | - mocker: MockerFixture, |
731 | | -) -> Callable[[Any], None]: |
732 | | - def _(result_or_exception: Any): |
733 | | - async def mock_consume_events(*args, **kwargs): |
734 | | - if isinstance(result_or_exception, Exception): |
735 | | - raise result_or_exception |
736 | | - # Yield the mock events |
737 | | - for event_id, event_data in result_or_exception: |
738 | | - yield event_id, event_data |
739 | | - |
740 | | - mock_task_manager = mocker.MagicMock() |
741 | | - mock_task_manager.consume_task_events = mock_consume_events |
742 | | - mocker.patch.object( |
743 | | - simcore_service_webserver.tasks._controller._rest, |
744 | | - "get_task_manager", |
745 | | - return_value=mock_task_manager, |
746 | | - ) |
747 | | - |
748 | | - return _ |
749 | | - |
750 | | - |
751 | | -class MockEvent: |
752 | | - def __init__(self, event_type: str, event_data: dict[str, Any]): |
753 | | - self.type = event_type |
754 | | - self.data = event_data |
755 | | - |
756 | | - |
757 | | -@pytest.mark.parametrize("user_role", _user_roles) |
758 | | -@pytest.mark.parametrize( |
759 | | - "backend_result_or_exception, expected_status", |
760 | | - [ |
761 | | - ( |
762 | | - [ |
763 | | - ("event-1", MockEvent("status", {"status": "running"})), |
764 | | - ("event-2", MockEvent("progress", {"percent": 50})), |
765 | | - ("event-3", MockEvent("status", {"status": "completed"})), |
766 | | - ], |
767 | | - status.HTTP_200_OK, |
768 | | - ), |
769 | | - ([], status.HTTP_200_OK), # No events |
770 | | - ], |
771 | | - ids=lambda x: ( |
772 | | - "with_events" if x and isinstance(x, list) and len(x) > 0 else "no_events" |
773 | | - ), |
774 | | -) |
775 | | -async def test_get_async_job_stream( |
776 | | - user_role: UserRole, |
777 | | - logged_user: UserInfoDict, |
778 | | - client: TestClient, |
779 | | - create_consume_events_mock: Callable[[Any], None], |
780 | | - faker: Faker, |
781 | | - backend_result_or_exception: Any, |
782 | | - expected_status: int, |
783 | | -): |
784 | | - create_consume_events_mock(backend_result_or_exception) |
785 | | - |
786 | | - _task_id = AsyncJobId(faker.uuid4()) |
787 | | - |
788 | | - response = await client.get( |
789 | | - f"/{API_VERSION}/tasks/{_task_id}/stream", |
790 | | - headers={"Accept": "text/event-stream"}, |
791 | | - ) |
792 | | - assert response.status == expected_status |
793 | | - |
794 | | - if response.status == status.HTTP_200_OK: |
795 | | - assert response.headers.get("Content-Type") == "text/event-stream" |
796 | | - |
797 | | - content = await response.text() |
798 | | - if backend_result_or_exception: |
799 | | - assert "data:" in content or len(backend_result_or_exception) == 0 |
800 | | - else: |
801 | | - assert content == "" |
0 commit comments