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
This commit is contained in:
Alex Gaynor 2010-10-11 18:17:37 +00:00
parent 3879c59074
commit 39595a9e0e
2 changed files with 138 additions and 64 deletions

View File

@ -15,6 +15,7 @@ undefined -- not random, just undefined.
from django.db import models from django.db import models
class Article(models.Model): class Article(models.Model):
headline = models.CharField(max_length=100) headline = models.CharField(max_length=100)
pub_date = models.DateTimeField() pub_date = models.DateTimeField()
@ -23,67 +24,3 @@ class Article(models.Model):
def __unicode__(self): def __unicode__(self):
return self.headline 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()
[<Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 1>]
# Override ordering with order_by, which is in the same format as the ordering
# attribute in models.
>>> Article.objects.order_by('headline')
[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>]
>>> Article.objects.order_by('pub_date', '-headline')
[<Article: Article 1>, <Article: Article 3>, <Article: Article 2>, <Article: Article 4>]
# Only the last order_by has any effect (since they each override any previous
# ordering).
>>> Article.objects.order_by('id')
[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>]
>>> Article.objects.order_by('id').order_by('-headline')
[<Article: Article 4>, <Article: Article 3>, <Article: Article 2>, <Article: Article 1>]
# Use the 'stop' part of slicing notation to limit the results.
>>> Article.objects.order_by('headline')[:2]
[<Article: Article 1>, <Article: Article 2>]
# Use the 'stop' and 'start' parts of slicing notation to offset the result list.
>>> Article.objects.order_by('headline')[1:3]
[<Article: Article 2>, <Article: Article 3>]
# Getting a single item should work too:
>>> Article.objects.all()[0]
<Article: Article 4>
# 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]
[<Article: Article 1>, <Article: Article 3>]
# Ordering can be based on fields included from an 'extra' clause
>>> Article.objects.extra(select={'foo': 'pub_date'}, order_by=['foo', 'headline'])
[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>]
# 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'])
[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>]
"""}

View File

@ -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")
)