App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / tests / regressiontests / admin_ordering / tests.py
blobb82f150f4081a565052296777e9ca86b02263f21
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):
12 pass
14 class MockSuperUser(object):
15 def has_perm(self, perm):
16 return True
18 request = MockRequest()
19 request.user = MockSuperUser()
22 class TestAdminOrdering(TestCase):
23 """
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
26 class.
27 """
29 def setUp(self):
30 self.request_factory = RequestFactory()
31 b1 = Band(name='Aerosmith', bio='', rank=3)
32 b1.save()
33 b2 = Band(name='Radiohead', bio='', rank=1)
34 b2.save()
35 b3 = Band(name='Van Halen', bio='', rank=2)
36 b3.save()
38 def test_default_ordering(self):
39 """
40 The default ordering should be by name, as specified in the inner Meta
41 class.
42 """
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):
48 """
49 Let's use a custom ModelAdmin that changes the ordering, and make sure
50 it actually changes.
51 """
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):
59 """
60 Let's use a custom ModelAdmin that changes the ordering dinamically.
61 """
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):
75 """
76 Let's make sure that InlineModelAdmin.queryset uses the ordering we define
77 in InlineModelAdmin.
78 """
80 def setUp(self):
81 b = Band(name='Aerosmith', bio='', rank=3)
82 b.save()
83 self.b = b
84 s1 = Song(band=b, name='Pink', duration=235)
85 s1.save()
86 s2 = Song(band=b, name='Dude (Looks Like a Lady)', duration=264)
87 s2.save()
88 s3 = Song(band=b, name='Jaded', duration=214)
89 s3.save()
91 def test_default_ordering(self):
92 """
93 The default ordering should be by name, as specified in the inner Meta
94 class.
95 """
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)