Skip to content

Commit

Permalink
Fix test suite failing if unidecode is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpg committed Oct 9, 2024
1 parent 9e84f93 commit f372c95
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
15 changes: 13 additions & 2 deletions taggit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@

try:
from unidecode import unidecode

unidecode_installed = True
except ImportError:
unidecode_installed = False


def unidecode(tag):
def slugify_unicode_stripping_prep(tag):
"""
This handles stripping via unidecode if it's installed,
otherwise is a no-op
"""
if unidecode_installed:
return unidecode(tag)
else:
return tag


Expand Down Expand Up @@ -97,7 +108,7 @@ def save(self, *args, **kwargs):

def slugify(self, tag, i=None):
if getattr(settings, "TAGGIT_STRIP_UNICODE_WHEN_SLUGIFYING", False):
slug = slugify(unidecode(tag))
slug = slugify(slugify_unicode_stripping_prep(tag))
else:
slug = slugify(tag, allow_unicode=True)
if i is not None:
Expand Down
29 changes: 28 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from unittest import skipIf

from django.test import TestCase, override_settings

from taggit import models as taggit_models

from tests.models import TestModel


Expand All @@ -22,7 +26,30 @@ def test_unicode_slugs(self):
sample_obj.tags.add("あい うえお")
self.assertEqual([tag.slug for tag in sample_obj.tags.all()], ["あい-うえお"])

def test_old_slugs(self):
def test_old_slugs_wo_unidecode(self):
"""
Test that the setting that gives us the old slugification behavior
is in place
"""
with override_settings(TAGGIT_STRIP_UNICODE_WHEN_SLUGIFYING=True):
old_installed_value = taggit_models.unidecode_installed
taggit_models.unidecode_installed = False
try:
sample_obj = TestModel.objects.create()
# a unicode tag will be slugified for space reasons but
# unicode-ness will be kept by default
sample_obj.tags.add("あい うえお")
self.assertEqual(
[tag.slug for tag in sample_obj.tags.all()], ["ai-ueo"]
)
finally:
taggit_models.unidecode_installed = old_installed_value

@skipIf(
not taggit_models.unidecode_installed,
"This test requires unidecode to be installed",
)
def test_old_slugs_with_unidecode(self):
"""
Test that the setting that gives us the old slugification behavior
is in place
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ deps =
djmain: https://github.com/django/django/archive/main.tar.gz
coverage
djangorestframework
unidecode
setenv =
PYTHONWARNINGS=all
commands =
Expand Down

0 comments on commit f372c95

Please sign in to comment.