Test Failed
Push — master ( 752e17...8c83fa )
by Swen
02:59
created

LocalizedIntegerFieldTestCase.setUpClass()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
1
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...
2
from django.db.utils import IntegrityError
0 ignored issues
show
Configuration introduced by
The import django.db.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...
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.db import connection
0 ignored issues
show
Configuration introduced by
The import django.db 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.fields import LocalizedIntegerField
8
9
from .fake_model import get_fake_model
10
11
12
class LocalizedIntegerFieldTestCase(TestCase):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
13
    """Tests whether the :see:LocalizedIntegerField
14
    and :see:LocalizedIntegerValue works properly."""
15
16
    TestModel = None
17
18
    @classmethod
19
    def setUpClass(cls):
20
        super().setUpClass()
21
22
        cls.TestModel = get_fake_model({
23
            'score': LocalizedIntegerField()
24
        })
25
26
    def test_basic(self):
27
        """Tests the basics of storing integer values."""
28
29
        obj = self.TestModel()
30
        for index, (lang_code, _) in enumerate(settings.LANGUAGES):
31
            obj.score.set(lang_code, index + 1)
32
        obj.save()
33
34
        obj = self.TestModel.objects.all().first()
35
        for index, (lang_code, _) in enumerate(settings.LANGUAGES):
36
            assert obj.score.get(lang_code) == index + 1
37
38
    def test_primary_language_required(self):
39
        """Tests whether the primary language is required by
40
        default and all other languages are optiona."""
41
42
        # not filling in anything should raise IntegrityError,
43
        # the primary language is required
44
        with self.assertRaises(IntegrityError):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedIntegerFieldTestCase does not seem to have a member named assertRaises.

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...
45
            obj = self.TestModel()
46
            obj.save()
47
48
        # when filling all other languages besides the primary language
49
        # should still raise an error because the primary is always required
50
        with self.assertRaises(IntegrityError):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedIntegerFieldTestCase does not seem to have a member named assertRaises.

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...
51
            obj = self.TestModel()
52
            for lang_code, _ in settings.LANGUAGES:
53
                if lang_code == settings.LANGUAGE_CODE:
54
                    continue
55
                obj.score.set(lang_code, 23)
56
            obj.save()
57
58
    def test_default_value_none(self):
59
        """Tests whether the default value for optional languages
60
        is NoneType."""
61
62
        obj = self.TestModel()
63
        obj.score.set(settings.LANGUAGE_CODE, 1234)
64
        obj.save()
65
66
        for lang_code, _ in settings.LANGUAGES:
67
            if lang_code == settings.LANGUAGE_CODE:
68
                continue
69
70
            assert obj.score.get(lang_code) is None
71
72
    def test_translate(self):
73
        """Tests whether casting the value to an integer
74
        results in the value being returned in the currently
75
        active language as an integer."""
76
77
        obj = self.TestModel()
78
        for index, (lang_code, _) in enumerate(settings.LANGUAGES):
79
            obj.score.set(lang_code, index + 1)
80
        obj.save()
81
82
        obj.refresh_from_db()
83
        for index, (lang_code, _) in enumerate(settings.LANGUAGES):
84
            with translation.override(lang_code):
85
                assert int(obj.score) == index + 1
86
                assert obj.score.translate() == index + 1
87
88
    def test_translate_primary_fallback(self):
89
        """Tests whether casting the value to an integer
90
        results in the value begin returned in the active
91
        language and falls back to the primary language
92
        if there is no value in that language."""
93
94
        obj = self.TestModel()
95
        obj.score.set(settings.LANGUAGE_CODE, 25)
96
97
        secondary_language = settings.LANGUAGES[-1][0]
98
        assert obj.score.get(secondary_language) is None
99
100
        with translation.override(secondary_language):
101
            assert obj.score.translate() == 25
102
            assert int(obj.score) == 25
103
104
    def test_get_default_value(self):
105
        """Tests whether getting the value in a specific
106
        language properly returns the specified default
107
        in case it is not available."""
108
109
        obj = self.TestModel()
110
        obj.score.set(settings.LANGUAGE_CODE, 25)
111
112
        secondary_language = settings.LANGUAGES[-1][0]
113
        assert obj.score.get(secondary_language) is None
114
        assert obj.score.get(secondary_language, 1337) == 1337
115
116
    def test_completely_optional(self):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
117
        """Tests whether having all languages optional
118
        works properly."""
119
120
        model = get_fake_model({
121
            'score': LocalizedIntegerField(null=True, required=[], blank=True)
122
        })
123
124
        obj = model()
125
        obj.save()
126
127
        for lang_code, _ in settings.LANGUAGES:
128
            assert getattr(obj.score, lang_code) is None
129
130
    def test_store_string(self):
131
        """Tests whether the field properly raises
132
        an error when trying to store a non-integer."""
133
134
        for lang_code, _ in settings.LANGUAGES:
135
            obj = self.TestModel()
136
            with self.assertRaises(IntegrityError):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedIntegerFieldTestCase does not seem to have a member named assertRaises.

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...
137
                obj.score.set(lang_code, 'haha')
138
                obj.save()
139
140
    def test_none_if_illegal_value_stored(self):
141
        """Tests whether None is returned for a language
142
        if the value stored in the database is not an
143
        integer."""
144
145
        obj = self.TestModel()
146
        obj.score.set(settings.LANGUAGE_CODE, 25)
147
        obj.save()
148
149
        with connection.cursor() as cursor:
150
            table_name = self.TestModel._meta.db_table
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _meta was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
151
            cursor.execute("update %s set score = 'en=>haha'" % table_name);
0 ignored issues
show
Coding Style introduced by
Unnecessary semicolon
Loading history...
152
153
        obj.refresh_from_db()
154
        assert obj.score.get(settings.LANGUAGE_CODE) is None
155