test_django_utils_translation.py 9.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
# -*- coding: utf-8 -*-
"""
Test methods exposed in common/lib/monkey_patch/django_utils_translation.py

Verify that the Django translation functions (gettext, ngettext,
pgettext, ugettext, and derivatives) all return the correct values
before, during, and after monkey-patching the django.utils.translation
module.

gettext, ngettext, pgettext, and ugettext must return a translation as
output for nonempty input.

ngettext, pgettext, npgettext, and ungettext must return an empty string
for an empty string as input.

gettext and ugettext will return translation headers, before and after
patching.

gettext and ugettext must return the empty string for any falsey input,
while patched.

*_noop must return the input text.

*_lazy must return the same text as their non-lazy counterparts.
"""
# pylint: disable=invalid-name
#  Let names like `gettext_*` stay lowercase; makes matching easier.
# pylint: disable=missing-docstring
#  All major functions are documented, the rest are self-evident shells.
# pylint: disable=no-member
#  Pylint doesn't see our decorator `translate_with` add the `_` method.
from unittest import TestCase

from ddt import data
from ddt import ddt
from django.utils.translation import _trans
from django.utils.translation import gettext
from django.utils.translation import gettext_lazy
from django.utils.translation import gettext_noop
from django.utils.translation import ngettext
from django.utils.translation import ngettext_lazy
from django.utils.translation import npgettext
from django.utils.translation import npgettext_lazy
from django.utils.translation import pgettext
from django.utils.translation import pgettext_lazy
from django.utils.translation import ugettext
from django.utils.translation import ugettext_lazy
from django.utils.translation import ugettext_noop
from django.utils.translation import ungettext
from django.utils.translation import ungettext_lazy

from monkey_patch.django_utils_translation import ATTRIBUTES as attributes_patched
from monkey_patch.django_utils_translation import is_patched
from monkey_patch.django_utils_translation import patch
from monkey_patch.django_utils_translation import unpatch

# Note: The commented-out function names are explicitly excluded, as
# they are not attributes of `django.utils.translation._trans`.
# https://github.com/django/django/blob/1.4.8/django/utils/translation/__init__.py#L69
attributes_not_patched = [
    'gettext_noop',
    'ngettext',
    'npgettext',
    'pgettext',
    'ungettext',
    # 'gettext_lazy',
    # 'ngettext_lazy',
    # 'npgettext_lazy',
    # 'pgettext_lazy',
    # 'ugettext_lazy',
    # 'ugettext_noop',
    # 'ungettext_lazy',
]


class MonkeyPatchTest(TestCase):
    def setUp(self):
        """
        Remember the current state, then reset
        """
        self.was_patched = unpatch()
        self.unpatch_all()
        self.addCleanup(self.cleanup)

    def cleanup(self):
        """
        Revert translation functions to previous state

        Since the end state varies, we always unpatch to remove any
        changes, then repatch again iff the module was already
        patched when the test began.
        """
        self.unpatch_all()
        if self.was_patched:
            patch()

    def unpatch_all(self):
        """
        Unpatch the module recursively
        """
        while is_patched():
            unpatch()


@ddt
class PatchTest(MonkeyPatchTest):
    """
    Verify monkey-patching and un-monkey-patching
    """
    @data(*attributes_not_patched)
    def test_not_patch(self, attribute_name):
        """
        Test that functions are not patched unintentionally
        """
        self.unpatch_all()
        old_attribute = getattr(_trans, attribute_name)
        patch()
        new_attribute = getattr(_trans, attribute_name)
        self.assertIs(old_attribute, new_attribute)

    @data(*attributes_patched)
    def test_unpatch(self, attribute):
        """
        Test that unpatch gracefully handles unpatched functions
        """
        patch()
        self.assertTrue(is_patched())
        self.unpatch_all()
        self.assertFalse(is_patched())
        old_attribute = getattr(_trans, attribute)
        self.unpatch_all()
        new_attribute = getattr(_trans, attribute)
        self.assertIs(old_attribute, new_attribute)
        self.assertFalse(is_patched())

    @data(*attributes_patched)
    def test_patch_attributes(self, attribute):
        """
        Test that patch changes the attribute
        """
        self.unpatch_all()
        self.assertFalse(is_patched())
        old_attribute = getattr(_trans, attribute)
        patch()
        new_attribute = getattr(_trans, attribute)
        self.assertIsNot(old_attribute, new_attribute)
        self.assertTrue(is_patched())
        old_attribute = getattr(_trans, attribute)
        patch()
        new_attribute = getattr(_trans, attribute)
        self.assertIsNot(old_attribute, new_attribute)
        self.assertTrue(is_patched())


def translate_with(function):
    """
    Decorate a class by setting its `_` translation function
    """
    def decorate(cls):
        def _(self, *args):
            # pylint: disable=unused-argument
            return function(*args)
        cls._ = _
        return cls
    return decorate


@translate_with(ugettext)
class UgettextTest(MonkeyPatchTest):
    """
    Test a Django translation function

    Here we consider `ugettext` to be the base/default case. All other
    translation functions extend, as needed.
    """
    is_unicode = True
    needs_patched = True
    header = 'Project-Id-Version: '

    def setUp(self):
        """
        Restore translation text and functions
        """
        super(UgettextTest, self).setUp()
        if self.is_unicode:
            self.empty = u''
            self.nonempty = u'(╯°□°)╯︵ ┻━┻'
        else:
            self.empty = ''
            self.nonempty = 'Hey! Where are you?!'

    def assert_translations(self):
        """
        Assert that the empty and nonempty translations are correct

        The `empty = empty[:]` syntax is intentional. Since subclasses
        may implement a lazy translation, we must perform a "string
        operation" to coerce it to a string value. We don't use `str` or
        `unicode` because we also assert the string type.
        """
        empty, nonempty = self.get_translations()
        empty = empty[:]
        nonempty = nonempty[:]
        if self.is_unicode:
            self.assertTrue(isinstance(empty, unicode))
            self.assertTrue(isinstance(nonempty, unicode))
        else:
            self.assertTrue(isinstance(empty, str))
            self.assertTrue(isinstance(nonempty, str))
        if self.needs_patched and not is_patched():
            self.assertIn(self.header, empty)
        else:
            self.assertNotIn(self.header, empty)
        self.assertNotIn(self.header, nonempty)

    def get_translations(self):
        """
        Translate the empty and nonempty strings, per `self._`
        """
        empty = self._(self.empty)
        nonempty = self._(self.nonempty)
        return (empty, nonempty)

    def test_patch(self):
        """
        Test that `self._` correctly translates text before, during, and
        after being monkey-patched.
        """
        self.assert_translations()
        was_successful = patch()
        self.assertTrue(was_successful)
        self.assert_translations()
        was_successful = unpatch()
        self.assertTrue(was_successful)
        self.assert_translations()


@translate_with(gettext)
class GettextTest(UgettextTest):
    is_unicode = False


@translate_with(pgettext)
class PgettextTest(UgettextTest):
    needs_patched = False
    l18n_context = 'monkey_patch'

    def get_translations(self):
        empty = self._(self.l18n_context, self.empty)
        nonempty = self._(self.l18n_context, self.nonempty)
        return (empty, nonempty)


@translate_with(ngettext)
class NgettextTest(GettextTest):
    number = 1
    needs_patched = False

    def get_translations(self):
        empty = self._(self.empty, self.empty, self.number)
        nonempty = self._(self.nonempty, self.nonempty, self.number)
        return (empty, nonempty)


@translate_with(npgettext)
class NpgettextTest(PgettextTest):
    number = 1

    def get_translations(self):
        empty = self._(self.l18n_context, self.empty, self.empty, self.number)
        nonempty = self._(self.l18n_context, self.nonempty, self.nonempty, self.number)
        return (empty, nonempty)


class NpgettextPluralTest(NpgettextTest):
    number = 2


class NgettextPluralTest(NgettextTest):
    number = 2


@translate_with(gettext_noop)
class GettextNoopTest(GettextTest):
    needs_patched = False


@translate_with(ugettext_noop)
class UgettextNoopTest(UgettextTest):
    needs_patched = False


@translate_with(ungettext)
class UngettextTest(NgettextTest):
    is_unicode = True


class UngettextPluralTest(UngettextTest):
    number = 2


@translate_with(gettext_lazy)
class GettextLazyTest(GettextTest):
    pass


@translate_with(ugettext_lazy)
class UgettextLazyTest(UgettextTest):
    pass


@translate_with(pgettext_lazy)
class PgettextLazyTest(PgettextTest):
    pass


@translate_with(ngettext_lazy)
class NgettextLazyTest(NgettextTest):
    pass


@translate_with(npgettext_lazy)
class NpgettextLazyTest(NpgettextTest):
    pass


class NpgettextLazyPluralTest(NpgettextLazyTest):
    number = 2


@translate_with(ungettext_lazy)
class UngettextLazyTest(UngettextTest):
    pass