|
1 | | -from datetime import UTC, datetime |
2 | 1 | from http import HTTPStatus |
3 | 2 |
|
4 | 3 | import httpx |
5 | | -import pytest |
6 | | -from sqlalchemy import text |
7 | | -from sqlalchemy.ext.asyncio import AsyncConnection |
8 | | - |
9 | | -from core.errors import StudyConflictError |
10 | | -from schemas.study import StudyType |
11 | | -from tests.users import ApiKey |
12 | 4 |
|
13 | 5 |
|
14 | 6 | async def test_get_task_study_by_id(py_api: httpx.AsyncClient) -> None: |
@@ -457,130 +449,3 @@ async def test_get_task_study_by_alias(py_api: httpx.AsyncClient) -> None: |
457 | 449 | "setup_ids": [], |
458 | 450 | } |
459 | 451 | assert response.json() == expected |
460 | | - |
461 | | - |
462 | | -@pytest.mark.mut |
463 | | -async def test_create_task_study(py_api: httpx.AsyncClient) -> None: |
464 | | - response = await py_api.post( |
465 | | - f"/studies?api_key={ApiKey.SOME_USER}", |
466 | | - json={ |
467 | | - "name": "Test Study", |
468 | | - "alias": "test-study", |
469 | | - "main_entity_type": "task", |
470 | | - "description": "A test study", |
471 | | - "tasks": [1, 2, 3], |
472 | | - "runs": [], |
473 | | - }, |
474 | | - ) |
475 | | - assert response.status_code == HTTPStatus.OK |
476 | | - new = response.json() |
477 | | - assert "study_id" in new |
478 | | - study_id = new["study_id"] |
479 | | - assert isinstance(study_id, int) |
480 | | - |
481 | | - study = await py_api.get(f"/studies/{study_id}") |
482 | | - assert study.status_code == HTTPStatus.OK |
483 | | - expected = { |
484 | | - "id": study_id, |
485 | | - "alias": "test-study", |
486 | | - "main_entity_type": "task", |
487 | | - "name": "Test Study", |
488 | | - "description": "A test study", |
489 | | - "visibility": "public", |
490 | | - "status": "in_preparation", |
491 | | - "creator": 2, |
492 | | - "data_ids": [1, 1, 1], |
493 | | - "task_ids": [1, 2, 3], |
494 | | - "run_ids": [], |
495 | | - "flow_ids": [], |
496 | | - "setup_ids": [], |
497 | | - } |
498 | | - new_study = study.json() |
499 | | - |
500 | | - creation_date = datetime.fromisoformat(new_study.pop("creation_date")) |
501 | | - assert creation_date.date() == datetime.now(UTC).date() |
502 | | - assert new_study == expected |
503 | | - |
504 | | - |
505 | | -async def _attach_tasks_to_study( |
506 | | - study_id: int, |
507 | | - task_ids: list[int], |
508 | | - api_key: str, |
509 | | - py_api: httpx.AsyncClient, |
510 | | - expdb_test: AsyncConnection, |
511 | | -) -> httpx.Response: |
512 | | - # Adding requires the study to be in preparation, |
513 | | - # but the current snapshot has no in-preparation studies. |
514 | | - await expdb_test.execute(text("UPDATE study SET status = 'in_preparation' WHERE id = 1")) |
515 | | - return await py_api.post( |
516 | | - f"/studies/attach?api_key={api_key}", |
517 | | - json={"study_id": study_id, "entity_ids": task_ids}, |
518 | | - ) |
519 | | - |
520 | | - |
521 | | -@pytest.mark.mut |
522 | | -async def test_attach_task_to_study(py_api: httpx.AsyncClient, expdb_test: AsyncConnection) -> None: |
523 | | - response = await _attach_tasks_to_study( |
524 | | - study_id=1, |
525 | | - task_ids=[2, 3, 4], |
526 | | - api_key=ApiKey.ADMIN, |
527 | | - py_api=py_api, |
528 | | - expdb_test=expdb_test, |
529 | | - ) |
530 | | - assert response.status_code == HTTPStatus.OK, response.content |
531 | | - assert response.json() == {"study_id": 1, "main_entity_type": StudyType.TASK} |
532 | | - |
533 | | - |
534 | | -@pytest.mark.mut |
535 | | -async def test_attach_task_to_study_needs_owner( |
536 | | - py_api: httpx.AsyncClient, expdb_test: AsyncConnection |
537 | | -) -> None: |
538 | | - await expdb_test.execute(text("UPDATE study SET status = 'in_preparation' WHERE id = 1")) |
539 | | - response = await _attach_tasks_to_study( |
540 | | - study_id=1, |
541 | | - task_ids=[2, 3, 4], |
542 | | - api_key=ApiKey.OWNER_USER, |
543 | | - py_api=py_api, |
544 | | - expdb_test=expdb_test, |
545 | | - ) |
546 | | - assert response.status_code == HTTPStatus.FORBIDDEN, response.content |
547 | | - |
548 | | - |
549 | | -@pytest.mark.mut |
550 | | -async def test_attach_task_to_study_already_linked_raises( |
551 | | - py_api: httpx.AsyncClient, |
552 | | - expdb_test: AsyncConnection, |
553 | | -) -> None: |
554 | | - await expdb_test.execute(text("UPDATE study SET status = 'in_preparation' WHERE id = 1")) |
555 | | - response = await _attach_tasks_to_study( |
556 | | - study_id=1, |
557 | | - task_ids=[1, 3, 4], |
558 | | - api_key=ApiKey.ADMIN, |
559 | | - py_api=py_api, |
560 | | - expdb_test=expdb_test, |
561 | | - ) |
562 | | - assert response.status_code == HTTPStatus.CONFLICT, response.content |
563 | | - assert response.headers["content-type"] == "application/problem+json" |
564 | | - error = response.json() |
565 | | - assert error["type"] == StudyConflictError.uri |
566 | | - assert error["detail"] == "Task 1 is already attached to study 1." |
567 | | - |
568 | | - |
569 | | -@pytest.mark.mut |
570 | | -async def test_attach_task_to_study_but_task_not_exist_raises( |
571 | | - py_api: httpx.AsyncClient, |
572 | | - expdb_test: AsyncConnection, |
573 | | -) -> None: |
574 | | - await expdb_test.execute(text("UPDATE study SET status = 'in_preparation' WHERE id = 1")) |
575 | | - response = await _attach_tasks_to_study( |
576 | | - study_id=1, |
577 | | - task_ids=[80123, 78914], |
578 | | - api_key=ApiKey.ADMIN, |
579 | | - py_api=py_api, |
580 | | - expdb_test=expdb_test, |
581 | | - ) |
582 | | - assert response.status_code == HTTPStatus.CONFLICT |
583 | | - assert response.headers["content-type"] == "application/problem+json" |
584 | | - error = response.json() |
585 | | - assert error["type"] == StudyConflictError.uri |
586 | | - assert error["detail"] == "One or more of the tasks do not exist." |
0 commit comments