summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Tamlyn <marc.tamlyn@gmail.com>2015-04-09 10:04:30 +0100
committerMarc Tamlyn <marc.tamlyn@gmail.com>2015-04-09 10:04:30 +0100
commit20a98d863f00fc48f9c7fd783d8d0539c6be41f5 (patch)
tree65c929e4bc12d9b3182437d24f3931b5804757ea
parent1273a7a0e808a43ce7a344bbf36d55b9436ecf41 (diff)
parent88d7fcebdebb726c6dc561bcce7916ed1d5256d7 (diff)
downloaddjango-20a98d863f00fc48f9c7fd783d8d0539c6be41f5.tar.gz
Merge pull request #4469 from funkybob/feature/test-tidies
Use mock.patch in migrations tests
-rw-r--r--tests/auth_tests/test_tokens.py4
-rw-r--r--tests/migrations/test_commands.py89
2 files changed, 40 insertions, 53 deletions
diff --git a/tests/auth_tests/test_tokens.py b/tests/auth_tests/test_tokens.py
index f2b94a35f8..fa26f223f7 100644
--- a/tests/auth_tests/test_tokens.py
+++ b/tests/auth_tests/test_tokens.py
@@ -1,4 +1,3 @@
-import sys
import unittest
from datetime import date, timedelta
@@ -6,6 +5,7 @@ from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.test import TestCase
+from django.utils.six import PY3
class TokenGeneratorTest(TestCase):
@@ -54,7 +54,7 @@ class TokenGeneratorTest(TestCase):
p2 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS + 1))
self.assertFalse(p2.check_token(user, tk1))
- @unittest.skipIf(sys.version_info[:2] >= (3, 0), "Unnecessary test with Python 3")
+ @unittest.skipIf(PY3, "Unnecessary test with Python 3")
def test_date_length(self):
"""
Make sure we don't allow overly long dates, causing a potential DoS.
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py
index 7e258b451d..ac4f55fa03 100644
--- a/tests/migrations/test_commands.py
+++ b/tests/migrations/test_commands.py
@@ -8,7 +8,6 @@ import os
from django.apps import apps
from django.core.management import CommandError, call_command
from django.db import DatabaseError, connection, models
-from django.db.migrations import questioner
from django.test import ignore_warnings, mock, override_settings
from django.utils import six
from django.utils.deprecation import RemovedInDjango20Warning
@@ -535,36 +534,30 @@ class MakeMigrationsTests(MigrationTestBase):
Makes sure that makemigrations enters and exits interactive mode properly.
"""
# Monkeypatch interactive questioner to auto reject
- old_input = questioner.input
- questioner.input = lambda _: "N"
- try:
- with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
- call_command("makemigrations", "migrations", merge=True, interactive=True, verbosity=0)
- merge_file = os.path.join(migration_dir, '0003_merge.py')
- self.assertFalse(os.path.exists(merge_file))
- except CommandError:
- self.fail("Makemigrations failed while running interactive questioner")
- finally:
- questioner.input = old_input
+ with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='N')):
+ try:
+ with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
+ call_command("makemigrations", "migrations", merge=True, interactive=True, verbosity=0)
+ merge_file = os.path.join(migration_dir, '0003_merge.py')
+ self.assertFalse(os.path.exists(merge_file))
+ except CommandError:
+ self.fail("Makemigrations failed while running interactive questioner")
def test_makemigrations_interactive_accept(self):
"""
Makes sure that makemigrations enters interactive mode and merges properly.
"""
# Monkeypatch interactive questioner to auto accept
- old_input = questioner.input
- questioner.input = lambda _: "y"
- out = six.StringIO()
- try:
- with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
- call_command("makemigrations", "migrations", merge=True, interactive=True, stdout=out)
- merge_file = os.path.join(migration_dir, '0003_merge.py')
- self.assertTrue(os.path.exists(merge_file))
- except CommandError:
- self.fail("Makemigrations failed while running interactive questioner")
- finally:
- questioner.input = old_input
- self.assertIn("Created new merge migration", force_text(out.getvalue()))
+ with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='y')):
+ out = six.StringIO()
+ try:
+ with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
+ call_command("makemigrations", "migrations", merge=True, interactive=True, stdout=out)
+ merge_file = os.path.join(migration_dir, '0003_merge.py')
+ self.assertTrue(os.path.exists(merge_file))
+ except CommandError:
+ self.fail("Makemigrations failed while running interactive questioner")
+ self.assertIn("Created new merge migration", force_text(out.getvalue()))
def test_makemigrations_non_interactive_not_null_addition(self):
"""
@@ -777,20 +770,17 @@ class MakeMigrationsTests(MigrationTestBase):
behavior when --noinput is specified.
"""
# Monkeypatch interactive questioner to auto reject
- old_input = questioner.input
- questioner.input = lambda _: "N"
out = six.StringIO()
- try:
- with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
- call_command("makemigrations", "migrations", merge=True, stdout=out)
- merge_file = os.path.join(migration_dir, '0003_merge.py')
- # This will fail if interactive is False by default
- self.assertFalse(os.path.exists(merge_file))
- except CommandError:
- self.fail("Makemigrations failed while running interactive questioner")
- finally:
- questioner.input = old_input
- self.assertNotIn("Created new merge migration", out.getvalue())
+ with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='N')):
+ try:
+ with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
+ call_command("makemigrations", "migrations", merge=True, stdout=out)
+ merge_file = os.path.join(migration_dir, '0003_merge.py')
+ # This will fail if interactive is False by default
+ self.assertFalse(os.path.exists(merge_file))
+ except CommandError:
+ self.fail("Makemigrations failed while running interactive questioner")
+ self.assertNotIn("Created new merge migration", out.getvalue())
@override_settings(
INSTALLED_APPS=[
@@ -817,19 +807,16 @@ class MakeMigrationsTests(MigrationTestBase):
unspecified app even if it has conflicting migrations.
"""
# Monkeypatch interactive questioner to auto accept
- old_input = questioner.input
- questioner.input = lambda _: "y"
- out = six.StringIO()
- try:
- with self.temporary_migration_module(app_label="migrated_app") as migration_dir:
- call_command("makemigrations", "migrated_app", merge=True, interactive=True, stdout=out)
- merge_file = os.path.join(migration_dir, '0003_merge.py')
- self.assertFalse(os.path.exists(merge_file))
- self.assertIn("No conflicts detected to merge.", out.getvalue())
- except CommandError:
- self.fail("Makemigrations fails resolving conflicts in an unspecified app")
- finally:
- questioner.input = old_input
+ with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='y')):
+ out = six.StringIO()
+ try:
+ with self.temporary_migration_module(app_label="migrated_app") as migration_dir:
+ call_command("makemigrations", "migrated_app", merge=True, interactive=True, stdout=out)
+ merge_file = os.path.join(migration_dir, '0003_merge.py')
+ self.assertFalse(os.path.exists(merge_file))
+ self.assertIn("No conflicts detected to merge.", out.getvalue())
+ except CommandError:
+ self.fail("Makemigrations fails resolving conflicts in an unspecified app")
def test_makemigrations_with_custom_name(self):
"""