tests.py 3.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from django.db import models
from utils.translation import MultilingualField, ModelTranslateMeta
from django.utils import translation
from django.test import TestCase, override_settings
from django.core.exceptions import FieldError

LANGUAGES = [
    ('en', 'English'),
    ('nl', 'Dutch'),
    ('fr', 'French'),
]


@override_settings(LANGUAGES=LANGUAGES)
class TestTranslateMeta(TestCase):

    def test_translate_adds_fields(self):
        class TestItem(models.Model, metaclass=ModelTranslateMeta):
            text = MultilingualField(models.TextField)

        self.assertTrue(hasattr(TestItem, 'text_en'))
        self.assertTrue(hasattr(TestItem, 'text_nl'))
        self.assertTrue(hasattr(TestItem, 'text_fr'))
        self.assertTrue(hasattr(TestItem, 'text'))

    def test_verbose_name_kwargs(self):
        class TestItem2(models.Model, metaclass=ModelTranslateMeta):
            text = MultilingualField(models.TextField, verbose_name='Text')

        nl = TestItem2._meta.get_field('text_nl').verbose_name
        en = TestItem2._meta.get_field('text_en').verbose_name
        fr = TestItem2._meta.get_field('text_fr').verbose_name
        self.assertIn('Text', nl)
        self.assertIn('Text', en)
        self.assertIn('Text', fr)
        self.assertEqual(len({nl, en, fr}), 3)

    def test_verbose_name_args(self):
        class TestItem3(models.Model, metaclass=ModelTranslateMeta):
            text = MultilingualField(models.TextField, 'Text')

        nl = TestItem3._meta.get_field('text_nl').verbose_name
        en = TestItem3._meta.get_field('text_en').verbose_name
        fr = TestItem3._meta.get_field('text_fr').verbose_name
        self.assertIn('Text', nl)
        self.assertIn('Text', en)
        self.assertIn('Text', fr)
        self.assertEqual(len({nl, en, fr}), 3)

50
51
52
53
54
55
56
57
58
59
60
61
    def test_no_verbose_name(self):
        class TestItem3b(models.Model, metaclass=ModelTranslateMeta):
            text = MultilingualField(models.TextField)

        nl = TestItem3b._meta.get_field('text_nl').verbose_name
        en = TestItem3b._meta.get_field('text_en').verbose_name
        fr = TestItem3b._meta.get_field('text_fr').verbose_name
        self.assertEqual('text (NL)', nl)
        self.assertEqual('text (EN)', en)
        self.assertEqual('text (FR)', fr)
        self.assertEqual(len({nl, en, fr}), 3)

62
63
64
65
66
67
    def test_other_kwargs(self):
        class TestItem4(models.Model, metaclass=ModelTranslateMeta):
            text = MultilingualField(models.CharField, 'Text', max_length=100)
        self.assertEqual(TestItem4._meta.get_field('text_nl').max_length, 100)

    def test_related_fields(self):
68
69
70
71
72
        for field_type in (models.ForeignKey, models.OneToOneField,
                           models.ManyToManyField):
            with self.assertRaises(NotImplementedError):
                class TestItem5(models.Model, metaclass=ModelTranslateMeta):
                    foreign = MultilingualField(field_type, 'TestItem5')
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

    def test_setter(self):
        class TestItem6(models.Model, metaclass=ModelTranslateMeta):
            text = MultilingualField(models.TextField)

        with self.assertRaises(AttributeError):
            TestItem6().text = 'text'  # Should not be able to set

    def test_accessor(self):
        class TestItem7(models.Model, metaclass=ModelTranslateMeta):
            text = MultilingualField(models.TextField, default='text')

        with translation.override('nl'):
            self.assertEqual(TestItem7().text, TestItem7().text_nl)

    def test_shadowing(self):
        with self.assertRaises(FieldError):
            class TestItem8(models.Model, metaclass=ModelTranslateMeta):
                text = MultilingualField(models.TextField)
                text_nl = MultilingualField(models.TextField)