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

*: use open() as context manager to ensure files are closed

parent f741c9fb
......@@ -68,7 +68,8 @@ class File(object):
file_path = tempfile.mktemp(suffix=".%s" % self.source_format,
dir=self.directory_name)
# stores the data in temporary file
open(file_path, 'wb').write(self.original_data)
with open(file_path, 'wb') as f:
f.write(self.original_data)
# If is a zipfile is need extract all files from whitin the compressed file
if is_zipfile(file_path):
zipfile = ZipFile(file_path)
......@@ -106,12 +107,14 @@ class File(object):
zipfile.write(file)
finally:
zipfile.close()
opened_zip = open(zip_path, 'r').read()
with open(zip_path, 'rb') as f:
opened_zip = f.read()
remove(zip_path)
chdir(current_dir_url)
return opened_zip
else:
return open(self.url, 'r').read()
with open(self.url, 'rb') as f:
return f.read()
def getUrl(self):
"""Returns full path."""
......
......@@ -44,14 +44,17 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self):
return [
# Test to verify if server fail when a empty string is sent
('', '', ''),
scenario_list = [
# Test to verify if server fail when a empty file is sent
(b'', '', ''),
]
# Try convert one video for a invalid format
(open(join('data', 'test.ogv')).read(), 'ogv', 'xyz'),
with open(join('data', 'test.ogv'), 'rb') as f:
scenario_list.append((f.read(), 'ogv', 'xyz'))
# Try convert one video to format not possible
(open(join('data', 'test.ogv')).read(), 'ogv', 'moov'),
]
with open(join('data', 'test.ogv'), 'rb') as f:
scenario_list.append((f.read(), 'ogv', 'moov'))
return scenario_list
def testFaultConversion(self):
"""Test fail convertion of Invalid video files"""
......
......@@ -41,7 +41,8 @@ class TestHandler(HandlerTestCase):
def testConvertPNGtoJPG(self):
"""Test conversion of png to jpg"""
png_file = open("data/test.png").read()
with open("data/test.png", "rb") as f:
png_file = f.read()
handler = Handler(self.tmp_url, png_file, "png", **self.kw)
jpg_file = handler.convert("jpg")
mime = magic.Magic(mime=True)
......@@ -50,7 +51,8 @@ class TestHandler(HandlerTestCase):
def testgetMetadataFromImage(self):
"""Test if metadata is extracted from image correctly"""
png_file = open("data/test.png").read()
with open("data/test.png", "rb") as f:
png_file = f.read()
handler = Handler(self.tmp_url, png_file, "png", **self.kw)
metadata = handler.getMetadata()
self.assertEqual(metadata.get("Compression"), "Zip")
......
......@@ -44,14 +44,17 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self):
return [
# Test to verify if server fail when a empty string is sent
('', '', ''),
# Try convert one video for a invalid format
(open(join('data', 'test.png')).read(), 'png', 'xyz'),
# Try convert one video to format not possible
(open(join('data', 'test.png')).read(), 'png', '8bim'),
scenario_list = [
# Test to verify if server fail when a empty file is sent
(b'', '', ''),
]
# Try convert one png for a invalid format
with open(join('data', 'test.png'), 'rb') as f:
scenario_list.append((f.read(), 'png', 'xyz'))
# Try convert one png to format not possible
with open(join('data', 'test.png'), 'rb') as f:
scenario_list.append((f.read(), 'png', '8bim'))
return scenario_list
def testFaultConversion(self):
"""Test fail convertion of Invalid image files"""
......
......@@ -58,7 +58,8 @@ class TestFileSystemDocument(unittest.TestCase):
document_filename = "document"
document_test_url = path.join(self.fsdocument.directory_name,
document_filename)
open(document_test_url, 'wb').write(decodestring("Test Document"))
with open(document_test_url, 'wb') as f:
f.write(decodestring(b"Test Document"))
self.fsdocument.reload(document_test_url)
self.assertEqual(path.exists(old_document_url), False)
self.assertNotEqual(self.fsdocument.original_data,
......@@ -82,7 +83,8 @@ class TestFileSystemDocument(unittest.TestCase):
def testLoadDocumentFile(self):
"""Test if the document is created correctly"""
url = self.fsdocument.getUrl()
tmp_document = open(url, 'r').read()
with open(url, 'rb') as f:
tmp_document = f.read()
self.assertEqual(self.data, tmp_document)
self.fsdocument.trash()
self.assertEqual(path.exists(url), False)
......@@ -93,7 +95,8 @@ class TestFileSystemDocument(unittest.TestCase):
document_filename = "document"
document_test_url = path.join(self.fsdocument.directory_name,
document_filename)
open(document_test_url, 'wb').write(self.data)
with open(document_test_url, 'wb') as f:
f.write(self.data)
self.fsdocument.reload(document_test_url)
url = self.fsdocument.getUrl()
self.assertEqual(path.exists(old_document_url), False)
......@@ -103,7 +106,8 @@ class TestFileSystemDocument(unittest.TestCase):
def testZipDocumentList(self):
"""Tests if the zip file is returned correctly"""
open(path.join(self.fsdocument.directory_name, 'document2'), 'w').write('test')
with open(path.join(self.fsdocument.directory_name, 'document2'), 'w') as f:
f.write('test')
zip_file = self.fsdocument.getContent(True)
mime = magic.Magic(mime=True)
mimetype = mime.from_buffer(zip_file)
......@@ -118,8 +122,8 @@ class TestFileSystemDocument(unittest.TestCase):
def testSendZipFile(self):
"""Tests if the htm is extrated from zipfile"""
zip_input_url = 'data/test.zip'
data = open(zip_input_url).read()
with open('./data/test.zip', 'rb') as f:
data = f.read()
zipdocument = FileSystemDocument(self.tmp_url, data, 'zip')
mime = magic.Magic(mime=True)
mimetype = mime.from_buffer(zipdocument.getContent(True))
......
......@@ -39,7 +39,8 @@ from cloudooo.handler.ooo.granulator import OOGranulator
class TestOOGranulator(HandlerTestCase):
def setUp(self):
data = open('./data/granulate_test.odt').read()
with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
self.oogranulator = OOGranulator(data, 'odt')
def testOdfWithoutContentXml(self):
......@@ -56,7 +57,8 @@ class TestOOGranulator(HandlerTestCase):
def testgetTableItemList(self):
"""Test if getTableItemList() returns the right tables list"""
data = open('./data/granulate_table_test.odt').read()
with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt')
table_list = [('Developers', ''),
('Prices', 'Table 1: Prices table from Mon Restaurant'),
......@@ -65,7 +67,8 @@ class TestOOGranulator(HandlerTestCase):
def testGetTable(self):
"""Test if getTable() returns on odf file with the right table"""
data = open('./data/granulate_table_test.odt').read()
with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt')
table_data_doc = oogranulator.getTable('Developers')
content_xml_str = ZipFile(StringIO(table_data_doc)).read('content.xml')
......@@ -79,21 +82,24 @@ class TestOOGranulator(HandlerTestCase):
def testGetTableItemWithoutSuccess(self):
"""Test if getTable() returns None for an non existent table name"""
data = open('./data/granulate_table_test.odt').read()
with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt')
table_data = oogranulator.getTable('NonExistentTable')
self.assertEqual(table_data, None)
def testGetColumnItemList(self):
"""Test if getColumnItemList() returns the right table columns list"""
data = open('./data/granulate_table_test.odt').read()
with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt')
self.assertEqual([[0, 'Name'], [1, 'Country']],
oogranulator.getColumnItemList('SoccerTeams'))
def testGetLineItemList(self):
"""Test if getLineItemList() returns the right table lines list"""
data = open('./data/granulate_table_test.odt').read()
with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt')
matrix = [['Name', 'Phone', 'Email'],
['Hugo', '+55 (22) 8888-8888', 'hugomaia@tiolive.com'],
......@@ -121,7 +127,8 @@ class TestOOGranulator(HandlerTestCase):
def testGetImageSuccessfully(self):
"""Test if getImage() returns the right image file successfully"""
data = open('./data/granulate_test.odt').read()
with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
zip = ZipFile(StringIO(data))
image_id = '10000000000000C80000009C38276C51.jpg'
original_image = zip.read('Pictures/%s' % image_id)
......@@ -131,13 +138,14 @@ class TestOOGranulator(HandlerTestCase):
def testGetImageWithoutSuccess(self):
"""Test if getImage() returns an empty string for not existent id"""
obtained_image = self.oogranulator.getImage('anything.png')
self.assertEqual('', obtained_image)
self.assertEqual(b'', obtained_image)
def testGetParagraphItemList(self):
"""Test if getParagraphItemList() returns the right paragraphs list, with
the ids always in the same order"""
for i in range(5):
data = open('./data/granulate_test.odt').read()
for _ in range(5):
with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt')
paragraph_list = oogranulator.getParagraphItemList()
self.assertEqual((0, 'P3'), paragraph_list[0])
......@@ -162,7 +170,8 @@ class TestOOGranulator(HandlerTestCase):
def testGetChapterItemList(self):
"""Test if getChapterItemList() returns the right chapters list"""
data = open('./data/granulate_chapters_test.odt').read()
with open('./data/granulate_chapters_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt')
self.assertEqual([(0, 'Title 0'), (1, 'Title 1'), (2, 'Title 2'),
(3, 'Title 3'), (4, 'Title 4'), (5, 'Title 5'),
......@@ -172,7 +181,8 @@ class TestOOGranulator(HandlerTestCase):
def testGetChapterItem(self):
"""Test if getChapterItem() returns the right chapter"""
data = open("./data/granulate_chapters_test.odt").read()
with open("./data/granulate_chapters_test.odt", 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt')
self.assertEqual(['Title 1', 1], oogranulator.getChapterItem(1))
......@@ -48,9 +48,8 @@ class TestHandler(HandlerTestCase):
def _save_document(self, document_output_url, data):
"""Create document in file system"""
new_file = open(document_output_url, "w")
new_file.write(data)
new_file.close()
with open(document_output_url, "wb") as f:
f.write(data)
self._file_path_list.append(document_output_url)
def _assert_document_output(self, document, expected_mimetype):
......@@ -70,18 +69,20 @@ class TestHandler(HandlerTestCase):
def testConvertOdtToDoc(self):
"""Test convert ODT to DOC"""
data = encodestring(open("data/test.odt").read())
with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url,
decodestring(data),
data,
'odt')
doc_exported = handler.convert("doc")
self._assert_document_output(doc_exported, "application/msword")
def testConvertDocToOdt(self):
"""Test convert DOC to ODT"""
data = encodestring(open("data/test.doc").read())
with open("data/test.doc", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url,
decodestring(data),
data,
'doc')
doc_exported = handler.convert("odt")
self._assert_document_output(doc_exported,
......@@ -89,9 +90,10 @@ class TestHandler(HandlerTestCase):
def testGetMetadata(self):
"""Test getMetadata"""
data = encodestring(open("data/test.odt").read())
with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url,
decodestring(data),
data,
'odt')
metadata = handler.getMetadata()
self.assertEqual(metadata.get('MIMEType'),
......@@ -102,9 +104,10 @@ class TestHandler(HandlerTestCase):
def testSetMetadata(self):
"""Test setMetadata"""
data = encodestring(open("data/test.odt").read())
with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url,
decodestring(data),
data,
'odt')
new_data = handler.setMetadata({"Title": "cloudooo Test -"})
new_handler = Handler(self.tmp_url,
......@@ -113,7 +116,7 @@ class TestHandler(HandlerTestCase):
metadata = new_handler.getMetadata()
self.assertEqual(metadata.get('Title'), "cloudooo Test -")
handler = Handler(self.tmp_url,
decodestring(data),
data,
'odt')
new_data = handler.setMetadata({"Title": "Namie's working record"})
new_handler = Handler(self.tmp_url,
......@@ -125,9 +128,10 @@ class TestHandler(HandlerTestCase):
def testConvertWithOpenOfficeStopped(self):
"""Test convert with openoffice stopped"""
openoffice.stop()
data = encodestring(open("data/test.doc").read())
with open("data/test.doc", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url,
decodestring(data),
data,
'doc')
doc_exported = handler.convert("odt")
self._assert_document_output(doc_exported,
......@@ -136,9 +140,10 @@ class TestHandler(HandlerTestCase):
def testGetMetadataWithOpenOfficeStopped(self):
"""Test getMetadata with openoffice stopped"""
openoffice.stop()
data = encodestring(open("data/test.odt").read())
with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url,
decodestring(data),
data,
'odt')
metadata = handler.getMetadata()
self.assertEqual(metadata.get('Title'), 'title')
......@@ -148,9 +153,10 @@ class TestHandler(HandlerTestCase):
def testSetMetadataWithOpenOfficeStopped(self):
"""Test setMetadata with openoffice stopped"""
openoffice.stop()
data = encodestring(open("data/test.doc").read())
with open("data/test.doc", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url,
decodestring(data),
data,
'doc')
new_data = handler.setMetadata({"Title": "cloudooo Test -"})
new_handler = Handler(self.tmp_url,
......@@ -162,9 +168,10 @@ class TestHandler(HandlerTestCase):
def testRefreshOdt(self):
"""Test refresh argument"""
# Check when refreshing is disabled
data = encodestring(open("data/test_fields.odt").read())
with open("data/test_fields.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url,
decodestring(data),
data,
'odt',
refresh=False)
doc_exported = handler.convert("odt")
......@@ -176,9 +183,8 @@ class TestHandler(HandlerTestCase):
namespaces=content_tree.nsmap))
# Check when refreshing is enabled
data = encodestring(open("data/test_fields.odt").read())
handler = Handler(self.tmp_url,
decodestring(data),
data,
'odt',
refresh=True)
doc_exported = handler.convert("odt")
......
......@@ -37,7 +37,8 @@ from cloudooo.handler.ooo.document import OdfDocument
class TestOdfDocument(HandlerTestCase):
def setUp(self):
data = open('./data/granulate_test.odt').read()
with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
self.oodocument = OdfDocument(data, 'odt')
def testReceivedGoodFile(self):
......@@ -47,7 +48,7 @@ class TestOdfDocument(HandlerTestCase):
def testGetContentXml(self):
"""Test if the getContentXml method returns the content.xml file"""
content_xml = self.oodocument.getContentXml()
self.assertTrue('The content of this file is just' in content_xml)
self.assertIn(b'The content of this file is just', content_xml)
def testGetExistentFile(self):
"""Test if the getFile method returns the requested file"""
......
......@@ -100,14 +100,17 @@ class TestConversion(TestCase):
self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self):
return [
# Test to verify if server fail when a empty string is sent
('', '', ''),
# Try convert one document for a invalid format
(open(join('data', 'test.doc')).read(), 'doc', 'xyz'),
# Try convert one document to format not possible
(open(join('data', 'test.odp')).read(), 'odp', 'doc'),
scenario_list = [
# Test to verify if server fail when a empty file is sent
(b'', '', ''),
]
# Try convert one document to an invalid format
with open(join('data', 'test.doc'), 'rb') as f:
scenario_list.append((f.read(), 'doc', 'xyz'))
# Try convert one video to format not possible
with open(join('data', 'test.odp'), 'rb') as f:
scenario_list.append((f.read(), 'odp', 'doc'))
return scenario_list
def testFaultConversion(self):
"""Test fail convertion of Invalid OOofiles"""
......@@ -128,16 +131,20 @@ class TestConversion(TestCase):
])
def ConvertScenarioList(self):
return [
scenario_list = []
# Test run_convert method
('test.doc', open(join('data', 'test.doc')).read(), 200, '',
with open(join('data', 'test.doc'), 'rb') as f:
scenario_list.append(
('test.doc', f.read(), 200, '',
['data', 'meta', 'mime'], '', 'application/vnd.oasis.opendocument.text'
),
))
# Test run_convert method with invalid file
('test.doc', open(join('data', 'test.doc')).read()[:30], 200, '',
with open(join('data', 'test.doc'), 'rb') as f:
scenario_list.append(
('test.doc', f.read()[:300], 200, '',
['data', 'meta', 'mime'], '', 'application/vnd.oasis.opendocument.text'
),
]
))
return scenario_list
def testRunConvertMethod(self):
"""Test run_convert method"""
......@@ -211,7 +218,8 @@ class TestGetMetadata(TestCase):
# and the support to this kind of tests will be dropped.
def testRunGenerateMethod(self):
"""Test run_generate method"""
data = open(join('data', 'test.odt'), 'r').read()
with open(join('data', 'test.odt'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.odt',
encodestring(data),
None, 'pdf',
......@@ -229,9 +237,10 @@ class TestGenerate(TestCase):
def testRunGenerateMethodConvertOdsToHTML(self):
"""Test run_generate method from ods to html. This test is to validate
a bug convertions to html"""
with open(join('data', 'test.ods'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.ods',
encodestring(
open(join('data', 'test.ods')).read()),
encodestring(data),
None, 'html',
"application/vnd.oasis.opendocument.spreadsheet")
response_code, response_dict, response_message = generate_result
......@@ -240,7 +249,8 @@ class TestGenerate(TestCase):
self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/zip')
output_url = join(self.tmp_url, "zip.zip")
open(output_url, 'w').write(decodestring(response_dict['data']))
with open(output_url, 'wb') as f:
f.write(decodestring(response_dict['data']))
self.assertTrue(is_zipfile(output_url))
filename_list = [file.filename for file in ZipFile(output_url).filelist]
for filename in filename_list:
......@@ -254,9 +264,10 @@ class TestGenerate(TestCase):
def testRunGenerateMethodConvertOdsToMsXslx(self):
"""Test run_generate method from ods to ms.xlsx. This test is to validate
a bug convertions to html"""
with open(join('data', 'test.ods'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.ods',
encodestring(
open(join('data', 'test.ods')).read()),
encodestring(data),
None, 'ms.xlsx',
"application/vnd.oasis.opendocument.spreadsheet")
response_code, response_dict, response_message = generate_result
......@@ -270,9 +281,10 @@ class TestGenerate(TestCase):
def testPNGFileToConvertOdpToHTML(self):
"""Test run_generate method from odp with png to html.
This test if returns good png files"""
with open(join('data', 'test_png.odp'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test_png.odp',
encodestring(
open(join('data', 'test_png.odp')).read()),
encodestring(data),
None, 'html',
'application/vnd.oasis.opendocument.presentation')
response_code, response_dict, response_message = generate_result
......@@ -281,16 +293,18 @@ class TestGenerate(TestCase):
self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/zip')
output_url = join(self.tmp_url, "zip.zip")
open(output_url, 'w').write(decodestring(response_dict['data']))
with open(output_url, 'wb') as f:
f.write(decodestring(response_dict['data']))
self.assertTrue(is_zipfile(output_url))
zipfile = ZipFile(output_url)
try:
png_path = join(self.tmp_url, "img0.png")
zipfile.extractall(self.tmp_url)
content_type = self._getFileType(encodestring(open(png_path).read()))
with open(png_path, 'rb') as f:
content_type = self._getFileType(encodestring(f.read()))
self.assertEqual(content_type, 'image/png')
m = magic.Magic()
self.assertTrue("8-bit/color RGB" in m.from_file(png_path))
self.assertIn("8-bit/color RGB", m.from_file(png_path))
finally:
zipfile.close()
if exists(output_url):
......@@ -301,9 +315,10 @@ class TestGenerate(TestCase):
def testRunGenerateMethodConvertOdpToHTML(self):
"""Test run_generate method from odp to html. This test is to validate
a bug convertions to html"""
with open(join('data', 'test.odp'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.odp',
encodestring(
open(join('data', 'test.odp')).read()),
encodestring(data),
None, 'html',
'application/vnd.oasis.opendocument.presentation')
response_code, response_dict, response_message = generate_result
......@@ -312,7 +327,8 @@ class TestGenerate(TestCase):
self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/zip')
output_url = join(self.tmp_url, "zip.zip")
open(output_url, 'w').write(decodestring(response_dict['data']))
with open(output_url, 'wb') as f:
f.write(decodestring(response_dict['data'].encode()))
self.assertTrue(is_zipfile(output_url))
filename_list = [file.filename for file in ZipFile(output_url).filelist]
for filename in filename_list:
......@@ -343,9 +359,10 @@ class TestGenerate(TestCase):
class TestSetMetadata(TestCase):
def testRunSetMetadata(self):
"""Test run_setmetadata method, updating the same metadata"""
with open(join('data', 'testMetadata.odt'), 'rb') as f:
data = f.read()
setmetadata_result = self.proxy.run_setmetadata('testMetadata.odt',
encodestring(
open(join('data', 'testMetadata.odt')).read()),
encodestring(data),
{"Title": "testSetMetadata", "Description": "Music"})
response_code, response_dict, response_message = setmetadata_result
self.assertEqual(response_code, 200)
......@@ -372,9 +389,10 @@ class TestSetMetadata(TestCase):
def testRunSetMetadataFailResponse(self):
"""Test run_setmetadata method with invalid document"""
with open(join('data', 'testMetadata.odt'), 'rb') as f:
data = f.read()[:100]
setmetadata_result = self.proxy.run_setmetadata('testMetadata.odt',
encodestring(
open(join('data', 'testMetadata.odt')).read()[:100]),
encodestring(data),
{"Title": "testSetMetadata", "Description": "Music"})
response_code, response_dict, response_message = setmetadata_result
self.assertEqual(response_code, 402)
......@@ -403,8 +421,10 @@ class TestGetTableItemList(TestCase):
table_list = [['Developers', ''],
['Prices', 'Table 1: Prices table from Mon Restaurant'],
['SoccerTeams', 'Tabela 2: Soccer Teams']]
with open("data/granulate_table_test.odt", "rb") as f:
data = f.read()
granulated_table = self.proxy.getTableItemList(
encodestring(open("data/granulate_table_test.odt").read()),
encodestring(data),
"odt")
self.assertEqual(table_list, granulated_table)
......@@ -413,14 +433,17 @@ class TestGetTableItemList(TestCase):
table_list = [['Table1', ''],
['Table2', 'Table 1: Prices table from Mon Restaurant'],
['Table3', 'Tabela 2: Soccer Teams']]
with open("data/granulate_table_test.doc", "rb") as f:
data = f.read()
granulated_table = self.proxy.getTableItemList(
encodestring(open("data/granulate_table_test.doc").read()),
encodestring(data),
"doc")
self.assertEqual(table_list, granulated_table)
def testGetTableFromOdt(self):
"""Test if getTableItemList can get a item of some granulated table from odt file"""
data = encodestring(open("./data/granulate_table_test.odt").read())
with open("./data/granulate_table_test.odt", "rb") as f:
data = encodestring(f.read())
granulated_table = self.proxy.getTableItemList(data, "odt")
table_item = decodestring(self.proxy.getTable(data, granulated_table[1][0],
"odt"))
......@@ -435,7 +458,8 @@ class TestGetTableItemList(TestCase):
def testGetTableFromDoc(self):
"""Test if getTableItemList can get a item of some granulated table from doc file"""
data = encodestring(open("./data/granulate_table_test.doc").read())
with open("./data/granulate_table_test.odt", "rb") as f:
data = encodestring(f.read())
granulated_table = self.proxy.getTableItemList(data, "doc")
self.proxy.getTable(data, granulated_table[1][0], "doc")
table_item = decodestring(self.proxy.getTable(data, granulated_table[1][0],
......@@ -451,24 +475,29 @@ class TestGetTableItemList(TestCase):
def testGetColumnItemListFromOdt(self):
"""Test if getColumnItemList can get the list of column item from odt file"""
with open("./data/granulate_table_test.odt", "rb") as f:
data = encodestring(f.read())
columns = self.proxy.getColumnItemList(
encodestring(open("./data/granulate_table_test.odt").read()),
data,
"SoccerTeams",
"odt")
self.assertEqual([[0, 'Name'], [1, 'Country']], columns)
def testGetColumnItemListFromDoc(self):
"""Test if getColumnItemList can get the list of column item from doc file"""
with open("./data/granulate_table_test.doc", "rb") as f:
data = encodestring(f.read())
#in the doc format the tables lose their names
columns = self.proxy.getColumnItemList(
encodestring(open("./data/granulate_table_test.doc").read()),
data,
"Table3",
"doc")
self.assertEqual([[0, 'Name'], [1, 'Country']], columns)
def testGetLineItemListFromOdt(self):
"""Test if getLineItemList can get the list of lines items from odt file"""
data = encodestring(open("./data/granulate_table_test.odt").read())
with open("./data/granulate_table_test.odt", "rb") as f:
data = encodestring(f.read())
line_item_list = self.proxy.getLineItemList(data, "Developers", "odt")
self.assertEqual([['Name', 'Hugo'], ['Phone', '+55 (22) 8888-8888'],
['Email', 'hugomaia@tiolive.com'], ['Name', 'Rafael'],
......@@ -477,7 +506,8 @@ class TestGetTableItemList(TestCase):
def testGetLineItemListFromDoc(self):
"""Test if getLineItemList can get the list of lines items from doc file"""
data = encodestring(open("./data/granulate_table_test.doc").read())
with open("./data/granulate_table_test.doc", "rb") as f:
data = encodestring(f.read())
line_item_list = self.proxy.getLineItemList(data, "Table1", "doc")
self.assertEqual([['Name', 'Hugo'], ['Phone', '+55 (22) 8888-8888'],
['Email', 'hugomaia@tiolive.com'], ['Name', 'Rafael'],
......@@ -488,7 +518,8 @@ class TestGetTableItemList(TestCase):
class TestImagetItemList(TestCase):
def testGetImageItemListFromOdt(self):
"""Test if getImageItemList can get the list of images items from odt file"""
data = encodestring(open("./data/granulate_test.odt").read())
with open("./data/granulate_test.odt", "rb") as f:
data = encodestring(f.read())
image_list = self.proxy.getImageItemList(data, "odt")
self.assertEqual([['10000000000000C80000009CBF079A6E41EE290C.jpg', ''],
['10000201000000C80000004E85B3F70C71E07CE8.png', 'TioLive Logo'],
......@@ -499,7 +530,8 @@ class TestImagetItemList(TestCase):
def testGetImageItemListFromDoc(self):
"""Test if getImageItemList can get the list of images items from doc file"""
data = encodestring(open("./data/granulate_test.doc").read())
with open("./data/granulate_test.doc", "rb") as f:
data = encodestring(f.read())
image_list = self.proxy.getImageItemList(data, "doc")
self.assertEqual([['10000000000000C80000009CBF079A6E41EE290C.jpg', ''],
['10000201000000C80000004E85B3F70C71E07CE8.png', 'TioLive Logo'],
......@@ -510,7 +542,8 @@ class TestImagetItemList(TestCase):
def testGetImageFromOdt(self):
"""Test if getImage can get a image from odt file after zip"""
data = encodestring(open("./data/granulate_test.odt").read())
with open("./data/granulate_test.odt", "rb") as f:
data = encodestring(f.read())
zip = ZipFile(StringIO(decodestring(data)))
image_id = '10000201000000C80000004E7B947D46.png'
original_image = zip.read('Pictures/%s' % image_id)
......@@ -519,7 +552,8 @@ class TestImagetItemList(TestCase):
def testGetImageFromDoc(self):
"""Test if getImage can get a image from doc file after zip"""
data = encodestring(open("./data/granulate_test.doc").read())
with open("./data/granulate_test.doc", "rb") as f:
data = encodestring(f.read())
#This conversion is necessary to catch the image from the doc file;
#so compare with the server return.
data_odt = self.proxy.convertFile(data, 'doc', 'odt', False)
......@@ -533,7 +567,8 @@ class TestImagetItemList(TestCase):
class TestParagraphItemList(TestCase):
def testGetParagraphItemList(self):
"""Test if getParagraphItemList can get paragraphs correctly from document"""
data = encodestring(open("./data/granulate_test.odt").read())
with open("./data/granulate_test.odt", "rb") as f:
data = encodestring(f.read())
paragraph_list = self.proxy.getParagraphItemList(data, "odt")
self.assertEqual([[0, 'P3'], [1, 'P1'], [2, 'P12'], [3, 'P6'], [4, 'P7'],
[5, 'P8'], [6, 'P6'], [7, 'P6'], [8, 'P13'], [9, 'P9'],
......@@ -545,7 +580,8 @@ class TestParagraphItemList(TestCase):
def testGetParagraphItem(self):
"""Test if getParagraph can get a paragraph"""
data = encodestring(open("./data/granulate_test.odt").read())
with open("./data/granulate_test.odt", "rb") as f:
data = encodestring(f.read())
paragraph = self.proxy.getParagraph(data, 1, "odt")
self.assertEqual(['', 'P1'], paragraph)
......@@ -553,7 +589,8 @@ class TestParagraphItemList(TestCase):
class TestChapterItemList(TestCase):
def testGetChapterItemList(self):
"""Test if getChapterItemList can get the chapters list correctly from document"""
data = encodestring(open("./data/granulate_chapters_test.odt").read())
with open("./data/granulate_chapters_test.odt", "rb") as f:
data = encodestring(f.read())
chapter_list = self.proxy.getChapterItemList(data, "odt")
self.assertEqual([[0, 'Title 0'], [1, 'Title 1'], [2, 'Title 2'],
[3, 'Title 3'], [4, 'Title 4'], [5, 'Title 5'],
......@@ -562,7 +599,8 @@ class TestChapterItemList(TestCase):
def testGetChapterItem(self):
"""Test if getChapterItem can get a chapter"""
data = encodestring(open("./data/granulate_chapters_test.odt").read())
with open("./data/granulate_chapters_test.odt", "rb") as f:
data = encodestring(f.read())
chapter = self.proxy.getChapterItem(1, data, "odt")
self.assertEqual(['Title 1', 1], chapter)
......@@ -573,7 +611,8 @@ class TestCSVEncoding(TestCase):
* the fields delimiter is guessed by python csv module.
"""
def test_decode_ascii(self):
data = encodestring(open("./data/csv_ascii.csv").read())
with open("./data/csv_ascii.csv", "rb") as f:
data = encodestring(f.read())
converted = decodestring(self.proxy.convertFile(data, "csv", "html"))
parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser)
......@@ -582,7 +621,8 @@ class TestCSVEncoding(TestCase):
[x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text])
def test_decode_utf8(self):
data = encodestring(open("./data/csv_utf8.csv").read())
with open("./data/csv_utf8.csv", "rb") as f:
data = encodestring(f.read())
converted = decodestring(self.proxy.convertFile(data, "csv", "html"))
parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser)
......@@ -594,7 +634,8 @@ class TestCSVEncoding(TestCase):
[x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text])
def test_decode_latin9(self):
data = encodestring(open("./data/csv_latin9.csv").read())
with open("./data/csv_latin9.csv", "rb") as f:
data = encodestring(f.read())
converted = decodestring(self.proxy.convertFile(data, "csv", "html"))
parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser)
......@@ -603,7 +644,8 @@ class TestCSVEncoding(TestCase):
[x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text])
def test_separator_semicolon(self):
data = encodestring(open("./data/csv_semicolon.csv").read())
with open("./data/csv_semicolon.csv", "rb") as f:
data = encodestring(f.read())
converted = decodestring(self.proxy.convertFile(data, "csv", "html"))
parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser)
......@@ -615,7 +657,8 @@ class TestCSVEncoding(TestCase):
[x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text])
def test_separator_tab(self):
data = encodestring(open("./data/tsv.tsv").read())
with open("./data/tsv.tsv", "rb") as f:
data = encodestring(f.read())
converted = decodestring(self.proxy.convertFile(data, "csv", "html"))
parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser)
......
......@@ -50,7 +50,8 @@ class TestUnoConverter(HandlerTestCase):
""" """
openoffice.acquire()
self.hostname, self.port = openoffice.getAddress()
data = open("data/test.odt", 'r').read()
with open("data/test.odt", "rb") as f:
data = f.read()
self.document = FileSystemDocument(self.tmp_url, data, 'odt')
def tearDown(self):
......
......@@ -41,14 +41,16 @@ class TestHandler(HandlerTestCase):
def testConvertPDFtoText(self):
"""Test conversion of pdf to txt"""
pdf_document = open("data/test.pdf").read()
with open("data/test.pdf", "rb") as f:
pdf_document = f.read()
handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw)
txt_document = handler.convert("txt")
self.assertTrue(txt_document.startswith("UNG Docs Architecture"))
self.assertTrue(txt_document.startswith(b"UNG Docs Architecture"))
def testgetMetadata(self):
"""Test if the metadata are extracted correctly"""
pdf_document = open("data/test.pdf").read()
with open("data/test.pdf", "rb") as f:
pdf_document = f.read()
handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw)
metadata = handler.getMetadata()
self.assertEqual(type(metadata), DictType)
......@@ -57,7 +59,8 @@ class TestHandler(HandlerTestCase):
def testsetMetadata(self):
"""Test if the metadata is inserted correctly"""
pdf_document = open("data/test.pdf").read()
with open("data/test.pdf", "rb") as f:
pdf_document = f.read()
handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw)
metadata_dict = {"title": "Set Metadata Test", "creator": "gabriel\'@"}
new_document = handler.setMetadata(metadata_dict)
......
......@@ -45,14 +45,17 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self):
return [
# Test to verify if server fail when a empty string is sent
('', '', ''),
scenario_list = [
# Test to verify if server fail when a empty file is sent
(b'', '', ''),
]
# Try convert one video for a invalid format
(open(join('data', 'test.pdf')).read(), 'pdf', 'xyz'),
with open(join('data', 'test.pdf'), 'rb') as f:
scenario_list.append((f.read(), 'pdf', 'xyz'))
# Try convert one video to format not possible
(open(join('data', 'test.pdf')).read(), 'pdf', 'ogv'),
]
with open(join('data', 'test.pdf'), 'rb') as f:
scenario_list.append((f.read(), 'pdf', 'ogv'))
return scenario_list
def testFaultConversion(self):
"""Test fail convertion of Invalid pdf files"""
......
......@@ -40,7 +40,8 @@ class TestHandler(HandlerTestCase):
self.kw = dict(env=dict(PATH=self.env_path))
def _testBase(self, html_path, **conversion_kw):
html_file = open(html_path).read()
with open(html_path, 'rb') as f:
html_file = f.read()
handler = Handler(self.tmp_url, html_file, "html", **self.kw)
pdf_file = handler.convert("pdf", **conversion_kw)
mime = magic.Magic(mime=True)
......@@ -67,16 +68,18 @@ class TestHandler(HandlerTestCase):
def testConvertHtmlWithTableOfContent(self):
"""Test conversion of html with an additional table of content"""
with open("data/test_toc.xsl", 'rb') as f:
xsl_style_sheet_data = f.read()
self._testBase(
"data/test_with_toc.html",
toc=True,
xsl_style_sheet_data=b64encode(open("data/test_toc.xsl").read()),
xsl_style_sheet_data=b64encode(xsl_style_sheet_data),
)
# XXX how to check for table of content presence ?
def testsetMetadata(self):
""" Test if metadata are inserted correclty """
handler = Handler(self.tmp_url, "", "png", **self.kw)
handler = Handler(self.tmp_url, b"", "png", **self.kw)
self.assertRaises(NotImplementedError, handler.setMetadata)
def testGetAllowedConversionFormatList(self):
......
......@@ -44,10 +44,11 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self):
return [
# Test to verify if server fail when a empty string is sent
('', '', ''),
# Try convert one html for a invalid format
(open(join('data', 'test_with_png_dataurl.html')).read(), 'html', 'xyz'),
scenario_list = [
# Test to verify if server fail when a empty file is sent
(b'', '', ''),
]
# Try convert one html for a invalid format
with open(join('data', 'test_with_png_dataurl.html'), 'rb') as f:
scenario_list.append((f.read(), 'html', 'xyz'))
return scenario_list
......@@ -321,7 +321,8 @@ class Handler(object):
absolute_path = os.path.join(root, file_name)
file_name = os.path.join(relative_root, file_name)
zipfile.write(absolute_path, file_name)
output_data = open(output_file_name).read()
with open(output_file_name, 'rb') as f:
output_data = f.read()
finally:
os.unlink(output_file_name)
return output_data
......
......@@ -52,12 +52,14 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self):
return [
# Test to verify if server fail when a empty string is sent
('', '', ''),
# Try convert one xlsx for a invalid format
(open(join('data', 'test.xlsx')).read(), 'xlsx', 'xyz'),
scenario_list = [
# Test to verify if server fail when a empty file is sent
(b'', '', ''),
]
# Try convert one xlsx for a invalid format
with open(join('data', 'test.xlsx'), 'rb') as f:
scenario_list.append((f.read(), 'xlsx', 'xyz'))
return scenario_list
def test_xlsx_to_xlsy(self):
with open(join('data', 'test.xlsx')) as f:
......
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