[Erp5-report] r42189 gabriel - /erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/

nobody at svn.erp5.org nobody at svn.erp5.org
Tue Jan 11 10:47:07 CET 2011


Author: gabriel
Date: Tue Jan 11 10:47:07 2011
New Revision: 42189

URL: http://svn.erp5.org?rev=42189&view=rev
Log:
clean up the code and use python-magic

Modified:
    erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/testFileSystemDocument.py
    erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/testOOHandler.py

Modified: erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/testFileSystemDocument.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/testFileSystemDocument.py?rev=42189&r1=42188&r2=42189&view=diff
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/testFileSystemDocument.py [utf8] (original)
+++ erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/testFileSystemDocument.py [utf8] Tue Jan 11 10:47:07 2011
@@ -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.Te
     
   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.Te
 
 def test_suite():
   return make_suite(TestFileSystemDocument)
-
-if "__main__" == __name__:
-  suite = unittest.TestLoader().loadTestsFromTestCase(TestFileSystemDocument)
-  unittest.TextTestRunner(verbosity=2).run(suite)

Modified: erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/testOOHandler.py
URL: http://svn.erp5.org/erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/testOOHandler.py?rev=42189&r1=42188&r2=42189&view=diff
==============================================================================
--- erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/testOOHandler.py [utf8] (original)
+++ erp5/trunk/utils/cloudooo/cloudooo/handler/ooo/tests/testOOHandler.py [utf8] Tue Jan 11 10:47:07 2011
@@ -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"""



More information about the Erp5-report mailing list