Package translate :: Package storage :: Module test_statsdb

[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.test_statsdb

      1  #!/usr/bin/env python
      2  from translate import storage
      3
      4  import os
      5  import os.path
      6
      7  import py.test
      8
      9  from translate.storage import statsdb, factory
     10  from translate.misc import wStringIO
     11  from translate.filters import checks
     12  import warnings
     13
     14  fr_terminology_extract = """
     15  msgid ""
     16  msgstr ""
     17  "Project-Id-Version: GnomeGlossary\n"
     18  "POT-Creation-Date: 2002-05-22 23:40+0200\n"
     19  "PO-Revision-Date: 2002-05-22 23:38+0200\n"
     20  "Last-Translator: Christophe Merlet (RedFox) <christophe@merlet.net>\n"
     21  "Language-Team: GNOME French Team <gnomefr@traduc.org>\n"
     22  "MIME-Version: 1.0\n"
     23  "Content-Type: text/plain; charset=ISO-8859-1\n"
     24  "Content-Transfer-Encoding: 8bit\n"
     25
     26  #. "English Definition"
     27  msgid "Term"
     28  msgstr "Terme"
     29
     30  #. "To terminate abruptly a processing activity in a computer system because it is impossible or undesirable for the activity to procees."
     31  msgid "abort"
     32  msgstr "annuler"
     33  """
     34
     35  jtoolkit_extract = """
     36  msgid ""
     37  msgstr ""
     38  "Project-Id-Version: PACKAGE VERSION\n"
     39  "Report-Msgid-Bugs-To: \n"
     40  "POT-Creation-Date: 2005-06-13 14:54-0500\n"
     41  "PO-Revision-Date: 2007-05-04 19:54+0200\n"
     42  "Last-Translator: F Wolff <friedel@translate.org.za>\n"
     43  "Language-Team: LANGUAGE <LL@li.org>\n"
     44  "MIME-Version: 1.0\n"
     45  "Content-Type: text/plain; charset=UTF-8\n"
     46  "Content-Transfer-Encoding: 8bit\n"
     47  "Plural-Forms: nplurals=2; plural=(n != 1);\n"
     48  "X-Generator: Pootle 1.0rc1\n"
     49  "Generated-By: pygettext.py 1.5\n"
     50
     51  #: web/server.py:57
     52  #, python-format
     53  #, fuzzy
     54  msgid "Login for %s"
     55  msgstr "Meld aan vir %s"
     56
     57  #: web/server.py:91
     58  msgid "Cancel this action and start a new session"
     59  msgstr "Kanselleer hierdie aksie en begin 'n nuwe sessie"
     60
     61  #: web/server.py:92
     62  msgid "Instead of confirming this action, log out and start from scratch"
     63  msgstr "Meld af en begin op nuut eerder as om hierdie aksie te bevestig."
     64
     65  #: web/server.py:97
     66  #, fuzzy
     67  msgid "Exit application"
     68  msgstr "Verlaat toepassing"
     69
     70  #: web/server.py:98
     71  msgid "Exit this application and return to the parent application"
     72  msgstr "Verlaat hierdie toepassing en gaan terug na die ouertoepassing"
     73
     74  #: web/server.py:105
     75  msgid ", please confirm login"
     76  msgstr ""
     77  """
     78
     79 -def rm_rf(path):
     80      for dirpath, _, filenames in os.walk(path):
     81          for filename in filenames:
     82              os.remove(os.path.join(dirpath, filename))
     83      os.removedirs(path)
     84
     85 -class TestStatsDb:
     86 -    def remove_dirs(self, path):
     87          if os.path.exists(path):
     88              rm_rf(path)
     89
     90 -    def get_test_path(self, method):
     91          return os.path.realpath("%s_%s" % (self.__class__.__name__, method.__name__))
     92
     93 -    def setup_method(self, method):
     94          """Allocates a unique self.filename for the method, making sure it doesn't exist"""
     95          self.path = self.get_test_path(method)
     96          self.remove_dirs(self.path)
     97          os.makedirs(self.path)
     98
     99 -    def teardown_method(self, method):
    100          """Makes sure that if self.filename was created by the method, it is cleaned up"""
    101          self.remove_dirs(self.path)
    102
    103 -    def setup_file_and_db(self, file_contents=fr_terminology_extract):
    104          cache = statsdb.StatsCache(os.path.join(self.path, "stats.db"))
    105          filename = os.path.join(self.path, "test.po")
    106          open(filename, "w").write(file_contents)
    107          f = factory.getobject(filename)
    108          return f, cache
    109
    110 -    def test_getfileid_recache_uncached_unit(self):
    111          """checks that a simple oo entry is parsed correctly"""
    112          checker = checks.UnitChecker()
    113          f, cache = self.setup_file_and_db()
    114          py.test.raises(AssertionError, cache.recacheunit, f.filename, checker, f.units[1])
    115
    116 -    def test_getfileid_recache_cached_unit(self):
    117          """checks that a simple oo entry is parsed correctly"""
    118          checker = checks.UnitChecker()
    119          f, cache = self.setup_file_and_db()
    120          cache.filestats(f.filename, checker)
    121          state = cache.recacheunit(f.filename, checker, f.units[1])
    122          assert state == ['translated', 'total']
    123
    124 -    def test_unitstats(self):
    125          f, cache = self.setup_file_and_db(jtoolkit_extract)
    126          u = cache.unitstats(f.filename)
    127          assert u['sourcewordcount'] == [3, 8, 11, 2, 9, 3]
    128
    129 -    def test_filestats(self):
    130          f, cache = self.setup_file_and_db(jtoolkit_extract)
    131          s = cache.filestats(f.filename, checks.UnitChecker())
    132          assert s['translated'] == [2, 3, 5]
    133          assert s['fuzzy'] == [1, 4]
    134          assert s['untranslated'] == [6]
    135          assert s['total'] == [1, 2, 3, 4, 5, 6]
    136
    137 -    def make_file_and_return_id(self, cache, filename):
    138          cache.cur.execute("""
    139              SELECT fileid, st_mtime, st_size FROM files
    140              WHERE path=?;""", (os.path.realpath(filename),))
    141          return cache.cur.fetchone()
    142
    143 -    def test_if_cached_after_filestats(self):
    144          f, cache = self.setup_file_and_db(jtoolkit_extract)
    145          cache.filestats(f.filename, checks.UnitChecker())
    146          assert self.make_file_and_return_id(cache, f.filename) != None
    147
    148 -    def test_if_cached_after_unitstats(self):
    149          f, cache = self.setup_file_and_db(jtoolkit_extract)
    150          cache.unitstats(f.filename, checks.UnitChecker())
    151          assert self.make_file_and_return_id(cache, f.filename) != None
    152
    153 -    def test_singletonness(self):
    154          f1, cache1 = self.setup_file_and_db(jtoolkit_extract)
    155          f2, cache2 = self.setup_file_and_db(fr_terminology_extract)
    156          assert cache1 == cache2
    157