diff --git a/AUTHORS b/AUTHORS index 387474967d..5a92b0c539 100644 --- a/AUTHORS +++ b/AUTHORS @@ -541,6 +541,7 @@ answer newbie questions, and generally made Django that much better: Ivan Sagalaev (Maniac) Vinay Sajip Bartolome Sanchez Salado + Nick Sandford Mark Sandstrom Kadesarin Sanjek Tim Saylor diff --git a/django/contrib/contenttypes/management.py b/django/contrib/contenttypes/management.py index 0159085d0b..f705330137 100644 --- a/django/contrib/contenttypes/management.py +++ b/django/contrib/contenttypes/management.py @@ -60,7 +60,7 @@ def update_contenttypes(app_config, verbosity=2, interactive=True, using=DEFAULT # Confirm that the content type is stale before deletion. if to_remove: - if kwargs.get('interactive', False): + if interactive: content_type_display = '\n'.join( ' %s | %s' % (ct.app_label, ct.model) for ct in to_remove diff --git a/tests/contenttypes_tests/tests.py b/tests/contenttypes_tests/tests.py index 2bec2927bd..1e3a92cc2c 100644 --- a/tests/contenttypes_tests/tests.py +++ b/tests/contenttypes_tests/tests.py @@ -1,16 +1,20 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, unicode_literals +import sys + from django.apps.registry import Apps, apps from django.contrib.contenttypes.fields import ( GenericForeignKey, GenericRelation ) +from django.contrib.contenttypes import management from django.contrib.contenttypes.models import ContentType from django.core import checks from django.db import connections, models, router from django.test import TestCase from django.test.utils import override_settings from django.utils.encoding import force_str +from django.utils.six import StringIO from .models import Author, Article, SchemeIncludedURL @@ -358,6 +362,40 @@ class GenericRelationshipTests(IsolatedModelsTestCase): self.assertEqual(errors, expected) +class UpdateContentTypesTests(TestCase): + def setUp(self): + self.before_count = ContentType.objects.count() + ContentType.objects.create(name='fake', app_label='contenttypes_tests', model='Fake') + self.app_config = apps.get_app_config('contenttypes_tests') + + def test_interactive_true(self): + """ + interactive mode of update_contenttypes() (the default) should delete + stale contenttypes. + """ + self.old_stdout = sys.stdout + sys.stdout = StringIO() + management.input = lambda x: force_str("yes") + management.update_contenttypes(self.app_config) + output = sys.stdout.getvalue() + sys.stdout = self.old_stdout + self.assertIn("Deleting stale content type", output) + self.assertEqual(ContentType.objects.count(), self.before_count) + + def test_interactive_false(self): + """ + non-interactive mode of update_contenttypes() shouldn't delete stale + content types. + """ + self.old_stdout = sys.stdout + sys.stdout = StringIO() + management.update_contenttypes(self.app_config, interactive=False) + output = sys.stdout.getvalue() + sys.stdout = self.old_stdout + self.assertIn("Stale content types remain.", output) + self.assertEqual(ContentType.objects.count(), self.before_count + 1) + + class TestRouter(object): def db_for_read(self, model, **hints): return 'other'