Back to index

plone3  3.1.7
test_transforms.py
Go to the documentation of this file.
00001 import os
00002 import logging
00003 from Testing import ZopeTestCase
00004 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00005 
00006 from utils import input_file_path, output_file_path, normalize_html,\
00007      load, matching_inputs
00008 from Products.PortalTransforms.data import datastream
00009 from Products.PortalTransforms.interfaces import idatastream
00010 from Products.MimetypesRegistry.MimeTypesTool import MimeTypesTool
00011 from Products.PortalTransforms.TransformEngine import TransformTool
00012 
00013 from Products.PortalTransforms.libtransforms.utils import MissingBinary
00014 from Products.PortalTransforms.transforms.image_to_gif import image_to_gif
00015 from Products.PortalTransforms.transforms.image_to_png import image_to_png
00016 from Products.PortalTransforms.transforms.image_to_jpeg import image_to_jpeg
00017 from Products.PortalTransforms.transforms.image_to_bmp import image_to_bmp
00018 from Products.PortalTransforms.transforms.image_to_tiff import image_to_tiff
00019 from Products.PortalTransforms.transforms.image_to_ppm  import image_to_ppm
00020 from Products.PortalTransforms.transforms.image_to_pcx  import image_to_pcx
00021 
00022 from os.path import exists
00023 import sys
00024 # we have to set locale because lynx output is locale sensitive !
00025 os.environ['LC_ALL'] = 'C'
00026 logger = logging.getLogger('PortalTransforms')
00027 
00028 class TransformTest(ATSiteTestCase):
00029 
00030     def do_convert(self, filename=None):
00031         if filename is None and exists(self.output + '.nofilename'):
00032             output = self.output + '.nofilename'
00033         else:
00034             output = self.output
00035         input = open(self.input)
00036         orig = input.read()
00037         input.close()
00038         data = datastream(self.transform.name())
00039         res_data = self.transform.convert(orig, data, filename=filename)
00040         self.assert_(idatastream.isImplementedBy(res_data))
00041         got = res_data.getData()
00042         try:
00043             output = open(output)
00044         except IOError:
00045             import sys
00046             print >>sys.stderr, 'No output file found.'
00047             print >>sys.stderr, 'File %s created, check it !' % self.output
00048             output = open(output, 'w')
00049             output.write(got)
00050             output.close()
00051             self.assert_(0)
00052         expected = output.read()
00053         if self.normalize is not None:
00054             expected = self.normalize(expected)
00055             got = self.normalize(got)
00056         output.close()
00057 
00058         self.assertEquals(got, expected,
00059                           '[%s]\n\n!=\n\n[%s]\n\nIN %s(%s)' % (
00060             got, expected, self.transform.name(), self.input))
00061         self.assertEquals(self.subobjects, len(res_data.getSubObjects()),
00062                           '%s\n\n!=\n\n%s\n\nIN %s(%s)' % (
00063             self.subobjects, len(res_data.getSubObjects()),
00064             self.transform.name(), self.input))
00065 
00066     def testSame(self):
00067         try:
00068             self.do_convert(filename=self.input)
00069         except MissingBinary, e:
00070             pass
00071 
00072     def testSameNoFilename(self):
00073         try:
00074             self.do_convert()
00075         except MissingBinary, e:
00076             pass
00077 
00078     def __repr__(self):
00079         return self.transform.name()
00080 
00081 class PILTransformsTest(ATSiteTestCase):
00082     def afterSetUp(self):
00083         ATSiteTestCase.afterSetUp(self)
00084         self.pt = self.portal.portal_transforms
00085 
00086     def test_image_to_bmp(self):
00087         self.pt.registerTransform(image_to_bmp())
00088         imgFile = open(input_file_path('logo.jpg'), 'rb')
00089         data = imgFile.read()
00090         self.failUnlessEqual(self.portal.mimetypes_registry.classify(data),'image/jpeg')
00091         data = self.pt.convertTo(target_mimetype='image/x-ms-bmp',orig=data)
00092         self.failUnlessEqual(data.getMetadata()['mimetype'], 'image/x-ms-bmp')
00093 
00094     def test_image_to_gif(self):
00095         self.pt.registerTransform(image_to_gif())
00096         imgFile = open(input_file_path('logo.png'), 'rb')
00097         data = imgFile.read()
00098         self.failUnlessEqual(self.portal.mimetypes_registry.classify(data),'image/png')
00099         data = self.pt.convertTo(target_mimetype='image/gif',orig=data)
00100         self.failUnlessEqual(data.getMetadata()['mimetype'], 'image/gif')
00101 
00102     def test_image_to_jpeg(self):
00103         self.pt.registerTransform(image_to_jpeg())
00104         imgFile = open(input_file_path('logo.gif'), 'rb')
00105         data = imgFile.read()
00106         self.failUnlessEqual(self.portal.mimetypes_registry.classify(data),'image/gif')
00107         data = self.pt.convertTo(target_mimetype='image/jpeg',orig=data)
00108         self.failUnlessEqual(data.getMetadata()['mimetype'], 'image/jpeg')
00109 
00110     def test_image_to_png(self):
00111         self.pt.registerTransform(image_to_png())
00112         imgFile = open(input_file_path('logo.jpg'), 'rb')
00113         data = imgFile.read()
00114         self.failUnlessEqual(self.portal.mimetypes_registry.classify(data),'image/jpeg')
00115         data = self.pt.convertTo(target_mimetype='image/png',orig=data)
00116         self.failUnlessEqual(data.getMetadata()['mimetype'], 'image/png')
00117 
00118     def test_image_to_pcx(self):
00119         self.pt.registerTransform(image_to_pcx())
00120         imgFile = open(input_file_path('logo.gif'), 'rb')
00121         data = imgFile.read()
00122         self.failUnlessEqual(self.portal.mimetypes_registry.classify(data),'image/gif')
00123         data = self.pt.convertTo(target_mimetype='image/pcx',orig=data)
00124         self.failUnlessEqual(data.getMetadata()['mimetype'], 'image/pcx')
00125 
00126     def test_image_to_ppm(self):
00127         self.pt.registerTransform(image_to_ppm())
00128         imgFile = open(input_file_path('logo.png'), 'rb')
00129         data = imgFile.read()
00130         self.failUnlessEqual(self.portal.mimetypes_registry.classify(data),'image/png')
00131         data = self.pt.convertTo(target_mimetype='image/x-portable-pixmap',orig=data)
00132         self.failUnlessEqual(data.getMetadata()['mimetype'], 'image/x-portable-pixmap')
00133 
00134     def test_image_to_tiff(self):
00135         self.pt.registerTransform(image_to_tiff())
00136         imgFile = open(input_file_path('logo.jpg'), 'rb')
00137         data = imgFile.read()
00138         self.failUnlessEqual(self.portal.mimetypes_registry.classify(data),'image/jpeg')
00139         data = self.pt.convertTo(target_mimetype='image/tiff',orig=data)
00140         self.failUnlessEqual(data.getMetadata()['mimetype'], 'image/tiff')
00141 
00142 
00143 TRANSFORMS_TESTINFO = (
00144     ('Products.PortalTransforms.transforms.pdf_to_html',
00145      "demo1.pdf", "demo1.html", None, 0
00146      ),
00147     ('Products.PortalTransforms.transforms.word_to_html',
00148      "test.doc", "test_word.html", normalize_html, 0
00149      ),
00150     ('Products.PortalTransforms.transforms.lynx_dump',
00151      "test_lynx.html", "test_lynx.txt", None, 0
00152      ),
00153     ('Products.PortalTransforms.transforms.html_to_text',
00154      "test_lynx.html", "test_html_to_text.txt", None, 0
00155      ),
00156     ('Products.PortalTransforms.transforms.identity',
00157      "rest1.rst", "rest1.rst", None, 0
00158      ),
00159     ('Products.PortalTransforms.transforms.text_to_html',
00160      "rest1.rst", "rest1.html", None, 0
00161      ),
00162     ('Products.PortalTransforms.transforms.safe_html',
00163      "test_safehtml.html", "test_safe.html", None, 0
00164      ),
00165     ('Products.PortalTransforms.transforms.image_to_bmp',
00166      "logo.jpg", "logo.bmp", None, 0
00167      ),
00168     ('Products.PortalTransforms.transforms.image_to_gif',
00169      "logo.bmp", "logo.gif", None, 0
00170      ),
00171     ('Products.PortalTransforms.transforms.image_to_jpeg',
00172      "logo.gif", "logo.jpg", None, 0
00173      ),
00174     ('Products.PortalTransforms.transforms.image_to_png',
00175      "logo.bmp", "logo.png", None, 0
00176      ),
00177     ('Products.PortalTransforms.transforms.image_to_ppm',
00178      "logo.gif", "logo.ppm", None, 0
00179      ),
00180     ('Products.PortalTransforms.transforms.image_to_tiff',
00181      "logo.png", "logo.tiff", None, 0
00182      ),
00183     ('Products.PortalTransforms.transforms.image_to_pcx',
00184      "logo.png", "logo.pcx", None, 0
00185      ),
00186     ('Products.PortalTransforms.transforms.markdown_to_html',
00187      "markdown.txt", "markdown.html", None, 0
00188      ),
00189     ('Products.PortalTransforms.transforms.textile_to_html',
00190      "input.textile", "textile.html", None, 0
00191     ), 
00192    )
00193 
00194 def initialise(transform, normalize, pattern):
00195     global TRANSFORMS_TESTINFO
00196     for fname in matching_inputs(pattern):
00197         outname = '%s.out' % fname.split('.')[0]
00198         #print transform, fname, outname
00199         TRANSFORMS_TESTINFO += ((transform, fname, outname, normalize, 0),)
00200 
00201 
00202 # ReST test cases
00203 initialise('Products.PortalTransforms.transforms.rest', normalize_html, "rest*.rst")
00204 # Python test cases
00205 initialise('Products.PortalTransforms.transforms.python', normalize_html, "*.py")
00206 
00207 # FIXME missing tests for image_to_html, st
00208 
00209 TR_NAMES = None
00210 
00211 def make_tests(test_descr=TRANSFORMS_TESTINFO):
00212     """generate tests classes from test info
00213 
00214     return the list of generated test classes
00215     """
00216     tests = []
00217     for _transform, tr_input, tr_output, _normalize, _subobjects in test_descr:
00218         # load transform if necessary
00219         if type(_transform) is type(''):
00220             try:
00221                 _transform = load(_transform).register()
00222             except MissingBinary:
00223                 # we are not interessted in tests with missing binaries
00224                 continue
00225             except:
00226                 import traceback
00227                 traceback.print_exc()
00228                 continue
00229 
00230         if TR_NAMES is not None and not _transform.name() in TR_NAMES:
00231             print 'skip test for', _transform.name()
00232             continue
00233 
00234         class TransformTestSubclass(TransformTest):
00235             input = input_file_path(tr_input)
00236             output = output_file_path(tr_output)
00237             transform = _transform
00238             normalize = lambda x, y: _normalize(y)
00239             subobjects = _subobjects
00240 
00241         tests.append(TransformTestSubclass)
00242 
00243     tests.append(PILTransformsTest)
00244     return tests
00245 
00246 
00247 def test_suite():
00248     from unittest import TestSuite, makeSuite
00249     suite = TestSuite()
00250     for test in make_tests():
00251         suite.addTest(makeSuite(test))
00252     return suite