Opened 17 years ago

Closed 16 years ago

Last modified 16 years ago

#6923 closed (wontfix)

Contrib.auth.tests.basic.py does not delete test users 'testuser' and 'testuser2'

Reported by: anonymous Owned by: nobody
Component: Contrib apps Version: dev
Severity: Keywords: auth
Cc: em@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In django.django.contrib.auth.tests.basic.py there are tests for checking that the auth module works correctly. To do this two test users are created, but not deleted. This means that user tests that use the name "testuser" or "testuser2" will get incorrect password errors, and not log in. The test database should be empty.

Change History (6)

comment:1 by anonymous, 17 years ago

Cc: em@… added

comment:2 by devin, 16 years ago

Resolution: worksforme
Status: newclosed

I've tried reproducing this with both Doctests and Django TestCase, but in both cases, the test database contains no Users. Since test databases are created in the __call__ method of TestCase, it doesn't seem like this could happen for Django TestCase. I don't know about the Doctests, but I couldn't reproduce using a variety of database engines.

comment:3 by Emil Stenström <em@…>, 16 years ago

Resolution: worksforme
Status: closedreopened

@devin: Sorry if I was unclear, this bug has to do with TestCase, not Doctests. The file it concerns is: http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/tests/basic.py?rev=7967 which clearly has two lines of type:

u = User.objects.create_user('testuser', 'test@example.com', 'testpw')

...but no corresponding line for deleting that user. It will of course be deleted after all tests are done, but that's too late. When I run "manage.py test" all tests are run, both the basic.py and my own, so "testuser" will be in the test database when my tests are run.

I hope this explanation warrants a reopening of the ticket, otherwise you may slap me.

comment:4 by Russell Keith-Magee, 16 years ago

Resolution: wontfix
Status: reopenedclosed

This is a known limitation of using doctests. Essentially, you can't write a doctest or vanilla unit test and expect that the database is clean at the start of the test.

The only permanent solutions that exist involve flushing the database at the start or end of every doctest. Unfortunately, flushing is an expensive operation, so we don't want to do it unless we know we need to.

The workaround is to modify your test; Either modify your test conditions to check for deltas rather than absolutes (e.g., find the number of users at the start of the test, and after adding some users check that current count - initial count = expected) or manually flush the database at the start of your test. You can do this by calling:

>>> from django.core.management import call_command
>>> call_command('flush', verbosity=0, interactive=False)

at the start of your test.

This is somewhat related to #5624; this ticket calls for finding a way to associate fixtures with doctests. Part of this process may be to find a way to explicitly request a database flush without loading a fixture.

comment:5 by Emil Stenström <em@…>, 16 years ago

Wouldn't it be easier to delete the users you create in this specific test? Two lines of code:

u.delete()
u2.delete()

I've encountered one other user hitting the same wall when trying to name my own test user "testuser" and getting strange errors.

comment:6 by Russell Keith-Magee, 16 years ago

Sure - deleting those two users will solve _this_ problem, _this_ time. However, it isn't a general solution. The general solution involves flushing the database, which is an expensive operation if it isn't required.

In your case, it obviously _is_ required. doctests make no guarantees about the state of the database at the start of the test, so you need to modify your test to guarantee the initially empty state of the database, using the manual call to flush I described previously.

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