Coverage for tests / unit / nbutils / test_configure_notebook.py: 76%
70 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-18 02:51 -0700
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-18 02:51 -0700
1import os
2import sys
3import warnings
5import matplotlib.pyplot as plt # type: ignore[import]
6import pytest
8from muutils.nbutils.configure_notebook import (
9 UnknownFigureFormatWarning,
10 plotshow,
11 setup_plots,
12)
14JUNK_DATA_PATH: str = "tests/_temp/test_cfg_notebook"
17@pytest.mark.parametrize(
18 "plot_mode",
19 [
20 # "inline", # cant use outside a jupyter notebook
21 "widget",
22 "ignore",
23 ],
24)
25def test_setup_plots_donothing(plot_mode):
26 setup_plots(plot_mode=plot_mode)
29def test_no_inline_outside_nb():
30 from muutils.nbutils.configure_notebook import configure_notebook
32 with pytest.raises(RuntimeError):
33 configure_notebook(plot_mode="inline")
36def test_setup_plots_save():
37 setup_plots(plot_mode="save", fig_basepath=JUNK_DATA_PATH)
38 assert os.path.exists(JUNK_DATA_PATH)
41def test_plotshow_save():
42 setup_plots(plot_mode="save", fig_basepath=JUNK_DATA_PATH)
43 with pytest.warns(UnknownFigureFormatWarning):
44 plt.plot([1, 2, 3], [1, 2, 3])
45 plotshow()
46 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "figure-1.pdf"))
47 with pytest.warns(UnknownFigureFormatWarning):
48 plt.plot([3, 6, 9], [2, 4, 8])
49 plotshow()
50 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "figure-2.pdf"))
53def test_plotshow_save_named():
54 setup_plots(plot_mode="save", fig_basepath=JUNK_DATA_PATH)
55 plt.plot([1, 2, 3], [1, 2, 3])
56 plotshow(fname="test.pdf")
57 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "test.pdf"))
58 plt.plot([3, 6, 9], [2, 4, 8])
59 plotshow(fname="another-test.pdf")
60 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "another-test.pdf"))
63def test_plotshow_save_mixed():
64 setup_plots(
65 plot_mode="save",
66 fig_basepath=JUNK_DATA_PATH,
67 fig_numbered_fname="mixedfig-{num}",
68 )
69 with pytest.warns(UnknownFigureFormatWarning):
70 plt.plot([1, 2, 3], [1, 2, 3])
71 plotshow()
72 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "mixedfig-1.pdf"))
73 plt.plot([3, 6, 9], [2, 4, 8])
74 plotshow(fname="mixed-test.pdf")
75 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "mixed-test.pdf"))
76 with pytest.warns(UnknownFigureFormatWarning):
77 plt.plot([1, 1, 1], [1, 9, 9])
78 plotshow()
79 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "mixedfig-3.pdf"))
82def test_warn_unknown_format():
83 with pytest.warns(UnknownFigureFormatWarning):
84 setup_plots(
85 plot_mode="save",
86 fig_basepath=JUNK_DATA_PATH,
87 fig_numbered_fname="mixedfig-{num}",
88 )
89 plt.plot([1, 2, 3], [1, 2, 3])
90 plotshow()
93def test_no_warn_unknown_format_2():
94 with pytest.warns(UnknownFigureFormatWarning):
95 setup_plots(
96 plot_mode="save",
97 fig_basepath=JUNK_DATA_PATH,
98 fig_numbered_fname="mixedfig-{num}",
99 )
100 plt.plot([1, 2, 3], [1, 2, 3])
101 plotshow("no-format")
104def test_no_warn_pdf_format():
105 with warnings.catch_warnings():
106 warnings.simplefilter("error")
107 # Filter matplotlib/Pillow internal deprecation on Python 3.9
108 if sys.version_info[:2] == (3, 9):
109 warnings.filterwarnings(
110 "ignore", message="'mode' parameter", category=DeprecationWarning
111 )
112 setup_plots(
113 plot_mode="save",
114 fig_basepath=JUNK_DATA_PATH,
115 fig_numbered_fname="fig-{num}.pdf",
116 )
117 plt.plot([1, 2, 3], [1, 2, 3])
118 plotshow()
121def test_plotshow_ignore():
122 setup_plots(plot_mode="ignore")
123 plt.plot([1, 2, 3], [1, 2, 3])
124 plotshow()
125 # this should do nothing