Commit ba1b7539 authored by Jérome Perrin's avatar Jérome Perrin

testIngestion: close files to prevent ResourceWarnings

parent a338c11f
......@@ -62,15 +62,6 @@ FILENAME_REGULAR_EXPRESSION = "(?P<reference>[A-Z&é@{]{3,7})-(?P<language>[a-z]
REFERENCE_REGULAR_EXPRESSION = "(?P<reference>[A-Z&é@{]{3,7})(-(?P<language>[a-z]{2}))?(-(?P<version>[0-9]{3}))?"
def makeFilePath(name):
return os.path.join(TEST_FILES_HOME, name)
def makeFileUpload(name, as_name=None):
if as_name is None:
as_name = name
path = makeFilePath(name)
return FileUpload(path, as_name)
class IngestionTestCase(ERP5TypeTestCase):
def getBusinessTemplateList(self):
......@@ -129,6 +120,16 @@ class IngestionTestCase(ERP5TypeTestCase):
skin_tool.custom._delObject(script_id)
self.commit()
def makeFilePath(self, name):
return os.path.join(TEST_FILES_HOME, name)
def makeFileUpload(self, name, as_name=None):
if as_name is None:
as_name = name
path = self.makeFilePath(name)
fu = FileUpload(path, as_name)
self.addCleanup(fu.close)
return fu
class TestIngestion(IngestionTestCase):
"""
......@@ -270,7 +271,7 @@ class TestIngestion(IngestionTestCase):
"""
for revision, format in enumerate(format_list):
filename = 'TEST-en-002.%s' %format
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document.edit(file=f)
self.tic()
self.assertTrue(document.hasFile())
......@@ -290,7 +291,7 @@ class TestIngestion(IngestionTestCase):
can be converted to any of the formats in asserted_target_list
"""
filename = 'TEST-en-002.' + format
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document.edit(file=f)
self.tic()
# We call clear cache to be sure that the target list is updated
......@@ -320,7 +321,7 @@ class TestIngestion(IngestionTestCase):
old_portal_type = ''
for extension, portal_type in extension_to_type:
filename = 'TEST-en-002.%s' %extension
file = makeFileUpload(filename)
file = self.makeFileUpload(filename)
# if we change portal type we must change version because
# mergeRevision would fail
if portal_type != old_portal_type:
......@@ -517,7 +518,7 @@ class TestIngestion(IngestionTestCase):
document = self.portal.restrictedTraverse(sequence.get('document_path'))
# First revision is 1 (like web pages)
self.assertEqual(document.getRevision(), '1')
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document.edit(file=f)
self.assertTrue(document.hasFile())
self.assertEqual(document.getFilename(), filename)
......@@ -531,7 +532,7 @@ class TestIngestion(IngestionTestCase):
Upload a file from view form and make sure this increases the revision
"""
document = self.portal.restrictedTraverse(sequence.get('document_path'))
f = makeFileUpload('TEST-en-002.doc')
f = self.makeFileUpload('TEST-en-002.doc')
revision = document.getRevision()
document.edit(file=f)
self.assertEqual(document.getRevision(), str(int(revision) + 1))
......@@ -542,7 +543,7 @@ class TestIngestion(IngestionTestCase):
"""
Upload a file from contribution.
"""
f = makeFileUpload('TEST-en-002.doc')
f = self.makeFileUpload('TEST-en-002.doc')
document = self.portal.portal_contributions.newContent(file=f)
sequence.edit(document_path=document.getPath())
self.commit()
......@@ -557,7 +558,7 @@ class TestIngestion(IngestionTestCase):
number_of_document = len(self.portal.document_module.objectIds())
self.assertNotIn('This document is modified.', document.asText())
f = makeFileUpload('TEST-en-002-modified.doc')
f = self.makeFileUpload('TEST-en-002-modified.doc')
f.filename = 'TEST-en-002.doc'
self.portal.portal_contributions.newContent(file=f)
......@@ -573,7 +574,7 @@ class TestIngestion(IngestionTestCase):
"""
Upload another file from contribution.
"""
f = makeFileUpload('ANOTHE-en-001.doc')
f = self.makeFileUpload('ANOTHE-en-001.doc')
document = self.portal.portal_contributions.newContent(id='two', file=f)
sequence.edit(document_path=document.getPath())
self.tic()
......@@ -602,7 +603,7 @@ class TestIngestion(IngestionTestCase):
self.assertEqual(property_dict['description'], 'comments')
self.assertEqual(property_dict['subject_list'], ['keywords'])
# Then make sure metadata discovery works
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document.edit(file=f)
self.assertEqual(document.getReference(), 'TEST')
self.assertEqual(document.getLanguage(), 'en')
......@@ -636,7 +637,7 @@ class TestIngestion(IngestionTestCase):
Upload with custom getPropertyDict methods
check that all metadata are correct
"""
f = makeFileUpload('TEST-en-002.doc')
f = self.makeFileUpload('TEST-en-002.doc')
document = self.portal.portal_contributions.newContent(file=f)
self.tic()
# Then make sure content discover works
......@@ -807,7 +808,7 @@ class TestIngestion(IngestionTestCase):
Try to export PDF to text and HTML
"""
document = self.portal.restrictedTraverse(sequence.get('document_path'))
f = makeFileUpload('TEST-en-002.pdf')
f = self.makeFileUpload('TEST-en-002.pdf')
document.edit(file=f)
mime, text = document.convert('text')
self.assertIn(b'magic', text)
......@@ -821,7 +822,7 @@ class TestIngestion(IngestionTestCase):
Check we are able to resize images
"""
image = self.portal.restrictedTraverse(sequence.get('document_path'))
f = makeFileUpload('TEST-en-002.jpg')
f = self.makeFileUpload('TEST-en-002.jpg')
image.edit(file=f)
self.tic()
mime, data = image.convert(None)
......@@ -950,8 +951,8 @@ class TestIngestion(IngestionTestCase):
"""
Email was sent in by someone to ERP5.
"""
f = open(makeFilePath('email_from.txt'), "rb")
document = self.receiveEmail(f.read())
with open(self.makeFilePath('email_from.txt'), "rb") as f:
self.receiveEmail(f.read())
self.tic()
def stepReceiveMultipleAttachmentsEmail(self, sequence=None,
......@@ -959,8 +960,8 @@ class TestIngestion(IngestionTestCase):
"""
Email was sent in by someone to ERP5.
"""
f = open(makeFilePath('email_multiple_attachments.eml'), "rb")
document = self.receiveEmail(f.read())
with open(self.makeFilePath('email_multiple_attachments.eml'), "rb") as f:
self.receiveEmail(f.read())
self.tic()
def stepVerifyEmailedMultipleDocumentsInitialContribution(self, sequence=None, sequence_list=None, **kw):
......@@ -1387,7 +1388,7 @@ class TestIngestion(IngestionTestCase):
"""
Upload a file from contribution.
"""
f = makeFileUpload('TEST-en-002.doc', 'T&é@{T-en-002.doc')
f = self.makeFileUpload('TEST-en-002.doc', 'T&é@{T-en-002.doc')
document = self.portal.portal_contributions.newContent(file=f)
sequence.edit(document_path=document.getPath())
self.commit()
......@@ -1459,7 +1460,7 @@ class TestIngestion(IngestionTestCase):
"""
portal = self.portal
contribution_tool = getToolByName(portal, 'portal_contributions')
file_object = makeFileUpload('TEST-en-002.doc')
file_object = self.makeFileUpload('TEST-en-002.doc')
document = contribution_tool.newContent(file=file_object)
self.assertEqual(document.getFilename(), 'TEST-en-002.doc')
my_filename = 'Something.doc'
......@@ -1482,7 +1483,7 @@ class TestIngestion(IngestionTestCase):
site='arctic/spitsbergen'))
portal.document_module.manage_setLocalRoles(user.Person_getUserId(), ['Assignor',])
self.tic()
file_object = makeFileUpload('TEST-en-002.doc')
file_object = self.makeFileUpload('TEST-en-002.doc')
document = contribution_tool.newContent(file=file_object)
document.discoverMetadata(document.getFilename(), user.Person_getUserId())
self.tic()
......@@ -1506,7 +1507,7 @@ class TestIngestion(IngestionTestCase):
portal.document_module.manage_setLocalRoles(other_user.Person_getUserId(), ['Assignor',])
self.tic()
file_object = makeFileUpload('TEST-en-002.doc')
file_object = self.makeFileUpload('TEST-en-002.doc')
document = contribution_tool.newContent(file=file_object)
# We only consider the higher group of assignments
......@@ -1928,8 +1929,8 @@ return result
as a application/octet-stream without explicit extension, become
a Spreadsheet ?
"""
path = makeFilePath('import_region_category.ods')
data = open(path, 'rb').read()
with open(self.makeFilePath('import_region_category.ods'), 'rb') as f:
data = f.read()
document = self.portal.portal_contributions.newContent(filename='toto',
data=data,
......@@ -1981,8 +1982,8 @@ return result
def test_User_Portal_Type_parameter_is_honoured(self):
"""Check that given portal_type is always honoured
"""
path = makeFilePath('import_region_category.xls')
data = open(path, 'rb').read()
with open(self.makeFilePath('import_region_category.xls'), 'rb') as f:
data = f.read()
document = self.portal.portal_contributions.newContent(
filename='import_region_category.xls',
......@@ -1999,8 +2000,8 @@ return result
def test_User_ID_parameter_is_honoured(self):
"""Check that given id is always honoured
"""
path = makeFilePath('import_region_category.xls')
data = open(path, 'rb').read()
with open(self.makeFilePath('import_region_category.xls'), 'rb') as f:
data = f.read()
document = self.portal.portal_contributions.newContent(
id='this_id',
......@@ -2024,8 +2025,8 @@ return result
def test_newContent_trough_http(self):
filename = 'import_region_category.xls'
path = makeFilePath(filename)
data = open(path, 'rb').read()
with open(self.makeFilePath(filename), 'rb') as f:
data = f.read()
reference = 'ITISAREFERENCE'
portal_url = self.portal.absolute_url()
......@@ -2119,7 +2120,7 @@ class Base_contributeMixin:
version=None,
description=None,
attach_document_to_context=True,
file=makeFileUpload('TEST-en-002.odt'))
file=self.makeFileUpload('TEST-en-002.odt'))
self.assertEqual('Text', contributed_document.getPortalType())
self.tic()
document_list = person.getFollowUpRelatedValueList()
......@@ -2164,7 +2165,7 @@ class Base_contributeMixin:
person = self.portal.person_module.newContent(portal_type='Person')
contributed_document = person.Base_contribute(
portal_type='PDF',
file=makeFileUpload('TEST-en-002.odt'))
file=self.makeFileUpload('TEST-en-002.odt'))
self.assertEqual('PDF', contributed_document.getPortalType())
def test_Base_contribute_input_parameter_dict(self):
......@@ -2173,7 +2174,7 @@ class Base_contributeMixin:
person = self.portal.person_module.newContent(portal_type='Person')
contributed_document = person.Base_contribute(
title='user supplied title',
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.tic()
self.assertEqual('user supplied title', contributed_document.getTitle())
......@@ -2186,7 +2187,7 @@ class Base_contributeMixin:
# we use as_name, to prevent regular expression from detecting a
# reference during ingestion, so that we can upload multiple documents
# in one test.
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'draft')
contributed_document.setReference(None)
......@@ -2195,7 +2196,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='shared',
synchronous_metadata_discovery=False,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'shared')
contributed_document.setReference(None)
......@@ -2204,7 +2205,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='shared',
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'shared')
contributed_document.setReference(None)
......@@ -2213,7 +2214,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='released',
synchronous_metadata_discovery=False,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'released')
contributed_document.setReference(None)
......@@ -2222,7 +2223,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='released',
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'released')
contributed_document.setReference(None)
......@@ -2231,7 +2232,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
synchronous_metadata_discovery=False,
publication_state='published',
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'published')
contributed_document.setReference(None)
......@@ -2240,7 +2241,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
synchronous_metadata_discovery=True,
publication_state='published',
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'published')
......@@ -2260,7 +2261,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='shared',
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'shared')
contributed_document.setReference(None)
......@@ -2269,14 +2270,14 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='shared',
synchronous_metadata_discovery=False,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'shared')
contributed_document.setReference(None)
contributed_document = person.Base_contribute(
publication_state=None,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'published')
......@@ -2300,7 +2301,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
ret = person.Base_contribute(
redirect_to_context=True,
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.assertIn(
('portal_status_message', 'PDF created successfully.'),
urlparse.parse_qsl(urlparse.urlparse(ret).query))
......@@ -2318,7 +2319,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
ret = person.Base_contribute(
redirect_to_context=True,
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.assertIn(
('portal_status_message', 'PDF updated successfully.'),
urlparse.parse_qsl(urlparse.urlparse(ret).query))
......@@ -2341,7 +2342,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
person.Base_contribute(
redirect_to_context=True,
synchronous_metadata_discovery=synchronous_metadata_discovery,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.assertIn(
('portal_status_message',
'You are not allowed to update the existing document which has the same coordinates.'),
......@@ -2359,7 +2360,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
"You are not allowed to update the existing document which has the same coordinates"):
person.Base_contribute(
synchronous_metadata_discovery=synchronous_metadata_discovery,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.assertEqual(document.getData(), b'')
def test_Base_contribute_publication_state_unauthorized(self):
......@@ -2379,7 +2380,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
publication_state='published',
redirect_to_context=True,
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.assertIn(
('portal_status_message', 'You are not allowed to contribute document in that state.'),
urlparse.parse_qsl(urlparse.urlparse(str(ctx.exception)).query))
......@@ -2394,7 +2395,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
person.Base_contribute(
publication_state='published',
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
# when using asynchronous metadata discovery, an error occurs in activity,
# but not document is published
......@@ -2402,7 +2403,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
publication_state='published',
redirect_to_context=True,
synchronous_metadata_discovery=False,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
with self.assertRaisesRegex(
Exception,
"Transition document_publication_workflow/publish unsupported"):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment