|
1 | 1 | import pytest |
2 | 2 | from requests import Session, Response |
3 | 3 | from unittest.mock import patch |
4 | | -from openml.testing import TestBase |
5 | | -from openml._api import FallbackProxy, ResourceAPI |
6 | | -from openml.enums import ResourceType, APIVersion |
| 4 | +from openml._api import FallbackProxy, ResourceAPI, ResourceV1API, ResourceV2API, TaskAPI |
| 5 | +from openml.enums import ResourceType |
7 | 6 | from openml.exceptions import OpenMLNotSupportedError |
| 7 | +import openml |
8 | 8 |
|
9 | 9 |
|
10 | | -class TestResourceAPIBase(TestBase): |
11 | | - resource: ResourceAPI | FallbackProxy |
12 | | - |
13 | | - @property |
14 | | - def http_client(self): |
15 | | - return self.resource._http |
16 | | - |
17 | | - def _publish(self): |
18 | | - resource_name = "task" |
19 | | - resource_files = {"description": """Resource Description File"""} |
20 | | - resource_id = 123 |
21 | | - |
22 | | - with patch.object(Session, "request") as mock_request: |
23 | | - mock_request.return_value = Response() |
24 | | - mock_request.return_value.status_code = 200 |
25 | | - mock_request.return_value._content = f'<oml:upload_task xmlns:oml="http://openml.org/openml">\n\t<oml:id>{resource_id}</oml:id>\n</oml:upload_task>\n'.encode("utf-8") |
26 | | - |
27 | | - published_resource_id = self.resource.publish( |
28 | | - resource_name, |
29 | | - files=resource_files, |
30 | | - ) |
31 | | - |
32 | | - self.assertEqual(resource_id, published_resource_id) |
33 | | - |
34 | | - mock_request.assert_called_once_with( |
35 | | - method="POST", |
36 | | - url=self.http_client.server + self.http_client.base_url + resource_name, |
37 | | - params={}, |
38 | | - data={'api_key': self.http_client.api_key}, |
39 | | - headers=self.http_client.headers, |
40 | | - files=resource_files, |
41 | | - ) |
42 | | - |
43 | | - def _delete(self): |
44 | | - resource_name = "task" |
45 | | - resource_id = 123 |
46 | | - |
47 | | - with patch.object(Session, "request") as mock_request: |
48 | | - mock_request.return_value = Response() |
49 | | - mock_request.return_value.status_code = 200 |
50 | | - mock_request.return_value._content = f'<oml:task_delete xmlns:oml="http://openml.org/openml">\n <oml:id>{resource_id}</oml:id>\n</oml:task_delete>\n'.encode("utf-8") |
51 | | - |
52 | | - self.resource.delete(resource_id) |
53 | | - |
54 | | - mock_request.assert_called_once_with( |
55 | | - method="DELETE", |
56 | | - url=self.http_client.server + self.http_client.base_url + resource_name + "/" + str(resource_id), |
57 | | - params={'api_key': self.http_client.api_key}, |
58 | | - data={}, |
59 | | - headers=self.http_client.headers, |
60 | | - files=None, |
61 | | - ) |
62 | | - |
63 | | - def _tag(self): |
64 | | - resource_id = 123 |
65 | | - resource_tag = "TAG" |
66 | | - |
67 | | - with patch.object(Session, "request") as mock_request: |
68 | | - mock_request.return_value = Response() |
69 | | - mock_request.return_value.status_code = 200 |
70 | | - mock_request.return_value._content = f'<oml:task_tag xmlns:oml="http://openml.org/openml"><oml:id>{resource_id}</oml:id><oml:tag>{resource_tag}</oml:tag></oml:task_tag>'.encode("utf-8") |
71 | | - |
72 | | - tags = self.resource.tag(resource_id, resource_tag) |
73 | | - self.assertIn(resource_tag, tags) |
74 | | - |
75 | | - mock_request.assert_called_once_with( |
76 | | - method="POST", |
77 | | - url=self.http_client.server + self.http_client.base_url + self.resource.resource_type + "/tag", |
78 | | - params={}, |
79 | | - data={'api_key': self.http_client.api_key, 'task_id': resource_id, 'tag': resource_tag}, |
80 | | - headers=self.http_client.headers, |
81 | | - files=None, |
82 | | - ) |
83 | | - |
84 | | - def _untag(self): |
85 | | - resource_id = 123 |
86 | | - resource_tag = "TAG" |
87 | | - |
88 | | - with patch.object(Session, "request") as mock_request: |
89 | | - mock_request.return_value = Response() |
90 | | - mock_request.return_value.status_code = 200 |
91 | | - mock_request.return_value._content = f'<oml:task_untag xmlns:oml="http://openml.org/openml"><oml:id>{resource_id}</oml:id></oml:task_untag>'.encode("utf-8") |
92 | | - |
93 | | - tags = self.resource.untag(resource_id, resource_tag) |
94 | | - self.assertNotIn(resource_tag, tags) |
95 | | - |
96 | | - mock_request.assert_called_once_with( |
97 | | - method="POST", |
98 | | - url=self.http_client.server + self.http_client.base_url + self.resource.resource_type + "/untag", |
99 | | - params={}, |
100 | | - data={'api_key': self.http_client.api_key, 'task_id': resource_id, 'tag': resource_tag}, |
101 | | - headers=self.http_client.headers, |
102 | | - files=None, |
103 | | - ) |
104 | | - |
105 | | -class TestResourceV1API(TestResourceAPIBase): |
106 | | - def setUp(self): |
107 | | - super().setUp() |
108 | | - self.resource = self._create_resource( |
109 | | - api_version=APIVersion.V1, |
110 | | - resource_type=ResourceType.TASK, |
111 | | - ) |
| 10 | +class DummyTaskAPI(ResourceAPI): |
| 11 | + resource_type: ResourceType = ResourceType.TASK |
112 | 12 |
|
113 | | - def test_publish(self): |
114 | | - self._publish() |
115 | 13 |
|
116 | | - def test_delete(self): |
117 | | - self._delete() |
| 14 | +class DummyTaskV1API(ResourceV1API, TaskAPI): |
| 15 | + pass |
118 | 16 |
|
119 | | - def test_tag(self): |
120 | | - self._tag() |
121 | 17 |
|
122 | | - def test_untag(self): |
123 | | - self._untag() |
| 18 | +class DummyTaskV2API(ResourceV2API, TaskAPI): |
| 19 | + pass |
124 | 20 |
|
125 | 21 |
|
126 | | -class TestResourceV2API(TestResourceAPIBase): |
127 | | - def setUp(self): |
128 | | - super().setUp() |
129 | | - self.resource = self._create_resource( |
130 | | - api_version=APIVersion.V2, |
131 | | - resource_type=ResourceType.TASK, |
132 | | - ) |
| 22 | +@pytest.fixture |
| 23 | +def dummy_task_v1(http_client_v1, minio_client) -> DummyTaskV1API: |
| 24 | + return DummyTaskV1API(http=http_client_v1, minio=minio_client) |
| 25 | + |
133 | 26 |
|
134 | | - def test_publish(self): |
135 | | - with pytest.raises(OpenMLNotSupportedError): |
136 | | - self._publish() |
| 27 | +@pytest.fixture |
| 28 | +def dummy_task_v2(http_client_v2, minio_client) -> DummyTaskV1API: |
| 29 | + return DummyTaskV2API(http=http_client_v2, minio=minio_client) |
137 | 30 |
|
138 | | - def test_delete(self): |
139 | | - with pytest.raises(OpenMLNotSupportedError): |
140 | | - self._delete() |
141 | 31 |
|
142 | | - def test_tag(self): |
143 | | - with pytest.raises(OpenMLNotSupportedError): |
144 | | - self._tag() |
| 32 | +@pytest.fixture |
| 33 | +def dummy_task_fallback(dummy_task_v1, dummy_task_v2) -> DummyTaskV1API: |
| 34 | + return FallbackProxy(dummy_task_v2, dummy_task_v1) |
145 | 35 |
|
146 | | - def test_untag(self): |
147 | | - with pytest.raises(OpenMLNotSupportedError): |
148 | | - self._untag() |
149 | 36 |
|
| 37 | +def _publish(resource): |
| 38 | + resource_name = resource.resource_type.value |
| 39 | + resource_files = {"description": "Resource Description File"} |
| 40 | + resource_id = 123 |
150 | 41 |
|
151 | | -class TestResourceFallbackAPI(TestResourceAPIBase): |
152 | | - @property |
153 | | - def http_client(self): |
154 | | - # since these methods are not implemented for v2, they will fallback to v1 api |
155 | | - return self.http_clients[APIVersion.V1] |
| 42 | + with patch.object(Session, "request") as mock_request: |
| 43 | + mock_request.return_value = Response() |
| 44 | + mock_request.return_value.status_code = 200 |
| 45 | + mock_request.return_value._content = ( |
| 46 | + f'<oml:upload_task xmlns:oml="http://openml.org/openml">\n' |
| 47 | + f"\t<oml:id>{resource_id}</oml:id>\n" |
| 48 | + f"</oml:upload_task>\n" |
| 49 | + ).encode("utf-8") |
156 | 50 |
|
157 | | - def setUp(self): |
158 | | - super().setUp() |
159 | | - resource_v1 = self._create_resource( |
160 | | - api_version=APIVersion.V1, |
161 | | - resource_type=ResourceType.TASK, |
| 51 | + published_resource_id = resource.publish( |
| 52 | + resource_name, |
| 53 | + files=resource_files, |
162 | 54 | ) |
163 | | - resource_v2 = self._create_resource( |
164 | | - api_version=APIVersion.V2, |
165 | | - resource_type=ResourceType.TASK, |
| 55 | + |
| 56 | + assert resource_id == published_resource_id |
| 57 | + |
| 58 | + mock_request.assert_called_once_with( |
| 59 | + method="POST", |
| 60 | + url=openml.config.server + resource_name, |
| 61 | + params={}, |
| 62 | + data={"api_key": openml.config.apikey}, |
| 63 | + headers=openml.config._HEADERS, |
| 64 | + files=resource_files, |
166 | 65 | ) |
167 | | - self.resource = FallbackProxy(resource_v2, resource_v1) |
168 | 66 |
|
169 | | - def test_publish(self): |
170 | | - self._publish() |
171 | 67 |
|
172 | | - def test_delete(self): |
173 | | - self._delete() |
| 68 | +def _delete(resource): |
| 69 | + resource_name = resource.resource_type.value |
| 70 | + resource_id = 123 |
| 71 | + |
| 72 | + with patch.object(Session, "request") as mock_request: |
| 73 | + mock_request.return_value = Response() |
| 74 | + mock_request.return_value.status_code = 200 |
| 75 | + mock_request.return_value._content = ( |
| 76 | + f'<oml:task_delete xmlns:oml="http://openml.org/openml">\n' |
| 77 | + f" <oml:id>{resource_id}</oml:id>\n" |
| 78 | + f"</oml:task_delete>\n" |
| 79 | + ).encode("utf-8") |
| 80 | + |
| 81 | + resource.delete(resource_id) |
| 82 | + |
| 83 | + mock_request.assert_called_once_with( |
| 84 | + method="DELETE", |
| 85 | + url=( |
| 86 | + openml.config.server |
| 87 | + + resource_name |
| 88 | + + "/" |
| 89 | + + str(resource_id) |
| 90 | + ), |
| 91 | + params={"api_key": openml.config.apikey}, |
| 92 | + data={}, |
| 93 | + headers=openml.config._HEADERS, |
| 94 | + files=None, |
| 95 | + ) |
| 96 | + |
| 97 | +def _tag(resource): |
| 98 | + resource_id = 123 |
| 99 | + resource_tag = "TAG" |
| 100 | + |
| 101 | + with patch.object(Session, "request") as mock_request: |
| 102 | + mock_request.return_value = Response() |
| 103 | + mock_request.return_value.status_code = 200 |
| 104 | + mock_request.return_value._content = ( |
| 105 | + f'<oml:task_tag xmlns:oml="http://openml.org/openml">' |
| 106 | + f"<oml:id>{resource_id}</oml:id>" |
| 107 | + f"<oml:tag>{resource_tag}</oml:tag>" |
| 108 | + f"</oml:task_tag>" |
| 109 | + ).encode("utf-8") |
| 110 | + |
| 111 | + tags = resource.tag(resource_id, resource_tag) |
| 112 | + |
| 113 | + assert resource_tag in tags |
| 114 | + |
| 115 | + mock_request.assert_called_once_with( |
| 116 | + method="POST", |
| 117 | + url=( |
| 118 | + openml.config.server |
| 119 | + + resource.resource_type |
| 120 | + + "/tag" |
| 121 | + ), |
| 122 | + params={}, |
| 123 | + data={ |
| 124 | + "api_key": openml.config.apikey, |
| 125 | + "task_id": resource_id, |
| 126 | + "tag": resource_tag, |
| 127 | + }, |
| 128 | + headers=openml.config._HEADERS, |
| 129 | + files=None, |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +def _untag(resource): |
| 134 | + resource_id = 123 |
| 135 | + resource_tag = "TAG" |
| 136 | + |
| 137 | + with patch.object(Session, "request") as mock_request: |
| 138 | + mock_request.return_value = Response() |
| 139 | + mock_request.return_value.status_code = 200 |
| 140 | + mock_request.return_value._content = ( |
| 141 | + f'<oml:task_untag xmlns:oml="http://openml.org/openml">' |
| 142 | + f"<oml:id>{resource_id}</oml:id>" |
| 143 | + f"</oml:task_untag>" |
| 144 | + ).encode("utf-8") |
| 145 | + |
| 146 | + tags = resource.untag(resource_id, resource_tag) |
| 147 | + |
| 148 | + assert resource_tag not in tags |
| 149 | + |
| 150 | + mock_request.assert_called_once_with( |
| 151 | + method="POST", |
| 152 | + url=( |
| 153 | + openml.config.server |
| 154 | + + resource.resource_type |
| 155 | + + "/untag" |
| 156 | + ), |
| 157 | + params={}, |
| 158 | + data={ |
| 159 | + "api_key": openml.config.apikey, |
| 160 | + "task_id": resource_id, |
| 161 | + "tag": resource_tag, |
| 162 | + }, |
| 163 | + headers=openml.config._HEADERS, |
| 164 | + files=None, |
| 165 | + ) |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +def test_v1_publish(dummy_task_v1, use_api_v1): |
| 170 | + _publish(dummy_task_v1) |
| 171 | + |
| 172 | + |
| 173 | +def test_v1_delete(dummy_task_v1, use_api_v1): |
| 174 | + _delete(dummy_task_v1) |
| 175 | + |
| 176 | + |
| 177 | +def test_v1_tag(dummy_task_v1, use_api_v1): |
| 178 | + _tag(dummy_task_v1) |
| 179 | + |
| 180 | + |
| 181 | +def test_v1_untag(dummy_task_v1, use_api_v1): |
| 182 | + _untag(dummy_task_v1) |
| 183 | + |
| 184 | + |
| 185 | +def test_v2_publish_not_supported(dummy_task_v2, use_api_v2): |
| 186 | + with pytest.raises(OpenMLNotSupportedError): |
| 187 | + _publish(dummy_task_v2) |
| 188 | + |
| 189 | + |
| 190 | +def test_v2_delete_not_supported(dummy_task_v2, use_api_v2): |
| 191 | + with pytest.raises(OpenMLNotSupportedError): |
| 192 | + _delete(dummy_task_v2) |
| 193 | + |
| 194 | + |
| 195 | +def test_v2_tag_not_supported(dummy_task_v2, use_api_v2): |
| 196 | + with pytest.raises(OpenMLNotSupportedError): |
| 197 | + _tag(dummy_task_v2) |
| 198 | + |
| 199 | + |
| 200 | +def test_v2_untag_not_supported(dummy_task_v2, use_api_v2): |
| 201 | + with pytest.raises(OpenMLNotSupportedError): |
| 202 | + _untag(dummy_task_v2) |
| 203 | + |
| 204 | + |
| 205 | +def test_fallback_publish(dummy_task_fallback, use_api_v1): |
| 206 | + _publish(dummy_task_fallback) |
| 207 | + |
| 208 | + |
| 209 | +def test_fallback_delete(dummy_task_fallback, use_api_v1): |
| 210 | + _delete(dummy_task_fallback) |
| 211 | + |
| 212 | + |
| 213 | +def test_fallback_tag(dummy_task_fallback, use_api_v1): |
| 214 | + _tag(dummy_task_fallback) |
174 | 215 |
|
175 | | - def test_tag(self): |
176 | | - self._tag() |
177 | 216 |
|
178 | | - def test_untag(self): |
179 | | - self._untag() |
| 217 | +def test_fallback_untag(dummy_task_fallback, use_api_v1): |
| 218 | + _untag(dummy_task_fallback) |
0 commit comments