Passed
Pull Request — master (#1)
by
unknown
02:17
created

LocalizedModelTestCase   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 53
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpClass() 0 7 1
A test_defaults() 0 9 2
A test_set() 0 12 4
B test_model_init_kwargs() 0 17 5
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