Commit ae2759f3 authored by Gabriel Monnerat's avatar Gabriel Monnerat

refactor and clean up tests

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@38567 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 9f575385
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
import unittest import unittest
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from base64 import decodestring from base64 import decodestring
from os.path import exists, join from os import path
from os import remove from os import remove
from zipfile import ZipFile, is_zipfile from zipfile import ZipFile, is_zipfile
from cloudooo.document import FileSystemDocument from cloudooo.document import FileSystemDocument
...@@ -54,17 +54,17 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -54,17 +54,17 @@ class TestFileSystemDocument(unittest.TestCase):
original state""" original state"""
old_document_url = self.fsdocument.getUrl() old_document_url = self.fsdocument.getUrl()
document_filename = "document" document_filename = "document"
document_test_url = join(self.fsdocument.directory_name, document_filename) document_test_url = path.join(self.fsdocument.directory_name, document_filename)
open(document_test_url,'wb').write(decodestring("Test Document")) open(document_test_url,'wb').write(decodestring("Test Document"))
self.fsdocument.reload(document_test_url) self.fsdocument.reload(document_test_url)
self.assertEquals(exists(old_document_url), False) self.assertEquals(path.exists(old_document_url), False)
self.assertNotEquals(self.fsdocument.original_data, self.assertNotEquals(self.fsdocument.original_data,
self.fsdocument.getContent()) self.fsdocument.getContent())
old_document_url = self.fsdocument.getUrl() old_document_url = self.fsdocument.getUrl()
self.fsdocument.restoreOriginal() self.fsdocument.restoreOriginal()
self.assertEquals(exists(old_document_url), False) self.assertEquals(path.exists(old_document_url), False)
self.assertNotEquals(old_document_url, self.fsdocument.getUrl()) self.assertNotEquals(old_document_url, self.fsdocument.getUrl())
self.assertEquals(exists(self.fsdocument.getUrl()), True) self.assertEquals(path.exists(self.fsdocument.getUrl()), True)
self.assertEquals(self.fsdocument.getContent(), self.data) self.assertEquals(self.fsdocument.getContent(), self.data)
def testgetContent(self): def testgetContent(self):
...@@ -74,7 +74,7 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -74,7 +74,7 @@ class TestFileSystemDocument(unittest.TestCase):
def testgetUrl(self): def testgetUrl(self):
"""Check if the url is correct""" """Check if the url is correct"""
url = self.fsdocument.getUrl() url = self.fsdocument.getUrl()
self.assertEquals(exists(url), True) self.assertEquals(path.exists(url), True)
def testLoadDocumentFile(self): def testLoadDocumentFile(self):
"""Test if the document is created correctly""" """Test if the document is created correctly"""
...@@ -82,30 +82,30 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -82,30 +82,30 @@ class TestFileSystemDocument(unittest.TestCase):
tmp_document = open(url,'r').read() tmp_document = open(url,'r').read()
self.assertEquals(self.data, tmp_document) self.assertEquals(self.data, tmp_document)
self.fsdocument.trash() self.fsdocument.trash()
self.assertEquals(exists(url), False) self.assertEquals(path.exists(url), False)
def testReload(self): def testReload(self):
"""Change url and check if occurs correctly""" """Change url and check if occurs correctly"""
old_document_url = self.fsdocument.getUrl() old_document_url = self.fsdocument.getUrl()
document_filename = "document" document_filename = "document"
document_test_url = join(self.fsdocument.directory_name, document_filename) document_test_url = path.join(self.fsdocument.directory_name, document_filename)
open(document_test_url,'wb').write(self.data) open(document_test_url,'wb').write(self.data)
self.fsdocument.reload(document_test_url) self.fsdocument.reload(document_test_url)
url = self.fsdocument.getUrl() url = self.fsdocument.getUrl()
self.assertEquals(exists(old_document_url), False) self.assertEquals(path.exists(old_document_url), False)
self.assertEquals(self.fsdocument.getContent(), self.data) self.assertEquals(self.fsdocument.getContent(), self.data)
self.fsdocument.trash() self.fsdocument.trash()
self.assertEquals(exists(url), False) self.assertEquals(path.exists(url), False)
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 = 'output/ziptest.zip' zip_output_url = path.join(self.tmp_url, 'ziptest.zip')
open(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) open(zip_output_url, 'w').write(zip_file)
stdout, stderr = Popen("file %s" % zip_output_url, stdout, stderr = Popen("file %s" % zip_output_url,
shell=True, stdout=PIPE).communicate() shell=True, stdout=PIPE).communicate()
self.assertEquals(stdout, 'output/ziptest.zip: Zip archive data, at least v2.0 to extract\n') self.assertEquals(stdout, '/tmp/ziptest.zip: Zip archive data, at least v2.0 to extract\n')
ziptest = ZipFile(zip_output_url, 'r') 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:
...@@ -118,14 +118,18 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -118,14 +118,18 @@ class TestFileSystemDocument(unittest.TestCase):
def testSendZipFile(self): def testSendZipFile(self):
"""Tests if the htm is extrated from zipfile""" """Tests if the htm is extrated from zipfile"""
zip_input_url = 'data/test.zip' zip_input_url = 'data/test.zip'
zip_output_url = 'output/zipdocument.zip' zip_output_url = path.join(self.tmp_url, 'zipdocument.zip')
data = open(zip_input_url).read() try:
zipdocument = FileSystemDocument(self.tmp_url, data, 'zip') data = open(zip_input_url).read()
open(zip_output_url, 'w').write(zipdocument.getContent(True)) zipdocument = FileSystemDocument(self.tmp_url, data, 'zip')
self.assertEquals(is_zipfile(zip_output_url), True) open(zip_output_url, 'w').write(zipdocument.getContent(True))
zipfile = ZipFile(zip_output_url) self.assertEquals(is_zipfile(zip_output_url), True)
self.assertEquals(sorted(zipfile.namelist()), zipfile = ZipFile(zip_output_url)
self.assertEquals(sorted(zipfile.namelist()),
sorted(['logo.gif','test.htm'])) sorted(['logo.gif','test.htm']))
finally:
if path.exists(zip_output_url):
remove(zip_output_url)
def test_suite(): def test_suite():
return make_suite(TestFileSystemDocument) return make_suite(TestFileSystemDocument)
......
...@@ -180,7 +180,6 @@ class TestMimeMapper(cloudoooTestCase): ...@@ -180,7 +180,6 @@ class TestMimeMapper(cloudoooTestCase):
hostname, port = openoffice.getAddress() hostname, port = openoffice.getAddress()
self.mimemapper.loadFilterList(hostname, self.mimemapper.loadFilterList(hostname,
port, port,
unomimemapper_bin=self.unomimemapper_bin,
python_path=self.python_path) python_path=self.python_path)
openoffice.release() openoffice.release()
......
...@@ -51,18 +51,12 @@ class TestOOHandler(cloudoooTestCase): ...@@ -51,18 +51,12 @@ class TestOOHandler(cloudoooTestCase):
True, True,
"\nStdout: %sMsg: %s" % (stdout, msg)) "\nStdout: %sMsg: %s" % (stdout, msg))
def afterSetUp(self):
""" """
self.kw = dict(unoconverter_bin=self.unoconverter_bin,
python_path=self.python_path)
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())
handler = OOHandler(self.tmp_url, handler = OOHandler(self.tmp_url,
decodestring(data), decodestring(data),
'odt', 'odt')
**self.kw)
doc_exported = handler.convert("doc") doc_exported = handler.convert("doc")
document_output_url = path.join(self.tmp_url, "testExport.doc") document_output_url = path.join(self.tmp_url, "testExport.doc")
self._save_document(document_output_url, doc_exported) self._save_document(document_output_url, doc_exported)
...@@ -74,8 +68,7 @@ class TestOOHandler(cloudoooTestCase): ...@@ -74,8 +68,7 @@ class TestOOHandler(cloudoooTestCase):
data = encodestring(open("data/test.doc").read()) data = encodestring(open("data/test.doc").read())
handler = OOHandler(self.tmp_url, handler = OOHandler(self.tmp_url,
decodestring(data), decodestring(data),
'doc', 'doc')
**self.kw)
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
document_output_url = path.join(self.tmp_url, "testConvert.odt") document_output_url = path.join(self.tmp_url, "testConvert.odt")
self._save_document(document_output_url, doc_exported) self._save_document(document_output_url, doc_exported)
...@@ -87,8 +80,7 @@ class TestOOHandler(cloudoooTestCase): ...@@ -87,8 +80,7 @@ class TestOOHandler(cloudoooTestCase):
data = encodestring(open("data/test.odt").read()) data = encodestring(open("data/test.odt").read())
handler = OOHandler(self.tmp_url, handler = OOHandler(self.tmp_url,
decodestring(data), decodestring(data),
'odt', 'odt')
**self.kw)
metadata = handler.getMetadata() metadata = handler.getMetadata()
self.assertEquals(metadata.get('Data'), '') self.assertEquals(metadata.get('Data'), '')
self.assertEquals(metadata.has_key('Data'), True) self.assertEquals(metadata.has_key('Data'), True)
...@@ -103,13 +95,11 @@ class TestOOHandler(cloudoooTestCase): ...@@ -103,13 +95,11 @@ class TestOOHandler(cloudoooTestCase):
data = encodestring(open("data/test.odt").read()) data = encodestring(open("data/test.odt").read())
handler = OOHandler(self.tmp_url, handler = OOHandler(self.tmp_url,
decodestring(data), decodestring(data),
'odt', 'odt')
**self.kw)
new_data = handler.setMetadata({"Title": "cloudooo Test -"}) new_data = handler.setMetadata({"Title": "cloudooo Test -"})
new_handler = OOHandler(self.tmp_url, new_handler = OOHandler(self.tmp_url,
new_data, new_data,
'odt', 'odt')
**self.kw)
metadata = new_handler.getMetadata() metadata = new_handler.getMetadata()
self.assertEquals(metadata.get('Title'), "cloudooo Test -") self.assertEquals(metadata.get('Title'), "cloudooo Test -")
...@@ -119,8 +109,7 @@ class TestOOHandler(cloudoooTestCase): ...@@ -119,8 +109,7 @@ class TestOOHandler(cloudoooTestCase):
data = encodestring(open("data/test.doc").read()) data = encodestring(open("data/test.doc").read())
handler = OOHandler(self.tmp_url, handler = OOHandler(self.tmp_url,
decodestring(data), decodestring(data),
'doc', 'doc')
**self.kw)
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
document_output_url = path.join(self.tmp_url, "testConvert.odt") document_output_url = path.join(self.tmp_url, "testConvert.odt")
self._save_document(document_output_url, doc_exported) self._save_document(document_output_url, doc_exported)
...@@ -133,8 +122,7 @@ class TestOOHandler(cloudoooTestCase): ...@@ -133,8 +122,7 @@ class TestOOHandler(cloudoooTestCase):
data = encodestring(open("data/test.odt").read()) data = encodestring(open("data/test.odt").read())
handler = OOHandler(self.tmp_url, handler = OOHandler(self.tmp_url,
decodestring(data), decodestring(data),
'odt', 'odt')
**self.kw)
metadata = handler.getMetadata() metadata = handler.getMetadata()
self.assertEquals(metadata.get('Title'), 'title') self.assertEquals(metadata.get('Title'), 'title')
self.assertEquals(metadata.get('MIMEType'), self.assertEquals(metadata.get('MIMEType'),
...@@ -146,13 +134,11 @@ class TestOOHandler(cloudoooTestCase): ...@@ -146,13 +134,11 @@ class TestOOHandler(cloudoooTestCase):
data = encodestring(open("data/test.doc").read()) data = encodestring(open("data/test.doc").read())
handler = OOHandler(self.tmp_url, handler = OOHandler(self.tmp_url,
decodestring(data), decodestring(data),
'doc', 'doc')
**self.kw)
new_data = handler.setMetadata({"Title": "cloudooo Test -"}) new_data = handler.setMetadata({"Title": "cloudooo Test -"})
new_handler = OOHandler(self.tmp_url, new_handler = OOHandler(self.tmp_url,
new_data, new_data,
'doc', 'doc')
**self.kw)
metadata = new_handler.getMetadata() metadata = new_handler.getMetadata()
self.assertEquals(metadata.get('Title'), "cloudooo Test -") self.assertEquals(metadata.get('Title'), "cloudooo Test -")
......
...@@ -42,8 +42,7 @@ class TestOpenOffice(cloudoooTestCase): ...@@ -42,8 +42,7 @@ class TestOpenOffice(cloudoooTestCase):
self.working_path, self.working_path,
self.virtual_display_id, self.virtual_display_id,
self.office_binary_path, self.office_binary_path,
self.uno_path, self.uno_path)
openoffice_tester_bin=self.openoffice_tester_bin)
self.openoffice.start() self.openoffice.start()
def tearDown(self): def tearDown(self):
......
...@@ -462,7 +462,6 @@ class TestServer(cloudoooTestCase): ...@@ -462,7 +462,6 @@ class TestServer(cloudoooTestCase):
else: else:
self.fail("Not exists one file with 'impr.html' format") self.fail("Not exists one file with 'impr.html' format")
def testRunGenerateMethodFailResponse(self): def testRunGenerateMethodFailResponse(self):
"""Test run_generate method with invalid document""" """Test run_generate method with invalid document"""
data = open(join('data','test.odt'), 'r').read()[:100] data = open(join('data','test.odt'), 'r').read()[:100]
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
############################################################################## ##############################################################################
import unittest import unittest
import jsonpickle import jsonpickle, pkg_resources
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from os.path import exists from os.path import exists
from cloudoooTestCase import cloudoooTestCase, make_suite from cloudoooTestCase import cloudoooTestCase, make_suite
...@@ -56,8 +56,8 @@ class TestUnoConverter(cloudoooTestCase): ...@@ -56,8 +56,8 @@ class TestUnoConverter(cloudoooTestCase):
"""Test script unoconverter""" """Test script unoconverter"""
mimemapper_pickled = jsonpickle.encode(mimemapper) mimemapper_pickled = jsonpickle.encode(mimemapper)
command = [self.python_path, command = [self.python_path,
"-c", pkg_resources.resource_filename("cloudooo",
"'from cloudooo.bin.unoconverter import main;main()'", "helper/unoconverter.py"),
"'--convert'", "'--convert'",
"'--uno_path=%s'" % self.uno_path, "'--uno_path=%s'" % self.uno_path,
"'--office_binary_path=%s'" % self.office_binary_path, "'--office_binary_path=%s'" % self.office_binary_path,
...@@ -82,37 +82,6 @@ class TestUnoConverter(cloudoooTestCase): ...@@ -82,37 +82,6 @@ class TestUnoConverter(cloudoooTestCase):
self.document.trash() self.document.trash()
self.assertEquals(exists(output_url), False) self.assertEquals(exists(output_url), False)
def _testUnoConverterWithoutMimemapper(self):
"""Test script unoconverter without mimemapper serialized"""
command = [self.python_path,
"-c",
"'from cloudooo.bin.unoconverter import main;main()'",
"'--convert'",
"'--uno_path=%s'" % self.uno_path,
"'--office_binary_path=%s'" % self.office_binary_path,
"'--hostname=%s'" % self.hostname,
"'--port=%s'" % self.port,
"'--document_url=%s'" % self.document.getUrl(),
"'--destination_format=%s'" % "doc",
"'--source_format=%s'" % "odt",
"'--unomimemapper_bin=%s'" % self.unomimemapper_bin]
stdout, stderr = Popen(' '.join(command), shell=True,
stdout=PIPE, stderr=PIPE).communicate()
if not stdout:
self.fail(stderr)
output_url = stdout.replace('\n', '')
self.assertEquals(exists(output_url), True)
stdout, stderr = Popen("file %s" % output_url, shell=True,
stdout=PIPE, stderr=PIPE).communicate()
self.assertEquals(self.file_msg_list[1] in stdout \
or \
self.file_msg_list[0] in stdout,
True,
"%s don't have %s" % (self.file_msg_list, stdout))
self.document.trash()
self.assertEquals(exists(output_url), False)
def test_suite(): def test_suite():
return make_suite(TestUnoConverter) return make_suite(TestUnoConverter)
......
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