Test Failed
Push — master ( 06f7ee...2741a6 )
by Swen
02:02
created

LocalizedExpressionsTestCase.test_localized_ref()   F

Complexity

Conditions 13

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
c 2
b 0
f 0
dl 0
loc 40
rs 2.7716

1 Method

Rating   Name   Duplication   Size   Complexity  
A LocalizedExpressionsTestCase.create_queryset() 0 5 1

How to fix   Complexity   

Complexity

Complex classes like LocalizedExpressionsTestCase.test_localized_ref() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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 import models
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...
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.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...
5
6
from localized_fields.fields import LocalizedField
7
from localized_fields.value import LocalizedValue
8
from localized_fields.expressions import LocalizedRef
9
10
from .fake_model import get_fake_model
11
12
13
class LocalizedExpressionsTestCase(TestCase):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
14
    """Tests whether expressions properly work with :see:LocalizedField."""
15
16
    TestModel1 = None
17
    TestModel2 = None
18
19
    @classmethod
20
    def setUpClass(cls):
21
        """Creates the test model in the database."""
22
23
        super(LocalizedExpressionsTestCase, cls).setUpClass()
24
25
        cls.TestModel1 = get_fake_model(
26
            'LocalizedExpressionsTestCase2',
27
            {
28
                'name': models.CharField(null=False, blank=False, max_length=255),
29
            }
30
        )
31
32
        cls.TestModel2 = get_fake_model(
33
            'LocalizedExpressionsTestCase1',
34
            {
35
                'text': LocalizedField(),
36
                'other': models.ForeignKey(cls.TestModel1, related_name='features')
37
            }
38
        )
39
40
    @classmethod
41
    def test_localized_ref(cls):
42
        """Tests whether the :see:LocalizedRef expression properly works."""
43
44
        obj = cls.TestModel1.objects.create(name='bla bla')
45
        for i in range(0, 10):
46
            cls.TestModel2.objects.create(
47
                text=LocalizedValue(dict(en='text_%d_en' % i, ro='text_%d_ro' % i, nl='text_%d_nl' % i)),
48
                other=obj
49
            )
50
51
        def create_queryset(ref):
52
            return (
53
                cls.TestModel1.objects
54
                .annotate(mytexts=ref)
55
                .values_list('mytexts', flat=True)
56
            )
57
58
        # assert that it properly selects the currently active language
59
        for lang_code, _ in settings.LANGUAGES:
60
            translation.activate(lang_code)
61
            queryset = create_queryset(LocalizedRef('features__text'))
62
63
            for index, value in enumerate(queryset):
64
                assert translation.get_language() in value
65
                assert str(index) in value
66
67
        # ensure that the default language is used in case no
68
        # language is active at all
69
        translation.deactivate_all()
70
        queryset = create_queryset(LocalizedRef('features__text'))
71
        for index, value in enumerate(queryset):
72
            assert settings.LANGUAGE_CODE in value
73
            assert str(index) in value
74
75
        # ensures that overriding the language works properly
76
        queryset = create_queryset(LocalizedRef('features__text', 'ro'))
77
        for index, value in enumerate(queryset):
78
            assert 'ro' in value
79
            assert str(index) in value
80