Use mock.patch in migrations tests

Currently some of the migrations tests rely on the fact 'input' is aliased
because of six, instead of using mock.patch.  Replace this code with proper
use of mock.patch.

Also, replace one case of excessively specific python version check with
testing six.PY3
This commit is contained in:
Curtis Maloney 2015-04-09 10:32:55 +10:00
parent 0824c02603
commit 88d7fcebde
2 changed files with 40 additions and 53 deletions

View File

@ -1,4 +1,3 @@
import sys
import unittest import unittest
from datetime import date, timedelta 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.models import User
from django.contrib.auth.tokens import PasswordResetTokenGenerator from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.test import TestCase from django.test import TestCase
from django.utils.six import PY3
class TokenGeneratorTest(TestCase): class TokenGeneratorTest(TestCase):
@ -54,7 +54,7 @@ class TokenGeneratorTest(TestCase):
p2 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS + 1)) p2 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS + 1))
self.assertFalse(p2.check_token(user, tk1)) 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): def test_date_length(self):
""" """
Make sure we don't allow overly long dates, causing a potential DoS. Make sure we don't allow overly long dates, causing a potential DoS.

View File

@ -8,7 +8,6 @@ import os
from django.apps import apps from django.apps import apps
from django.core.management import CommandError, call_command from django.core.management import CommandError, call_command
from django.db import DatabaseError, connection, models from django.db import DatabaseError, connection, models
from django.db.migrations import questioner
from django.test import ignore_warnings, mock, override_settings from django.test import ignore_warnings, mock, override_settings
from django.utils import six from django.utils import six
from django.utils.deprecation import RemovedInDjango20Warning from django.utils.deprecation import RemovedInDjango20Warning
@ -535,8 +534,7 @@ class MakeMigrationsTests(MigrationTestBase):
Makes sure that makemigrations enters and exits interactive mode properly. Makes sure that makemigrations enters and exits interactive mode properly.
""" """
# Monkeypatch interactive questioner to auto reject # Monkeypatch interactive questioner to auto reject
old_input = questioner.input with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='N')):
questioner.input = lambda _: "N"
try: try:
with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir: with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
call_command("makemigrations", "migrations", merge=True, interactive=True, verbosity=0) call_command("makemigrations", "migrations", merge=True, interactive=True, verbosity=0)
@ -544,16 +542,13 @@ class MakeMigrationsTests(MigrationTestBase):
self.assertFalse(os.path.exists(merge_file)) self.assertFalse(os.path.exists(merge_file))
except CommandError: except CommandError:
self.fail("Makemigrations failed while running interactive questioner") self.fail("Makemigrations failed while running interactive questioner")
finally:
questioner.input = old_input
def test_makemigrations_interactive_accept(self): def test_makemigrations_interactive_accept(self):
""" """
Makes sure that makemigrations enters interactive mode and merges properly. Makes sure that makemigrations enters interactive mode and merges properly.
""" """
# Monkeypatch interactive questioner to auto accept # Monkeypatch interactive questioner to auto accept
old_input = questioner.input with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='y')):
questioner.input = lambda _: "y"
out = six.StringIO() out = six.StringIO()
try: try:
with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir: with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
@ -562,8 +557,6 @@ class MakeMigrationsTests(MigrationTestBase):
self.assertTrue(os.path.exists(merge_file)) self.assertTrue(os.path.exists(merge_file))
except CommandError: except CommandError:
self.fail("Makemigrations failed while running interactive questioner") self.fail("Makemigrations failed while running interactive questioner")
finally:
questioner.input = old_input
self.assertIn("Created new merge migration", force_text(out.getvalue())) self.assertIn("Created new merge migration", force_text(out.getvalue()))
def test_makemigrations_non_interactive_not_null_addition(self): def test_makemigrations_non_interactive_not_null_addition(self):
@ -777,9 +770,8 @@ class MakeMigrationsTests(MigrationTestBase):
behavior when --noinput is specified. behavior when --noinput is specified.
""" """
# Monkeypatch interactive questioner to auto reject # Monkeypatch interactive questioner to auto reject
old_input = questioner.input
questioner.input = lambda _: "N"
out = six.StringIO() out = six.StringIO()
with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='N')):
try: try:
with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir: with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
call_command("makemigrations", "migrations", merge=True, stdout=out) call_command("makemigrations", "migrations", merge=True, stdout=out)
@ -788,8 +780,6 @@ class MakeMigrationsTests(MigrationTestBase):
self.assertFalse(os.path.exists(merge_file)) self.assertFalse(os.path.exists(merge_file))
except CommandError: except CommandError:
self.fail("Makemigrations failed while running interactive questioner") self.fail("Makemigrations failed while running interactive questioner")
finally:
questioner.input = old_input
self.assertNotIn("Created new merge migration", out.getvalue()) self.assertNotIn("Created new merge migration", out.getvalue())
@override_settings( @override_settings(
@ -817,8 +807,7 @@ class MakeMigrationsTests(MigrationTestBase):
unspecified app even if it has conflicting migrations. unspecified app even if it has conflicting migrations.
""" """
# Monkeypatch interactive questioner to auto accept # Monkeypatch interactive questioner to auto accept
old_input = questioner.input with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='y')):
questioner.input = lambda _: "y"
out = six.StringIO() out = six.StringIO()
try: try:
with self.temporary_migration_module(app_label="migrated_app") as migration_dir: with self.temporary_migration_module(app_label="migrated_app") as migration_dir:
@ -828,8 +817,6 @@ class MakeMigrationsTests(MigrationTestBase):
self.assertIn("No conflicts detected to merge.", out.getvalue()) self.assertIn("No conflicts detected to merge.", out.getvalue())
except CommandError: except CommandError:
self.fail("Makemigrations fails resolving conflicts in an unspecified app") self.fail("Makemigrations fails resolving conflicts in an unspecified app")
finally:
questioner.input = old_input
def test_makemigrations_with_custom_name(self): def test_makemigrations_with_custom_name(self):
""" """