I get a warning from this test but I don't know how to handle it. The warning is
/usr/lib/python3.5/unittest/case.py:600: ResourceWarning: unclosed file <_io.BufferedReader name='/tmp/20170926_084744_545761.pdf'>
testMethod()
The code I'm testing (with Mako and Bottle) is:
pdf.pdf_export(selected_date=selected_date, values=values, to_pdf_file=to_pdf_file)
return static_file(to_pdf_file, root='/tmp', download=filename)
My test case looks as follows
# Test is based on assumption of having the appropriate parameters for the expected output
def testPDFexportByLogic(self):
filter = ''
sortby = '1'
reverse1 = '1'
reverse = True
if reverse1 == '-1': reverse = False
oncall_logic = OncallLogic(self.resources, view_type=OncallViewType.daily)
oncall_logic.set_selections(selected_customer_id='417', listing_customer_ids='417',
selected_date='20170925')
pdf_file = oncall_logic.get_oncall_pdf_export_daily_events(self.resources, '417',
'417', '20170925', filter=filter,
sortby=int(sortby), reverse=reverse)
filename = 'pdf-temp'
with open(filename, 'wb') as w:
w.write(pdf_file.body.read())
self.assertIn("Sjukhus", str(self.convert_pdf_to_txt(filename)))
os.remove(filename)
The above test succeeds to render the PDF and check the text, but I get a warning from running the test. Is the warning from the actual test or from running the code which is tested? Which file is it that I must close, if any?
I restructure the code and I still get the warning. I still get the ResourceWarning. I don't understand it because I only open one file and the warning is about the other file. Why???
def testPDFexportByLogic(self):
filename = 'pdf-temp'
with open(filename, 'wb') as w:
oncall_logic = OncallLogic(self.resources, view_type=OncallViewType.daily)
oncall_logic.set_selections(selected_customer_id='417', listing_customer_ids='417',
selected_date='20170925')
pdf_file = oncall_logic.get_oncall_pdf_export_daily_events(self.resources, '417',
'417', '20170925')
w.write(pdf_file.body.read())
w.close()
self.assertIn("Sjukhus", str(self.convert_pdf_to_txt(filename)))
These are my import statements in the test code:
import os
import io
import pdfminer
from controllers.oncall import oncall
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from business.logic.oncall.oncall_logic import OncallLogic
from business.resources.entities.gui.gui_settings import OncallViewType
The following is more code that is tested:
def pdf_export(self, selected_date, values, to_pdf_file):
"""
@param selected_date: Date of the view to export
@type selected_date: str
@param values: Dict with values for the template the write on
@type values: dict
@param to_pdf_file: Name of the PDF file to write
@type to_pdf_file: str
"""
css = '/srv/http/python.dev/python-common/static_files/css/pdf-export-styles.css'
pdfkit.from_string(render_template("oncall/dayview_pdf_export", values), '/tmp/' + to_pdf_file, css=css)
Exception ignored in: <_io.FileIO name='/tmp/20170926_152918_365562.pdf' mode='rb' closefd=True> ResourceWarning: unclosed file <_io.BufferedReader name='/tmp/20170926_152918_365562.pdf'>
is the new trace but it probably doesn't help us much?reverse
this way:reverse = reverse1 != '-1'
.