diff options
Diffstat (limited to 'wkhtmltopdf/tests/tests.py')
-rw-r--r-- | wkhtmltopdf/tests/tests.py | 43 |
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' |