diff options
author | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 09:53:47 +0100 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 14:36:57 +0100 |
commit | 89f40e36246100df6a11316c31a76712ebc6c501 (patch) | |
tree | 6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/ordering | |
parent | b3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff) | |
download | django-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz |
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/ordering')
-rw-r--r-- | tests/ordering/__init__.py | 0 | ||||
-rw-r--r-- | tests/ordering/models.py | 38 | ||||
-rw-r--r-- | tests/ordering/tests.py | 167 |
3 files changed, 205 insertions, 0 deletions
diff --git a/tests/ordering/__init__.py b/tests/ordering/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/ordering/__init__.py diff --git a/tests/ordering/models.py b/tests/ordering/models.py new file mode 100644 index 0000000000..67126e1bda --- /dev/null +++ b/tests/ordering/models.py @@ -0,0 +1,38 @@ +""" +6. Specifying ordering + +Specify default ordering for a model using the ``ordering`` attribute, which +should be a list or tuple of field names. This tells Django how to order +``QuerySet`` results. + +If a field name in ``ordering`` starts with a hyphen, that field will be +ordered in descending order. Otherwise, it'll be ordered in ascending order. +The special-case field name ``"?"`` specifies random order. + +The ordering attribute is not required. If you leave it off, ordering will be +undefined -- not random, just undefined. +""" + +from django.db import models +from django.utils.encoding import python_2_unicode_compatible + + +@python_2_unicode_compatible +class Article(models.Model): + headline = models.CharField(max_length=100) + pub_date = models.DateTimeField() + class Meta: + ordering = ('-pub_date', 'headline') + + def __str__(self): + return self.headline + +@python_2_unicode_compatible +class ArticlePKOrdering(models.Model): + headline = models.CharField(max_length=100) + pub_date = models.DateTimeField() + class Meta: + ordering = ('-pk',) + + def __str__(self): + return self.headline diff --git a/tests/ordering/tests.py b/tests/ordering/tests.py new file mode 100644 index 0000000000..b1b5253682 --- /dev/null +++ b/tests/ordering/tests.py @@ -0,0 +1,167 @@ +from __future__ import absolute_import + +from datetime import datetime +from operator import attrgetter + +from django.test import TestCase + +from .models import Article, ArticlePKOrdering + + +class OrderingTests(TestCase): + def test_basic(self): + a1 = Article.objects.create( + headline="Article 1", pub_date=datetime(2005, 7, 26) + ) + a2 = Article.objects.create( + headline="Article 2", pub_date=datetime(2005, 7, 27) + ) + a3 = Article.objects.create( + headline="Article 3", pub_date=datetime(2005, 7, 27) + ) + a4 = Article.objects.create( + headline="Article 4", pub_date=datetime(2005, 7, 28) + ) + + # By default, Article.objects.all() orders by pub_date descending, then + # headline ascending. + self.assertQuerysetEqual( + Article.objects.all(), [ + "Article 4", + "Article 2", + "Article 3", + "Article 1", + ], + attrgetter("headline") + ) + + # Override ordering with order_by, which is in the same format as the + # ordering attribute in models. + self.assertQuerysetEqual( + Article.objects.order_by("headline"), [ + "Article 1", + "Article 2", + "Article 3", + "Article 4", + ], + attrgetter("headline") + ) + self.assertQuerysetEqual( + Article.objects.order_by("pub_date", "-headline"), [ + "Article 1", + "Article 3", + "Article 2", + "Article 4", + ], + attrgetter("headline") + ) + + # Only the last order_by has any effect (since they each override any + # previous ordering). + self.assertQuerysetEqual( + Article.objects.order_by("id"), [ + "Article 1", + "Article 2", + "Article 3", + "Article 4", + ], + attrgetter("headline") + ) + self.assertQuerysetEqual( + Article.objects.order_by("id").order_by("-headline"), [ + "Article 4", + "Article 3", + "Article 2", + "Article 1", + ], + attrgetter("headline") + ) + + # Use the 'stop' part of slicing notation to limit the results. + self.assertQuerysetEqual( + Article.objects.order_by("headline")[:2], [ + "Article 1", + "Article 2", + ], + attrgetter("headline") + ) + + # Use the 'stop' and 'start' parts of slicing notation to offset the + # result list. + self.assertQuerysetEqual( + Article.objects.order_by("headline")[1:3], [ + "Article 2", + "Article 3", + ], + attrgetter("headline") + ) + + # Getting a single item should work too: + self.assertEqual(Article.objects.all()[0], a4) + + # Use '?' to order randomly. + self.assertEqual( + len(list(Article.objects.order_by("?"))), 4 + ) + + # Ordering can be reversed using the reverse() method on a queryset. + # This allows you to extract things like "the last two items" (reverse + # and then take the first two). + self.assertQuerysetEqual( + Article.objects.all().reverse()[:2], [ + "Article 1", + "Article 3", + ], + attrgetter("headline") + ) + + # Ordering can be based on fields included from an 'extra' clause + self.assertQuerysetEqual( + Article.objects.extra(select={"foo": "pub_date"}, order_by=["foo", "headline"]), [ + "Article 1", + "Article 2", + "Article 3", + "Article 4", + ], + attrgetter("headline") + ) + + # If the extra clause uses an SQL keyword for a name, it will be + # protected by quoting. + self.assertQuerysetEqual( + Article.objects.extra(select={"order": "pub_date"}, order_by=["order", "headline"]), [ + "Article 1", + "Article 2", + "Article 3", + "Article 4", + ], + attrgetter("headline") + ) + + def test_order_by_pk(self): + """ + Ensure that 'pk' works as an ordering option in Meta. + Refs #8291. + """ + a1 = ArticlePKOrdering.objects.create( + pk=1, headline="Article 1", pub_date=datetime(2005, 7, 26) + ) + a2 = ArticlePKOrdering.objects.create( + pk=2, headline="Article 2", pub_date=datetime(2005, 7, 27) + ) + a3 = ArticlePKOrdering.objects.create( + pk=3, headline="Article 3", pub_date=datetime(2005, 7, 27) + ) + a4 = ArticlePKOrdering.objects.create( + pk=4, headline="Article 4", pub_date=datetime(2005, 7, 28) + ) + + self.assertQuerysetEqual( + ArticlePKOrdering.objects.all(), [ + "Article 4", + "Article 3", + "Article 2", + "Article 1", + ], + attrgetter("headline") + ) |