Skip to content

Commit c0c8b5c

Browse files
committed
fix and test image renderer
1 parent ef9b183 commit c0c8b5c

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Diff for: rest_pandas/renderers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from io import StringIO
1212

1313
import os
14+
from io import BytesIO
1415

1516

1617
class PandasBaseRenderer(BaseRenderer):
@@ -148,7 +149,7 @@ def get_pandas_kwargs(self, data, renderer_context):
148149
return {'ax': self.ax}
149150

150151
def get_output(self):
151-
data = StringIO()
152+
data = BytesIO()
152153
self.fig.savefig(data, format=self.format)
153154
return data.getvalue()
154155

Diff for: tests/test_images.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from rest_framework.test import APITestCase
2+
from tests.testapp.models import TimeSeries
3+
import unittest
4+
5+
try:
6+
import matplotlib
7+
except ImportError:
8+
matplotlib = None
9+
10+
11+
class ImageTestCase(APITestCase):
12+
def setUp(self):
13+
data = (
14+
('2014-01-01', 0.5),
15+
('2014-01-02', 0.4),
16+
('2014-01-03', 0.6),
17+
('2014-01-04', 0.2),
18+
('2014-01-05', 0.1),
19+
)
20+
for date, value in data:
21+
TimeSeries.objects.create(date=date, value=value)
22+
23+
@unittest.skipUnless(matplotlib, "requires matplotlib")
24+
def test_png(self):
25+
response = self.client.get("/timeseries.png")
26+
header = response.content[1:4]
27+
self.assertEqual(header, b"PNG")
28+
29+
@unittest.skipUnless(matplotlib, "requires matplotlib")
30+
def test_svg(self):
31+
response = self.client.get("/timeseries.svg")
32+
header = response.content[2:5]
33+
self.assertEqual(header, b"xml")

0 commit comments

Comments
 (0)