44import pytest
55from sqlalchemy .ext .asyncio import AsyncConnection
66
7- from core .errors import AuthenticationFailedError , TagAlreadyExistsError
7+ from core .errors import TagAlreadyExistsError
88from database .datasets import get_tags_for
9+ from database .users import User
10+ from routers .openml .datasets import tag_dataset
911from tests import constants
10- from tests .users import ApiKey
12+ from tests .users import ADMIN_USER , OWNER_USER , SOME_USER , ApiKey
1113
1214
1315@pytest .mark .parametrize (
@@ -22,73 +24,71 @@ async def test_dataset_tag_rejects_unauthorized(key: ApiKey, py_api: httpx.Async
2224 json = {"data_id" : next (iter (constants .PRIVATE_DATASET_ID )), "tag" : "test" },
2325 )
2426 assert response .status_code == HTTPStatus .UNAUTHORIZED
25- assert response .headers ["content-type" ] == "application/problem+json"
26- error = response .json ()
27- assert error ["type" ] == AuthenticationFailedError .uri
28- assert error ["code" ] == "103"
27+
28+
29+ @pytest .mark .parametrize (
30+ "tag" ,
31+ ["" , "h@" , " a" , "a" * 65 ],
32+ ids = ["too short" , "@" , "space" , "too long" ],
33+ )
34+ async def test_dataset_tag_invalid_tag_is_rejected (
35+ # Constraints for the tag are handled by FastAPI
36+ tag : str ,
37+ py_api : httpx .AsyncClient ,
38+ ) -> None :
39+ new = await py_api .post (
40+ f"/datasets/tag?api_key={ ApiKey .ADMIN } " ,
41+ json = {"data_id" : 1 , "tag" : tag },
42+ )
43+
44+ assert new .status_code == HTTPStatus .UNPROCESSABLE_ENTITY
45+ assert new .json ()["detail" ][0 ]["loc" ] == ["body" , "tag" ]
46+
47+
48+ # ── Direct call tests: tag_dataset ──
2949
3050
3151@pytest .mark .mut
3252@pytest .mark .parametrize (
33- "key " ,
34- [ApiKey . ADMIN , ApiKey . SOME_USER , ApiKey . OWNER_USER ],
53+ "user " ,
54+ [ADMIN_USER , SOME_USER , OWNER_USER ],
3555 ids = ["administrator" , "non-owner" , "owner" ],
3656)
37- async def test_dataset_tag (
38- key : ApiKey , expdb_test : AsyncConnection , py_api : httpx .AsyncClient
39- ) -> None :
57+ async def test_dataset_tag (user : User , expdb_test : AsyncConnection ) -> None :
4058 dataset_id , tag = next (iter (constants .PRIVATE_DATASET_ID )), "test"
41- response = await py_api .post (
42- f"/datasets/tag?api_key={ key } " ,
43- json = {"data_id" : dataset_id , "tag" : tag },
59+ result = await tag_dataset (
60+ data_id = dataset_id ,
61+ tag = tag ,
62+ user = user ,
63+ expdb_db = expdb_test ,
4464 )
45- assert response .status_code == HTTPStatus .OK
46- assert response .json () == {"data_tag" : {"id" : str (dataset_id ), "tag" : [tag ]}}
65+ assert result == {"data_tag" : {"id" : str (dataset_id ), "tag" : [tag ]}}
4766
4867 tags = await get_tags_for (id_ = dataset_id , connection = expdb_test )
4968 assert tag in tags
5069
5170
5271@pytest .mark .mut
53- async def test_dataset_tag_returns_existing_tags (py_api : httpx .AsyncClient ) -> None :
54- dataset_id , tag = 1 , "test"
55- response = await py_api .post (
56- f"/datasets/tag?api_key={ ApiKey .ADMIN } " ,
57- json = {"data_id" : dataset_id , "tag" : tag },
72+ async def test_dataset_tag_returns_existing_tags (expdb_test : AsyncConnection ) -> None :
73+ dataset_id , tag = 1 , "test" # Dataset 1 already is tagged with 'study_14'
74+ result = await tag_dataset (
75+ data_id = dataset_id ,
76+ tag = tag ,
77+ user = ADMIN_USER ,
78+ expdb_db = expdb_test ,
5879 )
59- assert response .status_code == HTTPStatus .OK
60- assert response .json () == {"data_tag" : {"id" : str (dataset_id ), "tag" : ["study_14" , tag ]}}
80+ assert result == {"data_tag" : {"id" : str (dataset_id ), "tag" : ["study_14" , tag ]}}
6181
6282
6383@pytest .mark .mut
64- async def test_dataset_tag_fails_if_tag_exists (py_api : httpx . AsyncClient ) -> None :
84+ async def test_dataset_tag_fails_if_tag_exists (expdb_test : AsyncConnection ) -> None :
6585 dataset_id , tag = 1 , "study_14" # Dataset 1 already is tagged with 'study_14'
66- response = await py_api .post (
67- f"/datasets/tag?api_key={ ApiKey .ADMIN } " ,
68- json = {"data_id" : dataset_id , "tag" : tag },
69- )
70- assert response .status_code == HTTPStatus .CONFLICT
71- assert response .headers ["content-type" ] == "application/problem+json"
72- error = response .json ()
73- assert error ["type" ] == TagAlreadyExistsError .uri
74- assert error ["code" ] == "473"
75- assert str (dataset_id ) in error ["detail" ]
76- assert tag in error ["detail" ]
77-
78-
79- @pytest .mark .parametrize (
80- "tag" ,
81- ["" , "h@" , " a" , "a" * 65 ],
82- ids = ["too short" , "@" , "space" , "too long" ],
83- )
84- async def test_dataset_tag_invalid_tag_is_rejected (
85- tag : str ,
86- py_api : httpx .AsyncClient ,
87- ) -> None :
88- new = await py_api .post (
89- f"/datasets/tag?api_key={ ApiKey .ADMIN } " ,
90- json = {"data_id" : 1 , "tag" : tag },
91- )
92-
93- assert new .status_code == HTTPStatus .UNPROCESSABLE_ENTITY
94- assert new .json ()["detail" ][0 ]["loc" ] == ["body" , "tag" ]
86+ with pytest .raises (TagAlreadyExistsError ) as e :
87+ await tag_dataset (
88+ data_id = dataset_id ,
89+ tag = tag ,
90+ user = ADMIN_USER ,
91+ expdb_db = expdb_test ,
92+ )
93+ assert str (dataset_id ) in e .value .detail
94+ assert tag in e .value .detail
0 commit comments