From 39595a9e0ea85edfea9bca7db2cf4416beb3374d Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 11 Oct 2010 18:17:37 +0000 Subject: [PATCH] Converted ordering tests from doctests to unittests. We have always been at war with doctests. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14147 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/ordering/models.py | 65 +------------ tests/modeltests/ordering/tests.py | 137 ++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 64 deletions(-) create mode 100644 tests/modeltests/ordering/tests.py diff --git a/tests/modeltests/ordering/models.py b/tests/modeltests/ordering/models.py index a53d93c330..25d3c2c90f 100644 --- a/tests/modeltests/ordering/models.py +++ b/tests/modeltests/ordering/models.py @@ -15,6 +15,7 @@ undefined -- not random, just undefined. from django.db import models + class Article(models.Model): headline = models.CharField(max_length=100) pub_date = models.DateTimeField() @@ -23,67 +24,3 @@ class Article(models.Model): def __unicode__(self): return self.headline - -__test__ = {'API_TESTS':""" -# Create a couple of Articles. ->>> from datetime import datetime ->>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26)) ->>> a1.save() ->>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27)) ->>> a2.save() ->>> a3 = Article(headline='Article 3', pub_date=datetime(2005, 7, 27)) ->>> a3.save() ->>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28)) ->>> a4.save() - -# By default, Article.objects.all() orders by pub_date descending, then -# headline ascending. ->>> Article.objects.all() -[, , , ] - -# Override ordering with order_by, which is in the same format as the ordering -# attribute in models. ->>> Article.objects.order_by('headline') -[, , , ] ->>> Article.objects.order_by('pub_date', '-headline') -[, , , ] - -# Only the last order_by has any effect (since they each override any previous -# ordering). ->>> Article.objects.order_by('id') -[, , , ] ->>> Article.objects.order_by('id').order_by('-headline') -[, , , ] - -# Use the 'stop' part of slicing notation to limit the results. ->>> Article.objects.order_by('headline')[:2] -[, ] - -# Use the 'stop' and 'start' parts of slicing notation to offset the result list. ->>> Article.objects.order_by('headline')[1:3] -[, ] - -# Getting a single item should work too: ->>> Article.objects.all()[0] - - -# Use '?' to order randomly. (We're using [...] in the output to indicate we -# don't know what order the output will be in. ->>> Article.objects.order_by('?') -[...] - -# 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). ->>> Article.objects.all().reverse()[:2] -[, ] - -# Ordering can be based on fields included from an 'extra' clause ->>> Article.objects.extra(select={'foo': 'pub_date'}, order_by=['foo', 'headline']) -[, , , ] - -# If the extra clause uses an SQL keyword for a name, it will be protected by quoting. ->>> Article.objects.extra(select={'order': 'pub_date'}, order_by=['order', 'headline']) -[, , , ] - -"""} diff --git a/tests/modeltests/ordering/tests.py b/tests/modeltests/ordering/tests.py new file mode 100644 index 0000000000..77862c528c --- /dev/null +++ b/tests/modeltests/ordering/tests.py @@ -0,0 +1,137 @@ +from datetime import datetime +from operator import attrgetter + +from django.test import TestCase + +from models import Article + + +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") + )