PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modeltests/ordering/tests.py

https://code.google.com/p/mango-py/
Python | 137 lines | 106 code | 14 blank | 17 comment | 0 complexity | f9b4dc124dc6b3160ae6242b08ff485d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from datetime import datetime
  2. from operator import attrgetter
  3. from django.test import TestCase
  4. from models import Article
  5. class OrderingTests(TestCase):
  6. def test_basic(self):
  7. a1 = Article.objects.create(
  8. headline="Article 1", pub_date=datetime(2005, 7, 26)
  9. )
  10. a2 = Article.objects.create(
  11. headline="Article 2", pub_date=datetime(2005, 7, 27)
  12. )
  13. a3 = Article.objects.create(
  14. headline="Article 3", pub_date=datetime(2005, 7, 27)
  15. )
  16. a4 = Article.objects.create(
  17. headline="Article 4", pub_date=datetime(2005, 7, 28)
  18. )
  19. # By default, Article.objects.all() orders by pub_date descending, then
  20. # headline ascending.
  21. self.assertQuerysetEqual(
  22. Article.objects.all(), [
  23. "Article 4",
  24. "Article 2",
  25. "Article 3",
  26. "Article 1",
  27. ],
  28. attrgetter("headline")
  29. )
  30. # Override ordering with order_by, which is in the same format as the
  31. # ordering attribute in models.
  32. self.assertQuerysetEqual(
  33. Article.objects.order_by("headline"), [
  34. "Article 1",
  35. "Article 2",
  36. "Article 3",
  37. "Article 4",
  38. ],
  39. attrgetter("headline")
  40. )
  41. self.assertQuerysetEqual(
  42. Article.objects.order_by("pub_date", "-headline"), [
  43. "Article 1",
  44. "Article 3",
  45. "Article 2",
  46. "Article 4",
  47. ],
  48. attrgetter("headline")
  49. )
  50. # Only the last order_by has any effect (since they each override any
  51. # previous ordering).
  52. self.assertQuerysetEqual(
  53. Article.objects.order_by("id"), [
  54. "Article 1",
  55. "Article 2",
  56. "Article 3",
  57. "Article 4",
  58. ],
  59. attrgetter("headline")
  60. )
  61. self.assertQuerysetEqual(
  62. Article.objects.order_by("id").order_by("-headline"), [
  63. "Article 4",
  64. "Article 3",
  65. "Article 2",
  66. "Article 1",
  67. ],
  68. attrgetter("headline")
  69. )
  70. # Use the 'stop' part of slicing notation to limit the results.
  71. self.assertQuerysetEqual(
  72. Article.objects.order_by("headline")[:2], [
  73. "Article 1",
  74. "Article 2",
  75. ],
  76. attrgetter("headline")
  77. )
  78. # Use the 'stop' and 'start' parts of slicing notation to offset the
  79. # result list.
  80. self.assertQuerysetEqual(
  81. Article.objects.order_by("headline")[1:3], [
  82. "Article 2",
  83. "Article 3",
  84. ],
  85. attrgetter("headline")
  86. )
  87. # Getting a single item should work too:
  88. self.assertEqual(Article.objects.all()[0], a4)
  89. # Use '?' to order randomly.
  90. self.assertEqual(
  91. len(list(Article.objects.order_by("?"))), 4
  92. )
  93. # Ordering can be reversed using the reverse() method on a queryset.
  94. # This allows you to extract things like "the last two items" (reverse
  95. # and then take the first two).
  96. self.assertQuerysetEqual(
  97. Article.objects.all().reverse()[:2], [
  98. "Article 1",
  99. "Article 3",
  100. ],
  101. attrgetter("headline")
  102. )
  103. # Ordering can be based on fields included from an 'extra' clause
  104. self.assertQuerysetEqual(
  105. Article.objects.extra(select={"foo": "pub_date"}, order_by=["foo", "headline"]), [
  106. "Article 1",
  107. "Article 2",
  108. "Article 3",
  109. "Article 4",
  110. ],
  111. attrgetter("headline")
  112. )
  113. # If the extra clause uses an SQL keyword for a name, it will be
  114. # protected by quoting.
  115. self.assertQuerysetEqual(
  116. Article.objects.extra(select={"order": "pub_date"}, order_by=["order", "headline"]), [
  117. "Article 1",
  118. "Article 2",
  119. "Article 3",
  120. "Article 4",
  121. ],
  122. attrgetter("headline")
  123. )