[Django] #24223: SessionMiddleware tests leaving Session objects in database

11 views
Skip to first unread message

Django

unread,
Jan 27, 2015, 9:57:24 AM1/27/15
to django-...@googlegroups.com
#24223: SessionMiddleware tests leaving Session objects in database
--------------------------------------+--------------------
Reporter: matt-leach | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/24223>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 27, 2015, 1:39:35 PM1/27/15
to django-...@googlegroups.com
#24223: SessionMiddleware tests leaving Session objects in database
--------------------------------------+------------------------------------

Reporter: matt-leach | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: contrib.sessions | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by charettes):

* cc: charettes (added)
* needs_better_patch: => 0
* component: Uncategorized => contrib.sessions
* needs_tests: => 0
* needs_docs: => 0
* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/24223#comment:1>

Django

unread,
Jan 27, 2015, 6:50:02 PM1/27/15
to django-...@googlegroups.com
#24223: SessionMiddleware tests leaving Session objects in database
--------------------------------------+------------------------------------

Reporter: matt-leach | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: contrib.sessions | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by matt-leach):

Will do

--
Ticket URL: <https://code.djangoproject.com/ticket/24223#comment:2>

Django

unread,
Jan 27, 2015, 6:50:27 PM1/27/15
to django-...@googlegroups.com
#24223: SessionMiddleware tests leaving Session objects in database
-------------------------------------+-------------------------------------
Reporter: matt-leach | Owner: matt-
Type: | leach
Cleanup/optimization | Status: assigned

Component: contrib.sessions | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by matt-leach):

* owner: nobody => matt-leach
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/24223#comment:3>

Django

unread,
Jan 28, 2015, 9:36:46 AM1/28/15
to django-...@googlegroups.com
#24223: SessionMiddleware tests leaving Session objects in database
-------------------------------------+-------------------------------------
Reporter: matt-leach | Owner: matt-
Type: | leach
Cleanup/optimization | Status: assigned
Component: contrib.sessions | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by matt-leach):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24223#comment:4>

Django

unread,
Jan 29, 2015, 2:37:23 PM1/29/15
to django-...@googlegroups.com
#24223: SessionMiddleware tests leaving Session objects in database
-------------------------------------+-------------------------------------
Reporter: matt-leach | Owner: matt-
Type: | leach
Cleanup/optimization | Status: closed
Component: contrib.sessions | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette <charette.s@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"55c76f4e3bab74c8544b72d11a99e94a1c2cfbce"]:
{{{
#!CommitTicketReference repository=""
revision="55c76f4e3bab74c8544b72d11a99e94a1c2cfbce"
Fixed #24223 -- Prevented a session test from leaking.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24223#comment:5>

Django

unread,
Jan 29, 2015, 2:38:22 PM1/29/15
to django-...@googlegroups.com
#24223: SessionMiddleware tests leaving Session objects in database
-------------------------------------+-------------------------------------
Reporter: matt-leach | Owner: matt-
Type: | leach
Cleanup/optimization | Status: closed
Component: contrib.sessions | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette <charette.s@…>):

In [changeset:"7b92acea7055c6b39207a813b52e683a3672e467"]:
{{{
#!CommitTicketReference repository=""
revision="7b92acea7055c6b39207a813b52e683a3672e467"
[1.8.x] Fixed #24223 -- Prevented a session test from leaking.

Backport of 55c76f4e3bab74c8544b72d11a99e94a1c2cfbce from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24223#comment:6>

Reply all
Reply to author
Forward
0 new messages