|
8 | 8 |
|
9 | 9 | import pandas
|
10 | 10 |
|
| 11 | +dummy_backend = types.ModuleType("pandas_dummy_backend") |
| 12 | +dummy_backend.plot = lambda *args, **kwargs: None |
11 | 13 |
|
12 |
| -def test_matplotlib_backend_error(): |
13 |
| - msg = ( |
14 |
| - "matplotlib is required for plotting when the default backend " |
15 |
| - '"matplotlib" is selected.' |
16 |
| - ) |
17 |
| - try: |
18 |
| - import matplotlib # noqa |
19 |
| - except ImportError: |
20 |
| - with pytest.raises(ImportError, match=msg): |
21 |
| - pandas.set_option("plotting.backend", "matplotlib") |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def restore_backend(): |
| 17 | + """Restore the plotting backend to matplotlib""" |
| 18 | + pandas.set_option("plotting.backend", "matplotlib") |
| 19 | + yield |
| 20 | + pandas.set_option("plotting.backend", "matplotlib") |
22 | 21 |
|
23 | 22 |
|
24 | 23 | def test_backend_is_not_module():
|
25 |
| - msg = ( |
26 |
| - '"not_an_existing_module" does not seem to be an installed module. ' |
27 |
| - "A pandas plotting backend must be a module that can be imported" |
28 |
| - ) |
| 24 | + msg = "Could not find plotting backend 'not_an_existing_module'." |
29 | 25 | with pytest.raises(ValueError, match=msg):
|
30 | 26 | pandas.set_option("plotting.backend", "not_an_existing_module")
|
31 | 27 |
|
| 28 | + assert pandas.options.plotting.backend == "matplotlib" |
32 | 29 |
|
33 |
| -def test_backend_is_correct(monkeypatch): |
34 |
| - monkeypatch.setattr( |
35 |
| - "pandas.core.config_init.importlib.import_module", lambda name: None |
36 |
| - ) |
37 |
| - pandas.set_option("plotting.backend", "correct_backend") |
38 |
| - assert pandas.get_option("plotting.backend") == "correct_backend" |
39 | 30 |
|
40 |
| - # Restore backend for other tests (matplotlib can be not installed) |
41 |
| - try: |
42 |
| - pandas.set_option("plotting.backend", "matplotlib") |
43 |
| - except ImportError: |
44 |
| - pass |
| 31 | +def test_backend_is_correct(monkeypatch, restore_backend): |
| 32 | + monkeypatch.setitem(sys.modules, "pandas_dummy_backend", dummy_backend) |
| 33 | + |
| 34 | + pandas.set_option("plotting.backend", "pandas_dummy_backend") |
| 35 | + assert pandas.get_option("plotting.backend") == "pandas_dummy_backend" |
| 36 | + assert ( |
| 37 | + pandas.plotting._core._get_plot_backend("pandas_dummy_backend") is dummy_backend |
| 38 | + ) |
45 | 39 |
|
46 | 40 |
|
47 | 41 | @td.skip_if_no_mpl
|
48 |
| -def test_register_entrypoint(): |
| 42 | +def test_register_entrypoint(restore_backend): |
49 | 43 |
|
50 | 44 | dist = pkg_resources.get_distribution("pandas")
|
51 | 45 | if dist.module_path not in pandas.__file__:
|
@@ -74,13 +68,18 @@ def test_register_entrypoint():
|
74 | 68 | assert result is mod
|
75 | 69 |
|
76 | 70 |
|
77 |
| -def test_register_import(): |
78 |
| - mod = types.ModuleType("my_backend2") |
79 |
| - mod.plot = lambda *args, **kwargs: 1 |
80 |
| - sys.modules["my_backend2"] = mod |
| 71 | +def test_setting_backend_without_plot_raises(): |
| 72 | + # GH-28163 |
| 73 | + module = types.ModuleType("pandas_plot_backend") |
| 74 | + sys.modules["pandas_plot_backend"] = module |
81 | 75 |
|
82 |
| - result = pandas.plotting._core._get_plot_backend("my_backend2") |
83 |
| - assert result is mod |
| 76 | + assert pandas.options.plotting.backend == "matplotlib" |
| 77 | + with pytest.raises( |
| 78 | + ValueError, match="Could not find plotting backend 'pandas_plot_backend'." |
| 79 | + ): |
| 80 | + pandas.set_option("plotting.backend", "pandas_plot_backend") |
| 81 | + |
| 82 | + assert pandas.options.plotting.backend == "matplotlib" |
84 | 83 |
|
85 | 84 |
|
86 | 85 | @td.skip_if_mpl
|
|
0 commit comments