diff --git a/tests/test_tasks/test_task.py b/tests/test_tasks/test_task.py index d6f8b8abd..fe7fa5f0e 100644 --- a/tests/test_tasks/test_task.py +++ b/tests/test_tasks/test_task.py @@ -1,6 +1,7 @@ import unittest from random import randint +from openml.exceptions import OpenMLServerException from openml.testing import TestBase from openml.datasets import ( get_dataset, @@ -42,16 +43,37 @@ def test_download_task(self): def test_upload_task(self): - dataset_id = self._get_compatible_rand_dataset() - # TODO consider implementing on the diff task types. - task = create_task( - task_type_id=self.task_type_id, - dataset_id=dataset_id, - target_name=self._get_random_feature(dataset_id), - estimation_procedure_id=self.estimation_procedure - ) + # We don't know if the task in question already exists, so we try a few times. Checking + # beforehand would not be an option because a concurrent unit test could potentially + # create the same task and make this unit test fail (i.e. getting a dataset and creating + # a task for it is not atomic). + for i in range(100): + try: + dataset_id = self._get_compatible_rand_dataset() + # TODO consider implementing on the diff task types. + task = create_task( + task_type_id=self.task_type_id, + dataset_id=dataset_id, + target_name=self._get_random_feature(dataset_id), + estimation_procedure_id=self.estimation_procedure + ) + + task_id = task.publish() + # success + break + except OpenMLServerException as e: + # Error code for 'task already exists' + # Should be 533 according to the docs + # (# https://www.openml.org/api_docs#!/task/post_task) + if e.code == 614: + continue + else: + raise e + else: + raise ValueError( + 'Could not create a valid task for task type ID {}'.format(self.task_type_id) + ) - task_id = task.publish() _delete_entity('task', task_id) def _get_compatible_rand_dataset(self) -> int: