aboutsummaryrefslogtreecommitdiffstats
path: root/wkhtmltopdf/tests/tests.py
diff options
context:
space:
mode:
authorJonathan Liuti <liuti.john@gmail.com>2016-03-04 09:51:15 +0100
committerJonathan Liuti <liuti.john@gmail.com>2016-03-04 09:51:15 +0100
commit7024713a768d5180ff4a831598cdc8ed47a5476c (patch)
treeb2c6b8cec8b733ede94365eeb52d86e8b2decf3b /wkhtmltopdf/tests/tests.py
parent44496fc3c418a94103bcd2da259f85f245778a15 (diff)
parent87b4aad9e9a88fafbf77dffd9694d613e721add6 (diff)
downloaddjango-wkhtmltopdf-7024713a768d5180ff4a831598cdc8ed47a5476c.tar.gz
django-wkhtmltopdf-7024713a768d5180ff4a831598cdc8ed47a5476c.tar.bz2
django-wkhtmltopdf-7024713a768d5180ff4a831598cdc8ed47a5476c.zip
Merge pull request #104 from halfnibble/master
Refactor to better support multiple pages.
Diffstat (limited to 'wkhtmltopdf/tests/tests.py')
-rw-r--r--wkhtmltopdf/tests/tests.py43
1 files changed, 42 insertions, 1 deletions
diff --git a/wkhtmltopdf/tests/tests.py b/wkhtmltopdf/tests/tests.py
index 822f529..7b48421 100644
--- a/wkhtmltopdf/tests/tests.py
+++ b/wkhtmltopdf/tests/tests.py
@@ -14,7 +14,8 @@ from django.utils.encoding import smart_str
from wkhtmltopdf.subprocess import CalledProcessError
from wkhtmltopdf.utils import (_options_to_args, make_absolute_paths,
- wkhtmltopdf, render_to_temporary_file)
+ wkhtmltopdf, render_to_temporary_file,
+ RenderedFile)
from wkhtmltopdf.views import PDFResponse, PDFTemplateView, PDFTemplateResponse
@@ -30,6 +31,7 @@ class UnicodeContentPDFTemplateView(PDFTemplateView):
context['title'] = u'♥'
return context
+
class TestUtils(TestCase):
def setUp(self):
# Clear standard error
@@ -94,6 +96,45 @@ class TestUtils(TestCase):
self.assertTrue(title in saved_content)
temp_file.close()
+ def _render_file(self, template, context):
+ """Helper method for testing rendered file deleted/persists tests."""
+ render = RenderedFile(template=template, context=context)
+ render.temporary_file.seek(0)
+ saved_content = smart_str(render.temporary_file.read())
+
+ return (saved_content, render.filename)
+
+ def test_rendered_file_deleted_on_production(self):
+ """If WKHTMLTOPDF_DEBUG=False, delete rendered file on object close."""
+ title = 'A test template.'
+ template = loader.get_template('sample.html')
+ debug = getattr(settings, 'WKHTMLTOPDF_DEBUG', settings.DEBUG)
+
+ saved_content, filename = self._render_file(template=template,
+ context={'title': title})
+ # First verify temp file was rendered correctly.
+ self.assertTrue(title in saved_content)
+
+ # Then check if file is deleted when debug=False.
+ self.assertFalse(debug)
+ self.assertFalse(os.path.isfile(filename))
+
+ def test_rendered_file_persists_on_debug(self):
+ """If WKHTMLTOPDF_DEBUG=True, the rendered file should persist."""
+ title = 'A test template.'
+ template = loader.get_template('sample.html')
+ with self.settings(WKHTMLTOPDF_DEBUG=True):
+ debug = getattr(settings, 'WKHTMLTOPDF_DEBUG', settings.DEBUG)
+
+ saved_content, filename = self._render_file(template=template,
+ context={'title': title})
+ # First verify temp file was rendered correctly.
+ self.assertTrue(title in saved_content)
+
+ # Then check if file persists when debug=True.
+ self.assertTrue(debug)
+ self.assertTrue(os.path.isfile(filename))
+
class TestViews(TestCase):
template = 'sample.html'