1 from __future__
import absolute_import
3 from django
.test
import TestCase
, RequestFactory
4 from django
.contrib
.admin
.options
import ModelAdmin
5 from django
.contrib
.auth
.models
import User
7 from .models
import (Band
, Song
, SongInlineDefaultOrdering
,
8 SongInlineNewOrdering
, DynOrderingBandAdmin
)
11 class MockRequest(object):
14 class MockSuperUser(object):
15 def has_perm(self
, perm
):
18 request
= MockRequest()
19 request
.user
= MockSuperUser()
22 class TestAdminOrdering(TestCase
):
24 Let's make sure that ModelAdmin.queryset uses the ordering we define in
25 ModelAdmin rather that ordering defined in the model's inner Meta
30 self
.request_factory
= RequestFactory()
31 b1
= Band(name
='Aerosmith', bio
='', rank
=3)
33 b2
= Band(name
='Radiohead', bio
='', rank
=1)
35 b3
= Band(name
='Van Halen', bio
='', rank
=2)
38 def test_default_ordering(self
):
40 The default ordering should be by name, as specified in the inner Meta
43 ma
= ModelAdmin(Band
, None)
44 names
= [b
.name
for b
in ma
.queryset(request
)]
45 self
.assertEqual([u
'Aerosmith', u
'Radiohead', u
'Van Halen'], names
)
47 def test_specified_ordering(self
):
49 Let's use a custom ModelAdmin that changes the ordering, and make sure
52 class BandAdmin(ModelAdmin
):
53 ordering
= ('rank',) # default ordering is ('name',)
54 ma
= BandAdmin(Band
, None)
55 names
= [b
.name
for b
in ma
.queryset(request
)]
56 self
.assertEqual([u
'Radiohead', u
'Van Halen', u
'Aerosmith'], names
)
58 def test_dynamic_ordering(self
):
60 Let's use a custom ModelAdmin that changes the ordering dinamically.
62 super_user
= User
.objects
.create(username
='admin', is_superuser
=True)
63 other_user
= User
.objects
.create(username
='other')
64 request
= self
.request_factory
.get('/')
65 request
.user
= super_user
66 ma
= DynOrderingBandAdmin(Band
, None)
67 names
= [b
.name
for b
in ma
.queryset(request
)]
68 self
.assertEqual([u
'Radiohead', u
'Van Halen', u
'Aerosmith'], names
)
69 request
.user
= other_user
70 names
= [b
.name
for b
in ma
.queryset(request
)]
71 self
.assertEqual([u
'Aerosmith', u
'Radiohead', u
'Van Halen'], names
)
74 class TestInlineModelAdminOrdering(TestCase
):
76 Let's make sure that InlineModelAdmin.queryset uses the ordering we define
81 b
= Band(name
='Aerosmith', bio
='', rank
=3)
84 s1
= Song(band
=b
, name
='Pink', duration
=235)
86 s2
= Song(band
=b
, name
='Dude (Looks Like a Lady)', duration
=264)
88 s3
= Song(band
=b
, name
='Jaded', duration
=214)
91 def test_default_ordering(self
):
93 The default ordering should be by name, as specified in the inner Meta
96 inline
= SongInlineDefaultOrdering(self
.b
, None)
97 names
= [s
.name
for s
in inline
.queryset(request
)]
98 self
.assertEqual([u
'Dude (Looks Like a Lady)', u
'Jaded', u
'Pink'], names
)
100 def test_specified_ordering(self
):
102 Let's check with ordering set to something different than the default.
104 inline
= SongInlineNewOrdering(self
.b
, None)
105 names
= [s
.name
for s
in inline
.queryset(request
)]
106 self
.assertEqual([u
'Jaded', u
'Pink', u
'Dude (Looks Like a Lady)'], names
)