|
8 | 8 | from functools import partial |
9 | 9 |
|
10 | 10 | import pytest |
| 11 | +import joblib |
11 | 12 |
|
12 | 13 | import numpy as np |
13 | 14 | from sklearn.datasets import get_data_home |
|
23 | 24 | from sklearn.datasets import load_boston |
24 | 25 | from sklearn.datasets import load_wine |
25 | 26 | from sklearn.datasets.base import Bunch |
| 27 | +from sklearn.datasets.base import _refresh_cache |
26 | 28 | from sklearn.datasets.tests.test_common import check_return_X_y |
27 | 29 |
|
28 | 30 | from sklearn.externals._pilutil import pillow_installed |
@@ -276,3 +278,55 @@ def test_bunch_dir(): |
276 | 278 | # check that dir (important for autocomplete) shows attributes |
277 | 279 | data = load_iris() |
278 | 280 | assert "data" in dir(data) |
| 281 | + |
| 282 | + |
| 283 | +def test_refresh_cache(monkeypatch): |
| 284 | + # uses pytests monkeypatch fixture |
| 285 | + # https://docs.pytest.org/en/latest/monkeypatch.html |
| 286 | + |
| 287 | + def _load_warn(*args, **kwargs): |
| 288 | + # raise the warning from "externals.joblib.__init__.py" |
| 289 | + # this is raised when a file persisted by the old joblib is loaded now |
| 290 | + msg = ("sklearn.externals.joblib is deprecated in 0.21 and will be " |
| 291 | + "removed in 0.23. Please import this functionality directly " |
| 292 | + "from joblib, which can be installed with: pip install joblib. " |
| 293 | + "If this warning is raised when loading pickled models, you " |
| 294 | + "may need to re-serialize those models with scikit-learn " |
| 295 | + "0.21+.") |
| 296 | + warnings.warn(msg, DeprecationWarning) |
| 297 | + return 0 |
| 298 | + |
| 299 | + def _load_warn_unrelated(*args, **kwargs): |
| 300 | + warnings.warn("unrelated warning", DeprecationWarning) |
| 301 | + return 0 |
| 302 | + |
| 303 | + def _dump_safe(*args, **kwargs): |
| 304 | + pass |
| 305 | + |
| 306 | + def _dump_raise(*args, **kwargs): |
| 307 | + # this happens if the file is read-only and joblib.dump fails to write |
| 308 | + # on it. |
| 309 | + raise IOError() |
| 310 | + |
| 311 | + # test if the dataset spesific warning is raised if load raises the joblib |
| 312 | + # warning, and dump fails to dump with new joblib |
| 313 | + monkeypatch.setattr(joblib, "load", _load_warn) |
| 314 | + monkeypatch.setattr(joblib, "dump", _dump_raise) |
| 315 | + msg = "This dataset will stop being loadable in scikit-learn" |
| 316 | + with pytest.warns(DeprecationWarning, match=msg): |
| 317 | + _refresh_cache('test', 0) |
| 318 | + |
| 319 | + # make sure no warning is raised if load raises the warning, but dump |
| 320 | + # manages to dump the new data |
| 321 | + monkeypatch.setattr(joblib, "load", _load_warn) |
| 322 | + monkeypatch.setattr(joblib, "dump", _dump_safe) |
| 323 | + with pytest.warns(None) as warns: |
| 324 | + _refresh_cache('test', 0) |
| 325 | + assert len(warns) == 0 |
| 326 | + |
| 327 | + # test if an unrelated warning is still passed through and not suppressed |
| 328 | + # by _refresh_cache |
| 329 | + monkeypatch.setattr(joblib, "load", _load_warn_unrelated) |
| 330 | + monkeypatch.setattr(joblib, "dump", _dump_safe) |
| 331 | + with pytest.warns(DeprecationWarning, match="unrelated warning"): |
| 332 | + _refresh_cache('test', 0) |
0 commit comments