diff options
-rw-r--r-- | wkhtmltopdf/tests.py | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/wkhtmltopdf/tests.py b/wkhtmltopdf/tests.py index c55d6f9..cd0213d 100644 --- a/wkhtmltopdf/tests.py +++ b/wkhtmltopdf/tests.py @@ -9,10 +9,8 @@ from django.test import TestCase from django.test.client import RequestFactory from .subprocess import CalledProcessError -from .utils import (override_settings, - _options_to_args, template_to_temp_file, wkhtmltopdf) -from .views import (PDFResponse, PdfResponse, PDFTemplateResponse, - PDFTemplateView, PdfTemplateView) +from .utils import override_settings, _options_to_args, wkhtmltopdf +from .views import PDFResponse, PDFTemplateView, PDFTemplateResponse class TestUtils(TestCase): @@ -20,6 +18,7 @@ class TestUtils(TestCase): # Clear standard error self._stderr = sys.stderr sys.stderr = open(os.devnull, 'w') + self.factory = RequestFactory() def tearDown(self): sys.stderr = self._stderr @@ -35,39 +34,36 @@ class TestUtils(TestCase): def test_wkhtmltopdf(self): """Should run wkhtmltopdf to generate a PDF""" title = 'A test template.' - temp_file = template_to_temp_file('sample.html', {'title': title}) - pdf_output = None + response = PDFTemplateResponse(self.factory.get('/'), None, context={'title': title}) + temp_file = response.render_to_temporary_file('sample.html') try: # Standard call - pdf_output = wkhtmltopdf(pages=[temp_file]) + pdf_output = wkhtmltopdf(pages=[temp_file.name]) self.assertTrue(pdf_output.startswith('%PDF'), pdf_output) # Single page - pdf_output = wkhtmltopdf(pages=temp_file) + pdf_output = wkhtmltopdf(pages=temp_file.name) self.assertTrue(pdf_output.startswith('%PDF'), pdf_output) # Unicode - pdf_output = wkhtmltopdf(pages=[temp_file], title=u'♥') + pdf_output = wkhtmltopdf(pages=[temp_file.name], title=u'♥') self.assertTrue(pdf_output.startswith('%PDF'), pdf_output) # Invalid arguments self.assertRaises(CalledProcessError, wkhtmltopdf, pages=[]) finally: - if os.path.exists(temp_file): - os.remove(temp_file) + temp_file.close() - def test_template_to_temp_file(self): + def test_PDFTemplateResponse_render_to_temporary_file(self): """Should render a template to a temporary file.""" title = 'A test template.' - temp_file = template_to_temp_file('sample.html', {'title': title}) - try: - with open(temp_file, 'r') as f: - saved_content = f.read() - self.assertTrue(title in saved_content) - finally: - if os.path.exists(temp_file): - os.remove(temp_file) + response = PDFTemplateResponse(self.factory.get('/'), None, context={'title': title}) + temp_file = response.render_to_temporary_file('sample.html') + temp_file.seek(0) + saved_content = temp_file.read() + self.assertTrue(title in saved_content) + temp_file.close() class TestViews(TestCase): |