Back to index

plone3  3.1.7
test_magic.py
Go to the documentation of this file.
00001 
00002 from Testing import ZopeTestCase
00003 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00004 
00005 from Products.MimetypesRegistry.mime_types.magic import guessMime
00006 from utils import input_file_path
00007 
00008 samplefiles = [
00009     ('OOoWriter', 'application/vnd.sun.xml.writer'),
00010     ('OOoCalc', 'application/vnd.sun.xml.calc'),
00011     ('sxw-ooo-trolltech', 'application/vnd.sun.xml.writer'), # file from limi
00012     ('simplezip', 'application/zip'),
00013 ]
00014 
00015 class TestGuessMagic(ATSiteTestCase):
00016     
00017     def afterSetUp(self):
00018         ATSiteTestCase.afterSetUp(self)
00019         self.registry = self.portal.mimetypes_registry
00020 
00021     def test_guessMime(self):
00022         for filename, expected in samplefiles:
00023             file = open(input_file_path(filename))
00024             data = file.read()
00025             file.close()
00026             
00027             # use method direct
00028             got = guessMime(data)
00029             self.failUnlessEqual(got, expected)
00030             
00031             # use mtr-tool
00032             got_from_tool = self.registry.classify(data)
00033             self.failUnlessEqual(got_from_tool, expected)            
00034             
00035             # now cut it to the first 8k if greater
00036             if len(data) > 8192:
00037                 data=data[:8192]
00038                 got_cutted = self.registry.classify(data)
00039                 self.failUnlessEqual(got_cutted, expected)          
00040 
00041 
00042 def test_suite():
00043     from unittest import TestSuite, makeSuite
00044     suite = TestSuite()
00045     suite.addTest(makeSuite(TestGuessMagic))
00046     return suite