Package translate :: Package storage :: Module test_zip

[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.test_zip

     1  #!/usr/bin/env python
     2
     3  """Tests for the zip storage module"""
     4
     5  from translate.storage import directory
     6  from translate.storage import zip
     7  import os
     8  from zipfile import ZipFile
     9
    10 -class TestZIPFile(object):
    11      """A test class to test the zip class that provides the directory interface."""
    12
    13 -    def setup_method(self, method):
    14          """sets up a test directory"""
    15          print "setup_method called on", self.__class__.__name__
    16          self.testzip = "%s_testzip.zip" % (self.__class__.__name__)
    17          self.cleardir(self.testzip)
    18          self.zip = ZipFile(self.testzip, mode="w")
    19
    20 -    def teardown_method(self, method):
    21          """removes the attributes set up by setup_method"""
    22          self.cleardir(self.testzip)
    23
    24 -    def cleardir(self, dirname):
    25          """removes the given directory"""
    26          if os.path.exists(self.testzip):
    27              os.remove(self.testzip)
    28          assert not os.path.exists(self.testzip)
    29
    30 -    def touchfiles(self, dir, filenames, content="", last=False):
    31          for filename in filenames:
    32              if dir:
    33                  self.zip.writestr(os.path.join(dir, filename), content)
    34              else:
    35                  self.zip.writestr(filename, content)
    36          if last:
    37              self.zip.close()
    38
    39 -    def mkdir(self, dir):
    40          """Makes a directory inside self.testzip."""
    41          pass
    42
    43 -    def test_created(self):
    44          """test that the directory actually exists"""
    45          print self.testzip
    46          assert os.path.isfile(self.testzip)
    47
    48 -    def test_basic(self):
    49          """Tests basic functionality."""
    50          files = ["a.po", "b.po", "c.po"]
    51          self.touchfiles(None, files, last=True)
    52
    53          d = zip.ZIPFile(self.testzip)
    54          filenames = [name for dir, name in d.getfiles()]
    55          assert filenames == files
    56
    57 -    def test_structure(self):
    58          """Tests a small directory structure."""
    59          files = ["a.po", "b.po", "c.po"]
    60          self.touchfiles(self.testzip, files)
    61          self.mkdir("bla")
    62          self.touchfiles(os.path.join(self.testzip, "bla"), files, last=True)
    63
    64          d = zip.ZIPFile(self.testzip)
    65          filenames = [name for dir, name in d.getfiles()]
    66          assert filenames == files*2
    67
    68 -    def test_getunits(self):
    69          """Tests basic functionality."""
    70          files = ["a.po", "b.po", "c.po"]
    71          posource = '''msgid "bla"\nmsgstr "blabla"\n'''
    72          self.touchfiles(self.testzip, files, posource, last=True)
    73
    74          d = zip.ZIPFile(self.testzip)
    75          for unit in d.getunits():
    76              assert unit.target == "blabla"
    77          assert len(d.getunits()) == 3
    78