Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#24223 closed Cleanup/optimization (fixed)

SessionMiddleware tests leaving Session objects in database

Reported by: Matt Leach Owned by: Matt Leach
Component: contrib.sessions Version: dev
Severity: Normal Keywords:
Cc: Simon Charette Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently with default settings several tests in contrib.sessions.SessionMiddlewareTests leave Session objects remaining in the database.

This can be seen by adding a tearDown method:

class SessionMiddlewareTests(unittest.TestCase):
    
    def tearDown(self):
        print "Session count:", len(Session.objects.all())

This results in the following output:

$ ./manage.py test django.contrib.sessions.tests.SessionMiddlewareTests -v 3

...

test_httponly_session_cookie (django.contrib.sessions.tests.SessionMiddlewareTests) ... Session count: 1
ok
test_no_httponly_session_cookie (django.contrib.sessions.tests.SessionMiddlewareTests) ... Session count: 2
ok
test_secure_session_cookie (django.contrib.sessions.tests.SessionMiddlewareTests) ... Session count: 3
ok
test_session_delete_on_end (django.contrib.sessions.tests.SessionMiddlewareTests) ... Session count: 3
ok
test_session_save_on_500 (django.contrib.sessions.tests.SessionMiddlewareTests) ... Session count: 4
ok

----------------------------------------------------------------------
Ran 5 tests in 0.011s

OK

Currently this does not result in failing tests when the test suite is run but it could lead to issues with tests such as contrib.sessions.DatabaseSessionTests.test_clearsessions_command:

class DatabaseSessionTests(SessionTestsMixin, TestCase):

    backend = DatabaseSession

    ...

    @override_settings(SESSION_ENGINE="django.contrib.sessions.backends.db")
    def test_clearsessions_command(self):
        """
        Test clearsessions command for clearing expired sessions.
        """
        self.assertEqual(0, Session.objects.count())

which would fail if run immediately after the SessionMiddleware tests.

I would suggest changing the SessionMiddlewareTests class to inherit from django.test.TestCase rather than unittest.TestCase which will ensure the database is flushed after the tests run.

Change History (6)

comment:1 by Simon Charette, 10 years ago

Cc: Simon Charette added
Component: Uncategorizedcontrib.sessions
Triage Stage: UnreviewedAccepted

This change makes sense to me, can you open a PR with the required change?

comment:2 by Matt Leach, 10 years ago

Will do

comment:3 by Matt Leach, 10 years ago

Owner: changed from nobody to Matt Leach
Status: newassigned

comment:4 by Matt Leach, 10 years ago

Has patch: set

comment:5 by Simon Charette <charette.s@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 55c76f4e3bab74c8544b72d11a99e94a1c2cfbce:

Fixed #24223 -- Prevented a session test from leaking.

comment:6 by Simon Charette <charette.s@…>, 10 years ago

In 7b92acea7055c6b39207a813b52e683a3672e467:

[1.8.x] Fixed #24223 -- Prevented a session test from leaking.

Backport of 55c76f4e3bab74c8544b72d11a99e94a1c2cfbce from master

Note: See TracTickets for help on using tickets.
Back to Top