Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 0 additions & 135 deletions tests/routers/openml/study_get_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
from datetime import UTC, datetime
from http import HTTPStatus

import httpx
import pytest
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncConnection

from core.errors import StudyConflictError
from schemas.study import StudyType
from tests.users import ApiKey


async def test_get_task_study_by_id(py_api: httpx.AsyncClient) -> None:
Expand Down Expand Up @@ -457,130 +449,3 @@ async def test_get_task_study_by_alias(py_api: httpx.AsyncClient) -> None:
"setup_ids": [],
}
assert response.json() == expected


@pytest.mark.mut
async def test_create_task_study(py_api: httpx.AsyncClient) -> None:
response = await py_api.post(
f"/studies?api_key={ApiKey.SOME_USER}",
json={
"name": "Test Study",
"alias": "test-study",
"main_entity_type": "task",
"description": "A test study",
"tasks": [1, 2, 3],
"runs": [],
},
)
assert response.status_code == HTTPStatus.OK
new = response.json()
assert "study_id" in new
study_id = new["study_id"]
assert isinstance(study_id, int)

study = await py_api.get(f"/studies/{study_id}")
assert study.status_code == HTTPStatus.OK
expected = {
"id": study_id,
"alias": "test-study",
"main_entity_type": "task",
"name": "Test Study",
"description": "A test study",
"visibility": "public",
"status": "in_preparation",
"creator": 2,
"data_ids": [1, 1, 1],
"task_ids": [1, 2, 3],
"run_ids": [],
"flow_ids": [],
"setup_ids": [],
}
new_study = study.json()

creation_date = datetime.fromisoformat(new_study.pop("creation_date"))
assert creation_date.date() == datetime.now(UTC).date()
assert new_study == expected


async def _attach_tasks_to_study(
study_id: int,
task_ids: list[int],
api_key: str,
py_api: httpx.AsyncClient,
expdb_test: AsyncConnection,
) -> httpx.Response:
# Adding requires the study to be in preparation,
# but the current snapshot has no in-preparation studies.
await expdb_test.execute(text("UPDATE study SET status = 'in_preparation' WHERE id = 1"))
return await py_api.post(
f"/studies/attach?api_key={api_key}",
json={"study_id": study_id, "entity_ids": task_ids},
)


@pytest.mark.mut
async def test_attach_task_to_study(py_api: httpx.AsyncClient, expdb_test: AsyncConnection) -> None:
response = await _attach_tasks_to_study(
study_id=1,
task_ids=[2, 3, 4],
api_key=ApiKey.ADMIN,
py_api=py_api,
expdb_test=expdb_test,
)
assert response.status_code == HTTPStatus.OK, response.content
assert response.json() == {"study_id": 1, "main_entity_type": StudyType.TASK}


@pytest.mark.mut
async def test_attach_task_to_study_needs_owner(
py_api: httpx.AsyncClient, expdb_test: AsyncConnection
) -> None:
await expdb_test.execute(text("UPDATE study SET status = 'in_preparation' WHERE id = 1"))
response = await _attach_tasks_to_study(
study_id=1,
task_ids=[2, 3, 4],
api_key=ApiKey.OWNER_USER,
py_api=py_api,
expdb_test=expdb_test,
)
assert response.status_code == HTTPStatus.FORBIDDEN, response.content


@pytest.mark.mut
async def test_attach_task_to_study_already_linked_raises(
py_api: httpx.AsyncClient,
expdb_test: AsyncConnection,
) -> None:
await expdb_test.execute(text("UPDATE study SET status = 'in_preparation' WHERE id = 1"))
response = await _attach_tasks_to_study(
study_id=1,
task_ids=[1, 3, 4],
api_key=ApiKey.ADMIN,
py_api=py_api,
expdb_test=expdb_test,
)
assert response.status_code == HTTPStatus.CONFLICT, response.content
assert response.headers["content-type"] == "application/problem+json"
error = response.json()
assert error["type"] == StudyConflictError.uri
assert error["detail"] == "Task 1 is already attached to study 1."


@pytest.mark.mut
async def test_attach_task_to_study_but_task_not_exist_raises(
py_api: httpx.AsyncClient,
expdb_test: AsyncConnection,
) -> None:
await expdb_test.execute(text("UPDATE study SET status = 'in_preparation' WHERE id = 1"))
response = await _attach_tasks_to_study(
study_id=1,
task_ids=[80123, 78914],
api_key=ApiKey.ADMIN,
py_api=py_api,
expdb_test=expdb_test,
)
assert response.status_code == HTTPStatus.CONFLICT
assert response.headers["content-type"] == "application/problem+json"
error = response.json()
assert error["type"] == StudyConflictError.uri
assert error["detail"] == "One or more of the tasks do not exist."
Loading