|
1
|
|
|
from django.test import TestCase |
|
2
|
|
|
from django.utils import translation |
|
3
|
|
|
|
|
4
|
|
|
from localized_fields.fields import LocalizedValue |
|
5
|
|
|
|
|
6
|
|
|
from .fake_model import get_fake_model |
|
7
|
|
|
from .test_localized_field import get_init_values |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class LocalizedModelTestCase(TestCase): |
|
11
|
|
|
"""Tests whether the :see:LocalizedValueDescriptor class.""" |
|
12
|
|
|
|
|
13
|
|
|
TestModel = None |
|
14
|
|
|
|
|
15
|
|
|
@classmethod |
|
16
|
|
|
def setUpClass(cls): |
|
17
|
|
|
"""Creates the test model in the database.""" |
|
18
|
|
|
|
|
19
|
|
|
super(LocalizedModelTestCase, cls).setUpClass() |
|
20
|
|
|
|
|
21
|
|
|
cls.TestModel = get_fake_model() |
|
22
|
|
|
|
|
23
|
|
|
@classmethod |
|
24
|
|
|
def test_defaults(cls): |
|
25
|
|
|
"""Tests whether all :see:LocalizedField |
|
26
|
|
|
fields are assigned an empty :see:LocalizedValue |
|
27
|
|
|
instance when the model is instanitiated.""" |
|
28
|
|
|
|
|
29
|
|
|
obj = cls.TestModel() |
|
30
|
|
|
|
|
31
|
|
|
assert isinstance(obj.title, LocalizedValue) |
|
32
|
|
|
|
|
33
|
|
|
@classmethod |
|
34
|
|
|
def test_set(cls): |
|
35
|
|
|
"""Tests whether the :see:LocalizedValueDescriptor |
|
36
|
|
|
class's see:set function works properly.""" |
|
37
|
|
|
|
|
38
|
|
|
obj = cls.TestModel() |
|
39
|
|
|
|
|
40
|
|
|
for language, value in get_init_values(): |
|
41
|
|
|
translation.activate(language) |
|
42
|
|
|
obj.title = value |
|
43
|
|
|
assert obj.title.get(language) == value |
|
44
|
|
|
assert getattr(obj.title, language) == value |
|
45
|
|
|
|
|
46
|
|
|
@classmethod |
|
47
|
|
|
def test_model_init_kwargs(cls): |
|
48
|
|
|
"""Tests whether all :see:LocalizedField |
|
49
|
|
|
fields are assigned an empty :see:LocalizedValue |
|
50
|
|
|
instance when the model is instanitiated.""" |
|
51
|
|
|
data = { |
|
52
|
|
|
'title': { |
|
53
|
|
|
'en': 'english_title', |
|
54
|
|
|
'ro': 'romanian_title', |
|
55
|
|
|
'nl': 'dutch_title' |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
obj = cls.TestModel(**data) |
|
59
|
|
|
assert isinstance(obj.title, LocalizedValue) |
|
60
|
|
|
assert obj.title.en == 'english_title' |
|
61
|
|
|
assert obj.title.ro == 'romanian_title' |
|
62
|
|
|
assert obj.title.nl == 'dutch_title' |
|
63
|
|
|
|