test_generate.py 2.82 KB
Newer Older
1 2 3 4
import os
import string
import random
import re
5
from polib import pofile
6 7
from unittest import TestCase
from datetime import datetime, timedelta
8
from pytz import UTC
9 10

import generate
11
from config import CONFIGURATION
12

13

14 15 16 17 18 19 20 21 22
class TestGenerate(TestCase):
    """
    Tests functionality of i18n/generate.py
    """
    generated_files = ('django-partial.po', 'djangojs.po', 'mako.po')

    def setUp(self):
        # Subtract 1 second to help comparisons with file-modify time succeed,
        # since os.path.getmtime() is not millisecond-accurate
23
        self.start_time = datetime.now(UTC) - timedelta(seconds=1)
24 25 26 27 28

    def test_merge(self):
        """
        Tests merge script on English source files.
        """
29 30
        filename = os.path.join(CONFIGURATION.source_messages_dir, random_name())
        generate.merge(CONFIGURATION.source_locale, target=filename)
31 32 33 34 35 36 37 38 39 40 41 42
        self.assertTrue(os.path.exists(filename))
        os.remove(filename)

    def test_main(self):
        """
        Runs generate.main() which should merge source files,
        then compile all sources in all configured languages.
        Validates output by checking all .mo files in all configured languages.
        .mo files should exist, and be recently created (modified
        after start of test suite)
        """
        generate.main()
43
        for locale in CONFIGURATION.locales:
44 45 46
            for filename in ('django', 'djangojs'):
                mofile = filename+'.mo'
                path = os.path.join(CONFIGURATION.get_messages_dir(locale), mofile)
47
                exists = os.path.exists(path)
48
                self.assertTrue(exists, msg='Missing file in locale %s: %s' % (locale, mofile))
49
                self.assertTrue(datetime.fromtimestamp(os.path.getmtime(path), UTC) >= self.start_time,
50
                                msg='File not recently modified: %s' % path)
51 52 53 54 55 56
            self.assert_merge_headers(locale)

    def assert_merge_headers(self, locale):
        """
        This is invoked by test_main to ensure that it runs after
        calling generate.main().
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71
        There should be exactly three merge comment headers
        in our merged .po file. This counts them to be sure.
        A merge comment looks like this:
        # #-#-#-#-#  django-partial.po (0.1a)  #-#-#-#-#

        """
        path = os.path.join(CONFIGURATION.get_messages_dir(locale), 'django.po')
        po = pofile(path)
        pattern = re.compile('^#-#-#-#-#', re.M)
        match = pattern.findall(po.header)
        self.assertEqual(len(match), 3,
                         msg="Found %s (should be 3) merge comments in the header for %s" % \
                         (len(match), path))

72 73 74 75 76

def random_name(size=6):
    """Returns random filename as string, like test-4BZ81W"""
    chars = string.ascii_uppercase + string.digits
    return 'test-' + ''.join(random.choice(chars) for x in range(size))