|
1
|
|
|
from django.test import TestCase |
|
|
|
|
|
|
2
|
|
|
from django.db import models |
|
|
|
|
|
|
3
|
|
|
from django.utils import translation |
|
|
|
|
|
|
4
|
|
|
from django.conf import settings |
|
|
|
|
|
|
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): |
|
|
|
|
|
|
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
|
|
|
|
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.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.