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