Commit 30a5c00d authored by Gabriel Monnerat's avatar Gabriel Monnerat

refactor tests to prevent code duplicated. Part of cloudoooTestCase.py was...

refactor tests to prevent code duplicated. Part of cloudoooTestCase.py was added in handlerTestCase.py to each handler implement your test case only when needed.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@43500 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 3fca6e1d
......@@ -28,24 +28,16 @@
import unittest
import sys
from ConfigParser import ConfigParser
from os import path, mkdir
from os import environ, putenv
from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.utils.utils import waitStartDaemon
from cloudooo.handler.ooo.mimemapper import mimemapper
from cloudooo.handler.tests.handlerTestCase import config, check_folder
config = ConfigParser()
testcase_path = path.dirname(__file__)
def check_folder(working_path, tmp_dir_path):
if not path.exists(working_path):
mkdir(working_path)
if not path.exists(tmp_dir_path):
mkdir(tmp_dir_path)
def make_suite(test_case):
"""Function is used to run all tests together"""
suite = unittest.TestSuite()
......@@ -103,27 +95,3 @@ def stopFakeEnvironment(stop_openoffice=True):
if stop_openoffice:
openoffice.stop()
return True
class CloudoooTestCase(unittest.TestCase):
"""Test Case to load cloudooo conf."""
def setUp(self):
"""Creates a environment to run the tests. Is called always before the
tests."""
server_cloudooo_conf = environ.get("server_cloudooo_conf", None)
if server_cloudooo_conf is not None:
config.read(server_cloudooo_conf)
self.hostname = config.get("server:main", "host")
self.cloudooo_port = config.get("server:main", "port")
self.openoffice_port = config.get("app:main", "openoffice_port")
self.office_binary_path = config.get("app:main", "office_binary_path")
self.python_path = sys.executable
self.working_path = config.get("app:main", "working_path")
self.tmp_url = path.join(self.working_path, "tmp")
check_folder(self.working_path, self.tmp_url)
self.uno_path = config.get("app:main", "uno_path")
self.afterSetUp()
def afterSetUp(self):
""" """
......@@ -28,13 +28,14 @@
from xmlrpclib import ServerProxy, Fault
from base64 import encodestring, decodestring
from cloudoooTestCase import CloudoooTestCase, make_suite
from cloudoooTestCase import make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
import magic
file_detector = magic.Magic()
DAEMON = True
class TestAllFormats(CloudoooTestCase):
class TestAllFormats(HandlerTestCase):
"""Test XmlRpc Server. Needs cloudooo server started"""
def afterSetUp(self):
......
......@@ -28,13 +28,14 @@
from xmlrpclib import ServerProxy, Fault
from base64 import encodestring, decodestring
from cloudoooTestCase import CloudoooTestCase, make_suite
from cloudoooTestCase import make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
import magic
file_detector = magic.Magic()
DAEMON = True
class TestAllFormatsERP5Compatibility(CloudoooTestCase):
class TestAllFormatsERP5Compatibility(HandlerTestCase):
"""
Test XmlRpc Server using ERP5 compatibility API.
Needs cloudooo server started
......
......@@ -29,7 +29,8 @@
from xmlrpclib import ServerProxy
from base64 import encodestring, decodestring
from multiprocessing import Process, Array
from cloudoooTestCase import CloudoooTestCase, make_suite
from cloudoooTestCase import make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
import magic
DAEMON = True
......@@ -43,7 +44,7 @@ def basicTestToGenerate(id, proxy, data, source_format, destination_format,
assert mimetype == 'application/pdf'
result_list[id] = True
class TestHighLoad(CloudoooTestCase):
class TestHighLoad(HandlerTestCase):
"""Test with many simultaneous connection"""
def afterSetUp(self):
......
......@@ -26,7 +26,7 @@
#
##############################################################################
from cloudoooTestCase import CloudoooTestCase
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.mimemapper import MimeMapper
from cloudoooTestCase import make_suite
......@@ -164,7 +164,7 @@ chart_expected_tuple = (
OPENOFFICE = True
class TestMimeMapper(CloudoooTestCase):
class TestMimeMapper(HandlerTestCase):
"""Test if object load filters correctly of OOo."""
def afterSetUp(self):
......
......@@ -27,13 +27,14 @@
##############################################################################
import cloudooo.handler.ooo.monitor as monitor
from cloudoooTestCase import CloudoooTestCase, make_suite
from cloudoooTestCase import make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.monitor.request import MonitorRequest
from cloudooo.handler.ooo.monitor.memory import MonitorMemory
OPENOFFICE = True
class TestMonitorInit(CloudoooTestCase):
class TestMonitorInit(HandlerTestCase):
"""Test Case to test if the monitors are controlled correctly"""
def afterSetUp(self):
......
......@@ -28,12 +28,13 @@
from time import sleep
from cloudooo.handler.ooo.monitor.request import MonitorRequest
from cloudoooTestCase import CloudoooTestCase, make_suite
from cloudoooTestCase import make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.application.openoffice import openoffice
OPENOFFICE = True
class TestMonitorRequest(CloudoooTestCase):
class TestMonitorRequest(HandlerTestCase):
"""Test all features of a monitor following the interface"""
def testMonitorTerminate(self):
......
......@@ -30,11 +30,12 @@
from zipfile import ZipFile
from StringIO import StringIO
from lxml import etree
from cloudoooTestCase import CloudoooTestCase, make_suite
from cloudoooTestCase import make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.granulator import OOGranulator
class TestOOGranulator(CloudoooTestCase):
class TestOOGranulator(HandlerTestCase):
def setUp(self):
data = open('./data/granulate_test.odt').read()
......
......@@ -29,7 +29,7 @@
import magic
from os import path
from base64 import encodestring, decodestring
from cloudoooTestCase import CloudoooTestCase
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.handler import OOHandler
from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudoooTestCase import make_suite
......@@ -39,7 +39,7 @@ from zipfile import ZipFile
OPENOFFICE = True
class TestOOHandler(CloudoooTestCase):
class TestOOHandler(HandlerTestCase):
"""Test OOHandler and manipulation of OOo Instance"""
_file_path_list = []
......@@ -64,7 +64,7 @@ class TestOOHandler(CloudoooTestCase):
file_path = self._file_path_list.pop()
if os.path.exists(file_path):
os.remove(file_path)
CloudoooTestCase.tearDown(self)
HandlerTestCase.tearDown(self)
def testConvertOdtToDoc(self):
"""Test convert ODT to DOC"""
......
......@@ -28,10 +28,11 @@
from zipfile import ZipFile
from lxml import etree
from cloudoooTestCase import CloudoooTestCase, make_suite
from cloudoooTestCase import make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.document import OdfDocument
class TestOdfDocument(CloudoooTestCase):
class TestOdfDocument(HandlerTestCase):
def setUp(self):
data = open('./data/granulate_test.odt').read()
......
......@@ -26,14 +26,14 @@
#
##############################################################################
from cloudoooTestCase import CloudoooTestCase
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.application.openoffice import OpenOffice
from cloudoooTestCase import make_suite
from cloudooo.handler.ooo.utils.utils import waitStopDaemon
OPENOFFICE = True
class TestOpenOffice(CloudoooTestCase):
class TestOpenOffice(HandlerTestCase):
"""Test OpenOffice object and manipulation of OOo Instance"""
def afterSetUp(self):
......
......@@ -30,14 +30,15 @@ from os.path import join, exists
from os import remove
from xmlrpclib import ServerProxy, Fault
from base64 import encodestring, decodestring
from cloudoooTestCase import CloudoooTestCase, make_suite
from cloudoooTestCase import make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from zipfile import ZipFile, is_zipfile
from types import DictType
import magic
DAEMON = True
class TestServer(CloudoooTestCase):
class TestServer(HandlerTestCase):
"""Test XmlRpc Server. Needs cloudooo server started"""
def afterSetUp(self):
......
......@@ -31,13 +31,14 @@ import magic
import pkg_resources
from subprocess import Popen, PIPE
from os.path import exists, join
from cloudoooTestCase import CloudoooTestCase, make_suite
from cloudoooTestCase import make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.document import FileSystemDocument
OPENOFFICE = True
class TestUnoConverter(CloudoooTestCase):
class TestUnoConverter(HandlerTestCase):
"""Test case to test all features of the unoconverter script"""
file_msg_list = ["Microsoft Office Document",
......
......@@ -31,11 +31,12 @@ import pkg_resources
from cloudooo.handler.ooo.application.openoffice import openoffice
from subprocess import Popen, PIPE
from os import environ, path
from cloudoooTestCase import CloudoooTestCase, make_suite
from cloudoooTestCase import make_suite
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
OPENOFFICE = True
class TestUnoMimeMapper(CloudoooTestCase):
class TestUnoMimeMapper(HandlerTestCase):
"""Test Case to test all features of script unomimemapper"""
def afterSetUp(self):
......
......@@ -60,10 +60,24 @@ class PDFHandler(object):
def getMetadata(self, base_document=False):
"""Returns a dictionary with all metadata of document.
Keywords Arguments:
base_document -- Boolean variable. if true, the document is also returned
along with the metadata.
"""
# XXX - refactor to use the binary provided by erp5 buildout
command = ["pdfinfo", self.document.getUrl()]
stdout, stderr = Popen(command,
stdout=PIPE,
stderr=PIPE).communicate()
info_list = filter(None, stdout.split("\n"))
metadata = {}
for info in iter(info_list):
if info.count(":") == 1:
info_name, info_value = info.split(":")
else:
info_name, info_value = info.split(" ")
info_name = info_name.replace(":", "")
info_value = info_value.strip()
metadata[info_name.lower()] = info_value
return metadata
def setMetadata(self, metadata):
"""Returns a document with new metadata.
......
......@@ -29,17 +29,28 @@
import unittest
from cloudooo.handler.pdf.handler import PDFHandler
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase
from types import DictType
class TestPDFHandler(unittest.TestCase):
class TestPDFHandler(HandlerTestCase):
def testConvertPDFtoText(self):
"""Test conversion of pdf to txt"""
pdf_document = open("data/test.pdf").read()
handler = PDFHandler("/tmp/", pdf_document, "pdf")
handler = PDFHandler(self.tmp_url, pdf_document, "pdf")
txt_document = handler.convert("txt")
self.assertTrue(txt_document.startswith("UNG Docs Architecture"))
def testgetMetadata(self):
"""Test if the metadata are extracted correctly"""
pdf_document = open("data/test.pdf").read()
handler = PDFHandler(self.tmp_url, pdf_document, "pdf")
metadata = handler.getMetadata()
self.assertEquals(type(metadata), DictType)
self.assertNotEquals(metadata, {})
self.assertEquals(metadata["title"], 'Free Cloud Alliance Presentation')
def test_suite():
suite = unittest.TestSuite()
......
##############################################################################
#
# Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved.
# Gabriel M. Monnerat <gabriel@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import unittest
from os import environ, path
from ConfigParser import ConfigParser
import sys
config = ConfigParser()
def check_folder(working_path, tmp_dir_path):
if not path.exists(working_path):
mkdir(working_path)
if not path.exists(tmp_dir_path):
mkdir(tmp_dir_path)
class HandlerTestCase(unittest.TestCase):
"""Test Case to load cloudooo conf."""
def setUp(self):
"""Creates a environment to run the tests. Is called always before the
tests."""
server_cloudooo_conf = environ.get("server_cloudooo_conf", None)
if server_cloudooo_conf is not None:
config.read(server_cloudooo_conf)
self.hostname = config.get("server:main", "host")
self.cloudooo_port = config.get("server:main", "port")
self.openoffice_port = config.get("app:main", "openoffice_port")
self.office_binary_path = config.get("app:main", "office_binary_path")
self.python_path = sys.executable
self.working_path = config.get("app:main", "working_path")
self.tmp_url = path.join(self.working_path, "tmp")
check_folder(self.working_path, self.tmp_url)
self.uno_path = config.get("app:main", "uno_path")
self.afterSetUp()
def afterSetUp(self):
""" """
......@@ -67,7 +67,6 @@ def application(global_config, **local_config):
if current_value:
value = '%s:%s' % (value, current_value)
environment_dict[variable_name] = value
gc.enable()
debug_mode = convertStringToBool(local_config.get('debug_mode'))
configureLogger(debug_mode=debug_mode)
......
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