Opened 3 years ago

Closed 6 months ago

#19869 closed New feature (wontfix)

Make changing the active language inside `LiveTestServerCase` possible

Reported by: void Owned by: gchp
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: apollo13 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 Changed 3 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 3 years ago by void

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 Changed 3 years ago by julien

  • Triage Stage changed from Unreviewed to Accepted

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 Changed 3 years ago by julien

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

Last edited 3 years ago by julien (previous) (diff)

comment:5 Changed 22 months ago by unaizalakain

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

comment:6 Changed 22 months ago by void

With some kind of message or signal, I think

comment:7 Changed 22 months ago by unaizalakain

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

comment:8 Changed 21 months ago by unaizalakain

  • Owner changed from nobody to unaizalakain
  • Status changed from new to assigned

comment:9 Changed 21 months ago by unaizalakain

  • Summary changed from Add ability to restart LiveServerThread inside a test method to Make changing the active language inside `LiveTestServerCase` possible

comment:10 Changed 21 months ago by unaizalakain

  • Has patch set

comment:11 Changed 21 months ago by unaizalakain

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 21 months ago by unaizalakain (previous) (diff)

comment:12 Changed 19 months ago by apollo13

  • Cc apollo13 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 Changed 19 months ago by unaizalakain

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 Changed 15 months ago by timo

  • Patch needs improvement set

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

comment:15 Changed 15 months ago by unaizalakain

  • Has patch unset
  • Owner unaizalakain deleted
  • Patch needs improvement unset
  • Status changed from assigned to new

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 Changed 15 months ago by gchp

  • Owner set to gchp
  • Status changed from new to assigned

comment:17 Changed 6 months ago by void

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 Changed 6 months ago by void

  • Resolution set to wontfix
  • Status changed from assigned to closed
Note: See TracTickets for help on using tickets.
Back to Top