1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
from __future__ import unicode_literals |
4
|
|
|
|
5
|
|
|
from django.test import TestCase |
6
|
|
|
|
7
|
|
|
from parler.templatetags.parler_tags import _url_qs |
8
|
|
|
from parler.utils import get_parler_languages_from_django_cms |
9
|
|
|
from parler.utils.i18n import get_language_title |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class UtilTestCase(TestCase): |
13
|
|
|
|
14
|
|
|
def test_get_parler_languages_from_django_cms(self): |
15
|
|
|
cms = { |
16
|
|
|
1: [ |
17
|
|
|
{ |
18
|
|
|
'code': 'en', |
19
|
|
|
'fallbacks': ['es'], |
20
|
|
|
'hide_untranslated': True, |
21
|
|
|
'name': 'English', |
22
|
|
|
'public': True, |
23
|
|
|
'redirect_on_fallback': True |
24
|
|
|
}, |
25
|
|
|
{ |
26
|
|
|
'code': 'es', |
27
|
|
|
'fallbacks': ['en'], |
28
|
|
|
'hide_untranslated': True, |
29
|
|
|
'name': 'Spanish', |
30
|
|
|
'public': True, |
31
|
|
|
'redirect_on_fallback': True |
32
|
|
|
}, |
33
|
|
|
{ |
34
|
|
|
'code': 'fr', |
35
|
|
|
'fallbacks': ['en'], |
36
|
|
|
'hide_untranslated': True, |
37
|
|
|
'name': 'French', |
38
|
|
|
'public': True, |
39
|
|
|
'redirect_on_fallback': True |
40
|
|
|
} |
41
|
|
|
], |
42
|
|
|
'default': { |
43
|
|
|
'fallbacks': ['en', ], |
44
|
|
|
'hide_untranslated': True, |
45
|
|
|
'public': True, |
46
|
|
|
'redirect_on_fallback': True |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
parler = { |
51
|
|
|
1: [ |
52
|
|
|
{ |
53
|
|
|
'code': 'en', |
54
|
|
|
'fallbacks': ['es'], |
55
|
|
|
'hide_untranslated': True, |
56
|
|
|
'redirect_on_fallback': True |
57
|
|
|
}, |
58
|
|
|
{ |
59
|
|
|
'code': 'es', |
60
|
|
|
'fallbacks': ['en'], |
61
|
|
|
'hide_untranslated': True, |
62
|
|
|
'redirect_on_fallback': True |
63
|
|
|
}, |
64
|
|
|
{ |
65
|
|
|
'code': 'fr', |
66
|
|
|
'fallbacks': ['en'], |
67
|
|
|
'hide_untranslated': True, |
68
|
|
|
'redirect_on_fallback': True |
69
|
|
|
} |
70
|
|
|
], |
71
|
|
|
'default': { |
72
|
|
|
'fallbacks': ['en', ], |
73
|
|
|
'hide_untranslated': True, |
74
|
|
|
'redirect_on_fallback': True |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
computed = get_parler_languages_from_django_cms(cms) |
79
|
|
|
for block, block_config in computed.items(): |
80
|
|
|
self.assertEqual(computed[block], parler[block]) |
81
|
|
|
|
82
|
|
|
def test_get_language_title(self): |
83
|
|
|
"""Test get_language_title utility function""" |
84
|
|
|
language_code = 'en' |
85
|
|
|
self.assertEqual(get_language_title(language_code), 'English') |
86
|
|
|
|
87
|
|
|
# Test the case where requested language is not in settings. |
88
|
|
|
# We can not override settings, since languages in get_language_title() |
89
|
|
|
# are initialised during import. So, we use fictional language code. |
90
|
|
|
language_code = 'xx' |
91
|
|
|
try: |
92
|
|
|
self.assertEqual(get_language_title(language_code), language_code) |
93
|
|
|
except KeyError: |
94
|
|
|
self.fail( |
95
|
|
|
"get_language_title() raises KeyError for missing language") |
96
|
|
|
|
97
|
|
|
def test_url_qs(self): |
98
|
|
|
matches = [ |
99
|
|
|
('http://www.example.com/search/', 'q=è453è5p4j5uih758') |
100
|
|
|
] |
101
|
|
|
for match in matches: |
102
|
|
|
merged = _url_qs(match[0], match[1]) |
103
|
|
|
self.assertTrue(merged) |