diff options
author | Jonathan Liuti <liuti.john@gmail.com> | 2015-11-25 13:29:18 +0100 |
---|---|---|
committer | Jonathan Liuti <liuti.john@gmail.com> | 2015-12-01 07:50:46 +0100 |
commit | fbf54be1da1cc90490336a57df6678a2f833a5f7 (patch) | |
tree | d8a329fb7c2ce5ae05221dffe411493b4f737e8a /wkhtmltopdf/tests | |
parent | 1a849e356d1c2487337c62768f16ea80883c9bf1 (diff) | |
download | django-wkhtmltopdf-fbf54be1da1cc90490336a57df6678a2f833a5f7.tar.gz django-wkhtmltopdf-fbf54be1da1cc90490336a57df6678a2f833a5f7.tar.bz2 django-wkhtmltopdf-fbf54be1da1cc90490336a57df6678a2f833a5f7.zip |
Extracted logic from views - fixed tests.
The logic was coupled with the views which made things difficult
to reuse if you wanted to use the pdf generation somehwere else
than in a view.
With this patch, the logic has been moved to `utils.py` and should
be more easy to reuse.
Tests have been adapted and made compatible with django > 1.7
Diffstat (limited to 'wkhtmltopdf/tests')
-rw-r--r-- | wkhtmltopdf/tests/tests.py | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/wkhtmltopdf/tests/tests.py b/wkhtmltopdf/tests/tests.py index 48a8b19..c15d88f 100644 --- a/wkhtmltopdf/tests/tests.py +++ b/wkhtmltopdf/tests/tests.py @@ -6,6 +6,7 @@ import os import sys from django.conf import settings +from django.template import loader, RequestContext from django.test import TestCase from django.test.client import RequestFactory from django.utils import six @@ -13,7 +14,7 @@ from django.utils.encoding import smart_str from wkhtmltopdf.subprocess import CalledProcessError from wkhtmltopdf.utils import (_options_to_args, make_absolute_paths, - wkhtmltopdf) + wkhtmltopdf, render_to_temporary_file) from wkhtmltopdf.views import PDFResponse, PDFTemplateView, PDFTemplateResponse @@ -50,10 +51,8 @@ class TestUtils(TestCase): def test_wkhtmltopdf(self): """Should run wkhtmltopdf to generate a PDF""" title = 'A test template.' - response = PDFTemplateResponse(self.factory.get('/'), - None, - context={'title': title}) - temp_file = response.render_to_temporary_file('sample.html') + template = loader.get_template('sample.html') + temp_file = render_to_temporary_file(template, context={'title': title}) try: # Standard call pdf_output = wkhtmltopdf(pages=[temp_file.name]) @@ -76,23 +75,20 @@ class TestUtils(TestCase): def test_wkhtmltopdf_with_unicode_content(self): """A wkhtmltopdf call should render unicode content properly""" title = u'♥' - response = PDFTemplateResponse(self.factory.get('/'), - None, - context={'title': title}) - temp_file = response.render_to_temporary_file('unicode.html') + template = loader.get_template('unicode.html') + temp_file = render_to_temporary_file(template, context={'title': title}) try: pdf_output = wkhtmltopdf(pages=[temp_file.name]) self.assertTrue(pdf_output.startswith(b'%PDF'), pdf_output) finally: temp_file.close() - def test_PDFTemplateResponse_render_to_temporary_file(self): + def test_render_to_temporary_file(self): """Should render a template to a temporary file.""" title = 'A test template.' - response = PDFTemplateResponse(self.factory.get('/'), - None, - context={'title': title}) - temp_file = response.render_to_temporary_file('sample.html') + + template = loader.get_template('sample.html') + temp_file = render_to_temporary_file(template, context={'title': title}) temp_file.seek(0) saved_content = smart_str(temp_file.read()) self.assertTrue(title in saved_content) @@ -179,7 +175,8 @@ class TestViews(TestCase): self.assertFalse(response.has_header('Content-Disposition')) # Render to temporary file - tempfile = response.render_to_temporary_file(self.template) + template = loader.get_template(self.template) + tempfile = render_to_temporary_file(template, context=context) tempfile.seek(0) html_content = smart_str(tempfile.read()) self.assertTrue(html_content.startswith('<html>')) @@ -205,7 +202,8 @@ class TestViews(TestCase): self.assertEqual(response.cmd_options, cmd_options) self.assertTrue(response.has_header('Content-Disposition')) - tempfile = response.render_to_temporary_file(self.footer_template) + footer_template = loader.get_template(self.footer_template) + tempfile = render_to_temporary_file(footer_template, context=RequestContext(request, context)) tempfile.seek(0) footer_content = smart_str(tempfile.read()) footer_content = make_absolute_paths(footer_content) |