Opened 12 years ago

Closed 10 years ago

#19869 closed New feature (wontfix)

Make changing the active language inside `LiveTestServerCase` possible

Reported by: Alexey Boriskin Owned by: Greg Chapple
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Florian Apolloner Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I was trying to make a selenium test to work with different locales in one test, like this:

for locale in locale_list:
    with translation.override(locale):
        self.selenium.get(something)
        ...test something...

But translation.override had affected only current thread, not LiveServerThread, probably because active translation is thread-local. Because I see no way to force LiveServerThread copy or share same thread-local state, it would be cool if I could have ability to stop LiveServerThread and recreate it in such cases. Currently it is started/stopped in setUpClass/tearDownClass which means that it is created once per TestCase run and shared between all the runs of all the test methods.


Change History (18)

comment:1 by Aymeric Augustin, 12 years ago

Can you set the Accept-Language header in the request, or use your website's "change language" feature?

comment:2 by Alexey Boriskin, 12 years ago

Selenium does not let you set custom request headers: http://code.google.com/p/selenium/issues/detail?id=141.
I am actually trying to test django admin, it have no such "change language" feature.

comment:3 by Julien Phalip, 12 years ago

Triage Stage: UnreviewedAccepted

I think one good way of doing this is to add a new method "activate_live_server_locale(self, locale)" to LiveServerTestCase that would activate the locale in the separate thread. It would be even better if it could work as a context manager like so:

class MyTests(LiveServerTestcase):

    def test_something(self):
        with self.activate_live_server_locale('de'):
            ...

comment:4 by Julien Phalip, 12 years ago

By the way, "activate_live_server_locale" is a bit of a mouthful, so suggestions for better names are welcome ;)

Last edited 12 years ago by Julien Phalip (previous) (diff)

comment:5 by Unai Zalakain, 11 years ago

How would you modify the thread local of the live server thread from the main thread?

comment:6 by Alexey Boriskin, 11 years ago

With some kind of message or signal, I think

comment:7 by Unai Zalakain, 11 years ago

I believe (though I may be wrong) that signals don't work across threads.

comment:8 by Unai Zalakain, 11 years ago

Owner: changed from nobody to Unai Zalakain
Status: newassigned

comment:9 by Unai Zalakain, 11 years ago

Summary: Add ability to restart LiveServerThread inside a test methodMake changing the active language inside `LiveTestServerCase` possible

comment:10 by Unai Zalakain, 11 years ago

Has patch: set

comment:11 by Unai Zalakain, 11 years ago

The set_language view could be used to change the live test server thread language but that would mean adding some kind of new method to the test case and the inability to use the already defined methods (django.utils.translation.override, django.utils.translation.activate, etc).

Instead, the PR opts for making the thread local that holds the current active language a non-local. This is done in the two threads so suddenly one thread is capable of accessing the other one's active language. This is afterwards reverted to thread locals again.

Last edited 11 years ago by Unai Zalakain (previous) (diff)

comment:12 by Florian Apolloner, 11 years ago

Cc: Florian Apolloner added

I'd very much prefer a method which just sets the active language in the other thread as suggested by julien. While the PR might work, it makes me run away in fear.

comment:13 by Unai Zalakain, 11 years ago

Well, it surely isn't the most elegant way of doing it but defining some special functions for changing the active language when those functions already exist doesn't seem right to me either.

I think disabling the language thread-local in the testing environment isn't too bad, after all, it's not needed.

comment:14 by Tim Graham, 10 years ago

Patch needs improvement: set

If the patch makes apollo13 run, I guess it needs improvement. :-)

comment:15 by Unai Zalakain, 10 years ago

Has patch: unset
Owner: Unai Zalakain removed
Patch needs improvement: unset
Status: assignednew

Then more than an improvement, I think a totally different patch is needed, although I don't understand the need for a language thread-local in a testing environment that doesn't support multithreading.

comment:16 by Greg Chapple, 10 years ago

Owner: set to Greg Chapple
Status: newassigned

comment:17 by Alexey Boriskin, 10 years ago

The initial goal of this ticket was to deal with issue that emerged while working on the #18767.
That is, translation.override was not able to change thread-local state of LiveServerThread, and it rendered everything in one language.
#18767 ticket is fixed now and thread-local issue resolved by introducing settings_changed signal and its receivers, in particular to the trans_real._active = threading.local() line in django.test.signals.

Therefore, I think it's safe to close this ticket as wontfix.

comment:18 by Alexey Boriskin, 10 years ago

Resolution: wontfix
Status: assignedclosed
Note: See TracTickets for help on using tickets.
Back to Top