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