Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test suite failing if unidecode is installed #924

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 39 additions & 4 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
from contextlib import contextmanager
from unittest import skipIf

from django.test import TestCase, override_settings

from taggit import models as taggit_models
from tests.models import TestModel


@contextmanager
def disable_unidecode():
"""
Disable unidecode temporarily
"""
old_installed_value = taggit_models.unidecode_installed
taggit_models.unidecode_installed = False
try:
yield
finally:
taggit_models.unidecode_installed = old_installed_value


class TestTaggableManager(TestCase):
def test_duplicates(self):
sample_obj = TestModel.objects.create()
Expand All @@ -22,17 +39,35 @@ 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 (
disable_unidecode(),
override_settings(TAGGIT_STRIP_UNICODE_WHEN_SLUGIFYING=True),
):
sample_obj = TestModel.objects.create()
sample_obj.tags.add("aあい うえおb")
# when unidecode is not installed, the unicode ends up being passed directly
# to slugify, and will get "wiped"
self.assertEqual([tag.slug for tag in sample_obj.tags.all()], ["a-b"])

@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
"""
with override_settings(TAGGIT_STRIP_UNICODE_WHEN_SLUGIFYING=True):
sample_obj = TestModel.objects.create()
# a unicode tag will be slugified for space reasons but
# unicode-ness will be kept by default
# unidecode will transform the tag on top of slugification
sample_obj.tags.add("あい うえお")
self.assertEqual([tag.slug for tag in sample_obj.tags.all()], [""])
self.assertEqual([tag.slug for tag in sample_obj.tags.all()], ["ai-ueo"])


class TestPrefetchCache(TestCase):
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ deps =
djmain: https://github.com/django/django/archive/main.tar.gz
coverage
djangorestframework
unidecode
setenv =
PYTHONWARNINGS=all
commands =
Expand Down
Loading