Skip to content

Commit 0b125d6

Browse files
authored
Merge pull request #1414 from openml/maint/to_pytest_test_dataset
convert static_cache_dir and workdir to fixture and test to pytest
2 parents dc4792c + 0f17918 commit 0b125d6

3 files changed

Lines changed: 75 additions & 55 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ test=[
8080
"mypy",
8181
"ruff",
8282
"requests-mock",
83+
"pytest-mock",
8384
]
8485
examples=[
8586
"matplotlib",

tests/conftest.py

Lines changed: 15 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")
@@ -292,3 +293,17 @@ def with_test_cache(test_files_directory, request):
292293
openml.config.set_root_cache_directory(_root_cache_directory)
293294
if tmp_cache.exists():
294295
shutil.rmtree(tmp_cache)
296+
297+
298+
299+
@pytest.fixture
300+
def static_cache_dir():
301+
302+
return Path(__file__).parent / "files"
303+
304+
@pytest.fixture
305+
def workdir(tmp_path):
306+
original_cwd = Path.cwd()
307+
os.chdir(tmp_path)
308+
yield tmp_path
309+
os.chdir(original_cwd)

tests/test_datasets/test_dataset.py

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

18+
import pytest
19+
1820

1921
@pytest.mark.production()
2022
class OpenMLDatasetTest(TestBase):
@@ -398,61 +400,63 @@ def test_get_sparse_categorical_data_id_395(self):
398400
assert len(feature.nominal_values) == 25
399401

400402

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

457461

458462

0 commit comments

Comments
 (0)