1
0
mirror of https://github.com/django/django.git synced 2024-11-18 07:26:04 +00:00

Merge pull request #4469 from funkybob/feature/test-tidies

Use mock.patch in migrations tests
This commit is contained in:
Marc Tamlyn 2015-04-09 10:04:30 +01:00
commit 20a98d863f
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,36 +534,30 @@ 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) merge_file = os.path.join(migration_dir, '0003_merge.py')
merge_file = os.path.join(migration_dir, '0003_merge.py') 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: call_command("makemigrations", "migrations", merge=True, interactive=True, stdout=out)
call_command("makemigrations", "migrations", merge=True, interactive=True, stdout=out) merge_file = os.path.join(migration_dir, '0003_merge.py')
merge_file = os.path.join(migration_dir, '0003_merge.py') 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") self.assertIn("Created new merge migration", force_text(out.getvalue()))
finally:
questioner.input = old_input
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,20 +770,17 @@ 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()
try: with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='N')):
with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir: try:
call_command("makemigrations", "migrations", merge=True, stdout=out) with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
merge_file = os.path.join(migration_dir, '0003_merge.py') call_command("makemigrations", "migrations", merge=True, stdout=out)
# This will fail if interactive is False by default merge_file = os.path.join(migration_dir, '0003_merge.py')
self.assertFalse(os.path.exists(merge_file)) # This will fail if interactive is False by default
except CommandError: self.assertFalse(os.path.exists(merge_file))
self.fail("Makemigrations failed while running interactive questioner") except CommandError:
finally: self.fail("Makemigrations failed while running interactive questioner")
questioner.input = old_input self.assertNotIn("Created new merge migration", out.getvalue())
self.assertNotIn("Created new merge migration", out.getvalue())
@override_settings( @override_settings(
INSTALLED_APPS=[ INSTALLED_APPS=[
@ -817,19 +807,16 @@ 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: call_command("makemigrations", "migrated_app", merge=True, interactive=True, stdout=out)
call_command("makemigrations", "migrated_app", merge=True, interactive=True, stdout=out) merge_file = os.path.join(migration_dir, '0003_merge.py')
merge_file = os.path.join(migration_dir, '0003_merge.py') self.assertFalse(os.path.exists(merge_file))
self.assertFalse(os.path.exists(merge_file)) 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):
""" """