Completed
Pull Request — master (#135)
by Iacopo
01:03
created

FormTests   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %
Metric Value
wmc 6
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test_unique_together() 0 13 2
A test_not_null_foreignkey_in_translation() 0 18 1
A test_form_save() 0 21 2
A test_form_fields() 0 6 1
1
import django
2
from django.core.exceptions import ValidationError
3
from django.utils import translation
4
from parler.forms import TranslatableModelForm
5
from .utils import AppTestCase
6
from .testapp.models import SimpleModel, UniqueTogetherModel, ForeignKeyTranslationModel, RegularModel
7
8
9
class SimpleForm(TranslatableModelForm):
10
11
    class Meta:
12
        model = SimpleModel
13
        if django.VERSION >= (1, 6):
14
            fields = '__all__'
15
16
17
class UniqueTogetherForm(TranslatableModelForm):
18
19
    class Meta:
20
        model = UniqueTogetherModel
21
        if django.VERSION >= (1, 6):
22
            fields = '__all__'
23
24
25
class ForeignKeyTranslationModelForm(TranslatableModelForm):
26
27
    class Meta:
28
        model = ForeignKeyTranslationModel
29
        if django.VERSION >= (1, 6):
30
            fields = '__all__'
31
32
33
class FormTests(AppTestCase):
34
    """
35
    Test model construction
36
    """
37
38
    def test_form_fields(self):
39
        """
40
        Check if the form fields exist.
41
        """
42
        self.assertTrue('shared' in SimpleForm.base_fields)
43
        self.assertTrue('tr_title' in SimpleForm.base_fields)
44
45
    def test_form_save(self):
46
        """
47
        Check if the form receives and stores data.
48
        """
49
        with translation.override('fr'):
50
            # Initialize form in other language.
51
            x = SimpleForm(data={'shared': 'TEST', 'tr_title': 'TRANS'})
52
            x.language_code = 'nl'
53
            self.assertFalse(x.errors)
54
55
            # Data should come out
56
            self.assertEqual(x.cleaned_data['shared'], 'TEST')
57
            self.assertEqual(x.cleaned_data['tr_title'], 'TRANS')
58
59
            # Data should be saved
60
            instance = x.save()
61
            self.assertEqual(instance.get_current_language(), 'nl')
62
63
            x = SimpleModel.objects.language('nl').get(pk=instance.pk)
64
            self.assertEqual(x.shared, 'TEST')
65
            self.assertEqual(x.tr_title, 'TRANS')
66
67
    def test_unique_together(self):
68
        UniqueTogetherModel(_current_language='en', slug='foo').save()
69
70
        # Different language code, no problem
71
        form = UniqueTogetherForm(data={'slug': 'foo'})
72
        form.language_code = 'fr'
73
        self.assertTrue(form.is_valid())
74
75
        # Same language code, should raise unique_together check
76
        form = UniqueTogetherForm(data={'slug': 'foo'})
77
        form.language_code = 'en'
78
        self.assertFalse(form.is_valid())
79
        self.assertRaises(ValidationError, lambda: form.instance.validate_unique())
80
81
    def test_not_null_foreignkey_in_translation(self):
82
        """
83
        Simulate scenario for model with translation field of type foreign key (not null).
84
          1. User create model with one translation (EN)
85
          2. Switch to another language in admin (FR)
86
        """
87
88
        # create object with translation
89
        r1 = RegularModel.objects.create(original_field='r1')
90
        a = ForeignKeyTranslationModel.objects.create(translated_foreign=r1, shared='EN')
91
92
        # same way as TranslatableAdmin.get_object() inicializing translation, when user swich to new translation language
93
        a.set_current_language('fr', initialize=True)
94
95
        # inicialize form
96
        form = ForeignKeyTranslationModelForm(instance=a)
97
98
        self.assertTrue(True)
99