Coverage for tests/unit/nbutils/test_configure_notebook.py: 100%

67 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-28 17:24 +0000

1import os 

2import warnings 

3 

4import matplotlib.pyplot as plt # type: ignore[import] 

5import pytest 

6 

7from muutils.nbutils.configure_notebook import ( 

8 UnknownFigureFormatWarning, 

9 plotshow, 

10 setup_plots, 

11) 

12 

13JUNK_DATA_PATH: str = "tests/_temp/test_cfg_notebook" 

14 

15 

16@pytest.mark.parametrize( 

17 "plot_mode", 

18 [ 

19 # "inline", # cant use outside a jupyter notebook 

20 "widget", 

21 "ignore", 

22 ], 

23) 

24def test_setup_plots_donothing(plot_mode): 

25 setup_plots(plot_mode=plot_mode) 

26 

27 

28def test_no_inline_outside_nb(): 

29 from muutils.nbutils.configure_notebook import configure_notebook 

30 

31 with pytest.raises(RuntimeError): 

32 configure_notebook(plot_mode="inline") 

33 

34 

35def test_setup_plots_save(): 

36 setup_plots(plot_mode="save", fig_basepath=JUNK_DATA_PATH) 

37 assert os.path.exists(JUNK_DATA_PATH) 

38 

39 

40def test_plotshow_save(): 

41 setup_plots(plot_mode="save", fig_basepath=JUNK_DATA_PATH) 

42 with pytest.warns(UnknownFigureFormatWarning): 

43 plt.plot([1, 2, 3], [1, 2, 3]) 

44 plotshow() 

45 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "figure-1.pdf")) 

46 with pytest.warns(UnknownFigureFormatWarning): 

47 plt.plot([3, 6, 9], [2, 4, 8]) 

48 plotshow() 

49 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "figure-2.pdf")) 

50 

51 

52def test_plotshow_save_named(): 

53 setup_plots(plot_mode="save", fig_basepath=JUNK_DATA_PATH) 

54 plt.plot([1, 2, 3], [1, 2, 3]) 

55 plotshow(fname="test.pdf") 

56 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "test.pdf")) 

57 plt.plot([3, 6, 9], [2, 4, 8]) 

58 plotshow(fname="another-test.pdf") 

59 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "another-test.pdf")) 

60 

61 

62def test_plotshow_save_mixed(): 

63 setup_plots( 

64 plot_mode="save", 

65 fig_basepath=JUNK_DATA_PATH, 

66 fig_numbered_fname="mixedfig-{num}", 

67 ) 

68 with pytest.warns(UnknownFigureFormatWarning): 

69 plt.plot([1, 2, 3], [1, 2, 3]) 

70 plotshow() 

71 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "mixedfig-1.pdf")) 

72 plt.plot([3, 6, 9], [2, 4, 8]) 

73 plotshow(fname="mixed-test.pdf") 

74 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "mixed-test.pdf")) 

75 with pytest.warns(UnknownFigureFormatWarning): 

76 plt.plot([1, 1, 1], [1, 9, 9]) 

77 plotshow() 

78 assert os.path.exists(os.path.join(JUNK_DATA_PATH, "mixedfig-3.pdf")) 

79 

80 

81def test_warn_unknown_format(): 

82 with pytest.warns(UnknownFigureFormatWarning): 

83 setup_plots( 

84 plot_mode="save", 

85 fig_basepath=JUNK_DATA_PATH, 

86 fig_numbered_fname="mixedfig-{num}", 

87 ) 

88 plt.plot([1, 2, 3], [1, 2, 3]) 

89 plotshow() 

90 

91 

92def test_no_warn_unknown_format_2(): 

93 with pytest.warns(UnknownFigureFormatWarning): 

94 setup_plots( 

95 plot_mode="save", 

96 fig_basepath=JUNK_DATA_PATH, 

97 fig_numbered_fname="mixedfig-{num}", 

98 ) 

99 plt.plot([1, 2, 3], [1, 2, 3]) 

100 plotshow("no-format") 

101 

102 

103def test_no_warn_pdf_format(): 

104 with warnings.catch_warnings(): 

105 warnings.simplefilter("error") 

106 setup_plots( 

107 plot_mode="save", 

108 fig_basepath=JUNK_DATA_PATH, 

109 fig_numbered_fname="fig-{num}.pdf", 

110 ) 

111 plt.plot([1, 2, 3], [1, 2, 3]) 

112 plotshow() 

113 

114 

115def test_plotshow_ignore(): 

116 setup_plots(plot_mode="ignore") 

117 plt.plot([1, 2, 3], [1, 2, 3]) 

118 plotshow() 

119 # this should do nothing