Django

Code

Ticket #10899 (reopened)

Opened 1 year ago

Last modified 1 year ago

easier manipulation of sessions by test client

Reported by: tallfred Assigned to: nobody
Milestone: Component: Testing framework
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 1
Needs tests: 1 Patch needs improvement: 1

Description

Creating and modifying sessions is painful for the test client...

class SimpleTest(TestCase):
   def test_stuff(self):

      # ugly and long create of session if session doesn't exist
      from django.conf import settings
      engine = import_module(settings.SESSION_ENGINE)
      store = engine.SessionStore()
      store.save()  # we need to make load() work, or the cookie is worthless
      self.cookies[settings.SESSION_COOKIE_NAME] = store.session_key

      #   ugly and long set of session
      session = self.client.session 
      session['foo'] = 33
      session.save()

      # pretty and faster
      self.client.session['foo'] = 33

      #   ugly and long pop of session
      session = self.client.session 
      val = session.pop('foo')
      session.save()

      # pretty and faster
      val = self.client.session.pop('foo')

The attached patch makes the "pretty and faster" possible. It's faster because every session get doesn't have to go to the database. The pretty code fails before the patch because each fetch of self.client.session creates a new SessionStore? with a small scope, not lasting long enough to be saved.

Attachments

easy_session_manipulation.diff (3.9 kB) - added by tallfred on 04/22/09 17:28:13.
easy_session_manipulation6.diff (5.5 kB) - added by tallfred on 04/22/09 23:51:16.

Change History

04/22/09 17:28:13 changed by tallfred

  • attachment easy_session_manipulation.diff added.

04/22/09 23:50:50 changed by tallfred

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

Here is an updated patch...

  1. Only save sessions when actually modified
  2. Only session.save() when django.contrib.sessions is used.
  3. Survive calls of cycle_key()
  4. Simplify login()
  5. Rename new _session_cache to _session_store to avoid confusion
  6. Updated comments

04/22/09 23:51:16 changed by tallfred

  • attachment easy_session_manipulation6.diff added.

05/07/09 08:11:00 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

(In [10686]) Fixed #10899 -- Ensured that log messages for deletions in the admin contain useful descriptions. Thanks to Jeremy Dunck for the patch.

05/08/09 11:12:51 changed by russellm

(In [10720]) [1.0.X] Fixed #10899 -- Ensured that log messages for deletions in the admin contain useful descriptions. Thanks to Jeremy Dunck for the patch.

Merge of r10686 from trunk.

05/11/09 06:12:47 changed by toxik

  • status changed from closed to reopened.
  • needs_better_patch set to 1.
  • resolution deleted.
  • needs_tests set to 1.
  • needs_docs set to 1.

Seems like you got the wrong ticket number, Russell.

As an aside, here's how I access the session in a very session backend agnostic way:

from django.test import TestCase
from django.test.client import ClientHandler

class SessionHandler(ClientHandler):
    def get_response(self, request):
        response = super(SessionHandler, self).get_response(request)
        response.session = request.session.copy()
        return response

class SessionTestCase(TestCase):
    def _pre_setup(self):
        super(SessionTestCase, self)._pre_setup()
        self.client.handler = SessionHandler()

class FooTestCase(SessionTestCase):
    def test_session_exists(self):
        resp = self.client.get("/")
        self.assert_(hasattr(resp, "session"))
        self.assert_("my_session_key" in resp.session)
        self.assertEqual(resp.session["my_session_key"], "Hello world!")

As you can see, it'd be very easy for the original ClientHandler class to just copy over the session to the response object.

06/08/09 17:26:08 changed by SmileyChris

Good job - I scratched my head for a bit figuring out why my session tests were failing (due to my implementation of the obvious "pretty and faster" code).

Hint: you don't need the _failure method for property - just don't provide a set or del method for the property and Python will raise an attribute error if someone tries to.

I'd leave out your ClientHandler changes from this ticket - open that in a new ticket if you want, but no point in making this one any more complex.

06/08/09 17:26:23 changed by SmileyChris

  • stage changed from Unreviewed to Accepted.

Add/Change #10899 (easier manipulation of sessions by test client)




Change Properties
Action