From 8d3fcfa39e8aab5618d9b7f6a592006e9af8cefc Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Wed, 2 Mar 2016 17:53:12 -0800 Subject: [PATCH] Cleaned up tests to use call_command() instead of Command.execute(). --- django/core/management/commands/test.py | 2 +- tests/admin_scripts/tests.py | 9 ++----- tests/auth_tests/test_management.py | 34 ++++++++++--------------- tests/createsuperuser/tests.py | 9 +++---- 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/django/core/management/commands/test.py b/django/core/management/commands/test.py index 313e12f271..21f2a97850 100644 --- a/django/core/management/commands/test.py +++ b/django/core/management/commands/test.py @@ -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) diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index 8f2ba6cab1..8a65b136c2 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -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') diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py index e8388db763..84a414a435 100644 --- a/tests/auth_tests/test_management.py +++ b/tests/auth_tests/test_management.py @@ -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( diff --git a/tests/createsuperuser/tests.py b/tests/createsuperuser/tests.py index d5c891c118..3187e9b54d 100644 --- a/tests/createsuperuser/tests.py +++ b/tests/createsuperuser/tests.py @@ -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(