PageRenderTime 136ms CodeModel.GetById 128ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/admin_registration/tests.py

https://code.google.com/p/mango-py/
Python | 61 lines | 47 code | 10 blank | 4 comment | 0 complexity | 6863daa3f26e403ad3d75c4edeee22ae MD5 | raw file
 1from django.test import TestCase
 2from django.core.exceptions import ImproperlyConfigured
 3from django.contrib import admin
 4
 5from models import Person, Place, Location
 6
 7class NameAdmin(admin.ModelAdmin):
 8    list_display = ['name']
 9    save_on_top = True
10
11class TestRegistration(TestCase):
12    def setUp(self):
13        self.site = admin.AdminSite()
14
15    def test_bare_registration(self):
16        self.site.register(Person)
17        self.assertTrue(
18            isinstance(self.site._registry[Person], admin.options.ModelAdmin)
19        )
20
21    def test_registration_with_model_admin(self):
22        self.site.register(Person, NameAdmin)
23        self.assertTrue(
24            isinstance(self.site._registry[Person], NameAdmin)
25        )
26
27    def test_prevent_double_registration(self):
28        self.site.register(Person)
29        self.assertRaises(admin.sites.AlreadyRegistered,
30                          self.site.register,
31                          Person)
32
33    def test_registration_with_star_star_options(self):
34        self.site.register(Person, search_fields=['name'])
35        self.assertEqual(self.site._registry[Person].search_fields, ['name'])
36
37    def test_star_star_overrides(self):
38        self.site.register(Person, NameAdmin,
39                           search_fields=["name"], list_display=['__str__'])
40        self.assertEqual(self.site._registry[Person].search_fields, ['name'])
41        self.assertEqual(self.site._registry[Person].list_display,
42                         ['action_checkbox', '__str__'])
43        self.assertTrue(self.site._registry[Person].save_on_top)
44
45    def test_iterable_registration(self):
46        self.site.register([Person, Place], search_fields=['name'])
47        self.assertTrue(
48            isinstance(self.site._registry[Person], admin.options.ModelAdmin)
49        )
50        self.assertEqual(self.site._registry[Person].search_fields, ['name'])
51        self.assertTrue(
52            isinstance(self.site._registry[Place], admin.options.ModelAdmin)
53        )
54        self.assertEqual(self.site._registry[Place].search_fields, ['name'])
55
56    def test_abstract_model(self):
57        """
58        Exception is raised when trying to register an abstract model.
59        Refs #12004.
60        """
61        self.assertRaises(ImproperlyConfigured, self.site.register, Location)