1 from __future__
import absolute_import
3 from django
.contrib
import admin
4 from django
.core
.exceptions
import ImproperlyConfigured
5 from django
.test
import TestCase
7 from .models
import Person
, Place
, Location
10 class NameAdmin(admin
.ModelAdmin
):
11 list_display
= ['name']
14 class TestRegistration(TestCase
):
16 self
.site
= admin
.AdminSite()
18 def test_bare_registration(self
):
19 self
.site
.register(Person
)
21 isinstance(self
.site
._registry
[Person
], admin
.options
.ModelAdmin
)
24 def test_registration_with_model_admin(self
):
25 self
.site
.register(Person
, NameAdmin
)
27 isinstance(self
.site
._registry
[Person
], NameAdmin
)
30 def test_prevent_double_registration(self
):
31 self
.site
.register(Person
)
32 self
.assertRaises(admin
.sites
.AlreadyRegistered
,
36 def test_registration_with_star_star_options(self
):
37 self
.site
.register(Person
, search_fields
=['name'])
38 self
.assertEqual(self
.site
._registry
[Person
].search_fields
, ['name'])
40 def test_star_star_overrides(self
):
41 self
.site
.register(Person
, NameAdmin
,
42 search_fields
=["name"], list_display
=['__str__'])
43 self
.assertEqual(self
.site
._registry
[Person
].search_fields
, ['name'])
44 self
.assertEqual(self
.site
._registry
[Person
].list_display
,
46 self
.assertTrue(self
.site
._registry
[Person
].save_on_top
)
48 def test_iterable_registration(self
):
49 self
.site
.register([Person
, Place
], search_fields
=['name'])
51 isinstance(self
.site
._registry
[Person
], admin
.options
.ModelAdmin
)
53 self
.assertEqual(self
.site
._registry
[Person
].search_fields
, ['name'])
55 isinstance(self
.site
._registry
[Place
], admin
.options
.ModelAdmin
)
57 self
.assertEqual(self
.site
._registry
[Place
].search_fields
, ['name'])
59 def test_abstract_model(self
):
61 Exception is raised when trying to register an abstract model.
64 self
.assertRaises(ImproperlyConfigured
, self
.site
.register
, Location
)