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.