diff --git a/tests/routers/openml/setups_tag_test.py b/tests/routers/openml/setups_tag_test.py index 44d31f2..05e326a 100644 --- a/tests/routers/openml/setups_tag_test.py +++ b/tests/routers/openml/setups_tag_test.py @@ -1,4 +1,3 @@ -import re from http import HTTPStatus import httpx @@ -6,7 +5,9 @@ from sqlalchemy import text from sqlalchemy.ext.asyncio import AsyncConnection -from tests.users import ApiKey +from core.errors import SetupNotFoundError, TagAlreadyExistsError +from routers.openml.setups import tag_setup +from tests.users import SOME_USER, ApiKey async def test_setup_tag_missing_auth(py_api: httpx.AsyncClient) -> None: @@ -16,41 +17,68 @@ async def test_setup_tag_missing_auth(py_api: httpx.AsyncClient) -> None: assert response.json()["detail"] == "Authentication failed" -async def test_setup_tag_unknown_setup(py_api: httpx.AsyncClient) -> None: +@pytest.mark.mut +async def test_setup_tag_api_success( + py_api: httpx.AsyncClient, expdb_test: AsyncConnection +) -> None: + tag = "setup_tag_via_http" response = await py_api.post( f"/setup/tag?api_key={ApiKey.SOME_USER}", - json={"setup_id": 999999, "tag": "test_tag"}, + json={"setup_id": 1, "tag": tag}, ) - assert response.status_code == HTTPStatus.NOT_FOUND - assert re.match(r"Setup \d+ not found.", response.json()["detail"]) + + assert response.status_code == HTTPStatus.OK + assert response.json()["setup_tag"]["tag"][-1] == tag + + rows = await expdb_test.execute( + text("SELECT * FROM setup_tag WHERE id = 1 AND tag = :tag"), + parameters={"tag": tag}, + ) + assert len(rows.all()) == 1 + + +# ── Direct call tests: tag_setup ── + + +async def test_setup_tag_unknown_setup(expdb_test: AsyncConnection) -> None: + with pytest.raises(SetupNotFoundError, match=r"Setup \d+ not found."): + await tag_setup( + setup_id=999999, + tag="test_tag", + user=SOME_USER, + expdb_db=expdb_test, + ) @pytest.mark.mut -async def test_setup_tag_already_exists( - py_api: httpx.AsyncClient, expdb_test: AsyncConnection -) -> None: +async def test_setup_tag_already_exists(expdb_test: AsyncConnection) -> None: + tag = "setup_tag_conflict" await expdb_test.execute( - text("INSERT INTO setup_tag (id, tag, uploader) VALUES (1, 'existing_tag_123', 2);") + text("INSERT INTO setup_tag (id, tag, uploader) VALUES (1, :tag, 2);"), + parameters={"tag": tag}, ) - response = await py_api.post( - f"/setup/tag?api_key={ApiKey.SOME_USER}", - json={"setup_id": 1, "tag": "existing_tag_123"}, - ) - assert response.status_code == HTTPStatus.CONFLICT - assert response.json()["detail"] == "Setup 1 already has tag 'existing_tag_123'." + with pytest.raises(TagAlreadyExistsError, match=rf"Setup 1 already has tag '{tag}'\."): + await tag_setup( + setup_id=1, + tag=tag, + user=SOME_USER, + expdb_db=expdb_test, + ) @pytest.mark.mut -async def test_setup_tag_success(py_api: httpx.AsyncClient, expdb_test: AsyncConnection) -> None: - response = await py_api.post( - f"/setup/tag?api_key={ApiKey.SOME_USER}", - json={"setup_id": 1, "tag": "my_new_success_tag"}, +async def test_setup_tag_direct_success(expdb_test: AsyncConnection) -> None: + tag = "setup_tag_via_direct" + result = await tag_setup( + setup_id=1, + tag=tag, + user=SOME_USER, + expdb_db=expdb_test, ) - assert response.status_code == HTTPStatus.OK - assert "my_new_success_tag" in response.json()["setup_tag"]["tag"] - + assert result["setup_tag"]["tag"][-1] == tag rows = await expdb_test.execute( - text("SELECT * FROM setup_tag WHERE id = 1 AND tag = 'my_new_success_tag'") + text("SELECT * FROM setup_tag WHERE id = 1 AND tag = :tag"), + parameters={"tag": tag}, ) assert len(rows.all()) == 1