Package translate :: Package storage :: Module test_factory
[hide private]
[frames] | no frames]

Source Code for Module translate.storage.test_factory

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  from translate.storage import factory 
  5  from translate.storage.directory import Directory 
  6  from translate.misc import wStringIO 
  7   
  8  from gzip import GzipFile 
  9  try: 
 10      # bz2 is not available on python 2.3 
 11      from bz2 import BZ2File 
 12  except ImportError: 
 13      BZ2File = None 
 14  import os 
 15   
16 -def classname(filename):
17 """returns the classname to ease testing""" 18 classinstance = factory.getclass(filename) 19 return str(classinstance.__name__).lower()
20
21 -def givefile(filename, content):
22 """returns a file dummy object with the given content""" 23 file = wStringIO.StringIO(content) 24 file.name = filename 25 return file
26
27 -class BaseTestFactory:
28 - def setup_method(self, method):
29 """sets up a test directory""" 30 self.testdir = "%s_testdir" % (self.__class__.__name__) 31 self.cleardir(self.testdir) 32 os.mkdir(self.testdir)
33
34 - def teardown_method(self, method):
35 """removes the attributes set up by setup_method""" 36 self.cleardir(self.testdir)
37
38 - def cleardir(self, dirname):
39 """removes the given directory""" 40 if os.path.exists(dirname): 41 for dirpath, subdirs, filenames in os.walk(dirname, topdown=False): 42 for name in filenames: 43 os.remove(os.path.join(dirpath, name)) 44 for name in subdirs: 45 os.rmdir(os.path.join(dirpath, name)) 46 if os.path.exists(dirname): os.rmdir(dirname) 47 assert not os.path.exists(dirname)
48 cleardir = classmethod(cleardir) 49
50 - def test_getclass(self):
51 assert classname("file.po") == "pofile" 52 assert classname("file.pot") == "pofile" 53 assert classname("file.dtd.po") == "pofile" 54 55 assert classname("file.tmx") == "tmxfile" 56 assert classname("file.af.tmx") == "tmxfile" 57 assert classname("file.tbx") == "tbxfile" 58 assert classname("file.po.xliff") == "xlifffile" 59 60 assert not classname("file.po") == "tmxfile" 61 assert not classname("file.po") == "xlifffile" 62 63 assert classname("file.po.gz") == "pofile" 64 assert classname("file.pot.gz") == "pofile" 65 assert classname("file.dtd.po.gz") == "pofile" 66 67 assert classname("file.tmx.gz") == "tmxfile" 68 assert classname("file.af.tmx.gz") == "tmxfile" 69 assert classname("file.tbx.gz") == "tbxfile" 70 assert classname("file.po.xliff.gz") == "xlifffile" 71 72 assert not classname("file.po.gz") == "tmxfile" 73 assert not classname("file.po.gz") == "xlifffile" 74 75 assert classname("file.po.bz2") == "pofile" 76 assert classname("file.pot.bz2") == "pofile" 77 assert classname("file.dtd.po.bz2") == "pofile" 78 79 assert classname("file.tmx.bz2") == "tmxfile" 80 assert classname("file.af.tmx.bz2") == "tmxfile" 81 assert classname("file.tbx.bz2") == "tbxfile" 82 assert classname("file.po.xliff.bz2") == "xlifffile" 83 84 assert not classname("file.po.bz2") == "tmxfile" 85 assert not classname("file.po.bz2") == "xlifffile"
86
87 - def test_getobject(self):
88 """Tests that we get a valid object.""" 89 fileobj = givefile(self.filename, self.file_content) 90 store = factory.getobject(fileobj) 91 assert isinstance(store, self.expected_instance)
92
93 - def test_get_noname_object(self):
94 """Tests that we get a valid object from a file object without a name.""" 95 fileobj = wStringIO.StringIO(self.file_content) 96 assert not hasattr(fileobj, 'name') 97 store = factory.getobject(fileobj) 98 assert isinstance(store, self.expected_instance)
99
100 - def test_gzfile(self):
101 """Test that we can open a gzip file correctly.""" 102 filename = os.path.join(self.testdir, self.filename + '.gz') 103 gzfile = GzipFile(filename, mode="wb") 104 gzfile.write(self.file_content) 105 gzfile.close() 106 store = factory.getobject(filename) 107 assert isinstance(store, self.expected_instance)
108
109 - def test_bz2file(self):
110 """Test that we can open a gzip file correctly.""" 111 if not BZ2File: 112 return 113 filename = os.path.join(self.testdir, self.filename + '.bz2') 114 bz2file = BZ2File(filename, mode="wb") 115 bz2file.write(self.file_content) 116 bz2file.close() 117 store = factory.getobject(filename) 118 assert isinstance(store, self.expected_instance)
119
120 - def test_directory(self):
121 """Test that a directory is correctly detected.""" 122 object = factory.getobject(self.testdir) 123 assert isinstance(object, Directory)
124
125 -class TestPOFactory(BaseTestFactory):
126 expected_instance = factory.po.pofile 127 filename = 'dummy.po' 128 file_content = '''#: test.c\nmsgid "test"\nmsgstr "rest"\n'''
129
130 -class TestXliffFactory(BaseTestFactory):
131 expected_instance = factory.xliff.xlifffile 132 filename = 'dummy.xliff' 133 file_content = '''<?xml version="1.0" encoding="utf-8"?> 134 <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1"> 135 <file> 136 <body> 137 <trans-unit> 138 <source>test</source> 139 <target>rest</target> 140 </trans-unit> 141 </body> 142 </file> 143 </xliff>'''
144
145 -class TestPOXliffFactory(BaseTestFactory):
146 expected_instance = factory.poxliff.PoXliffFile 147 filename = 'dummy.xliff' 148 file_content = '''<?xml version="1.0" encoding="utf-8"?> 149 <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1"> 150 <file datatype="po" original="file.po" source-language="en-US"><body><trans-unit approved="no" id="1" restype="x-gettext-domain-header" xml:space="preserve"> 151 <source>MIME-Version: 1.0 152 Content-Type: text/plain; charset=UTF-8 153 Content-Transfer-Encoding: 8bit 154 </source> 155 <target>MIME-Version: 1.0 156 Content-Type: text/plain; charset=UTF-8 157 Content-Transfer-Encoding: 8bit 158 </target> 159 </trans-unit></body></file></xliff>'''
160
161 -class TestWordfastFactory(BaseTestFactory):
162 expected_instance = factory.wordfast.WordfastTMFile 163 filename = 'dummy.txt' 164 file_content = '''%20070801~103212 %User ID,S,S SMURRAY,SMS Samuel Murray-Smit,SM Samuel Murray-Smit,MW Mary White,DS Deepak Shota,MT! Machine translation (15),AL! Alignment (10),SM Samuel Murray, %TU=00000075 %AF-ZA %Wordfast TM v.5.51r/00 %EN-ZA %---80597535 Subject (5),EL,EL Electronics,AC Accounting,LE Legal,ME Mechanics,MD Medical,LT Literary,AG Agriculture,CO Commercial Client (5),LS,LS LionSoft Corp,ST SuperTron Inc,CA CompArt Ltd 165 20070801~103248 SM 0 AF-ZA Langeraad en duimpie EN-ZA Big Ben and Little John EL LS'''
166