Passed
Push — master ( 69718c...20d9e6 )
by Swen
02:10
created

LocalizedAutoSlugFieldTestCase.setUpClass()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
1
from django import forms
2
from django.conf import settings
3
from django.test import TestCase
4
from django.utils.text import slugify
5
6
from localized_fields.fields import LocalizedAutoSlugField
7
from localized_fields.forms import LocalizedFieldForm
8
9
from .fake_model import get_fake_model
10
11
12
class LocalizedAutoSlugFieldTestCase(TestCase):
13
    """Tests the :see:LocalizedAutoSlugField class."""
14
15
    TestModel = None
16
17
    @classmethod
18
    def setUpClass(cls):
19
        """Creates the test model in the database."""
20
21
        super(LocalizedAutoSlugFieldTestCase, cls).setUpClass()
22
23
        cls.TestModel = get_fake_model()
24
25
    def test_populate(self):
26
        """Tests whether the :see:LocalizedAutoSlugField's
27
        populating feature works correctly."""
28
29
        obj = self.TestModel()
30
        obj.title.en = 'this is my title'
31
        obj.save()
32
33
        assert obj.slug.get('en') == slugify(obj.title.en)
34
35
    def test_populate_multiple_languages(self):
36
        """Tests whether the :see:LocalizedAutoSlugField's
37
        populating feature correctly works for all languages."""
38
39
        obj = self.TestModel()
40
41
        for lang_code, lang_name in settings.LANGUAGES:
42
            obj.title.set(lang_code, 'title %s' % lang_name)
43
44
        obj.save()
45
46
        for lang_code, lang_name in settings.LANGUAGES:
47
            assert obj.slug.get(lang_code) == 'title-%s' % lang_name.lower()
48
49
    def test_unique_slug(self):
50
        """Tests whether the :see:LocalizedAutoSlugField
51
        correctly generates unique slugs."""
52
53
        obj = self.TestModel()
54
        obj.title.en = 'title'
55
        obj.save()
56
57
        another_obj = self.TestModel()
58
        another_obj.title.en = 'title'
59
        another_obj.save()
60
61
        assert another_obj.slug.en == 'title-1'
62
63
    @staticmethod
64
    def test_deconstruct():
65
        """Tests whether the :see:deconstruct
66
        function properly retains options
67
        specified in the constructor."""
68
69
        field = LocalizedAutoSlugField(populate_from='title')
70
        _, _, _, kwargs = field.deconstruct()
71
72
        assert 'populate_from' in kwargs
73
        assert kwargs['populate_from'] == field.populate_from
74
75
    @staticmethod
76
    def test_formfield():
77
        """Tests whether the :see:formfield method
78
        returns a valid form field that is hidden."""
79
80
        field = LocalizedAutoSlugField(populate_from='title')
81
        form_field = field.formfield()
82
83
        assert isinstance(form_field, LocalizedFieldForm)
84
        assert isinstance(form_field.widget, forms.HiddenInput)
85