Passed
Push — master ( 16e239...7316d3 )
by Swen
01:59
created

test_str_fallback_custom_fallback()   A

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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