Skip to content

Commit

Permalink
Support resolving django.utils.translation.ungettext deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
browniebroke committed May 10, 2020
1 parent 842fc59 commit c28839a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Currently implemented:
* ``django_codemod.commands.django_40.UGetTextNoopToGetTextNoopCommand``: migrate deprecated
``ugettext_noop()`` function to ``gettext_noop()``.

* ``django_codemod.commands.django_40.UNGetTextToNGetTextCommand``: migrate deprecated
``ungettext()`` function to ``ngettext()``.

Credits
-------

Expand Down
8 changes: 8 additions & 0 deletions django_codemod/commands/django_40.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,11 @@ class UGetTextNoopToGetTextNoopCommand(UGetTextToGetTextCommand):
DESCRIPTION: str = "Replaces ugettext_noop() by gettext_noop()."
old_name = "ugettext_noop"
new_name = "gettext_noop"


class UNGetTextToNGetTextCommand(UGetTextToGetTextCommand):
"""Help resolve deprecation of django.utils.translation.ungettext."""

DESCRIPTION: str = "Replaces ungettext() by ngettext()."
old_name = "ungettext"
new_name = "ngettext"
51 changes: 51 additions & 0 deletions tests/commands/test_django_40.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
UGetTextToGetTextCommand,
UGetTextLazyToGetTextLazyCommand,
UGetTextNoopToGetTextNoopCommand,
UNGetTextToNGetTextCommand,
)


Expand Down Expand Up @@ -277,3 +278,53 @@ def test_already_imported_substitution(self) -> None:
result = gettext_noop(content)
"""
self.assertCodemod(before, after)


class TestUNGetTextToNGetTextCommand(CodemodTest):

TRANSFORM = UNGetTextToNGetTextCommand

def test_noop(self) -> None:
"""Test when nothing should change."""
before = """
from django import conf
from django.utils import translation
foo = ngettext("bar", "bars", count)
"""
after = """
from django import conf
from django.utils import translation
foo = ngettext("bar", "bars", count)
"""

self.assertCodemod(before, after)

def test_simple_substitution(self) -> None:
"""Check simple use case."""
before = """
from django.utils.translation import ungettext
result = ungettext(content, plural_content, count)
"""
after = """
from django.utils.translation import ngettext
result = ngettext(content, plural_content, count)
"""
self.assertCodemod(before, after)

def test_already_imported_substitution(self) -> None:
"""Test case where ngettext is already in the imports."""
before = """
from django.utils.translation import ungettext, ngettext
result = ungettext(content, plural_content, count)
"""
after = """
from django.utils.translation import ngettext
result = ngettext(content, plural_content, count)
"""
self.assertCodemod(before, after)

0 comments on commit c28839a

Please sign in to comment.