<commit_before>import copy

from django import forms
from django.db import models
from django.conf import settings
from django.test import TestCase
from django.db.utils import IntegrityError
from django.utils.text import slugify

from localized_fields.fields import (
    LocalizedField,
    LocalizedUniqueSlugField
)

from .fake_model import get_fake_model


class LocalizedSlugFieldTestCase(TestCase):
    """Tests the localized slug classes."""

    AutoSlugModel = None
    Model = None

    @classmethod
    def setUpClass(cls):
        """Creates the test models in the database."""

        super(LocalizedSlugFieldTestCase, cls).setUpClass()

        cls.Model = get_fake_model(
            {
                'title': LocalizedField(),
                'name': models.CharField(max_length=255),
                'slug': LocalizedUniqueSlugField(populate_from='title')
            }
        )

    @staticmethod
    def test_unique_slug_with_time():
        """Tests whether the primary key is included in
        the slug when the 'use_pk' option is enabled."""

        title = 'myuniquetitle'

        PkModel = get_fake_model(
            {
                'title': LocalizedField(),
                'slug': LocalizedUniqueSlugField(populate_from='title', include_time=True)
            }
        )

        obj = PkModel()
        obj.title.en = title
        obj.save()

        assert obj.slug.en.startswith('%s-' % title)

    @classmethod
    def test_uniue_slug_no_change(cls):
        """Tests whether slugs are not re-generated if not needed."""

        NoChangeSlugModel = get_fake_model(
            {
                'title': LocalizedField(),
                'slug': LocalizedUniqueSlugField(populate_from='title', include_time=True)
            }
        )

        title = 'myuniquetitle'

        obj = NoChangeSlugModel()
        obj.title.en = title
        obj.title.nl = title
        obj.save()

        old_slug_en = copy.deepcopy(obj.slug.en)
        old_slug_nl = copy.deepcopy(obj.slug.nl)
        obj.title.nl += 'beer'
        obj.save()

        assert old_slug_en == obj.slug.en
        assert old_slug_nl != obj.slug.nl

    @classmethod
    def test_unique_slug_unique_max_retries(cls):
        """Tests whether the unique slug implementation doesn't
        try to find a slug forever and gives up after a while."""

        title = 'myuniquetitle'

        obj = cls.Model()
        obj.title.en = title<commit_msg>Add test that confirms slug is re-computed when value changes<commit_after>import copy

from django import forms
from django.db import models
from django.conf import settings
from django.test import TestCase
from django.db.utils import IntegrityError
from django.utils.text import slugify

from localized_fields.fields import (
    LocalizedField,
    LocalizedUniqueSlugField
)

from .fake_model import get_fake_model


class LocalizedSlugFieldTestCase(TestCase):
    """Tests the localized slug classes."""

    AutoSlugModel = None
    Model = None

    @classmethod
    def setUpClass(cls):
        """Creates the test models in the database."""

        super(LocalizedSlugFieldTestCase, cls).setUpClass()

        cls.Model = get_fake_model(
            {
                'title': LocalizedField(),
                'name': models.CharField(max_length=255),
                'slug': LocalizedUniqueSlugField(populate_from='title')
            }
        )

    @staticmethod
    def test_unique_slug_with_time():
        """Tests whether the primary key is included in
        the slug when the 'use_pk' option is enabled."""

        title = 'myuniquetitle'

        PkModel = get_fake_model(
            {
                'title': LocalizedField(),
                'slug': LocalizedUniqueSlugField(populate_from='title', include_time=True)
            }
        )

        obj = PkModel()
        obj.title.en = title
        obj.save()

        assert obj.slug.en.startswith('%s-' % title)

    @classmethod
    def test_uniue_slug_no_change(cls):
        """Tests whether slugs are not re-generated if not needed."""

        NoChangeSlugModel = get_fake_model(
            {
                'title': LocalizedField(),
                'slug': LocalizedUniqueSlugField(populate_from='title', include_time=True)
            }
        )

        title = 'myuniquetitle'

        obj = NoChangeSlugModel()
        obj.title.en = title
        obj.title.nl = title
        obj.save()

        old_slug_en = copy.deepcopy(obj.slug.en)
        old_slug_nl = copy.deepcopy(obj.slug.nl)
        obj.title.nl += 'beer'
        obj.save()

        assert old_slug_en == obj.slug.en
        assert old_slug_nl != obj.slug.nl

    @classmethod
    def test_unique_slug_update(cls):
        obj = cls.Model.objects.create(title={settings.LANGUAGE_CODE: 'mytitle'})
        assert obj.slug.get() == 'mytitle'
        obj.title.set(settings.LANGUAGE_CODE, 'othertitle')
        obj.save()
        assert obj.slug.get() == 'othertitle'

    @classmethod
    def test_unique_slug_unique_max_retries(cls):
        """Tests whether the unique slug implementation doesn't
        try to find a slug forever and gives up after a while."""

        title = 'myuniquetitle'

        obj = cls.Model()
        obj.title.en = title
{'fraction_non_alphanumeric': 0.09468690702087286, 'fraction_numerical': 0.0011385199240986717, 'mean_word_length': 2.2617574257425743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6057650', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1535, 'n_words': 365}