|
15 | 15 | from openml.exceptions import PyOpenMLError |
16 | 16 | from openml.testing import TestBase |
17 | 17 |
|
| 18 | +import pytest |
| 19 | + |
18 | 20 |
|
19 | 21 | @pytest.mark.production() |
20 | 22 | class OpenMLDatasetTest(TestBase): |
@@ -398,61 +400,63 @@ def test_get_sparse_categorical_data_id_395(self): |
398 | 400 | assert len(feature.nominal_values) == 25 |
399 | 401 |
|
400 | 402 |
|
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 |
456 | 460 |
|
457 | 461 |
|
458 | 462 |
|
|
0 commit comments