-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_analysis_viz_base.py
More file actions
96 lines (79 loc) · 4.09 KB
/
test_analysis_viz_base.py
File metadata and controls
96 lines (79 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""Tests for analysis/viz_base.py — safe_savefig."""
from unittest.mock import MagicMock, patch
import analysis.viz_base as viz_base
# ── MATPLOTLIB_AVAILABLE ───────────────────────────────────────────────────
class TestMatplotlibAvailable:
def test_is_bool(self):
assert isinstance(viz_base.MATPLOTLIB_AVAILABLE, bool)
# ── safe_savefig — matplotlib unavailable ─────────────────────────────────
class TestSavefigMatplotlibUnavailable:
def test_returns_none_when_unavailable(self, tmp_path):
with patch.object(viz_base, 'MATPLOTLIB_AVAILABLE', False), \
patch.object(viz_base, 'plt', None):
result = viz_base.safe_savefig(tmp_path / "out.png")
assert result is None
def test_no_file_created_when_unavailable(self, tmp_path):
out = tmp_path / "out.png"
with patch.object(viz_base, 'MATPLOTLIB_AVAILABLE', False), \
patch.object(viz_base, 'plt', None):
viz_base.safe_savefig(out)
assert not out.exists()
# ── safe_savefig — matplotlib available ───────────────────────────────────
class TestSavefigMatplotlibAvailable:
def _mock_plt(self):
mock = MagicMock()
mock.savefig = MagicMock()
mock.close = MagicMock()
return mock
def test_returns_string_path_on_success(self, tmp_path):
mock_plt = self._mock_plt()
out = tmp_path / "fig.png"
with patch.object(viz_base, 'MATPLOTLIB_AVAILABLE', True), \
patch.object(viz_base, 'plt', mock_plt):
result = viz_base.safe_savefig(out)
assert result == str(out)
def test_savefig_called_with_correct_path(self, tmp_path):
mock_plt = self._mock_plt()
out = tmp_path / "fig.png"
with patch.object(viz_base, 'MATPLOTLIB_AVAILABLE', True), \
patch.object(viz_base, 'plt', mock_plt):
viz_base.safe_savefig(out, dpi=150)
mock_plt.savefig.assert_called_once_with(str(out), dpi=150, bbox_inches='tight')
def test_close_called_on_success(self, tmp_path):
mock_plt = self._mock_plt()
with patch.object(viz_base, 'MATPLOTLIB_AVAILABLE', True), \
patch.object(viz_base, 'plt', mock_plt):
viz_base.safe_savefig(tmp_path / "fig.png")
mock_plt.close.assert_called()
def test_close_called_even_on_savefig_failure(self, tmp_path):
"""plt.close must be called even when plt.savefig raises."""
mock_plt = self._mock_plt()
mock_plt.savefig.side_effect = RuntimeError("disk full")
out = tmp_path / "fig.png"
with patch.object(viz_base, 'MATPLOTLIB_AVAILABLE', True), \
patch.object(viz_base, 'plt', mock_plt):
result = viz_base.safe_savefig(out)
assert result is None
mock_plt.close.assert_called()
def test_returns_none_on_savefig_failure(self, tmp_path):
mock_plt = self._mock_plt()
mock_plt.savefig.side_effect = OSError("permission denied")
with patch.object(viz_base, 'MATPLOTLIB_AVAILABLE', True), \
patch.object(viz_base, 'plt', mock_plt):
result = viz_base.safe_savefig(tmp_path / "fig.png")
assert result is None
def test_parent_directory_created(self, tmp_path):
mock_plt = self._mock_plt()
nested = tmp_path / "a" / "b" / "fig.png"
with patch.object(viz_base, 'MATPLOTLIB_AVAILABLE', True), \
patch.object(viz_base, 'plt', mock_plt):
viz_base.safe_savefig(nested)
assert nested.parent.exists()
def test_custom_logger_used(self, tmp_path):
mock_plt = self._mock_plt()
custom_log = MagicMock()
out = tmp_path / "fig.png"
with patch.object(viz_base, 'MATPLOTLIB_AVAILABLE', True), \
patch.object(viz_base, 'plt', mock_plt):
viz_base.safe_savefig(out, log=custom_log)
custom_log.info.assert_called()