22import pytest
33from openml .testing import TestAPIBase
44from openml ._api import ResourceV1API , ResourceV2API , FallbackProxy
5- from openml .enums import ResourceType
5+ from openml .enums import ResourceType , APIVersion
66from openml .exceptions import OpenMLNotSupportedError
77
88
9- class TestResourceV1API (TestAPIBase ):
10- def setUp (self ):
11- super ().setUp ()
12- self .resource = ResourceV1API (self .http_client )
13- self .resource .resource_type = ResourceType .TASK
14-
15- @pytest .mark .uses_test_server ()
16- def test_publish_and_delete (self ):
9+ @pytest .mark .uses_test_server ()
10+ class TestResourceAPIBase (TestAPIBase ):
11+ def _publish_and_delete (self ):
1712 task_xml = """
1813 <oml:task_inputs xmlns:oml="http://openml.org/openml">
1914 <oml:task_type_id>5</oml:task_type_id>
@@ -22,30 +17,19 @@ def test_publish_and_delete(self):
2217 </oml:task_inputs>
2318 """
2419
25- task_id = None
26- try :
27- # Publish the task
28- task_id = self .resource .publish (
29- "task" ,
30- files = {"description" : task_xml },
31- )
32-
33- # Get the task to verify it exists
34- get_response = self .http_client .get (f"task/{ task_id } " )
35- self .assertEqual (get_response .status_code , 200 )
36-
37- finally :
38- # delete the task if it was created
39- if task_id is not None :
40- success = self .resource .delete (task_id )
41- self .assertTrue (success )
20+ task_id = self .resource .publish (
21+ "task" ,
22+ files = {"description" : task_xml },
23+ )
24+ self .assertIsNotNone (task_id )
4225
26+ success = self .resource .delete (task_id )
27+ self .assertTrue (success )
4328
44- @pytest .mark .uses_test_server ()
45- def test_tag_and_untag (self ):
29+ def _tag_and_untag (self ):
4630 resource_id = 1
4731 unique_indicator = str (time ()).replace ("." , "" )
48- tag = f"TestResourceV1API_test_tag_and_untag_ { unique_indicator } "
32+ tag = f"{ self . __class__ . __name__ } _test_tag_and_untag_ { unique_indicator } "
4933
5034 tags = self .resource .tag (resource_id , tag )
5135 self .assertIn (tag , tags )
@@ -54,52 +38,51 @@ def test_tag_and_untag(self):
5438 self .assertNotIn (tag , tags )
5539
5640
57- class TestResourceV2API ( TestResourceV1API ):
41+ class TestResourceV1API ( TestResourceAPIBase ):
5842 def setUp (self ):
5943 super ().setUp ()
60-
61- self .server = ""
62- self .base_url = ""
63- self .api_key = ""
64- self .http_client = self ._get_http_client (
65- server = self .server ,
66- base_url = self .base_url ,
67- api_key = self .api_key ,
68- retries = self .retries ,
69- retry_policy = self .retry_policy ,
70- cache = self .cache ,
71- )
72-
73- self .resource = ResourceV2API (self .http_client )
44+ http_client = self .http_clients [APIVersion .V1 ]
45+ self .resource = ResourceV1API (http_client )
7446 self .resource .resource_type = ResourceType .TASK
7547
76- @pytest .mark .xfail (raises = OpenMLNotSupportedError )
7748 def test_publish_and_delete (self ):
78- super ().test_tag_and_untag ()
79-
49+ self ._publish_and_delete ()
8050
81- @pytest .mark .xfail (raises = OpenMLNotSupportedError )
8251 def test_tag_and_untag (self ):
83- super (). test_tag_and_untag ()
52+ self . _tag_and_untag ()
8453
8554
86- class TestResourceFallbackAPI ( TestResourceV1API ):
55+ class TestResourceV2API ( TestResourceAPIBase ):
8756 def setUp (self ):
8857 super ().setUp ()
58+ http_client = self .http_clients [APIVersion .V2 ]
59+ self .resource = ResourceV2API (http_client )
60+ self .resource .resource_type = ResourceType .TASK
61+
62+ def test_publish_and_delete (self ):
63+ with pytest .raises (OpenMLNotSupportedError ):
64+ self ._tag_and_untag ()
65+
66+ def test_tag_and_untag (self ):
67+ with pytest .raises (OpenMLNotSupportedError ):
68+ self ._tag_and_untag ()
8969
90- self .http_client_v2 = self ._get_http_client (
91- server = "" ,
92- base_url = "" ,
93- api_key = "" ,
94- retries = self .retries ,
95- retry_policy = self .retry_policy ,
96- cache = self .cache ,
97- )
9870
99- resource_v1 = ResourceV1API (self .http_client )
71+ class TestResourceFallbackAPI (TestResourceAPIBase ):
72+ def setUp (self ):
73+ super ().setUp ()
74+ http_client_v1 = self .http_clients [APIVersion .V1 ]
75+ resource_v1 = ResourceV1API (http_client_v1 )
10076 resource_v1 .resource_type = ResourceType .TASK
10177
102- resource_v2 = ResourceV2API (self .http_client_v2 )
78+ http_client_v2 = self .http_clients [APIVersion .V2 ]
79+ resource_v2 = ResourceV2API (http_client_v2 )
10380 resource_v2 .resource_type = ResourceType .TASK
10481
10582 self .resource = FallbackProxy (resource_v2 , resource_v1 )
83+
84+ def test_publish_and_delete (self ):
85+ self ._publish_and_delete ()
86+
87+ def test_tag_and_untag (self ):
88+ self ._tag_and_untag ()
0 commit comments