|
1
|
|
|
import re |
|
2
|
|
|
from django.conf import settings |
|
|
|
|
|
|
3
|
|
|
from django.test import TestCase |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
from localized_fields.value import LocalizedValue |
|
6
|
|
|
from localized_fields.widgets import LocalizedFieldWidget |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class LocalizedFieldWidgetTestCase(TestCase): |
|
|
|
|
|
|
10
|
|
|
"""Tests the workings of the :see:LocalizedFieldWidget class.""" |
|
11
|
|
|
|
|
12
|
|
|
@staticmethod |
|
13
|
|
|
def test_widget_creation(): |
|
14
|
|
|
"""Tests whether a widget is created for every |
|
15
|
|
|
language correctly.""" |
|
16
|
|
|
|
|
17
|
|
|
widget = LocalizedFieldWidget() |
|
18
|
|
|
assert len(widget.widgets) == len(settings.LANGUAGES) |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
@staticmethod |
|
21
|
|
|
def test_decompress(): |
|
22
|
|
|
"""Tests whether a :see:LocalizedValue instance |
|
23
|
|
|
can correctly be "decompressed" over the available |
|
24
|
|
|
widgets.""" |
|
25
|
|
|
|
|
26
|
|
|
localized_value = LocalizedValue() |
|
27
|
|
|
for lang_code, lang_name in settings.LANGUAGES: |
|
28
|
|
|
localized_value.set(lang_code, lang_name) |
|
29
|
|
|
|
|
30
|
|
|
widget = LocalizedFieldWidget() |
|
31
|
|
|
decompressed_values = widget.decompress(localized_value) |
|
32
|
|
|
|
|
33
|
|
|
for (lang_code, _), value in zip(settings.LANGUAGES, decompressed_values): |
|
34
|
|
|
assert localized_value.get(lang_code) == value |
|
35
|
|
|
|
|
36
|
|
|
@staticmethod |
|
37
|
|
|
def test_decompress_none(): |
|
38
|
|
|
"""Tests whether the :see:LocalizedFieldWidget correctly |
|
39
|
|
|
handles :see:None.""" |
|
40
|
|
|
|
|
41
|
|
|
widget = LocalizedFieldWidget() |
|
42
|
|
|
decompressed_values = widget.decompress(None) |
|
43
|
|
|
|
|
44
|
|
|
for _, value in zip(settings.LANGUAGES, decompressed_values): |
|
45
|
|
|
assert not value |
|
46
|
|
|
|
|
47
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
|
|
48
|
|
|
def test_get_context_required(): |
|
49
|
|
|
"""Tests whether the :see:get_context correctly |
|
50
|
|
|
handles 'required' attribute, separately for each subwidget.""" |
|
51
|
|
|
|
|
52
|
|
|
widget = LocalizedFieldWidget() |
|
53
|
|
|
widget.widgets[0].is_required = True |
|
|
|
|
|
|
54
|
|
|
widget.widgets[1].is_required = False |
|
|
|
|
|
|
55
|
|
|
context = widget.get_context(name='test', value=LocalizedValue(), |
|
56
|
|
|
attrs=dict(required=True)) |
|
57
|
|
|
assert context['widget']['subwidgets'][0]['attrs']['required'] |
|
58
|
|
|
assert 'required' not in context['widget']['subwidgets'][1]['attrs'] |
|
59
|
|
|
|
|
60
|
|
|
@staticmethod |
|
61
|
|
|
def test_get_context_langs(): |
|
62
|
|
|
"""Tests whether the :see:get_context contains 'lang_code' and |
|
63
|
|
|
'lang_name' attribute for each subwidget.""" |
|
64
|
|
|
|
|
65
|
|
|
widget = LocalizedFieldWidget() |
|
66
|
|
|
context = widget.get_context(name='test', value=LocalizedValue(), |
|
67
|
|
|
attrs=dict()) |
|
68
|
|
|
subwidgets_context = context['widget']['subwidgets'] |
|
69
|
|
|
for widget, context in zip(widget.widgets, subwidgets_context): |
|
|
|
|
|
|
70
|
|
|
assert 'lang_code' in context |
|
71
|
|
|
assert 'lang_name' in context |
|
72
|
|
|
assert widget.lang_code == context['lang_code'] |
|
73
|
|
|
assert widget.lang_name == context['lang_name'] |
|
74
|
|
|
|
|
75
|
|
|
@staticmethod |
|
76
|
|
|
def test_render(): |
|
77
|
|
|
"""Tests whether the :see:LocalizedFieldWidget correctly |
|
78
|
|
|
render.""" |
|
79
|
|
|
|
|
80
|
|
|
widget = LocalizedFieldWidget() |
|
81
|
|
|
output = widget.render(name='title', value=None) |
|
|
|
|
|
|
82
|
|
|
assert bool(re.search('<label (.|\n|\t)*>\w+<\/label>', output)) |
|
|
|
|
|
|
83
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.