|
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 import LocalizedAutoSlugField |
|
7
|
|
|
|
|
8
|
|
|
from .fake_model import get_fake_model |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class LocalizedAutoSlugFieldTestCase(TestCase): |
|
12
|
|
|
"""Tests the :see:LocalizedAutoSlugField class.""" |
|
13
|
|
|
|
|
14
|
|
|
TestModel = None |
|
15
|
|
|
|
|
16
|
|
|
@classmethod |
|
17
|
|
|
def setUpClass(cls): |
|
18
|
|
|
"""Creates the test model in the database.""" |
|
19
|
|
|
|
|
20
|
|
|
super(LocalizedAutoSlugFieldTestCase, cls).setUpClass() |
|
21
|
|
|
|
|
22
|
|
|
cls.TestModel = get_fake_model() |
|
23
|
|
|
|
|
24
|
|
|
def test_populate(self): |
|
25
|
|
|
"""Tests whether the :see:LocalizedAutoSlugField's |
|
26
|
|
|
populating feature works correctly.""" |
|
27
|
|
|
|
|
28
|
|
|
obj = self.TestModel() |
|
29
|
|
|
obj.title.en = 'this is my title' |
|
30
|
|
|
obj.save() |
|
31
|
|
|
|
|
32
|
|
|
assert obj.slug.get('en') == slugify(obj.title.en) |
|
33
|
|
|
|
|
34
|
|
|
def test_populate_multiple_languages(self): |
|
35
|
|
|
"""Tests whether the :see:LocalizedAutoSlugField's |
|
36
|
|
|
populating feature correctly works for all languages.""" |
|
37
|
|
|
|
|
38
|
|
|
obj = self.TestModel() |
|
39
|
|
|
|
|
40
|
|
|
for lang_code, lang_name in settings.LANGUAGES: |
|
41
|
|
|
obj.title.set(lang_code, 'title %s' % lang_name) |
|
42
|
|
|
|
|
43
|
|
|
obj.save() |
|
44
|
|
|
|
|
45
|
|
|
for lang_code, lang_name in settings.LANGUAGES: |
|
46
|
|
|
assert obj.slug.get(lang_code) == 'title-%s' % lang_name.lower() |
|
47
|
|
|
|
|
48
|
|
|
def test_unique_slug(self): |
|
49
|
|
|
"""Tests whether the :see:LocalizedAutoSlugField |
|
50
|
|
|
correctly generates unique slugs.""" |
|
51
|
|
|
|
|
52
|
|
|
obj = self.TestModel() |
|
53
|
|
|
obj.title.en = 'title' |
|
54
|
|
|
obj.save() |
|
55
|
|
|
|
|
56
|
|
|
another_obj = self.TestModel() |
|
57
|
|
|
another_obj.title.en = 'title' |
|
58
|
|
|
another_obj.save() |
|
59
|
|
|
|
|
60
|
|
|
assert another_obj.slug.en == 'title-1' |
|
61
|
|
|
|
|
62
|
|
|
def test_unique_slug_utf(self): |
|
63
|
|
|
"""Tests whether generating a slug works |
|
64
|
|
|
when the value consists completely out |
|
65
|
|
|
of non-ASCII characters.""" |
|
66
|
|
|
|
|
67
|
|
|
obj = self.TestModel() |
|
68
|
|
|
obj.title.en = 'مكاتب للايجار بشارع بورسعيد' |
|
69
|
|
|
obj.save() |
|
70
|
|
|
|
|
71
|
|
|
assert obj.slug.en == 'مكاتب-للايجار-بشارع-بورسعيد' |
|
72
|
|
|
|
|
73
|
|
|
@staticmethod |
|
74
|
|
|
def test_deconstruct(): |
|
75
|
|
|
"""Tests whether the :see:deconstruct |
|
76
|
|
|
function properly retains options |
|
77
|
|
|
specified in the constructor.""" |
|
78
|
|
|
|
|
79
|
|
|
field = LocalizedAutoSlugField(populate_from='title') |
|
80
|
|
|
_, _, _, kwargs = field.deconstruct() |
|
81
|
|
|
|
|
82
|
|
|
assert 'populate_from' in kwargs |
|
83
|
|
|
assert kwargs['populate_from'] == field.populate_from |
|
84
|
|
|
|
|
85
|
|
|
@staticmethod |
|
86
|
|
|
def test_formfield(): |
|
87
|
|
|
"""Tests whether the :see:formfield method |
|
88
|
|
|
returns a valid form field that is hidden.""" |
|
89
|
|
|
|
|
90
|
|
|
field = LocalizedAutoSlugField(populate_from='title') |
|
91
|
|
|
form_field = field.formfield() |
|
92
|
|
|
|
|
93
|
|
|
assert isinstance(form_field, forms.CharField) |
|
94
|
|
|
assert isinstance(form_field.widget, forms.HiddenInput) |
|
95
|
|
|
|