Passed
Push — master ( 83605c...084f5d )
by Swen
02:08 queued 30s
created

test_get_context_required()   A

Complexity

Conditions 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 3
1
import re
2
from django.conf import settings
0 ignored issues
show
Configuration introduced by
The import django.conf could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
3
from django.test import TestCase
0 ignored issues
show
Configuration introduced by
The import django.test could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4
5
from localized_fields.value import LocalizedValue
6
from localized_fields.widgets import LocalizedFieldWidget
7
8
9
class LocalizedFieldWidgetTestCase(TestCase):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
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)
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldWidget does not seem to have a member named widgets.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
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
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldWidget does not seem to have a member named widgets.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
54
        widget.widgets[1].is_required = False
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldWidget does not seem to have a member named widgets.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
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):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldWidget does not seem to have a member named widgets.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
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)
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldWidget does not seem to have a member named render.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
82
        assert bool(re.search('<label (.|\n|\t)*>\w+<\/label>', output))
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \w was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
Bug introduced by
A suspicious escape sequence \/ was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
83