Skip to content

Commit 5bc37b8

Browse files
committed
fixes with publish/delete
1 parent c9617f9 commit 5bc37b8

3 files changed

Lines changed: 57 additions & 18 deletions

File tree

openml/_api/resources/base/versions.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from collections.abc import Mapping
4-
from typing import Any
4+
from typing import Any, cast
55

66
import xmltodict
77

@@ -76,7 +76,7 @@ def untag(self, resource_id: int, tag: str) -> list[str]:
7676
def _get_endpoint_name(self) -> str:
7777
if self.resource_type == ResourceType.DATASET:
7878
return "data"
79-
return self.resource_type.name
79+
return cast("str", self.resource_type.value)
8080

8181
def _handle_delete_exception(
8282
self, resource_type: str, exception: OpenMLServerException
@@ -114,23 +114,23 @@ def _handle_delete_exception(
114114
raise exception
115115

116116
def _extract_id_from_upload(self, parsed: Mapping[str, Any]) -> int:
117-
# reads id from
118-
# sample parsed dict: {"oml:openml": {"oml:upload_flow": {"oml:id": "42"}}}
117+
# reads id from upload response
118+
# actual parsed dict: {"oml:upload_flow": {"@xmlns:oml": "...", "oml:id": "42"}}
119119

120120
# xmltodict always gives exactly one root key
121121
((_, root_value),) = parsed.items()
122122

123123
if not isinstance(root_value, Mapping):
124124
raise ValueError("Unexpected XML structure")
125125

126-
# upload node (e.g. oml:upload_task, oml:study_upload, ...)
127-
((_, upload_value),) = root_value.items()
126+
# Look for oml:id directly in the root value
127+
if "oml:id" in root_value:
128+
id_value = root_value["oml:id"]
129+
if isinstance(id_value, (str, int)):
130+
return int(id_value)
128131

129-
if not isinstance(upload_value, Mapping):
130-
raise ValueError("Unexpected upload node structure")
131-
132-
# ID is the only leaf value
133-
for v in upload_value.values():
132+
# Fallback: check all values for numeric/string IDs
133+
for v in root_value.values():
134134
if isinstance(v, (str, int)):
135135
return int(v)
136136

tests/test_api/test_http.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,5 @@ def test_post_and_delete(self):
149149
finally:
150150
# DELETE the task if it was created
151151
if task_id is not None:
152-
try:
153-
del_response = self.http_client.delete(f"task/{task_id}")
154-
# optional: verify delete
155-
if del_response.status_code != 200:
156-
print(f"Warning: delete failed for task {task_id}")
157-
except Exception as e:
158-
print(f"Warning: failed to delete task {task_id}: {e}")
152+
del_response = self.http_client.delete(f"task/{task_id}")
153+
self.assertEqual(del_response.status_code, 200)

tests/test_api/test_versions.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
from openml.testing import TestAPIBase
3+
from openml._api.resources.base.versions import ResourceV1
4+
from openml._api.resources.base.resources import ResourceType
5+
6+
7+
class TestResourceV1(TestAPIBase):
8+
def setUp(self):
9+
super().setUp()
10+
self.resource = ResourceV1(self.http_client)
11+
self.resource.resource_type = ResourceType.TASK
12+
13+
@pytest.mark.uses_test_server()
14+
def test_publish_and_delete(self):
15+
task_xml = """
16+
<oml:task_inputs xmlns:oml="http://openml.org/openml">
17+
<oml:task_type_id>5</oml:task_type_id>
18+
<oml:input name="source_data">193</oml:input>
19+
<oml:input name="estimation_procedure">17</oml:input>
20+
</oml:task_inputs>
21+
"""
22+
23+
task_id = None
24+
try:
25+
# Publish the task
26+
task_id = self.resource.publish(
27+
"task",
28+
files={"description": task_xml},
29+
)
30+
31+
# Get the task to verify it exists
32+
get_response = self.http_client.get(f"task/{task_id}")
33+
self.assertEqual(get_response.status_code, 200)
34+
35+
finally:
36+
# delete the task if it was created
37+
if task_id is not None:
38+
success = self.resource.delete(task_id)
39+
self.assertTrue(success)
40+
41+
42+
@pytest.mark.uses_test_server()
43+
def test_tag_and_untag(self):
44+
pass

0 commit comments

Comments
 (0)