PageRenderTime 82ms CodeModel.GetById 61ms app.highlight 7ms RepoModel.GetById 13ms app.codeStats 0ms

/tests/regressiontests/admin_ordering/tests.py

https://code.google.com/p/mango-py/
Python | 73 lines | 62 code | 2 blank | 9 comment | 3 complexity | 282455461d02a1b200513293eba6f61a MD5 | raw file
 1from django.test import TestCase
 2from django.contrib.admin.options import ModelAdmin
 3
 4from models import Band, Song, SongInlineDefaultOrdering, SongInlineNewOrdering
 5
 6class TestAdminOrdering(TestCase):
 7    """
 8    Let's make sure that ModelAdmin.queryset uses the ordering we define in
 9    ModelAdmin rather that ordering defined in the model's inner Meta
10    class.
11    """
12
13    def setUp(self):
14        b1 = Band(name='Aerosmith', bio='', rank=3)
15        b1.save()
16        b2 = Band(name='Radiohead', bio='', rank=1)
17        b2.save()
18        b3 = Band(name='Van Halen', bio='', rank=2)
19        b3.save()
20
21    def test_default_ordering(self):
22        """
23        The default ordering should be by name, as specified in the inner Meta
24        class.
25        """
26        ma = ModelAdmin(Band, None)
27        names = [b.name for b in ma.queryset(None)]
28        self.assertEqual([u'Aerosmith', u'Radiohead', u'Van Halen'], names)
29
30    def test_specified_ordering(self):
31        """
32        Let's use a custom ModelAdmin that changes the ordering, and make sure
33        it actually changes.
34        """
35        class BandAdmin(ModelAdmin):
36            ordering = ('rank',) # default ordering is ('name',)
37        ma = BandAdmin(Band, None)
38        names = [b.name for b in ma.queryset(None)]
39        self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names)
40
41class TestInlineModelAdminOrdering(TestCase):
42    """
43    Let's make sure that InlineModelAdmin.queryset uses the ordering we define
44    in InlineModelAdmin.
45    """
46
47    def setUp(self):
48        b = Band(name='Aerosmith', bio='', rank=3)
49        b.save()
50        self.b = b
51        s1 = Song(band=b, name='Pink', duration=235)
52        s1.save()
53        s2 = Song(band=b, name='Dude (Looks Like a Lady)', duration=264)
54        s2.save()
55        s3 = Song(band=b, name='Jaded', duration=214)
56        s3.save()
57
58    def test_default_ordering(self):
59        """
60        The default ordering should be by name, as specified in the inner Meta
61        class.
62        """
63        inline = SongInlineDefaultOrdering(self.b, None)
64        names = [s.name for s in inline.queryset(None)]
65        self.assertEqual([u'Dude (Looks Like a Lady)', u'Jaded', u'Pink'], names)
66
67    def test_specified_ordering(self):
68        """
69        Let's check with ordering set to something different than the default.
70        """
71        inline = SongInlineNewOrdering(self.b, None)
72        names = [s.name for s in inline.queryset(None)]
73        self.assertEqual([u'Jaded', u'Pink', u'Dude (Looks Like a Lady)'], names)