Completed
Push — master ( 0bffe1...d6203d )
by Diederik van der
11s
created

TranslationRelationTestCase   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 58
Duplicated Lines 0 %
Metric Value
wmc 2
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B test_related_objects_in_translation_model() 0 39 1
A test_translation_is_modified() 0 16 1
1
from .utils import AppTestCase
2
3
from .testapp.models import TranslationRelated, TranslationRelatedRelation
4
from .testapp.models import ForeignKeyTranslationModel, RegularModel
5
6
7
class TranslationRelationTestCase(AppTestCase):
8
9
    def test_related_objects_in_translation_model(self):
10
        instance = TranslationRelated()
11
        instance.set_current_language(self.other_lang1)
12
13
        # This should not raise errors
14
        instance.title = 'Title Lang1'
15
        instance.save()
16
17
        instance.set_current_language(self.other_lang2)
18
19
        # This should not raise errors
20
        instance.title = 'Title Lang2'
21
        instance.save()
22
23
        translation1 = instance.get_translation(self.other_lang1)
24
25
        translation1.translation_relations.create(name='relation 1.1')
26
        translation1.translation_relations.create(name='relation 1.2')
27
28
        translation2 = instance.get_translation(self.other_lang2)
29
        translation2.translation_relations.create(name='relation 2.1')
30
31
        total_related_objects = TranslationRelatedRelation.objects.filter(
32
            translation__master=instance
33
        ).count()
34
35
        lang1_related_object = TranslationRelatedRelation.objects.filter(
36
            translation__language_code=self.other_lang1,
37
            translation__master=instance,
38
        ).count()
39
40
        lang2_related_objects = TranslationRelatedRelation.objects.filter(
41
            translation__language_code=self.other_lang2,
42
            translation__master=instance,
43
        ).count()
44
45
        self.assertEqual(3, total_related_objects)
46
        self.assertEqual(2, lang1_related_object)
47
        self.assertEqual(1, lang2_related_objects)
48
49
    def test_translation_is_modified(self):
50
        r1 = RegularModel.objects.create(original_field='r1')
51
        r2 = RegularModel.objects.create(original_field='r2')
52
53
        instance = ForeignKeyTranslationModel.objects.create(
54
            translated_foreign=r1,
55
            shared='shared',
56
        )
57
        translation = instance.get_translation(instance.language_code)
58
59
        self.assertFalse(translation.is_modified)
60
61
        instance.translated_foreign = r2
62
        translation = instance.get_translation(instance.language_code)
63
64
        self.assertTrue(translation.is_modified)
65