Cleaned up tests to use call_command() instead of Command.execute().

This commit is contained in:
Jon Dufresne 2016-03-02 17:53:12 -08:00 committed by Tim Graham
parent 9ed4a788aa
commit 8d3fcfa39e
4 changed files with 20 additions and 34 deletions

View File

@ -61,7 +61,7 @@ class Command(BaseCommand):
if options.get('liveserver') is not None:
os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = options['liveserver']
del options['liveserver']
del options['liveserver']
test_runner = TestRunner(**options)
failures = test_runner.run_tests(test_labels)

View File

@ -1269,10 +1269,6 @@ class CustomTestRunner(DiscoverRunner):
class ManageTestCommand(AdminScriptTestCase):
def setUp(self):
from django.core.management.commands.test import Command as TestCommand
self.cmd = TestCommand()
def test_liveserver(self):
"""
Ensure that the --liveserver option sets the environment variable
@ -1284,14 +1280,13 @@ class ManageTestCommand(AdminScriptTestCase):
address_predefined = 'DJANGO_LIVE_TEST_SERVER_ADDRESS' in os.environ
old_address = os.environ.get('DJANGO_LIVE_TEST_SERVER_ADDRESS')
self.cmd.handle(verbosity=0, testrunner='admin_scripts.tests.CustomTestRunner')
call_command('test', verbosity=0, testrunner='admin_scripts.tests.CustomTestRunner')
# Original state hasn't changed
self.assertEqual('DJANGO_LIVE_TEST_SERVER_ADDRESS' in os.environ, address_predefined)
self.assertEqual(os.environ.get('DJANGO_LIVE_TEST_SERVER_ADDRESS'), old_address)
self.cmd.handle(verbosity=0, testrunner='admin_scripts.tests.CustomTestRunner',
liveserver='blah')
call_command('test', verbosity=0, testrunner='admin_scripts.tests.CustomTestRunner', liveserver='blah')
# Variable was correctly set
self.assertEqual(os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'], 'blah')

View File

@ -19,7 +19,7 @@ from django.core.management import call_command
from django.core.management.base import CommandError
from django.db import models
from django.test import (
SimpleTestCase, TestCase, override_settings, override_system_checks,
SimpleTestCase, TestCase, mock, override_settings, override_system_checks,
)
from django.test.utils import isolate_apps
from django.utils import six
@ -124,13 +124,12 @@ class ChangepasswordManagementCommandTestCase(TestCase):
self.stdout.close()
self.stderr.close()
def test_that_changepassword_command_changes_joes_password(self):
@mock.patch.object(changepassword.Command, '_get_pass', return_value='not qwerty')
def test_that_changepassword_command_changes_joes_password(self, mock_get_pass):
"Executing the changepassword management command should change joe's password"
self.assertTrue(self.user.check_password('qwerty'))
command = changepassword.Command()
command._get_pass = lambda *args: 'not qwerty'
command.execute(username="joe", stdout=self.stdout)
call_command('changepassword', username='joe', stdout=self.stdout)
command_output = self.stdout.getvalue().strip()
self.assertEqual(
@ -139,42 +138,35 @@ class ChangepasswordManagementCommandTestCase(TestCase):
)
self.assertTrue(User.objects.get(username="joe").check_password("not qwerty"))
def test_that_max_tries_exits_1(self):
@mock.patch.object(changepassword.Command, '_get_pass', side_effect=lambda *args: str(args))
def test_that_max_tries_exits_1(self, mock_get_pass):
"""
A CommandError should be thrown by handle() if the user enters in
mismatched passwords three times.
"""
command = changepassword.Command()
command._get_pass = lambda *args: str(args) or 'foo'
with self.assertRaises(CommandError):
command.execute(username="joe", stdout=self.stdout, stderr=self.stderr)
call_command('changepassword', username='joe', stdout=self.stdout, stderr=self.stderr)
def test_password_validation(self):
@mock.patch.object(changepassword.Command, '_get_pass', return_value='1234567890')
def test_password_validation(self, mock_get_pass):
"""
A CommandError should be raised if the user enters in passwords which
fail validation three times.
"""
command = changepassword.Command()
command._get_pass = lambda *args: '1234567890'
abort_msg = "Aborting password change for user 'joe' after 3 attempts"
with self.assertRaisesMessage(CommandError, abort_msg):
command.execute(username="joe", stdout=self.stdout, stderr=self.stderr)
call_command('changepassword', username='joe', stdout=self.stdout, stderr=self.stderr)
self.assertIn('This password is entirely numeric.', self.stderr.getvalue())
def test_that_changepassword_command_works_with_nonascii_output(self):
@mock.patch.object(changepassword.Command, '_get_pass', return_value='not qwerty')
def test_that_changepassword_command_works_with_nonascii_output(self, mock_get_pass):
"""
#21627 -- Executing the changepassword management command should allow
non-ASCII characters from the User object representation.
"""
# 'Julia' with accented 'u':
User.objects.create_user(username='J\xfalia', password='qwerty')
command = changepassword.Command()
command._get_pass = lambda *args: 'not qwerty'
command.execute(username="J\xfalia", stdout=self.stdout)
call_command('changepassword', username='J\xfalia', stdout=self.stdout)
@override_settings(

View File

@ -1,7 +1,7 @@
from django.contrib.auth import models
from django.contrib.auth.management.commands import changepassword
from django.core.management import call_command
from django.test import TestCase
from django.test import TestCase, mock
from django.utils.six import StringIO
@ -11,17 +11,16 @@ class MultiDBChangepasswordManagementCommandTestCase(TestCase):
def setUp(self):
self.user = models.User.objects.db_manager('other').create_user(username='joe', password='qwerty')
def test_that_changepassword_command_with_database_option_uses_given_db(self):
@mock.patch.object(changepassword.Command, '_get_pass', return_value='not qwerty')
def test_that_changepassword_command_with_database_option_uses_given_db(self, mock_get_pass):
"""
Executing the changepassword management command with a database option
should operate on the specified DB
"""
self.assertTrue(self.user.check_password('qwerty'))
command = changepassword.Command()
command._get_pass = lambda *args: 'not qwerty'
out = StringIO()
command.execute(username="joe", database='other', stdout=out)
call_command('changepassword', username='joe', database='other', stdout=out)
command_output = out.getvalue().strip()
self.assertEqual(