Commit 7a356983 authored by Gabriel Monnerat's avatar Gabriel Monnerat

clean up the code and use python-magic

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@42189 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 501ed14f
......@@ -27,7 +27,8 @@
##############################################################################
import unittest
from subprocess import Popen, PIPE
import magic
from StringIO import StringIO
from base64 import decodestring
from os import path
from os import remove
......@@ -102,25 +103,22 @@ class TestFileSystemDocument(unittest.TestCase):
def testZipDocumentList(self):
"""Tests if the zip file is returned correctly"""
zip_output_url = path.join(self.tmp_url, 'ziptest.zip')
open(path.join(self.fsdocument.directory_name, 'document2'), 'w').write('test')
zip_file = self.fsdocument.getContent(True)
open(zip_output_url, 'w').write(zip_file)
command = ["file", zip_output_url]
stdout, stderr = Popen(command,
stdout=PIPE).communicate()
self.assertEquals(stdout, '/tmp/ziptest.zip: Zip archive data, at least v2.0 to extract\n')
ziptest = ZipFile(zip_output_url, 'r')
mime = magic.Magic(mime=True)
mimetype = mime.from_buffer(zip_file)
self.assertEquals(mimetype, 'application/zip')
ziptest = ZipFile(StringIO(zip_file), 'r')
self.assertEquals(len(ziptest.filelist), 2)
for file in ziptest.filelist:
if file.filename.endswith("document2"):
self.assertEquals(file.file_size, 4)
else:
self.assertEquals(file.file_size, 9)
remove(zip_output_url)
def testSendZipFile(self):
"""Tests if the htm is extrated from zipfile"""
# XXX it seems that only zipfile module is tested here
zip_input_url = 'data/test.zip'
zip_output_url = path.join(self.tmp_url, 'zipdocument.zip')
try:
......@@ -138,7 +136,3 @@ class TestFileSystemDocument(unittest.TestCase):
def test_suite():
return make_suite(TestFileSystemDocument)
if "__main__" == __name__:
suite = unittest.TestLoader().loadTestsFromTestCase(TestFileSystemDocument)
unittest.TextTestRunner(verbosity=2).run(suite)
......@@ -26,9 +26,8 @@
#
##############################################################################
import unittest
import magic
from os import path
from subprocess import Popen, PIPE
from base64 import encodestring, decodestring
from cloudoooTestCase import CloudoooTestCase
from cloudooo.handler.ooo.handler import OOHandler
......@@ -52,14 +51,11 @@ class TestOOHandler(CloudoooTestCase):
new_file.close()
self._file_path_list.append(document_output_url)
def _assert_document_output(self, document_output_url, msg):
def _assert_document_output(self, document, expected_mimetype):
"""Check if the document was created correctly"""
command_list = ["file", "-b", document_output_url]
stdout, stderr = Popen(command_list,
stdout=PIPE).communicate()
self.assertEquals(msg in stdout,
True,
"\nStdout: %sMsg: %s" % (stdout, msg))
mime = magic.Magic(mime_encoding=True)
mimetype = mime.from_buffer(document)
self.assertEquals(mimetype, expected_mimetype)
def tearDown(self):
"""Cleanup temp files
......@@ -70,7 +66,6 @@ class TestOOHandler(CloudoooTestCase):
os.remove(file_path)
CloudoooTestCase.tearDown(self)
def testConvertOdtToDoc(self):
"""Test convert ODT to DOC"""
data = encodestring(open("data/test.odt").read())
......@@ -78,10 +73,7 @@ class TestOOHandler(CloudoooTestCase):
decodestring(data),
'odt')
doc_exported = handler.convert("doc")
document_output_url = path.join(self.tmp_url, "testExport.doc")
self._save_document(document_output_url, doc_exported)
msg = 'Microsoft Office Document'
self._assert_document_output(document_output_url, msg)
self._assert_document_output(doc_exported, "application/msword")
def testConvertDocToOdt(self):
"""Test convert DOC to ODT"""
......@@ -90,10 +82,8 @@ class TestOOHandler(CloudoooTestCase):
decodestring(data),
'doc')
doc_exported = handler.convert("odt")
document_output_url = path.join(self.tmp_url, "testConvert.odt")
self._save_document(document_output_url, doc_exported)
msg = 'OpenDocument Text\n'
self._assert_document_output(document_output_url, msg)
self._assert_document_output(doc_exported,
"application/vnd.oasis.opendocument.text")
def testGetMetadata(self):
"""Test getMetadata"""
......@@ -140,10 +130,8 @@ class TestOOHandler(CloudoooTestCase):
decodestring(data),
'doc')
doc_exported = handler.convert("odt")
document_output_url = path.join(self.tmp_url, "testConvert.odt")
self._save_document(document_output_url, doc_exported)
msg = 'OpenDocument Text\n'
self._assert_document_output(document_output_url, msg)
self._assert_document_output(doc_exported,
"application/vnd.oasis.opendocument.text")
def testGetMetadataWithOpenOfficeStopped(self):
"""Test getMetadata with openoffice stopped"""
......
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