Passed
Push — master ( 5e58bc...04f153 )
by Swen
01:51
created

LocalizedValueTestCase   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 38
c 8
b 0
f 0
dl 0
loc 181
rs 8.3999

14 Methods

Rating   Name   Duplication   Size   Complexity  
A test_get_explicit() 0 11 3
A test_eq() 0 12 3
A test_init() 0 11 3
A test_construct_string() 0 7 2
A tearDown() 0 6 1
A test_init_array() 0 11 2
A test_get_default_language() 0 12 3
A test_init_default_values() 0 10 3
A test_set() 0 11 4
A test_deconstruct() 0 11 2
A test_str() 0 11 3
B test_str_fallback() 0 28 4
A test_str_fallback_custom_fallback() 0 17 3
A test_construct_expression() 0 7 2
1
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...
2
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...
3
from django.utils import translation
0 ignored issues
show
Configuration introduced by
The import django.utils 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
from django.db.models import F
0 ignored issues
show
Configuration introduced by
The import django.db.models 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...
5
6
from localized_fields.value import LocalizedValue
7
8
from .data import get_init_values
9
10
11
class LocalizedValueTestCase(TestCase):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
12
    """Tests the :see:LocalizedValue class."""
13
14
    @staticmethod
15
    def tearDown():
16
        """Assures that the current language
17
        is set back to the default."""
18
19
        translation.activate(settings.LANGUAGE_CODE)
20
21
    @staticmethod
22
    def test_init():
23
        """Tests whether the __init__ function
24
        of the :see:LocalizedValue class works
25
        as expected."""
26
27
        keys = get_init_values()
28
        value = LocalizedValue(keys)
29
30
        for lang_code, _ in settings.LANGUAGES:
31
            assert getattr(value, lang_code, None) == keys[lang_code]
32
33
    @staticmethod
34
    def test_init_default_values():
35
        """Tests whether the __init__ function
36
        of the :see:LocalizedValue accepts the
37
        default value or an empty dict properly."""
38
39
        value = LocalizedValue()
40
41
        for lang_code, _ in settings.LANGUAGES:
42
            assert getattr(value, lang_code) is None
43
44
    @staticmethod
45
    def test_init_array():
46
        """Tests whether the __init__ function
47
        of :see:LocalizedValue properly handles an
48
        array.
49
50
        Arrays can be passed to LocalizedValue as
51
        a result of a ArrayAgg operation."""
52
53
        value = LocalizedValue(['my value'])
54
        assert value.get(settings.LANGUAGE_CODE) == 'my value'
55
56
    @staticmethod
57
    def test_get_explicit():
58
        """Tests whether the the :see:LocalizedValue
59
        class's :see:get function works properly
60
        when specifying an explicit value."""
61
62
        keys = get_init_values()
63
        localized_value = LocalizedValue(keys)
64
65
        for language, value in keys.items():
66
            assert localized_value.get(language) == value
67
68
    @staticmethod
69
    def test_get_default_language():
70
        """Tests whether the :see:LocalizedValue
71
        class's see:get function properly
72
        gets the value in the default language."""
73
74
        keys = get_init_values()
75
        localized_value = LocalizedValue(keys)
76
77
        for language, _ in keys.items():
78
            translation.activate(language)
79
            assert localized_value.get() == keys[settings.LANGUAGE_CODE]
80
81
    @staticmethod
82
    def test_set():
83
        """Tests whether the :see:LocalizedValue
84
        class's see:set function works properly."""
85
86
        localized_value = LocalizedValue()
87
88
        for language, value in get_init_values():
89
            localized_value.set(language, value)
90
            assert localized_value.get(language) == value
91
            assert getattr(localized_value, language) == value
92
93
    @staticmethod
94
    def test_str():
95
        """Tests whether the :see:LocalizedValue
96
        class's __str__ works properly."""
97
98
        keys = get_init_values()
99
        localized_value = LocalizedValue(keys)
100
101
        for language, value in keys.items():
102
            translation.activate(language)
103
            assert str(localized_value) == value
104
105
    @staticmethod
106
    def test_eq():
107
        """Tests whether the __eq__ operator
108
        of :see:LocalizedValue works properly."""
109
110
        a = LocalizedValue({'en': 'a', 'ar': 'b'})
111
        b = LocalizedValue({'en': 'a', 'ar': 'b'})
112
113
        assert a == b
114
115
        b.en = 'b'
116
        assert a != b
117
118
    @staticmethod
119
    def test_str_fallback():
120
        """Tests whether the :see:LocalizedValue
121
        class's __str__'s fallback functionality
122
        works properly."""
123
124
        test_value = 'myvalue'
125
126
        localized_value = LocalizedValue({
127
            settings.LANGUAGE_CODE: test_value
128
        })
129
130
        other_language = settings.LANGUAGES[-1][0]
131
132
        # make sure that, by default it returns
133
        # the value in the default language
134
        assert str(localized_value) == test_value
135
136
        # make sure that it falls back to the
137
        # primary language when there's no value
138
        # available in the current language
139
        translation.activate(other_language)
140
        assert str(localized_value) == test_value
141
142
        # make sure that it's just __str__ falling
143
        # back and that for the other language
144
        # there's no actual value
145
        assert localized_value.get(other_language) != test_value
146
147
    @staticmethod
148
    def test_str_fallback_custom_fallback():
149
        """Tests whether the :see:LocalizedValue class's
150
        __str__'s fallback functionality properly respects
151
        the LOCALIZED_FIELDS_FALLBACKS setting."""
152
153
        settings.LOCALIZED_FIELDS_FALLBACKS = {
154
            'nl': ['ro']
155
        }
156
157
        localized_value = LocalizedValue({
158
            settings.LANGUAGE_CODE: settings.LANGUAGE_CODE,
159
            'ro': 'ro'
160
        })
161
162
        with translation.override('nl'):
163
            assert str(localized_value) == 'ro'
164
165
    @staticmethod
166
    def test_deconstruct():
167
        """Tests whether the :see:LocalizedValue
168
        class's :see:deconstruct function works properly."""
169
170
        keys = get_init_values()
171
        value = LocalizedValue(keys)
172
173
        path, args, kwargs = value.deconstruct()
0 ignored issues
show
Unused Code introduced by
The variable kwargs seems to be unused.
Loading history...
Unused Code introduced by
The variable path seems to be unused.
Loading history...
174
175
        assert args[0] == keys
176
177
    @staticmethod
178
    def test_construct_string():
179
        """Tests whether the :see:LocalizedValue's constructor
180
        assumes the primary language when passing a single string."""
181
182
        value = LocalizedValue('beer')
183
        assert value.get(settings.LANGUAGE_CODE) == 'beer'
184
185
    @staticmethod
186
    def test_construct_expression():
187
        """Tests whether passing expressions as values
188
        works properly and are not converted to string."""
189
190
        value = LocalizedValue(dict(en=F('other')))
191
        assert isinstance(value.en, F)
192