Skip to content

Commit 4878729

Browse files
ianhiclaude
andcommitted
Refactor: Use pytest parametrize for format tests
Use pytest.mark.parametrize to reduce code duplication in format tests. Remove PGF test as LaTeX backend is not available in CI environment. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 317a1b8 commit 4878729

1 file changed

Lines changed: 13 additions & 82 deletions

File tree

tests/test_download.py

Lines changed: 13 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99
import pytest
1010

1111

12-
def test_send_save_buffer_respects_format():
12+
@pytest.mark.parametrize(
13+
"format_name,signature_check",
14+
[
15+
("png", lambda buf: len(buf) > 0 and buf[:8] == b'\x89PNG\r\n\x1a\n'),
16+
("pdf", lambda buf: buf[:4] == b'%PDF'),
17+
("svg", lambda buf: b'<?xml' in buf or b'<svg' in buf),
18+
]
19+
)
20+
def test_send_save_buffer_respects_format(format_name, signature_check):
1321
"""Test that _send_save_buffer respects savefig.format rcParam."""
1422
matplotlib.use('module://ipympl.backend_nbagg')
1523

16-
# Test PNG format (default)
17-
plt.rcParams['savefig.format'] = 'png'
24+
plt.rcParams['savefig.format'] = format_name
1825
fig, ax = plt.subplots()
1926
ax.plot([1, 2, 3], [1, 4, 2])
2027

@@ -30,61 +37,12 @@ def test_send_save_buffer_respects_format():
3037
# Check message format
3138
msg_data = json.loads(call_args[0][0]['data'])
3239
assert msg_data['type'] == 'save'
33-
assert msg_data['format'] == 'png'
40+
assert msg_data['format'] == format_name
3441

35-
# Check buffer is not empty
42+
# Check buffer signature
3643
buffers = call_args[1]['buffers']
3744
assert len(buffers) == 1
38-
assert len(buffers[0]) > 0
39-
40-
plt.close(fig)
41-
42-
43-
def test_send_save_buffer_respects_pdf_format():
44-
"""Test that _send_save_buffer respects PDF format."""
45-
matplotlib.use('module://ipympl.backend_nbagg')
46-
47-
plt.rcParams['savefig.format'] = 'pdf'
48-
fig, ax = plt.subplots()
49-
ax.plot([1, 2, 3], [1, 4, 2])
50-
51-
canvas = fig.canvas
52-
canvas.send = MagicMock()
53-
54-
canvas._send_save_buffer()
55-
56-
call_args = canvas.send.call_args
57-
msg_data = json.loads(call_args[0][0]['data'])
58-
assert msg_data['format'] == 'pdf'
59-
60-
# Verify buffer starts with PDF signature
61-
buffers = call_args[1]['buffers']
62-
assert buffers[0][:4] == b'%PDF'
63-
64-
plt.close(fig)
65-
66-
67-
def test_send_save_buffer_respects_svg_format():
68-
"""Test that _send_save_buffer respects SVG format."""
69-
matplotlib.use('module://ipympl.backend_nbagg')
70-
71-
plt.rcParams['savefig.format'] = 'svg'
72-
fig, ax = plt.subplots()
73-
ax.plot([1, 2, 3], [1, 4, 2])
74-
75-
canvas = fig.canvas
76-
canvas.send = MagicMock()
77-
78-
canvas._send_save_buffer()
79-
80-
call_args = canvas.send.call_args
81-
msg_data = json.loads(call_args[0][0]['data'])
82-
assert msg_data['format'] == 'svg'
83-
84-
# Verify buffer contains SVG content
85-
buffers = call_args[1]['buffers']
86-
buffer_str = buffers[0].decode('utf-8')
87-
assert '<?xml' in buffer_str or '<svg' in buffer_str
45+
assert signature_check(buffers[0])
8846

8947
plt.close(fig)
9048

@@ -189,30 +147,3 @@ def test_send_save_buffer_respects_transparent():
189147
assert len(buffers[0]) > 0
190148

191149
plt.close(fig)
192-
193-
194-
def test_send_save_buffer_with_pgf_format():
195-
"""Test that _send_save_buffer works with PGF format."""
196-
matplotlib.use('module://ipympl.backend_nbagg')
197-
198-
# Test with PGF format (LaTeX graphics format)
199-
plt.rcParams['savefig.format'] = 'pgf'
200-
201-
fig, ax = plt.subplots()
202-
ax.plot([1, 2, 3], [1, 4, 2])
203-
204-
canvas = fig.canvas
205-
canvas.send = MagicMock()
206-
207-
# Should work without warnings
208-
canvas._send_save_buffer()
209-
210-
# Should send the buffer with format='pgf'
211-
assert canvas.send.called
212-
call_args = canvas.send.call_args
213-
assert 'data' in call_args[0][0]
214-
import json
215-
msg_data = json.loads(call_args[0][0]['data'])
216-
assert msg_data['format'] == 'pgf'
217-
218-
plt.close(fig)

0 commit comments

Comments
 (0)