Skip to content

Commit c5598e0

Browse files
committed
create static_cache_dir and workdir
1 parent d36593d commit c5598e0

2 files changed

Lines changed: 95 additions & 54 deletions

File tree

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import openml
3838
from openml.testing import TestBase
3939

40+
import inspect
4041

4142
# creating logger for unit test file deletion status
4243
logger = logging.getLogger("unit_tests")
@@ -288,3 +289,31 @@ def with_test_cache(test_files_directory, request):
288289
openml.config.set_root_cache_directory(_root_cache_directory)
289290
if tmp_cache.exists():
290291
shutil.rmtree(tmp_cache)
292+
293+
294+
def find_test_files_dir(start_path: Path, max_levels: int = 1) -> Path:
295+
"""
296+
Starting from start_path, climb up to max_levels parents looking for 'files' directory.
297+
Returns the Path to the 'files' directory if found.
298+
Raises FileNotFoundError if not found within max_levels parents.
299+
"""
300+
current = start_path.resolve()
301+
for _ in range(max_levels):
302+
candidate = current / "files"
303+
if candidate.is_dir():
304+
return candidate
305+
current = current.parent
306+
raise FileNotFoundError(f"Cannot find 'files' directory within {max_levels} levels up from {start_path}")
307+
308+
@pytest.fixture
309+
def static_cache_dir():
310+
311+
start_path = Path(__file__).parent
312+
return find_test_files_dir(start_path)
313+
314+
@pytest.fixture
315+
def workdir(tmp_path):
316+
original_cwd = os.getcwd()
317+
os.chdir(tmp_path)
318+
yield tmp_path
319+
os.chdir(original_cwd)

tests/test_datasets/test_dataset.py

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from openml.exceptions import PyOpenMLError
1616
from openml.testing import TestBase
1717

18+
import pytest
1819

1920
@pytest.mark.production()
2021
class OpenMLDatasetTest(TestBase):
@@ -399,60 +400,7 @@ def test_get_sparse_categorical_data_id_395(self):
399400

400401

401402
class OpenMLDatasetFunctionTest(TestBase):
402-
@unittest.mock.patch("openml.datasets.dataset.pickle")
403-
@unittest.mock.patch("openml.datasets.dataset._get_features_pickle_file")
404-
def test__read_features(self, filename_mock, pickle_mock):
405-
"""Test we read the features from the xml if no cache pickle is available.
406-
407-
This test also does some simple checks to verify that the features are read correctly
408-
"""
409-
filename_mock.return_value = os.path.join(self.workdir, "features.xml.pkl")
410-
pickle_mock.load.side_effect = FileNotFoundError
411-
features = openml.datasets.dataset._read_features(
412-
os.path.join(
413-
self.static_cache_dir,
414-
"org",
415-
"openml",
416-
"test",
417-
"datasets",
418-
"2",
419-
"features.xml",
420-
),
421-
)
422-
assert isinstance(features, dict)
423-
assert len(features) == 39
424-
assert isinstance(features[0], OpenMLDataFeature)
425-
assert features[0].name == "family"
426-
assert len(features[0].nominal_values) == 9
427-
# pickle.load is never called because the features pickle file didn't exist
428-
assert pickle_mock.load.call_count == 0
429-
assert pickle_mock.dump.call_count == 1
430-
431-
@unittest.mock.patch("openml.datasets.dataset.pickle")
432-
@unittest.mock.patch("openml.datasets.dataset._get_qualities_pickle_file")
433-
def test__read_qualities(self, filename_mock, pickle_mock):
434-
"""Test we read the qualities from the xml if no cache pickle is available.
435-
436-
This test also does some minor checks to ensure that the qualities are read correctly.
437-
"""
438-
filename_mock.return_value = os.path.join(self.workdir, "qualities.xml.pkl")
439-
pickle_mock.load.side_effect = FileNotFoundError
440-
qualities = openml.datasets.dataset._read_qualities(
441-
os.path.join(
442-
self.static_cache_dir,
443-
"org",
444-
"openml",
445-
"test",
446-
"datasets",
447-
"2",
448-
"qualities.xml",
449-
),
450-
)
451-
assert isinstance(qualities, dict)
452-
assert len(qualities) == 106
453-
# pickle.load is never called because the qualities pickle file didn't exist
454-
assert pickle_mock.load.call_count == 0
455-
assert pickle_mock.dump.call_count == 1
403+
456404

457405
def test__check_qualities(self):
458406
qualities = [{"oml:name": "a", "oml:value": "0.5"}]
@@ -466,3 +414,67 @@ def test__check_qualities(self):
466414
qualities = [{"oml:name": "a", "oml:value": None}]
467415
qualities = openml.datasets.dataset._check_qualities(qualities)
468416
assert qualities["a"] != qualities["a"]
417+
418+
419+
420+
def test__read_features(mocker, workdir, static_cache_dir):
421+
"""Test we read the features from the xml if no cache pickle is available.
422+
423+
This test also does some simple checks to verify that the features are read correctly
424+
"""
425+
filename_mock = mocker.patch("openml.datasets.dataset._get_features_pickle_file")
426+
pickle_mock = mocker.patch("openml.datasets.dataset.pickle")
427+
428+
filename_mock.return_value = os.path.join(workdir, "features.xml.pkl")
429+
pickle_mock.load.side_effect = FileNotFoundError
430+
431+
features = openml.datasets.dataset._read_features(
432+
os.path.join(
433+
static_cache_dir,
434+
"org",
435+
"openml",
436+
"test",
437+
"datasets",
438+
"2",
439+
"features.xml",
440+
),
441+
)
442+
assert isinstance(features, dict)
443+
assert len(features) == 39
444+
assert isinstance(features[0], OpenMLDataFeature)
445+
assert features[0].name == "family"
446+
assert len(features[0].nominal_values) == 9
447+
# pickle.load is never called because the features pickle file didn't exist
448+
assert pickle_mock.load.call_count == 0
449+
assert pickle_mock.dump.call_count == 1
450+
451+
452+
def test__read_qualities(static_cache_dir, workdir, mocker):
453+
"""Test we read the qualities from the xml if no cache pickle is available.
454+
455+
This test also does some minor checks to ensure that the qualities are read correctly.
456+
"""
457+
458+
filename_mock = mocker.patch("openml.datasets.dataset._get_qualities_pickle_file")
459+
pickle_mock = mocker.patch("openml.datasets.dataset.pickle")
460+
461+
filename_mock.return_value=os.path.join(workdir, "qualities.xml.pkl")
462+
pickle_mock.load.side_effect = FileNotFoundError
463+
464+
qualities = openml.datasets.dataset._read_qualities(
465+
os.path.join(
466+
static_cache_dir,
467+
"org",
468+
"openml",
469+
"test",
470+
"datasets",
471+
"2",
472+
"qualities.xml",
473+
),
474+
)
475+
assert isinstance(qualities, dict)
476+
assert len(qualities) == 106
477+
assert pickle_mock.load.call_count == 0
478+
assert pickle_mock.dump.call_count == 1
479+
480+

0 commit comments

Comments
 (0)