summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2016-03-02 17:53:12 -0800
committerTim Graham <timograham@gmail.com>2016-03-05 12:59:30 -0500
commit8d3fcfa39e8aab5618d9b7f6a592006e9af8cefc (patch)
tree356d8ffec99fc2dcd235bf6d18a3c3f211b4f2d5
parent9ed4a788aa8d6ba6a57a2daa15253c3047048dfb (diff)
Cleaned up tests to use call_command() instead of Command.execute().
-rw-r--r--django/core/management/commands/test.py2
-rw-r--r--tests/admin_scripts/tests.py9
-rw-r--r--tests/auth_tests/test_management.py34
-rw-r--r--tests/createsuperuser/tests.py9
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(