[Django] #34181: The active locale context can leak across tests when using LocaleMiddleware

3 views
Skip to first unread message

Django

unread,
Nov 24, 2022, 8:31:14 AM11/24/22
to django-...@googlegroups.com
#34181: The active locale context can leak across tests when using LocaleMiddleware
------------------------------------------------+------------------------
Reporter: Raphaël Barrois | Owner: nobody
Type: Bug | Status: new
Component: Internationalization | Version: 4.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
Django's LocaleMiddleware only activates a translation, but doesn't
deactivate it since #5241 /
https://github.com/django/django/commit/aa089b106b6cfc9a47cd54a0f9eb44bd44811ed9.

This has an unwanted side-effect in tests:

{{{
class I18NTests(django.test.TestCase):
def test_lang(self):
response = self.client.get("/", HTTP_LANGUAGE="fr-FR")

def test_unrelated(self):
# This value returns a string marked for translation
context = some_helper()
self.assertEqual("Hello folks", str(context["greeting"]))
}}}

If `test_lang` runs last, the test suite will succeed.
However, if `test_lang` runs before the other, the test suite will fail:
the locale context will still be set to `fr` from the execution of
`LocaleMiddleware`.

Most project with i18n enabled assume that all tests run under the
project's default locale (i.e `LANGUAGE_CODE`), yet running some tests in
other languages will fail.

I can think of a few ways to address this:
- **Documentation** Explain that tests should never check the text value
of a returned function without setting an explicit translation context
(but that's not exactly intuitive for users);
- **Test-side fix** Adjust `django.test.Client` to restore the initial
process locale after the request — or add that to the default `TestCase`
tear-down;
- **Server-side fix** find a way to deactivate the translation context
when it is safe to do so.

--
Ticket URL: <https://code.djangoproject.com/ticket/34181>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 24, 2022, 9:03:48 AM11/24/22
to django-...@googlegroups.com
#34181: The active locale context can leak across tests when using LocaleMiddleware
-------------------------------------+-------------------------------------

Reporter: Raphaël Barrois | Owner: nobody
Type: Bug | Status: closed
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #34180

--
Ticket URL: <https://code.djangoproject.com/ticket/34181#comment:1>

Reply all
Reply to author
Forward
0 new messages