test_extract.py 3.06 KB
Newer Older
1 2
import os
import polib
3 4 5
from unittest import TestCase
from nose.plugins.skip import SkipTest
from datetime import datetime, timedelta
6
from pytz import UTC
7 8

import extract
9
from config import CONFIGURATION
10 11 12 13

# Make sure setup runs only once
SETUP_HAS_RUN = False

14

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

    def setUp(self):
        # Skip this test because it takes too long (>1 minute)
        # TODO: figure out how to declare a "long-running" test suite
        # and add this test to it.
25
        raise SkipTest()
26 27

        global SETUP_HAS_RUN
28

29 30
        # Subtract 1 second to help comparisons with file-modify time succeed,
        # since os.path.getmtime() is not millisecond-accurate
31
        self.start_time = datetime.now(UTC) - timedelta(seconds=1)
32 33 34 35 36 37
        super(TestExtract, self).setUp()
        if not SETUP_HAS_RUN:
            # Run extraction script. Warning, this takes 1 minute or more
            extract.main()
            SETUP_HAS_RUN = True

38
    def get_files(self):
39 40 41 42 43 44
        """
        This is a generator.
        Returns the fully expanded filenames for all extracted files
        Fails assertion if one of the files doesn't exist.
        """
        for filename in self.generated_files:
45
            path = os.path.join(CONFIGURATION.source_messages_dir, filename)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
            exists = os.path.exists(path)
            self.assertTrue(exists, msg='Missing file: %s' % filename)
            if exists:
                yield path

    def test_files(self):
        """
        Asserts that each auto-generated file has been modified since 'extract' was launched.
        Intended to show that the file has been touched by 'extract'.
        """

        for path in self.get_files():
            self.assertTrue(datetime.fromtimestamp(os.path.getmtime(path)) > self.start_time,
                            msg='File not recently modified: %s' % os.path.basename(path))

    def test_is_keystring(self):
        """
        Verifies is_keystring predicate
        """
        entry1 = polib.POEntry()
        entry2 = polib.POEntry()
        entry1.msgid = "_.lms.admin.warning.keystring"
        entry2.msgid = "This is not a keystring"
        self.assertTrue(extract.is_key_string(entry1.msgid))
        self.assertFalse(extract.is_key_string(entry2.msgid))
71

72 73 74 75 76
    def test_headers(self):
        """Verify all headers have been modified"""
        for path in self.get_files():
            po = polib.pofile(path)
            header = po.header
77 78 79 80 81
            self.assertEqual(
                header.find('edX translation file'),
                0,
                msg='Missing header in %s:\n"%s"' % (os.path.basename(path), header)
            )
82 83 84

    def test_metadata(self):
        """Verify all metadata has been modified"""
85
        for path in self.get_files():
86 87 88 89 90
            po = polib.pofile(path)
            metadata = po.metadata
            value = metadata['Report-Msgid-Bugs-To']
            expected = 'translation_team@edx.org'
            self.assertEquals(expected, value)