@@ 170-186 (lines=17) @@ | ||
167 | x.safe_translation_getter('tr_title', language_code=self.other_lang1), |
|
168 | 'TITLE_FALLBACK') |
|
169 | ||
170 | def test_any_fallback_model(self): |
|
171 | """ |
|
172 | Test whether a failure in the fallback language can return any saved language (if configured for it). |
|
173 | """ |
|
174 | x = AnyLanguageModel() |
|
175 | x.set_current_language(self.other_lang1) |
|
176 | x.tr_title = "TITLE_XX" |
|
177 | ||
178 | x.save() |
|
179 | ||
180 | with translation.override(self.other_lang2): |
|
181 | x = AnyLanguageModel.objects.get(pk=x.pk) |
|
182 | self.assertRaises(TranslationDoesNotExist, lambda: x._get_translated_model(use_fallback=True)) |
|
183 | self.assertEqual(x.tr_title, 'TITLE_XX') # Even though there is no current language, there is a value. |
|
184 | ||
185 | self.assertNumQueries(0, lambda: x._get_any_translated_model()) # Can fetch from cache next time. |
|
186 | self.assertEqual(x._get_any_translated_model().language_code, self.other_lang1) |
|
187 | ||
188 | def test_any_fallback_function(self): |
|
189 | x = SimpleModel() |
|
@@ 188-202 (lines=15) @@ | ||
185 | self.assertNumQueries(0, lambda: x._get_any_translated_model()) # Can fetch from cache next time. |
|
186 | self.assertEqual(x._get_any_translated_model().language_code, self.other_lang1) |
|
187 | ||
188 | def test_any_fallback_function(self): |
|
189 | x = SimpleModel() |
|
190 | x.set_current_language(self.other_lang1) |
|
191 | x.tr_title = "TITLE_XX" |
|
192 | ||
193 | x.save() |
|
194 | ||
195 | with translation.override(self.other_lang2): |
|
196 | x = SimpleModel.objects.get(pk=x.pk) |
|
197 | self.assertRaises(TranslationDoesNotExist, lambda: x._get_translated_model(use_fallback=True)) |
|
198 | self.assertIs(x.safe_translation_getter('tr_title', 'DEFAULT'), 'DEFAULT') # No lanuage, gives default |
|
199 | self.assertEqual(x.safe_translation_getter('tr_title', any_language=True), 'TITLE_XX') # Even though there is no current language, there is a value. |
|
200 | ||
201 | self.assertNumQueries(0, lambda: x._get_any_translated_model()) # Can fetch from cache next time. |
|
202 | self.assertEqual(x._get_any_translated_model().language_code, self.other_lang1) |
|
203 | ||
204 | def test_save_ignore_fallback_marker(self): |
|
205 | """ |