Skip to content

Commit 8b07a20

Browse files
committed
modified tests
1 parent 49789a7 commit 8b07a20

2 files changed

Lines changed: 129 additions & 148 deletions

File tree

tests/test_api/test_run.py

Lines changed: 129 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,136 @@
1-
"""Tests for Run V1 → V2 API Migration."""
1+
# License: BSD 3-Clause
22
from __future__ import annotations
33

4+
from unittest.mock import patch
5+
46
import pytest
7+
from requests import Response, Session
58

69
import openml
7-
from openml._api.resources import RunV1API, RunV2API
8-
from openml.enums import APIVersion
10+
from openml._api import RunV1API, RunV2API
911
from openml.exceptions import OpenMLNotSupportedError
1012
from openml.runs.run import OpenMLRun
11-
from openml.testing import TestAPIBase
12-
13-
14-
@pytest.mark.uses_test_server()
15-
class TestRunAPIBase(TestAPIBase):
16-
resource: RunV1API | RunV2API
17-
18-
def _assert_run_shape(self, run: OpenMLRun) -> None:
19-
self.assertIsInstance(run, OpenMLRun)
20-
self.assertEqual(run.run_id, 1)
21-
self.assertIsInstance(run.task_id, int)
22-
23-
def _get(self) -> OpenMLRun:
24-
run = self.resource.get(run_id=1)
25-
self._assert_run_shape(run)
26-
return run
27-
28-
def _list(self) -> None:
29-
limit = 5
30-
runs_df = self.resource.list(limit=limit, offset=0)
31-
32-
self.assertEqual(len(runs_df), limit)
33-
self.assertIn("run_id", runs_df.columns)
34-
self.assertIn("task_id", runs_df.columns)
35-
self.assertIn("setup_id", runs_df.columns)
36-
self.assertIn("flow_id", runs_df.columns)
37-
38-
def _publish_and_delete(self) -> None:
39-
from sklearn.neighbors import KNeighborsClassifier
40-
41-
task = openml.tasks.get_task(19)
42-
clf = KNeighborsClassifier(n_neighbors=3)
43-
run = openml.runs.run_model_on_task(clf, task)
44-
45-
file_elements = run._get_file_elements()
46-
if "description" not in file_elements:
47-
file_elements["description"] = run._to_xml()
48-
49-
run_id = self.resource.publish(path="run", files=file_elements)
50-
self.assertIsInstance(run_id, int)
51-
self.assertGreater(run_id, 0)
52-
53-
self.resource.delete(run_id)
54-
55-
with pytest.raises(Exception):
56-
self.resource.get(run_id=run_id)
57-
58-
59-
class TestRunV1API(TestRunAPIBase):
60-
def setUp(self):
61-
super().setUp()
62-
http_client = self.http_clients[APIVersion.V1]
63-
self.resource = RunV1API(http=http_client, minio=self.minio_client)
64-
65-
def test_get(self):
66-
self._get()
67-
68-
def test_list(self):
69-
self._list()
70-
71-
def test_publish_and_delete(self):
72-
self._publish_and_delete()
73-
74-
75-
class TestRunV2API(TestRunAPIBase):
76-
def setUp(self):
77-
super().setUp()
78-
http_client = self.http_clients[APIVersion.V2]
79-
self.resource = RunV2API(http=http_client, minio=self.minio_client)
80-
81-
def test_get(self):
82-
with pytest.raises(
83-
OpenMLNotSupportedError,
84-
match="RunV2API: v2 API does not support `get` for resource `run`",
85-
):
86-
self._get()
87-
88-
def test_list(self):
89-
with pytest.raises(
90-
OpenMLNotSupportedError,
91-
match="RunV2API: v2 API does not support `list` for resource `run`",
92-
):
93-
self._list()
94-
95-
def test_publish_and_delete(self):
96-
with pytest.raises(
97-
OpenMLNotSupportedError,
98-
match="RunV2API: v2 API does not support `publish` for resource `run`",
99-
):
100-
self._publish_and_delete()
101-
102-
103-
class TestRunCombinedAPI(TestAPIBase):
104-
def setUp(self):
105-
super().setUp()
106-
self.resource_v1 = RunV1API(
107-
http=self.http_clients[APIVersion.V1],
108-
minio=self.minio_client,
109-
)
110-
self.resource_v2 = RunV2API(
111-
http=self.http_clients[APIVersion.V2],
112-
minio=self.minio_client,
113-
)
114-
115-
def test_get_contracts(self):
116-
run_v1 = self.resource_v1.get(run_id=1)
117-
self.assertIsInstance(run_v1, OpenMLRun)
118-
self.assertEqual(run_v1.run_id, 1)
119-
120-
with pytest.raises(
121-
OpenMLNotSupportedError,
122-
match="RunV2API: v2 API does not support `get` for resource `run`",
123-
):
124-
self.resource_v2.get(run_id=1)
125-
126-
def test_list_contracts(self):
127-
limit = 5
128-
runs_v1 = self.resource_v1.list(limit=limit, offset=0)
129-
self.assertEqual(len(runs_v1), limit)
130-
self.assertIn("run_id", runs_v1.columns)
131-
self.assertIn("task_id", runs_v1.columns)
132-
self.assertIn("setup_id", runs_v1.columns)
133-
self.assertIn("flow_id", runs_v1.columns)
134-
135-
with pytest.raises(
136-
OpenMLNotSupportedError,
137-
match="RunV2API: v2 API does not support `list` for resource `run`",
138-
):
139-
self.resource_v2.list(limit=limit, offset=0)
140-
141-
def test_publish_contracts(self):
142-
with pytest.raises(
143-
OpenMLNotSupportedError,
144-
match="RunV2API: v2 API does not support `publish` for resource `run`",
145-
):
146-
self.resource_v2.publish(path="run", files={"description": "<run/>"})
13+
14+
15+
@pytest.fixture
16+
def run_v1(http_client_v1, minio_client) -> RunV1API:
17+
return RunV1API(http=http_client_v1, minio=minio_client)
18+
19+
20+
@pytest.fixture
21+
def run_v2(http_client_v2, minio_client) -> RunV2API:
22+
return RunV2API(http=http_client_v2, minio=minio_client)
23+
24+
25+
def _assert_run_shape(run: OpenMLRun) -> None:
26+
assert isinstance(run, OpenMLRun)
27+
assert isinstance(run.run_id, int)
28+
assert run.run_id > 0
29+
assert isinstance(run.task_id, int)
30+
31+
32+
@pytest.mark.test_server()
33+
def test_run_v1_get(run_v1):
34+
run = run_v1.get(run_id=1)
35+
_assert_run_shape(run)
36+
37+
38+
@pytest.mark.test_server()
39+
def test_run_v1_list(run_v1):
40+
limit = 5
41+
runs_df = run_v1.list(limit=limit, offset=0)
42+
43+
assert len(runs_df) == limit
44+
assert "run_id" in runs_df.columns
45+
assert "task_id" in runs_df.columns
46+
assert "setup_id" in runs_df.columns
47+
assert "flow_id" in runs_df.columns
48+
49+
50+
def test_run_v1_publish_mocked(run_v1, use_api_v1, test_api_key):
51+
files = {"description": "<run/>"}
52+
53+
with patch.object(Session, "request") as mock_request:
54+
mock_request.return_value = Response()
55+
mock_request.return_value.status_code = 200
56+
mock_request.return_value._content = (
57+
'<oml:upload_run xmlns:oml="http://openml.org/openml">\n'
58+
" <oml:id>456</oml:id>\n"
59+
"</oml:upload_run>\n"
60+
).encode("utf-8")
61+
62+
result = run_v1.publish(path="run", files=files)
63+
64+
assert result == 456
65+
mock_request.assert_called_once_with(
66+
method="POST",
67+
url=openml.config.server + "run",
68+
params={},
69+
data={"api_key": test_api_key},
70+
headers=openml.config._HEADERS,
71+
files=files,
72+
)
73+
74+
75+
def test_run_v1_delete_mocked(run_v1, use_api_v1, test_api_key):
76+
run_id = 456
77+
78+
with patch.object(Session, "request") as mock_request:
79+
mock_request.return_value = Response()
80+
mock_request.return_value.status_code = 200
81+
mock_request.return_value._content = (
82+
'<oml:run_delete xmlns:oml="http://openml.org/openml">\n'
83+
" <oml:id>456</oml:id>\n"
84+
"</oml:run_delete>\n"
85+
).encode("utf-8")
86+
87+
result = run_v1.delete(run_id)
88+
89+
assert result is True
90+
mock_request.assert_called_once_with(
91+
method="DELETE",
92+
url=openml.config.server + f"run/{run_id}",
93+
params={"api_key": test_api_key},
94+
data={},
95+
headers=openml.config._HEADERS,
96+
files=None,
97+
)
98+
99+
100+
def test_run_v2_get_not_supported(run_v2):
101+
with pytest.raises(
102+
OpenMLNotSupportedError,
103+
match="RunV2API: v2 API does not support `get` for resource `run`",
104+
):
105+
run_v2.get(run_id=1)
106+
107+
108+
def test_run_v2_list_not_supported(run_v2):
109+
with pytest.raises(
110+
OpenMLNotSupportedError,
111+
match="RunV2API: v2 API does not support `list` for resource `run`",
112+
):
113+
run_v2.list(limit=5, offset=0)
114+
115+
116+
def test_run_v2_publish_not_supported(run_v2):
117+
with pytest.raises(
118+
OpenMLNotSupportedError,
119+
match="RunV2API: v2 API does not support `publish` for resource `run`",
120+
):
121+
run_v2.publish(path="run", files={"description": "<run/>"})
122+
123+
124+
@pytest.mark.test_server()
125+
def test_run_v1_v2_contracts(run_v1, run_v2):
126+
run_from_v1 = run_v1.get(run_id=1)
127+
_assert_run_shape(run_from_v1)
128+
129+
with pytest.raises(OpenMLNotSupportedError, match="does not support `get`"):
130+
run_v2.get(run_id=1)
131+
132+
with pytest.raises(OpenMLNotSupportedError, match="does not support `list`"):
133+
run_v2.list(limit=5, offset=0)
134+
135+
with pytest.raises(OpenMLNotSupportedError, match="does not support `publish`"):
136+
run_v2.publish(path="run", files={"description": "<run/>"})

tests/test_runs/test_run_functions.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,15 +1657,6 @@ def test_run_on_dataset_with_missing_labels_array(self):
16571657
# repeat, fold, row_id, 6 confidences, prediction and correct label
16581658
assert len(row) == 12
16591659

1660-
@pytest.mark.test_server()
1661-
def test_get_cached_run(self):
1662-
openml.config.set_root_cache_directory(self.static_cache_dir)
1663-
openml.runs.functions._get_cached_run(1)
1664-
1665-
def test_get_uncached_run(self):
1666-
openml.config.set_root_cache_directory(self.static_cache_dir)
1667-
with pytest.raises(openml.exceptions.OpenMLCacheException):
1668-
openml.runs.functions._get_cached_run(10)
16691660
@pytest.mark.sklearn()
16701661
@pytest.mark.test_server()
16711662
def test_run_flow_on_task_downloaded_flow(self):

0 commit comments

Comments
 (0)