From 386da4c92ac150a3fcc798b5040c61df9e96c68e Mon Sep 17 00:00:00 2001 From: igennova Date: Sat, 28 Mar 2026 04:07:29 +0530 Subject: [PATCH 1/2] Split setup tag tests: API vs direct --- tests/routers/openml/setups_tag_test.py | 68 ++++++++++++++++--------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/tests/routers/openml/setups_tag_test.py b/tests/routers/openml/setups_tag_test.py index 44d31f2..867d622 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,62 @@ 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: 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": "my_new_success_api_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 "my_new_success_api_tag" in response.json()["setup_tag"]["tag"] + + rows = await expdb_test.execute( + text("SELECT * FROM setup_tag WHERE id = 1 AND tag = 'my_new_success_api_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: await expdb_test.execute( text("INSERT INTO setup_tag (id, tag, uploader) VALUES (1, 'existing_tag_123', 2);") ) - 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=r"Setup 1 already has tag 'existing_tag_123'."): + await tag_setup( + setup_id=1, + tag="existing_tag_123", + 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: + result = await tag_setup( + setup_id=1, + tag="my_direct_success_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 "my_direct_success_tag" in result["setup_tag"]["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 = 'my_direct_success_tag'") ) assert len(rows.all()) == 1 From ee61275c77b79f23758cb3840054cb2820b8e78c Mon Sep 17 00:00:00 2001 From: igennova Date: Sat, 28 Mar 2026 04:21:39 +0530 Subject: [PATCH 2/2] fix(setups_tag tests): assert tag list and tidy tag strings --- tests/routers/openml/setups_tag_test.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/routers/openml/setups_tag_test.py b/tests/routers/openml/setups_tag_test.py index 867d622..05e326a 100644 --- a/tests/routers/openml/setups_tag_test.py +++ b/tests/routers/openml/setups_tag_test.py @@ -21,16 +21,18 @@ async def test_setup_tag_missing_auth(py_api: httpx.AsyncClient) -> None: 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": 1, "tag": "my_new_success_api_tag"}, + json={"setup_id": 1, "tag": tag}, ) assert response.status_code == HTTPStatus.OK - assert "my_new_success_api_tag" in response.json()["setup_tag"]["tag"] + assert response.json()["setup_tag"]["tag"][-1] == tag rows = await expdb_test.execute( - text("SELECT * FROM setup_tag WHERE id = 1 AND tag = 'my_new_success_api_tag'") + text("SELECT * FROM setup_tag WHERE id = 1 AND tag = :tag"), + parameters={"tag": tag}, ) assert len(rows.all()) == 1 @@ -50,13 +52,15 @@ async def test_setup_tag_unknown_setup(expdb_test: AsyncConnection) -> None: @pytest.mark.mut 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}, ) - with pytest.raises(TagAlreadyExistsError, match=r"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="existing_tag_123", + tag=tag, user=SOME_USER, expdb_db=expdb_test, ) @@ -64,15 +68,17 @@ async def test_setup_tag_already_exists(expdb_test: AsyncConnection) -> None: @pytest.mark.mut async def test_setup_tag_direct_success(expdb_test: AsyncConnection) -> None: + tag = "setup_tag_via_direct" result = await tag_setup( setup_id=1, - tag="my_direct_success_tag", + tag=tag, user=SOME_USER, expdb_db=expdb_test, ) - assert "my_direct_success_tag" in result["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_direct_success_tag'") + text("SELECT * FROM setup_tag WHERE id = 1 AND tag = :tag"), + parameters={"tag": tag}, ) assert len(rows.all()) == 1