Skip to content

Commit 22444bc

Browse files
igennovaigennovaPGijsberspre-commit-ci[bot]
authored
Refactor setups_tag tests to use direct method calls for non-happy-path tests (#298)
## Summary: Following the testing guidelines from #295: - Keep one happy-path test via `py_api `. - Convert error/variation tests to direct calls to tag_setup() . - Add a direct-call success test . --------- Co-authored-by: igennova <luckynegi025@gmail.com> Co-authored-by: Pieter Gijsbers <p.gijsbers@tue.nl> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 562c413 commit 22444bc

1 file changed

Lines changed: 53 additions & 24 deletions

File tree

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import re
21
from http import HTTPStatus
32

43
import httpx
54
import pytest
65
from sqlalchemy import text
76
from sqlalchemy.ext.asyncio import AsyncConnection
87

9-
from tests.users import ApiKey
8+
from core.errors import SetupNotFoundError, TagAlreadyExistsError
9+
from routers.openml.setups import tag_setup
10+
from tests.users import SOME_USER, ApiKey
1011

1112

1213
async def test_setup_tag_missing_auth(py_api: httpx.AsyncClient) -> None:
@@ -16,41 +17,69 @@ async def test_setup_tag_missing_auth(py_api: httpx.AsyncClient) -> None:
1617
assert response.json()["detail"] == "Authentication failed"
1718

1819

19-
async def test_setup_tag_unknown_setup(py_api: httpx.AsyncClient) -> None:
20+
@pytest.mark.mut
21+
async def test_setup_tag_api_success(
22+
py_api: httpx.AsyncClient, expdb_test: AsyncConnection
23+
) -> None:
24+
tag = "setup_tag_via_http"
2025
response = await py_api.post(
2126
f"/setup/tag?api_key={ApiKey.SOME_USER}",
22-
json={"setup_id": 999999, "tag": "test_tag"},
27+
json={"setup_id": 1, "tag": tag},
2328
)
24-
assert response.status_code == HTTPStatus.NOT_FOUND
25-
assert re.match(r"Setup \d+ not found.", response.json()["detail"])
29+
30+
assert response.status_code == HTTPStatus.OK
31+
expected = {"setup_tag": {"id": "1", "tag": ["setup_tag_via_http"]}}
32+
assert expected == response.json()
33+
34+
rows = await expdb_test.execute(
35+
text("SELECT * FROM setup_tag WHERE id = 1 AND tag = :tag"),
36+
parameters={"tag": tag},
37+
)
38+
assert len(rows.all()) == 1
39+
40+
41+
# ── Direct call tests: tag_setup ──
42+
43+
44+
async def test_setup_tag_unknown_setup(expdb_test: AsyncConnection) -> None:
45+
with pytest.raises(SetupNotFoundError, match=r"Setup \d+ not found."):
46+
await tag_setup(
47+
setup_id=999999,
48+
tag="test_tag",
49+
user=SOME_USER,
50+
expdb_db=expdb_test,
51+
)
2652

2753

2854
@pytest.mark.mut
29-
async def test_setup_tag_already_exists(
30-
py_api: httpx.AsyncClient, expdb_test: AsyncConnection
31-
) -> None:
55+
async def test_setup_tag_already_exists(expdb_test: AsyncConnection) -> None:
56+
tag = "setup_tag_conflict"
3257
await expdb_test.execute(
33-
text("INSERT INTO setup_tag (id, tag, uploader) VALUES (1, 'existing_tag_123', 2);")
58+
text("INSERT INTO setup_tag (id, tag, uploader) VALUES (1, :tag, 2);"),
59+
parameters={"tag": tag},
3460
)
35-
response = await py_api.post(
36-
f"/setup/tag?api_key={ApiKey.SOME_USER}",
37-
json={"setup_id": 1, "tag": "existing_tag_123"},
38-
)
39-
assert response.status_code == HTTPStatus.CONFLICT
40-
assert response.json()["detail"] == "Setup 1 already has tag 'existing_tag_123'."
61+
with pytest.raises(TagAlreadyExistsError, match=rf"Setup 1 already has tag '{tag}'\."):
62+
await tag_setup(
63+
setup_id=1,
64+
tag=tag,
65+
user=SOME_USER,
66+
expdb_db=expdb_test,
67+
)
4168

4269

4370
@pytest.mark.mut
44-
async def test_setup_tag_success(py_api: httpx.AsyncClient, expdb_test: AsyncConnection) -> None:
45-
response = await py_api.post(
46-
f"/setup/tag?api_key={ApiKey.SOME_USER}",
47-
json={"setup_id": 1, "tag": "my_new_success_tag"},
71+
async def test_setup_tag_direct_success(expdb_test: AsyncConnection) -> None:
72+
tag = "setup_tag_via_direct"
73+
result = await tag_setup(
74+
setup_id=1,
75+
tag=tag,
76+
user=SOME_USER,
77+
expdb_db=expdb_test,
4878
)
4979

50-
assert response.status_code == HTTPStatus.OK
51-
assert "my_new_success_tag" in response.json()["setup_tag"]["tag"]
52-
80+
assert result["setup_tag"]["tag"][-1] == tag
5381
rows = await expdb_test.execute(
54-
text("SELECT * FROM setup_tag WHERE id = 1 AND tag = 'my_new_success_tag'")
82+
text("SELECT * FROM setup_tag WHERE id = 1 AND tag = :tag"),
83+
parameters={"tag": tag},
5584
)
5685
assert len(rows.all()) == 1

0 commit comments

Comments
 (0)