-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_plotting.py
More file actions
269 lines (214 loc) · 9.11 KB
/
test_plotting.py
File metadata and controls
269 lines (214 loc) · 9.11 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""Tests for sovereign_debt_py.plotting"""
import datetime
import pytest
import matplotlib
matplotlib.use("Agg") # headless – must be set before importing pyplot
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.axes import Axes
import sovereign_debt_py.plotting as sdp
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _close(fig):
plt.close(fig)
# ---------------------------------------------------------------------------
# plot_yield_curve
# ---------------------------------------------------------------------------
class TestPlotYieldCurve:
TENORS = [1, 2, 5, 10]
YIELDS = [0.04, 0.045, 0.05, 0.052]
def test_returns_fig_ax(self):
fig, ax = sdp.plot_yield_curve(self.TENORS, self.YIELDS)
assert isinstance(fig, Figure)
assert isinstance(ax, Axes)
_close(fig)
def test_title_applied(self):
fig, ax = sdp.plot_yield_curve(
self.TENORS, self.YIELDS, title="Sovereign Curve"
)
assert ax.get_title() == "Sovereign Curve"
_close(fig)
def test_length_mismatch_raises(self):
with pytest.raises(ValueError, match="same length"):
sdp.plot_yield_curve([1, 2, 3], [0.04, 0.05])
def test_invalid_style_raises(self):
with pytest.raises(ValueError, match="style"):
sdp.plot_yield_curve(self.TENORS, self.YIELDS, style="bad_style")
def test_all_styles_run(self):
for style in ("line", "markers", "line+markers"):
fig, ax = sdp.plot_yield_curve(self.TENORS, self.YIELDS, style=style)
_close(fig)
def test_uses_supplied_axes(self):
fig, ax = plt.subplots()
fig2, ax2 = sdp.plot_yield_curve(self.TENORS, self.YIELDS, fig=fig, ax=ax)
assert fig2 is fig
assert ax2 is ax
_close(fig)
# ---------------------------------------------------------------------------
# plot_timeseries
# ---------------------------------------------------------------------------
class TestPlotTimeseries:
DATES = [
datetime.date(2023, 1, 1),
datetime.date(2023, 2, 1),
datetime.date(2023, 3, 1),
datetime.date(2023, 4, 1),
]
VALUES = [0.04, 0.041, 0.042, 0.043]
def test_returns_fig_ax(self):
fig, ax = sdp.plot_timeseries(self.DATES, self.VALUES)
assert isinstance(fig, Figure)
assert isinstance(ax, Axes)
_close(fig)
def test_iso_string_dates(self):
dates = ["2023-01-01", "2023-02-01", "2023-03-01"]
fig, ax = sdp.plot_timeseries(dates, [0.04, 0.041, 0.042])
assert isinstance(fig, Figure)
_close(fig)
def test_datetime_objects(self):
dates = [datetime.datetime(2023, 1, d) for d in [1, 15, 28]]
fig, ax = sdp.plot_timeseries(dates, [1.0, 2.0, 3.0])
assert isinstance(ax, Axes)
_close(fig)
def test_length_mismatch_raises(self):
with pytest.raises(ValueError, match="same length"):
sdp.plot_timeseries(self.DATES, [0.04, 0.05])
# ---------------------------------------------------------------------------
# plot_rolling_average
# ---------------------------------------------------------------------------
class TestPlotRollingAverage:
DATES = [datetime.date(2023, m, 1) for m in range(1, 13)]
VALUES = list(range(12))
def test_returns_fig_ax(self):
fig, ax = sdp.plot_rolling_average(self.DATES, self.VALUES, window=3)
assert isinstance(fig, Figure)
assert isinstance(ax, Axes)
_close(fig)
def test_custom_labels(self):
fig, ax = sdp.plot_rolling_average(
self.DATES,
self.VALUES,
window=3,
base_label="Raw",
roll_label="3M MA",
)
legend_texts = [t.get_text() for t in ax.get_legend().get_texts()]
assert "Raw" in legend_texts
assert "3M MA" in legend_texts
_close(fig)
def test_window_too_large_raises(self):
with pytest.raises(ValueError):
sdp.plot_rolling_average(self.DATES, self.VALUES, window=100)
def test_length_mismatch_raises(self):
with pytest.raises(ValueError, match="same length"):
sdp.plot_rolling_average(self.DATES, [0.1, 0.2], window=2)
# ---------------------------------------------------------------------------
# plot_spread
# ---------------------------------------------------------------------------
class TestPlotSpread:
DATES = [datetime.date(2023, 1, d) for d in range(1, 7)]
A = [0.05, 0.051, 0.052, 0.053, 0.054, 0.055]
B = [0.03, 0.031, 0.032, 0.033, 0.034, 0.035]
def test_returns_fig_ax(self):
fig, ax = sdp.plot_spread(self.DATES, self.A, self.B)
assert isinstance(fig, Figure)
assert isinstance(ax, Axes)
_close(fig)
def test_numeric_x_axis(self):
tenors = [1, 2, 5, 10, 20, 30]
fig, ax = sdp.plot_spread(tenors, self.A, self.B)
assert isinstance(fig, Figure)
_close(fig)
def test_length_mismatch_raises(self):
with pytest.raises(ValueError, match="same length"):
sdp.plot_spread(self.DATES, self.A, [0.03, 0.04])
# ---------------------------------------------------------------------------
# plot_fan_chart
# ---------------------------------------------------------------------------
class TestPlotFanChart:
X = list(range(2024, 2031))
P50 = [60.0, 62.0, 64.0, 63.0, 61.0, 60.0, 59.0]
BANDS = {
(0.10, 0.90): ([55.0] * 7, [70.0] * 7),
(0.25, 0.75): ([58.0] * 7, [66.0] * 7),
}
def test_returns_fig_ax(self):
fig, ax = sdp.plot_fan_chart(self.X, self.P50, self.BANDS)
assert isinstance(fig, Figure)
assert isinstance(ax, Axes)
_close(fig)
def test_title_applied(self):
fig, ax = sdp.plot_fan_chart(
self.X, self.P50, self.BANDS, title="DSA Fan"
)
assert ax.get_title() == "DSA Fan"
_close(fig)
def test_length_mismatch_raises(self):
with pytest.raises(ValueError, match="same length"):
sdp.plot_fan_chart(
self.X,
[60.0] * 5, # wrong length
{},
)
# ---------------------------------------------------------------------------
# fig_to_png_bytes
# ---------------------------------------------------------------------------
class TestFigToPngBytes:
PNG_MAGIC = b"\x89PNG"
def test_returns_png_bytes(self):
fig, ax = sdp.plot_yield_curve([1, 2, 5, 10], [0.04, 0.045, 0.05, 0.052])
png = sdp.fig_to_png_bytes(fig)
assert isinstance(png, bytes)
assert len(png) > 0
assert png[:4] == self.PNG_MAGIC
_close(fig)
def test_custom_dimensions(self):
fig, _ = sdp.plot_yield_curve([1, 2, 5], [0.03, 0.04, 0.05])
png = sdp.fig_to_png_bytes(fig, width_px=400, height_px=300, dpi=72)
assert png[:4] == self.PNG_MAGIC
_close(fig)
def test_close_flag_closes_figure(self):
fig, _ = sdp.plot_yield_curve([1, 2, 5], [0.03, 0.04, 0.05])
sdp.fig_to_png_bytes(fig, close=True)
# After close=True the figure should no longer be in plt.get_fignums()
assert fig.number not in plt.get_fignums()
def test_timeseries_to_png(self):
dates = ["2023-01-01", "2023-06-01", "2023-12-01"]
fig, _ = sdp.plot_timeseries(dates, [0.04, 0.05, 0.045])
png = sdp.fig_to_png_bytes(fig)
assert png[:4] == self.PNG_MAGIC
_close(fig)
# ---------------------------------------------------------------------------
# Input validation utilities (direct)
# ---------------------------------------------------------------------------
class TestCoreValidation:
def test_to_1d_array_list(self):
from sovereign_debt_py.plotting.core import to_1d_array
import numpy as np
arr = to_1d_array([1.0, 2.0, 3.0])
assert arr.shape == (3,)
def test_to_1d_array_2d_raises(self):
from sovereign_debt_py.plotting.core import to_1d_array
import numpy as np
with pytest.raises(ValueError, match="1-D"):
to_1d_array(np.array([[1, 2], [3, 4]]))
def test_validate_same_length_ok(self):
from sovereign_debt_py.plotting.core import validate_same_length
import numpy as np
validate_same_length(np.array([1, 2, 3]), np.array([4, 5, 6])) # no error
def test_validate_same_length_fail(self):
from sovereign_debt_py.plotting.core import validate_same_length
import numpy as np
with pytest.raises(ValueError, match="same length"):
validate_same_length(np.array([1, 2, 3]), np.array([4, 5]))
def test_coerce_dates_iso_string(self):
from sovereign_debt_py.plotting.core import coerce_dates
result = coerce_dates(["2023-01-15", "2023-06-30"])
assert result[0].year == 2023
assert result[0].month == 1
assert result[0].day == 15
def test_coerce_dates_invalid_raises(self):
from sovereign_debt_py.plotting.core import coerce_dates
with pytest.raises(ValueError):
coerce_dates(["not-a-date"])