From 39b61bff13f41b7c779d57e16eef717dfffbe6b9 Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 2 Mar 2016 04:54:50 -0800 Subject: Add test for temporary file is closed/deleted. --- wkhtmltopdf/tests/tests.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/wkhtmltopdf/tests/tests.py b/wkhtmltopdf/tests/tests.py index 822f529..b3c9ea1 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,28 @@ class TestUtils(TestCase): self.assertTrue(title in saved_content) temp_file.close() + def _render_file(self, template, context): + """Helper method for test_rendered_filed_deletion test.""" + 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_filed_deletion(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 actually rendered. + self.assertTrue(title in saved_content) + + # Then check if file is deleted when DEBUG=False. + self.assertEqual(os.path.isfile(filename), debug) + class TestViews(TestCase): template = 'sample.html' -- cgit v1.2.3