3 """Tests for the zip storage module"""
5 from translate
.storage
import directory
6 from translate
.storage
import zip
8 from zipfile
import ZipFile
10 class TestZIPFile(object):
11 """A test class to test the zip class that provides the directory interface."""
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")
20 def teardown_method(self
, method
):
21 """removes the attributes set up by setup_method"""
22 self
.cleardir(self
.testzip
)
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
)
30 def touchfiles(self
, dir, filenames
, content
="", last
=False):
31 for filename
in filenames
:
33 self
.zip.writestr(os
.path
.join(dir, filename
), content
)
35 self
.zip.writestr(filename
, content
)
40 """Makes a directory inside self.testzip."""
43 def test_created(self
):
44 """test that the directory actually exists"""
46 assert os
.path
.isfile(self
.testzip
)
49 """Tests basic functionality."""
50 files
= ["a.po", "b.po", "c.po"]
51 self
.touchfiles(None, files
, last
=True)
53 d
= zip.ZIPFile(self
.testzip
)
54 filenames
= [name
for dir, name
in d
.getfiles()]
55 assert filenames
== files
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
)
62 self
.touchfiles(os
.path
.join(self
.testzip
, "bla"), files
, last
=True)
64 d
= zip.ZIPFile(self
.testzip
)
65 filenames
= [name
for dir, name
in d
.getfiles()]
66 assert filenames
== files
*2
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)
74 d
= zip.ZIPFile(self
.testzip
)
75 for unit
in d
.getunits():
76 assert unit
.target
== "blabla"
77 assert len(d
.getunits()) == 3