From 7d6cf286f236a5a73cd3e2755b11c3ea4f90d1ff Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Thu, 27 Jun 2019 13:23:39 +0200 Subject: [PATCH 1/6] Collecting and cleaning unit test dump --- openml/testing.py | 19 +++++++++++++++++++ tests/test_datasets/test_dataset_functions.py | 14 +++++++++++--- tests/test_tasks/test_split.py | 6 +++++- tests/test_tasks/test_task_functions.py | 8 ++++++++ tests/test_tasks/test_task_methods.py | 9 +++++++++ 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/openml/testing.py b/openml/testing.py index 1ce0862d03..614764931a 100644 --- a/openml/testing.py +++ b/openml/testing.py @@ -103,6 +103,25 @@ def tearDown(self): openml.config.server = self.production_server openml.config.connection_n_retries = self.connection_n_retries + def _track_old_files(self): + files = os.walk(self.static_cache_dir) + self.old_file_list = [] + for root, _, filenames in files: + for filename in filenames: + self.old_file_list.append(os.path.join(root, filename)) + + def _remove_new_files(self): + files = os.walk(self.static_cache_dir) + self.new_file_list = [] + for root, _, filenames in files: + for filename in filenames: + self.new_file_list.append(os.path.join(root, filename)) + # filtering the files generated during this run + self.new_file_list = list(set(self.new_file_list) - + set(self.old_file_list)) + for file in self.new_file_list: + os.remove(file) + def _get_sentinel(self, sentinel=None): if sentinel is None: # Create a unique prefix for the flow. Necessary because the flow diff --git a/tests/test_datasets/test_dataset_functions.py b/tests/test_datasets/test_dataset_functions.py index 61aeb6904a..6d0f5fcf54 100644 --- a/tests/test_datasets/test_dataset_functions.py +++ b/tests/test_datasets/test_dataset_functions.py @@ -1,4 +1,5 @@ import os +import sys import random from itertools import product from unittest import mock @@ -6,6 +7,7 @@ import arff import pytest +import shutil import numpy as np import pandas as pd import scipy.sparse @@ -44,18 +46,24 @@ def tearDown(self): def _remove_pickle_files(self): cache_dir = self.static_cache_dir + self.lock_path = os.path.join(openml.config.get_cache_directory(), 'locks') for did in ['-1', '2']: with lockutils.external_lock( name='datasets.functions.get_dataset:%s' % did, - lock_path=os.path.join(openml.config.get_cache_directory(), 'locks'), + lock_path=self.lock_path, ): - pickle_path = os.path.join(cache_dir, 'datasets', did, - 'dataset.pkl') + if sys.version[0] is '3': + pickle_path = os.path.join(openml.config.get_cache_directory(), 'datasets', + did, 'dataset.pkl.py3') + else: + pickle_path = os.path.join(openml.config.get_cache_directory(), 'datasets', + did, 'dataset.pkl') try: os.remove(pickle_path) except (OSError, FileNotFoundError): # Replaced a bare except. Not sure why either of these would be acceptable. pass + shutil.rmtree(self.lock_path, ignore_errors=True) def _get_empty_param_for_dataset(self): diff --git a/tests/test_tasks/test_split.py b/tests/test_tasks/test_split.py index 46c6564a13..583667e853 100644 --- a/tests/test_tasks/test_split.py +++ b/tests/test_tasks/test_split.py @@ -1,4 +1,5 @@ import inspect +import sys import os import numpy as np @@ -20,7 +21,10 @@ def setUp(self): "tasks", "1882", "datasplits.arff" ) # TODO Needs to be adapted regarding the python version - self.pd_filename = self.arff_filename.replace(".arff", ".pkl") + if sys.version[0] is '3': + self.pd_filename = self.arff_filename.replace(".arff", ".pkl.py3") + else: + self.pd_filename = self.arff_filename.replace(".arff", ".pkl") def tearDown(self): try: diff --git a/tests/test_tasks/test_task_functions.py b/tests/test_tasks/test_task_functions.py index dfdbd4847a..4a6dffd75e 100644 --- a/tests/test_tasks/test_task_functions.py +++ b/tests/test_tasks/test_task_functions.py @@ -12,6 +12,14 @@ class TestTask(TestBase): _multiprocess_can_split_ = True + def setUp(self): + super(TestTask, self).setUp() + self._track_old_files() + + def tearDown(self): + self._remove_new_files() + super(TestTask, self).tearDown() + def test__get_cached_tasks(self): openml.config.cache_directory = self.static_cache_dir tasks = openml.tasks.functions._get_cached_tasks() diff --git a/tests/test_tasks/test_task_methods.py b/tests/test_tasks/test_task_methods.py index 55cbba64b2..d6d33ce162 100644 --- a/tests/test_tasks/test_task_methods.py +++ b/tests/test_tasks/test_task_methods.py @@ -1,3 +1,4 @@ +import os from time import time import openml @@ -7,6 +8,14 @@ # Common methods between tasks class OpenMLTaskMethodsTest(TestBase): + def setUp(self): + super(OpenMLTaskMethodsTest, self).setUp() + self._track_old_files() + + def tearDown(self): + self._remove_new_files() + super(OpenMLTaskMethodsTest, self).tearDown() + def test_tagging(self): task = openml.tasks.get_task(1) tag = "testing_tag_{}_{}".format(self.id(), time()) From 299477a87edb8e0beb01e1e543fb756e94c82541 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Mon, 1 Jul 2019 16:45:15 +0200 Subject: [PATCH 2/6] Adding session level fixture with yield to delay deletion of files --- openml/testing.py | 55 ++++++++++++------- tests/test_datasets/test_dataset_functions.py | 7 +-- tests/test_tasks/test_split.py | 4 +- tests/test_tasks/test_task_functions.py | 4 +- tests/test_tasks/test_task_methods.py | 5 +- 5 files changed, 41 insertions(+), 34 deletions(-) diff --git a/openml/testing.py b/openml/testing.py index 614764931a..a333282e78 100644 --- a/openml/testing.py +++ b/openml/testing.py @@ -17,6 +17,41 @@ import openml from openml.tasks import TaskTypeEnum +import pytest + + +@pytest.fixture(scope="session", autouse=True) +def cleanup_fixture(): + """Cleans up files generated by Unit tests + + This function is called at the beginning of the invocation of + TestBase (defined below), by each of class that inherits TestBase. + The 'yield' creates a checkpoint and breaks away to continue running + the unit tests of the sub class. When all the tests end, execution + resumes from the checkpoint. + """ + # TODO: generalize this path better akin to static_cache_dir + directory = "{}/tests/files/".format(os.getcwd()) + files = os.walk(directory) + old_file_list = [] + for root, _, filenames in files: + for filename in filenames: + old_file_list.append(os.path.join(root, filename)) + # context switches to other remaining tests + # pauses the code execution here till all tests in the 'session' is over + yield + # resumes from here after all collected tests are completed + files = os.walk(directory) + new_file_list = [] + for root, _, filenames in files: + for filename in filenames: + new_file_list.append(os.path.join(root, filename)) + # filtering the files generated during this run + new_file_list = list(set(new_file_list) - set(old_file_list)) + print("Files to delete: {}".format(new_file_list)) + for file in new_file_list: + os.remove(file) + class TestBase(unittest.TestCase): """Base class for tests @@ -44,7 +79,6 @@ def setUp(self, n_levels: int = 1): Number of nested directories the test is in. Necessary to resolve the path to the ``files`` directory, which is located directly under the ``tests`` directory. """ - # This cache directory is checked in to git to simulate a populated # cache self.maxDiff = None @@ -103,25 +137,6 @@ def tearDown(self): openml.config.server = self.production_server openml.config.connection_n_retries = self.connection_n_retries - def _track_old_files(self): - files = os.walk(self.static_cache_dir) - self.old_file_list = [] - for root, _, filenames in files: - for filename in filenames: - self.old_file_list.append(os.path.join(root, filename)) - - def _remove_new_files(self): - files = os.walk(self.static_cache_dir) - self.new_file_list = [] - for root, _, filenames in files: - for filename in filenames: - self.new_file_list.append(os.path.join(root, filename)) - # filtering the files generated during this run - self.new_file_list = list(set(self.new_file_list) - - set(self.old_file_list)) - for file in self.new_file_list: - os.remove(file) - def _get_sentinel(self, sentinel=None): if sentinel is None: # Create a unique prefix for the flow. Necessary because the flow diff --git a/tests/test_datasets/test_dataset_functions.py b/tests/test_datasets/test_dataset_functions.py index 6d0f5fcf54..a88df795bd 100644 --- a/tests/test_datasets/test_dataset_functions.py +++ b/tests/test_datasets/test_dataset_functions.py @@ -7,7 +7,6 @@ import arff import pytest -import shutil import numpy as np import pandas as pd import scipy.sparse @@ -17,7 +16,7 @@ from openml import OpenMLDataset from openml.exceptions import OpenMLCacheException, OpenMLHashException, \ OpenMLPrivateDatasetError -from openml.testing import TestBase +from openml.testing import TestBase, cleanup_fixture from openml.utils import _tag_entity, _create_cache_directory_for_id from openml.datasets.functions import (create_dataset, attributes_arff_from_df, @@ -45,14 +44,13 @@ def tearDown(self): super(TestOpenMLDataset, self).tearDown() def _remove_pickle_files(self): - cache_dir = self.static_cache_dir self.lock_path = os.path.join(openml.config.get_cache_directory(), 'locks') for did in ['-1', '2']: with lockutils.external_lock( name='datasets.functions.get_dataset:%s' % did, lock_path=self.lock_path, ): - if sys.version[0] is '3': + if sys.version[0] == '3': pickle_path = os.path.join(openml.config.get_cache_directory(), 'datasets', did, 'dataset.pkl.py3') else: @@ -63,7 +61,6 @@ def _remove_pickle_files(self): except (OSError, FileNotFoundError): # Replaced a bare except. Not sure why either of these would be acceptable. pass - shutil.rmtree(self.lock_path, ignore_errors=True) def _get_empty_param_for_dataset(self): diff --git a/tests/test_tasks/test_split.py b/tests/test_tasks/test_split.py index 583667e853..92db5505e3 100644 --- a/tests/test_tasks/test_split.py +++ b/tests/test_tasks/test_split.py @@ -5,7 +5,7 @@ import numpy as np from openml import OpenMLSplit -from openml.testing import TestBase +from openml.testing import TestBase, cleanup_fixture class OpenMLSplitTest(TestBase): @@ -21,7 +21,7 @@ def setUp(self): "tasks", "1882", "datasplits.arff" ) # TODO Needs to be adapted regarding the python version - if sys.version[0] is '3': + if sys.version[0] == '3': self.pd_filename = self.arff_filename.replace(".arff", ".pkl.py3") else: self.pd_filename = self.arff_filename.replace(".arff", ".pkl") diff --git a/tests/test_tasks/test_task_functions.py b/tests/test_tasks/test_task_functions.py index 4a6dffd75e..3d2f08a6ab 100644 --- a/tests/test_tasks/test_task_functions.py +++ b/tests/test_tasks/test_task_functions.py @@ -1,7 +1,7 @@ import os from unittest import mock -from openml.testing import TestBase +from openml.testing import TestBase, cleanup_fixture from openml import OpenMLSplit, OpenMLTask from openml.exceptions import OpenMLCacheException import openml @@ -14,10 +14,8 @@ class TestTask(TestBase): def setUp(self): super(TestTask, self).setUp() - self._track_old_files() def tearDown(self): - self._remove_new_files() super(TestTask, self).tearDown() def test__get_cached_tasks(self): diff --git a/tests/test_tasks/test_task_methods.py b/tests/test_tasks/test_task_methods.py index d6d33ce162..0d28d3e8b7 100644 --- a/tests/test_tasks/test_task_methods.py +++ b/tests/test_tasks/test_task_methods.py @@ -1,8 +1,7 @@ -import os from time import time import openml -from openml.testing import TestBase +from openml.testing import TestBase, cleanup_fixture # Common methods between tasks @@ -10,10 +9,8 @@ class OpenMLTaskMethodsTest(TestBase): def setUp(self): super(OpenMLTaskMethodsTest, self).setUp() - self._track_old_files() def tearDown(self): - self._remove_new_files() super(OpenMLTaskMethodsTest, self).tearDown() def test_tagging(self): From 5ecafe8b0725b7b51095e264d466e2e804f96bb1 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Wed, 3 Jul 2019 15:20:23 +0200 Subject: [PATCH 3/6] Adding PEP8 ignore F401 --- ci_scripts/flake8_diff.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci_scripts/flake8_diff.sh b/ci_scripts/flake8_diff.sh index d745773410..696e8c7f34 100755 --- a/ci_scripts/flake8_diff.sh +++ b/ci_scripts/flake8_diff.sh @@ -3,5 +3,5 @@ # Update /CONTRIBUTING.md if these commands change. # The reason for not advocating using this script directly is that it # might not work out of the box on Windows. -flake8 --ignore E402,W503 --show-source --max-line-length 100 $options +flake8 --ignore E402,W503,F401 --show-source --max-line-length 100 $options mypy openml --ignore-missing-imports --follow-imports skip From 758aa3082931144051a234b1f7ffe56f6a38e261 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Wed, 3 Jul 2019 15:29:49 +0200 Subject: [PATCH 4/6] Changelog update + pytest argument fix --- ci_scripts/test.sh | 2 +- doc/progress.rst | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ci_scripts/test.sh b/ci_scripts/test.sh index 80b35f04f0..2a837583e4 100644 --- a/ci_scripts/test.sh +++ b/ci_scripts/test.sh @@ -22,7 +22,7 @@ run_tests() { PYTEST_ARGS='' fi - pytest -n 4 --duration=20 --timeout=600 --timeout-method=thread -sv --ignore='test_OpenMLDemo.py' $PYTEST_ARGS $test_dir + pytest -n 4 --durations=20 --timeout=600 --timeout-method=thread -sv --ignore='test_OpenMLDemo.py' $PYTEST_ARGS $test_dir } if [[ "$RUN_FLAKE8" == "true" ]]; then diff --git a/doc/progress.rst b/doc/progress.rst index 83e46d494d..bc00fd300a 100644 --- a/doc/progress.rst +++ b/doc/progress.rst @@ -11,6 +11,7 @@ Changelog * ADD #687: Adds a function to retrieve the list of evaluation measures available. * ADD #695: A function to retrieve all the data quality measures available. +* FIX #447: All files created by unit tests no longer persist. 0.9.0 ~~~~~ From fa776100f0dd98853c67c24a4fae9b7cec0b1e71 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Wed, 3 Jul 2019 17:49:00 +0200 Subject: [PATCH 5/6] Messy first draft of possible designs --- openml/testing.py | 109 +++++++++++++++++++++++ tests/test_flows/test_flow.py | 24 +++-- tests/test_flows/test_flow_functions.py | 4 +- tests/test_study/test_study_functions.py | 5 +- 4 files changed, 135 insertions(+), 7 deletions(-) diff --git a/openml/testing.py b/openml/testing.py index 1ce0862d03..f7070a8cf3 100644 --- a/openml/testing.py +++ b/openml/testing.py @@ -17,6 +17,54 @@ import openml from openml.tasks import TaskTypeEnum +import pytest + + +@pytest.fixture(scope="session") #, autouse=True) +def cleanup_fixture(): + """Cleans up files generated by Unit tests + + This function is called at the beginning of the invocation of + TestBase (defined below), by each of class that inherits TestBase. + The 'yield' creates a checkpoint and breaks away to continue running + the unit tests of the sub class. When all the tests end, execution + resumes from the checkpoint. + """ + # TODO: generalize this path better akin to static_cache_dir + directory = "{}/tests/files/".format(os.getcwd()) + files = os.walk(directory) + old_file_list = [] + for root, _, filenames in files: + for filename in filenames: + old_file_list.append(os.path.join(root, filename)) + # context switches to other remaining tests + # pauses the code execution here till all tests in the 'session' is over + yield + # resumes from here after all collected tests are completed + files = os.walk(directory) + new_file_list = [] + for root, _, filenames in files: + for filename in filenames: + new_file_list.append(os.path.join(root, filename)) + # filtering the files generated during this run + new_file_list = list(set(new_file_list) - set(old_file_list)) + print("Files to delete: {}".format(new_file_list)) + for file in new_file_list: + os.remove(file) + + # Removing files from test server + print("TestBase.tracker: {}".format(TestBase.tracker)) + openml.config.server = TestBase.test_server + openml.config.apikey = TestBase.apikey + for entity_type in TestBase.tracker: + for i, entity in enumerate(TestBase.tracker[entity_type]): + try: + print("Deleting: {} {}". format(entity_type, entity)) + openml.utils._delete_entity(entity_type, entity) + TestBase.tracker[entity_type].pop(i) + except Exception as e: + print(e) + class TestBase(unittest.TestCase): """Base class for tests @@ -26,6 +74,61 @@ class TestBase(unittest.TestCase): Currently hard-codes a read-write key. Hopefully soon allows using a test server, not the production server. """ + tracker = {} + test_server = None + apikey = None + + @classmethod + def _track_test_server_dumps(self, entity_type, entity_id): + if entity_type not in TestBase.tracker: + TestBase.tracker[entity_type] = [entity_id] + else: + TestBase.tracker[entity_type].append(entity_id) + + @pytest.fixture(scope="session", autouse=True) + def _cleanup_fixture(self): + """Cleans up files generated by Unit tests + + This function is called at the beginning of the invocation of + TestBase (defined below), by each of class that inherits TestBase. + The 'yield' creates a checkpoint and breaks away to continue running + the unit tests of the sub class. When all the tests end, execution + resumes from the checkpoint. + """ + # TODO: generalize this path better akin to static_cache_dir + directory = "{}/tests/files/".format(os.getcwd()) + files = os.walk(directory) + old_file_list = [] + for root, _, filenames in files: + for filename in filenames: + old_file_list.append(os.path.join(root, filename)) + # context switches to other remaining tests + # pauses the code execution here till all tests in the 'session' is over + yield + # resumes from here after all collected tests are completed + files = os.walk(directory) + new_file_list = [] + for root, _, filenames in files: + for filename in filenames: + new_file_list.append(os.path.join(root, filename)) + # filtering the files generated during this run + new_file_list = list(set(new_file_list) - set(old_file_list)) + print("Files to delete: {}".format(new_file_list)) + for file in new_file_list: + os.remove(file) + + # Removing files from test server + print("TestBase.tracker: {}".format(TestBase.tracker)) + openml.config.server = TestBase.test_server + openml.config.apikey = TestBase.apikey + for entity_type in TestBase.tracker: + for i, entity in enumerate(TestBase.tracker[entity_type]): + try: + print("Deleting: {} {}". format(entity_type, entity)) + openml.utils._delete_entity(entity_type, entity) + TestBase.tracker[entity_type].pop(i) + except Exception as e: + print(e) def setUp(self, n_levels: int = 1): """Setup variables and temporary directories. @@ -75,6 +178,10 @@ def setUp(self, n_levels: int = 1): self.production_server = "https://openml.org/api/v1/xml" self.test_server = "https://test.openml.org/api/v1/xml" + # For global file deletion on test server + TestBase.test_server = self.test_server + TestBase.apikey = openml.config.apikey + openml.config.server = self.test_server openml.config.avoid_duplicate_runs = False openml.config.cache_directory = self.workdir @@ -91,6 +198,8 @@ def setUp(self, n_levels: int = 1): openml.config.connection_n_retries = 10 def tearDown(self): + print("\nTRACKER: {}".format(TestBase.tracker)) + os.chdir(self.cwd) try: shutil.rmtree(self.workdir) diff --git a/tests/test_flows/test_flow.py b/tests/test_flows/test_flow.py index 7b8c66cab2..8b72be9096 100644 --- a/tests/test_flows/test_flow.py +++ b/tests/test_flows/test_flow.py @@ -30,7 +30,7 @@ from openml._api_calls import _perform_api_call import openml.exceptions import openml.extensions.sklearn -from openml.testing import TestBase +from openml.testing import TestBase #, cleanup_fixture import openml.utils @@ -41,6 +41,10 @@ def setUp(self): super().setUp() self.extension = openml.extensions.sklearn.SklearnExtension() + def tearDown(self): + # TestBase.tracker.append(TestFlow.tracker) + super().tearDown() + def test_get_flow(self): # We need to use the production server here because 4024 is not the # test server @@ -176,7 +180,9 @@ def test_publish_flow(self): flow, _ = self._add_sentinel_to_flow_name(flow, None) - flow.publish() + temp = flow.publish() + self._track_test_server_dumps('flow', flow.flow_id) + print("\ntest_flow: {}".format(flow.flow_id)) self.assertIsInstance(flow.flow_id, int) @mock.patch('openml.flows.functions.flow_exists') @@ -186,7 +192,10 @@ def test_publish_existing_flow(self, flow_exists_mock): flow_exists_mock.return_value = 1 with self.assertRaises(openml.exceptions.PyOpenMLError) as context_manager: - flow.publish(raise_error_if_exists=True) + temp = flow.publish(raise_error_if_exists=True) + self._track_test_server_dumps('flow', flow.flow_id) + print("\ntest_flow: {}".format(flow.flow_id)) + print(TestFlow.tracker) self.assertTrue('OpenMLFlow already exists' in context_manager.exception.message) @@ -196,7 +205,9 @@ def test_publish_flow_with_similar_components(self): ]) flow = self.extension.model_to_flow(clf) flow, _ = self._add_sentinel_to_flow_name(flow, None) - flow.publish() + temp = flow.publish() + self._track_test_server_dumps('flow', flow.flow_id) + print("\ntest_flow: {}".format(flow.flow_id)) # For a flow where both components are published together, the upload # date should be equal self.assertEqual( @@ -212,7 +223,10 @@ def test_publish_flow_with_similar_components(self): clf1 = sklearn.tree.DecisionTreeClassifier(max_depth=2) flow1 = self.extension.model_to_flow(clf1) flow1, sentinel = self._add_sentinel_to_flow_name(flow1, None) - flow1.publish() + temp = flow1.publish() + self._track_test_server_dumps('flow', flow1.flow_id) + print("\ntest_flow: {}".format(flow.flow_id)) + print(TestFlow.tracker) # In order to assign different upload times to the flows! time.sleep(1) diff --git a/tests/test_flows/test_flow_functions.py b/tests/test_flows/test_flow_functions.py index 087623d3d3..32c8e1efb5 100644 --- a/tests/test_flows/test_flow_functions.py +++ b/tests/test_flows/test_flow_functions.py @@ -7,7 +7,7 @@ import pandas as pd import openml -from openml.testing import TestBase +from openml.testing import TestBase, cleanup_fixture import openml.extensions.sklearn @@ -251,6 +251,8 @@ def test_sklearn_to_flow_list_of_lists(self): # Test flow is accepted by server self._add_sentinel_to_flow_name(flow) flow.publish() + TestBase._track_test_server_dumps('flow', flow.flow_id) + print("\ntest_flow_functions: {}".format(flow.flow_id)) # Test deserialization works server_flow = openml.flows.get_flow(flow.flow_id, reinstantiate=True) diff --git a/tests/test_study/test_study_functions.py b/tests/test_study/test_study_functions.py index c87dd8e159..33725f6683 100644 --- a/tests/test_study/test_study_functions.py +++ b/tests/test_study/test_study_functions.py @@ -1,6 +1,6 @@ import openml import openml.study -from openml.testing import TestBase +from openml.testing import TestBase, cleanup_fixture import pandas as pd @@ -77,6 +77,9 @@ def test_publish_benchmark_suite(self): task_ids=fixture_task_ids ) study_id = study.publish() + self._track_test_server_dumps('study', study_id) + print("\ntest_study_functions: {}".format(study_id)) + self.assertGreater(study_id, 0) # verify main meta data From 2bc519e3a652d985a82d452186be48d5dfebcea8 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Thu, 4 Jul 2019 16:37:50 +0200 Subject: [PATCH 6/6] Leaner implementation without additional imports --- ci_scripts/flake8_diff.sh | 2 +- openml/testing.py | 65 +++++++++---------- tests/test_datasets/test_dataset_functions.py | 2 +- tests/test_tasks/test_split.py | 2 +- tests/test_tasks/test_task_functions.py | 2 +- tests/test_tasks/test_task_methods.py | 2 +- 6 files changed, 37 insertions(+), 38 deletions(-) diff --git a/ci_scripts/flake8_diff.sh b/ci_scripts/flake8_diff.sh index 696e8c7f34..d745773410 100755 --- a/ci_scripts/flake8_diff.sh +++ b/ci_scripts/flake8_diff.sh @@ -3,5 +3,5 @@ # Update /CONTRIBUTING.md if these commands change. # The reason for not advocating using this script directly is that it # might not work out of the box on Windows. -flake8 --ignore E402,W503,F401 --show-source --max-line-length 100 $options +flake8 --ignore E402,W503 --show-source --max-line-length 100 $options mypy openml --ignore-missing-imports --follow-imports skip diff --git a/openml/testing.py b/openml/testing.py index a333282e78..0162379f1a 100644 --- a/openml/testing.py +++ b/openml/testing.py @@ -20,39 +20,6 @@ import pytest -@pytest.fixture(scope="session", autouse=True) -def cleanup_fixture(): - """Cleans up files generated by Unit tests - - This function is called at the beginning of the invocation of - TestBase (defined below), by each of class that inherits TestBase. - The 'yield' creates a checkpoint and breaks away to continue running - the unit tests of the sub class. When all the tests end, execution - resumes from the checkpoint. - """ - # TODO: generalize this path better akin to static_cache_dir - directory = "{}/tests/files/".format(os.getcwd()) - files = os.walk(directory) - old_file_list = [] - for root, _, filenames in files: - for filename in filenames: - old_file_list.append(os.path.join(root, filename)) - # context switches to other remaining tests - # pauses the code execution here till all tests in the 'session' is over - yield - # resumes from here after all collected tests are completed - files = os.walk(directory) - new_file_list = [] - for root, _, filenames in files: - for filename in filenames: - new_file_list.append(os.path.join(root, filename)) - # filtering the files generated during this run - new_file_list = list(set(new_file_list) - set(old_file_list)) - print("Files to delete: {}".format(new_file_list)) - for file in new_file_list: - os.remove(file) - - class TestBase(unittest.TestCase): """Base class for tests @@ -62,6 +29,38 @@ class TestBase(unittest.TestCase): Hopefully soon allows using a test server, not the production server. """ + @pytest.fixture(scope="session", autouse=True) + def _cleanup_fixture(self): + """Cleans up files generated by Unit tests + + This function is called at the beginning of the invocation of + TestBase (defined below), by each of class that inherits TestBase. + The 'yield' creates a checkpoint and breaks away to continue running + the unit tests of the sub class. When all the tests end, execution + resumes from the checkpoint. + """ + # TODO: generalize this path better akin to static_cache_dir + directory = "{}/tests/files/".format(os.getcwd()) + files = os.walk(directory) + old_file_list = [] + for root, _, filenames in files: + for filename in filenames: + old_file_list.append(os.path.join(root, filename)) + # context switches to other remaining tests + # pauses the code execution here till all tests in the 'session' is over + yield + # resumes from here after all collected tests are completed + files = os.walk(directory) + new_file_list = [] + for root, _, filenames in files: + for filename in filenames: + new_file_list.append(os.path.join(root, filename)) + # filtering the files generated during this run + new_file_list = list(set(new_file_list) - set(old_file_list)) + print("Files to delete in local: {}".format(new_file_list)) + for file in new_file_list: + os.remove(file) + def setUp(self, n_levels: int = 1): """Setup variables and temporary directories. diff --git a/tests/test_datasets/test_dataset_functions.py b/tests/test_datasets/test_dataset_functions.py index a88df795bd..fb22709527 100644 --- a/tests/test_datasets/test_dataset_functions.py +++ b/tests/test_datasets/test_dataset_functions.py @@ -16,7 +16,7 @@ from openml import OpenMLDataset from openml.exceptions import OpenMLCacheException, OpenMLHashException, \ OpenMLPrivateDatasetError -from openml.testing import TestBase, cleanup_fixture +from openml.testing import TestBase from openml.utils import _tag_entity, _create_cache_directory_for_id from openml.datasets.functions import (create_dataset, attributes_arff_from_df, diff --git a/tests/test_tasks/test_split.py b/tests/test_tasks/test_split.py index 92db5505e3..bbb2a26000 100644 --- a/tests/test_tasks/test_split.py +++ b/tests/test_tasks/test_split.py @@ -5,7 +5,7 @@ import numpy as np from openml import OpenMLSplit -from openml.testing import TestBase, cleanup_fixture +from openml.testing import TestBase class OpenMLSplitTest(TestBase): diff --git a/tests/test_tasks/test_task_functions.py b/tests/test_tasks/test_task_functions.py index 3d2f08a6ab..f773752d55 100644 --- a/tests/test_tasks/test_task_functions.py +++ b/tests/test_tasks/test_task_functions.py @@ -1,7 +1,7 @@ import os from unittest import mock -from openml.testing import TestBase, cleanup_fixture +from openml.testing import TestBase from openml import OpenMLSplit, OpenMLTask from openml.exceptions import OpenMLCacheException import openml diff --git a/tests/test_tasks/test_task_methods.py b/tests/test_tasks/test_task_methods.py index 0d28d3e8b7..4a07894148 100644 --- a/tests/test_tasks/test_task_methods.py +++ b/tests/test_tasks/test_task_methods.py @@ -1,7 +1,7 @@ from time import time import openml -from openml.testing import TestBase, cleanup_fixture +from openml.testing import TestBase # Common methods between tasks