diff options
author | Russell Keith-Magee <russell@keith-magee.com> | 2014-03-08 11:24:13 +0800 |
---|---|---|
committer | Russell Keith-Magee <russell@keith-magee.com> | 2014-03-08 11:25:23 +0800 |
commit | 70ec4d776ef0e68960ccee21476b8654e9399f53 (patch) | |
tree | cd4b68aad597845ab8542cb2f2f8a646f75d3b40 /tests/admin_checks | |
parent | 219d928852c256a81d09dbaa29ed4cec42d2fdfa (diff) | |
download | django-70ec4d776ef0e68960ccee21476b8654e9399f53.tar.gz |
Fixed #22034 -- Added a specific set of relation checks for GenericInlineModelAdmin.
Thanks to jwa for the report.
Diffstat (limited to 'tests/admin_checks')
-rw-r--r-- | tests/admin_checks/models.py | 11 | ||||
-rw-r--r-- | tests/admin_checks/tests.py | 125 |
2 files changed, 134 insertions, 2 deletions
diff --git a/tests/admin_checks/models.py b/tests/admin_checks/models.py index 5db1747a64..3cbb7fb49e 100644 --- a/tests/admin_checks/models.py +++ b/tests/admin_checks/models.py @@ -4,7 +4,8 @@ Tests of ModelAdmin system checks logic. from django.db import models from django.utils.encoding import python_2_unicode_compatible - +from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes.fields import GenericForeignKey class Album(models.Model): title = models.CharField(max_length=150) @@ -55,3 +56,11 @@ class State(models.Model): class City(models.Model): state = models.ForeignKey(State) + + +class Influence(models.Model): + name = models.TextField() + + content_type = models.ForeignKey(ContentType) + object_id = models.PositiveIntegerField() + content_object = GenericForeignKey('content_type', 'object_id') diff --git a/tests/admin_checks/tests.py b/tests/admin_checks/tests.py index 1c46951502..5008555556 100644 --- a/tests/admin_checks/tests.py +++ b/tests/admin_checks/tests.py @@ -4,11 +4,12 @@ import warnings from django import forms from django.contrib import admin +from django.contrib.contenttypes.admin import GenericStackedInline from django.core import checks from django.core.exceptions import ImproperlyConfigured from django.test import TestCase -from .models import Song, Book, Album, TwoAlbumFKAndAnE, City, State +from .models import Song, Book, Album, TwoAlbumFKAndAnE, City, State, Influence class SongForm(forms.ModelForm): @@ -183,6 +184,128 @@ class SystemChecksTestCase(TestCase): ] self.assertEqual(errors, expected) + def test_valid_generic_inline_model_admin(self): + """ + Regression test for #22034 - check that generic inlines don't look for + normal ForeignKey relations. + """ + + class InfluenceInline(GenericStackedInline): + model = Influence + + class SongAdmin(admin.ModelAdmin): + inlines = [InfluenceInline] + + errors = SongAdmin.check(model=Song) + self.assertEqual(errors, []) + + def test_generic_inline_model_admin_non_generic_model(self): + """ + Ensure that a model without a GenericForeignKey raises problems if it's included + in an GenericInlineModelAdmin definition. + """ + + class BookInline(GenericStackedInline): + model = Book + + class SongAdmin(admin.ModelAdmin): + inlines = [BookInline] + + errors = SongAdmin.check(model=Song) + expected = [ + checks.Error( + "'admin_checks.Book' has no GenericForeignKey.", + hint=None, + obj=BookInline, + id='admin.E301', + ) + ] + self.assertEqual(errors, expected) + + def test_generic_inline_model_admin_bad_ct_field(self): + "A GenericInlineModelAdmin raises problems if the ct_field points to a non-existent field." + + class InfluenceInline(GenericStackedInline): + model = Influence + ct_field = 'nonexistent' + + class SongAdmin(admin.ModelAdmin): + inlines = [InfluenceInline] + + errors = SongAdmin.check(model=Song) + expected = [ + checks.Error( + "'ct_field' references 'nonexistent', which is not a field on 'admin_checks.Influence'.", + hint=None, + obj=InfluenceInline, + id='admin.E302', + ) + ] + self.assertEqual(errors, expected) + + def test_generic_inline_model_admin_bad_fk_field(self): + "A GenericInlineModelAdmin raises problems if the ct_fk_field points to a non-existent field." + + class InfluenceInline(GenericStackedInline): + model = Influence + ct_fk_field = 'nonexistent' + + class SongAdmin(admin.ModelAdmin): + inlines = [InfluenceInline] + + errors = SongAdmin.check(model=Song) + expected = [ + checks.Error( + "'ct_fk_field' references 'nonexistent', which is not a field on 'admin_checks.Influence'.", + hint=None, + obj=InfluenceInline, + id='admin.E303', + ) + ] + self.assertEqual(errors, expected) + + def test_generic_inline_model_admin_non_gfk_ct_field(self): + "A GenericInlineModelAdmin raises problems if the ct_field points to a field that isn't part of a GenericForeignKey" + + class InfluenceInline(GenericStackedInline): + model = Influence + ct_field = 'name' + + class SongAdmin(admin.ModelAdmin): + inlines = [InfluenceInline] + + errors = SongAdmin.check(model=Song) + expected = [ + checks.Error( + "'admin_checks.Influence' has no GenericForeignKey using content type field 'name' and object ID field 'object_id'.", + hint=None, + obj=InfluenceInline, + id='admin.E304', + ) + ] + self.assertEqual(errors, expected) + + def test_generic_inline_model_admin_non_gfk_fk_field(self): + "A GenericInlineModelAdmin raises problems if the ct_fk_field points to a field that isn't part of a GenericForeignKey" + + class InfluenceInline(GenericStackedInline): + model = Influence + ct_fk_field = 'name' + + class SongAdmin(admin.ModelAdmin): + inlines = [InfluenceInline] + + errors = SongAdmin.check(model=Song) + expected = [ + checks.Error( + "'admin_checks.Influence' has no GenericForeignKey using content type field 'content_type' and object ID field 'name'.", + hint=None, + obj=InfluenceInline, + id='admin.E304', + ) + ] + self.assertEqual(errors, expected) + def test_app_label_in_admin_checks(self): """ Regression test for #15669 - Include app label in admin system check messages |