Passed
Push — master ( 0fa79d...5a4f44 )
by Swen
01:58
created

LocalizedValueTestCase.test_init_array()   A

Complexity

Conditions 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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