From 09cdfda2499e5e38b4cbf62c928a7586ee534bdf Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 27 Sep 2010 15:11:55 +0000 Subject: [PATCH] Migrated admin_registration doctests. Thanks to Sebastian Hillig. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13882 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../admin_registration/models.py | 53 ------------------ .../admin_registration/tests.py | 54 +++++++++++++++++++ 2 files changed, 54 insertions(+), 53 deletions(-) create mode 100644 tests/regressiontests/admin_registration/tests.py diff --git a/tests/regressiontests/admin_registration/models.py b/tests/regressiontests/admin_registration/models.py index 35cf8afce8..4a2d4e9614 100644 --- a/tests/regressiontests/admin_registration/models.py +++ b/tests/regressiontests/admin_registration/models.py @@ -3,62 +3,9 @@ Tests for various ways of registering models with the admin site. """ from django.db import models -from django.contrib import admin class Person(models.Model): name = models.CharField(max_length=200) class Place(models.Model): name = models.CharField(max_length=200) - -__test__ = {'API_TESTS':""" - - -# Bare registration ->>> site = admin.AdminSite() ->>> site.register(Person) ->>> site._registry[Person] - - -# Registration with a ModelAdmin ->>> site = admin.AdminSite() ->>> class NameAdmin(admin.ModelAdmin): -... list_display = ['name'] -... save_on_top = True - ->>> site.register(Person, NameAdmin) ->>> site._registry[Person] - - -# You can't register the same model twice ->>> site.register(Person) -Traceback (most recent call last): - ... -AlreadyRegistered: The model Person is already registered - -# Registration using **options ->>> site = admin.AdminSite() ->>> site.register(Person, search_fields=['name']) ->>> site._registry[Person].search_fields -['name'] - -# With both admin_class and **options the **options override the fields in -# the admin class. ->>> site = admin.AdminSite() ->>> site.register(Person, NameAdmin, search_fields=["name"], list_display=['__str__']) ->>> site._registry[Person].search_fields -['name'] ->>> site._registry[Person].list_display -['action_checkbox', '__str__'] ->>> site._registry[Person].save_on_top -True - -# You can also register iterables instead of single classes -- a nice shortcut ->>> site = admin.AdminSite() ->>> site.register([Person, Place], search_fields=['name']) ->>> site._registry[Person] - ->>> site._registry[Place] - - -"""} diff --git a/tests/regressiontests/admin_registration/tests.py b/tests/regressiontests/admin_registration/tests.py new file mode 100644 index 0000000000..e2a5d7e017 --- /dev/null +++ b/tests/regressiontests/admin_registration/tests.py @@ -0,0 +1,54 @@ +from django.test import TestCase + +from django.contrib import admin + +from models import Person, Place + +class NameAdmin(admin.ModelAdmin): + list_display = ['name'] + save_on_top = True + +class TestRegistration(TestCase): + def setUp(self): + self.site = admin.AdminSite() + + def test_bare_registration(self): + self.site.register(Person) + self.assertTrue( + isinstance(self.site._registry[Person], admin.options.ModelAdmin) + ) + + def test_registration_with_model_admin(self): + self.site.register(Person, NameAdmin) + self.assertTrue( + isinstance(self.site._registry[Person], NameAdmin) + ) + + def test_prevent_double_registration(self): + self.site.register(Person) + self.assertRaises(admin.sites.AlreadyRegistered, + self.site.register, + Person) + + def test_registration_with_star_star_options(self): + self.site.register(Person, search_fields=['name']) + self.assertEqual(self.site._registry[Person].search_fields, ['name']) + + def test_star_star_overrides(self): + self.site.register(Person, NameAdmin, + search_fields=["name"], list_display=['__str__']) + self.assertEqual(self.site._registry[Person].search_fields, ['name']) + self.assertEqual(self.site._registry[Person].list_display, + ['action_checkbox', '__str__']) + self.assertTrue(self.site._registry[Person].save_on_top) + + def test_iterable_registration(self): + self.site.register([Person, Place], search_fields=['name']) + self.assertTrue( + isinstance(self.site._registry[Person], admin.options.ModelAdmin) + ) + self.assertEqual(self.site._registry[Person].search_fields, ['name']) + self.assertTrue( + isinstance(self.site._registry[Place], admin.options.ModelAdmin) + ) + self.assertEqual(self.site._registry[Place].search_fields, ['name'])