1 # -*- coding: utf-8 -*-
2 from django.conf import settings
3 from django.contrib.auth.models import User
4 from django.contrib.contenttypes.models import ContentType
5 from django.db import models
6 from django.test import TestCase
7 from django.utils.thread_support import currentThread
8 from django.utils.translation import get_language
9 from django.utils.translation import trans_real
11 # TODO: tests for TranslationAdmin
13 from modeltranslation import translator
15 settings.LANGUAGES = (('de', 'Deutsch'),
19 class TestModel(models.Model):
20 title = models.CharField(max_length=255)
21 text = models.TextField(null=True)
24 class TestTranslationOptions(translator.TranslationOptions):
25 fields = ('title', 'text',)
28 translator.translator._registry = {}
29 translator.translator.register(TestModel, TestTranslationOptions)
32 class ModelTranslationTest(TestCase):
33 """Basic tests for the modeltranslation application."""
34 urls = 'modeltranslation.testurls'
37 trans_real.activate("de")
40 trans_real.deactivate()
42 def test_registration(self):
43 self.client.post('/set_language/', data={'language': 'de'})
44 #self.client.session['django_language'] = 'de-de'
45 #self.client.cookies[settings.LANGUAGE_COOKIE_NAME] = 'de-de'
47 langs = tuple(l[0] for l in settings.LANGUAGES)
48 self.failUnlessEqual(2, len(langs))
49 self.failUnless('de' in langs)
50 self.failUnless('en' in langs)
51 self.failUnless(translator.translator)
53 # Check that only one model is registered for translation
54 self.failUnlessEqual(len(translator.translator._registry), 1)
56 # Try to unregister a model that is not registered
57 self.assertRaises(translator.NotRegistered,
58 translator.translator.unregister, User)
60 # Try to get options for a model that is not registered
61 self.assertRaises(translator.NotRegistered,
62 translator.translator.get_options_for_model, User)
64 def test_translated_models(self):
65 # First create an instance of the test model to play with
66 inst = TestModel.objects.create(title="Testtitle", text="Testtext")
67 field_names = dir(inst)
68 self.failUnless('id' in field_names)
69 self.failUnless('title' in field_names)
70 self.failUnless('text' in field_names)
71 self.failUnless('title_de' in field_names)
72 self.failUnless('title_en' in field_names)
73 self.failUnless('text_de' in field_names)
74 self.failUnless('text_en' in field_names)
78 def test_set_translation(self):
79 self.failUnlessEqual(get_language(), "de")
80 # First create an instance of the test model to play with
81 title1_de = "title de"
82 title1_en = "title en"
83 title2_de = "title2 de"
84 inst1 = TestModel(title_en=title1_en, text="Testtext")
85 inst1.title = title1_de
86 inst2 = TestModel(title=title2_de, text="Testtext")
90 self.failUnlessEqual(inst1.title, title1_de)
91 self.failUnlessEqual(inst1.title_en, title1_en)
93 self.failUnlessEqual(inst2.title, title2_de)
94 self.failUnlessEqual(inst2.title_en, None)
99 # Check that the translation fields are correctly saved and provide the
100 # correct value when retrieving them again.
101 n = TestModel.objects.get(title=title1_de)
102 self.failUnlessEqual(n.title, title1_de)
103 self.failUnlessEqual(n.title_en, title1_en)
105 def test_titleonly(self):
106 title1_de = "title de"
107 n = TestModel.objects.create(title=title1_de)
108 self.failUnlessEqual(n.title, title1_de)
109 # Because the original field "title" was specified in the constructor
110 # it is directly passed into the instance's __dict__ and the descriptor
111 # which updates the associated default translation field is not called
112 # and the default translation will be None.
113 self.failUnlessEqual(n.title_de, None)
114 self.failUnlessEqual(n.title_en, None)
116 # Now assign the title, that triggers the descriptor and the default
117 # translation field is updated
119 self.failUnlessEqual(n.title, title1_de)
120 self.failUnlessEqual(n.title_de, title1_de)
121 self.failUnlessEqual(n.title_en, None)
123 def test_rule1(self):
125 Rule 1: Reading the value from the original field returns the value in
126 translated to the current language.
128 title1_de = "title de"
129 title1_en = "title en"
130 text_de = "Dies ist ein deutscher Satz"
131 text_en = "This is an english sentence"
134 n = TestModel.objects.create(title_de=title1_de, title_en=title1_en,
135 text_de=text_de, text_en=text_en)
138 # language is set to "de" at this point
139 self.failUnlessEqual(get_language(), "de")
140 self.failUnlessEqual(n.title, title1_de)
141 self.failUnlessEqual(n.title_de, title1_de)
142 self.failUnlessEqual(n.title_en, title1_en)
143 self.failUnlessEqual(n.text, text_de)
144 self.failUnlessEqual(n.text_de, text_de)
145 self.failUnlessEqual(n.text_en, text_en)
147 trans_real.activate("en")
148 self.failUnlessEqual(get_language(), "en")
149 # Title should now be return the english one (just by switching the
151 self.failUnlessEqual(n.title, title1_en)
152 self.failUnlessEqual(n.text, text_en)
154 n = TestModel.objects.create(title_de=title1_de, title_en=title1_en,
155 text_de=text_de, text_en=text_en)
157 # language is set to "en" at this point
158 self.failUnlessEqual(n.title, title1_en)
159 self.failUnlessEqual(n.title_de, title1_de)
160 self.failUnlessEqual(n.title_en, title1_en)
161 self.failUnlessEqual(n.text, text_en)
162 self.failUnlessEqual(n.text_de, text_de)
163 self.failUnlessEqual(n.text_en, text_en)
164 trans_real.activate("de")
165 self.failUnlessEqual(get_language(), "de")
166 self.failUnlessEqual(n.title, title1_de)
167 self.failUnlessEqual(n.text, text_de)
168 trans_real.deactivate()
170 def test_rule2(self):
172 Rule 2: Assigning a value to the original field also updates the value
173 in the associated translation field of the default language
175 self.failUnlessEqual(get_language(), "de")
176 title1_de = "title de"
177 title1_en = "title en"
178 n = TestModel.objects.create(title_de=title1_de, title_en=title1_en)
179 self.failUnlessEqual(n.title, title1_de)
180 self.failUnlessEqual(n.title_de, title1_de)
181 self.failUnlessEqual(n.title_en, title1_en)
183 title2 = "Neuer Titel"
186 self.failUnlessEqual(n.title, title2)
187 self.failUnlessEqual(n.title, n.title_de)
189 trans_real.activate("en")
190 self.failUnlessEqual(get_language(), "en")
194 n.title_de = title1_de
196 self.failUnlessEqual(n.title, title3)
197 self.failUnlessEqual(n.title, n.title_en)
198 self.failUnlessEqual(title1_de, n.title_de)
200 trans_real.deactivate()
202 def test_rule3(self):
204 Rule 3: Assigning a value to a translation field of the default
205 language also updates the original field - note that the value of the
206 original field will not be updated until the model instance is saved.
208 title1_de = "title de"
209 title1_en = "title en"
210 n = TestModel.objects.create(title_de=title1_de, title_en=title1_en)
211 self.failUnlessEqual(get_language(), "de")
212 self.failUnlessEqual(n.title, title1_de)
213 self.failUnlessEqual(n.title_de, title1_de)
214 self.failUnlessEqual(n.title_en, title1_en)
216 n.title_de = "Neuer Titel"
218 self.failUnlessEqual(n.title, n.title_de)
221 trans_real.activate("en")
222 self.failUnlessEqual(get_language(), "en")
223 n.title_en = "New title"
224 # the n.title field is not updated before the instance is saved
226 self.failUnlessEqual(n.title, n.title_en)
227 trans_real.deactivate()
229 def test_rule4(self):
231 Rule 4: If both fields - the original and the translation field of the
232 default language - are updated at the same time, the translation field
235 self.failUnlessEqual(get_language(), "de")
236 title1_de = "title de"
237 title1_en = "title en"
238 n = TestModel.objects.create(title_de=title1_de, title_en=title1_en)
239 self.failUnlessEqual(n.title, title1_de)
240 self.failUnlessEqual(n.title_de, title1_de)
241 self.failUnlessEqual(n.title_en, title1_en)
247 n.title_de = title2_de
248 n.title_en = title2_en
250 self.failUnlessEqual(n.title, title2_de)
251 self.failUnlessEqual(n.title_de, title2_de)
252 self.failUnlessEqual(n.title_en, title2_en)
256 self.failUnlessEqual(n.title, title_foo)
257 self.failUnlessEqual(n.title_de, title_foo)
258 self.failUnlessEqual(n.title_en, title2_en)