Tuesday, October 6, 2015

Re: django.test: @override_settings(LANGUAGE_CODE = 'xx') does not affect the default language in a test

On Tue, Oct 6, 2015 at 3:11 PM, Yunshi Tan <yunshi.tan@polyconseil.fr> wrote:
> Let's say we have a default language code in settings.py
>
> LANGUAGE_CODE = 'en'
>
> Then we have
>
> >> from django.utils import translation
> >> assert translation.get_language() == 'en'
>
> We are good by now.
>
> Then in a test, I would like to override the default language. Let's say I
> have
>
> @override_settings(LANGUAGE_CODE='fr')
>
> class SomeTestCase(TestCase):
>
> def test_some_case(self):
> self.assert(translation.get_language(), 'en') # it will pass
> self.assert(translation.get_language(), 'fr') # it would fail
>
>
> Is this a bug?

I don't think so, if simply because 'translation.get_language()' gets
the currently activated language, not what the current value of
"settings.LANGUAGE_CODE" is.

LANGUAGE_CODE shouldn't normally change, so I don't think any code
detecting when it is changed is warranted. It seems that what you want
to do is to change the currently activated language and perform some
tests - translation.activate_language() serves this purpose better
than patching settings.

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1JfSQegCNFv-hfrBAy_hc0eQH5WE%2BKyKWssmxB3k4gVYA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment