2013-01-12 16:37:19 +08:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import Article, IndexErrorArticle, Person
|
2013-01-12 16:37:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
class EarliestOrLatestTests(TestCase):
|
|
|
|
"""Tests for the earliest() and latest() objects methods"""
|
|
|
|
|
2017-06-22 04:21:27 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super().setUpClass()
|
|
|
|
cls._article_get_latest_by = Article._meta.get_latest_by
|
|
|
|
|
2013-01-12 16:37:19 +08:00
|
|
|
def tearDown(self):
|
2017-06-22 04:21:27 +08:00
|
|
|
Article._meta.get_latest_by = self._article_get_latest_by
|
2013-01-12 16:37:19 +08:00
|
|
|
|
|
|
|
def test_earliest(self):
|
|
|
|
# Because no Articles exist yet, earliest() raises ArticleDoesNotExist.
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(Article.DoesNotExist):
|
|
|
|
Article.objects.earliest()
|
2013-01-12 16:37:19 +08:00
|
|
|
|
|
|
|
a1 = Article.objects.create(
|
|
|
|
headline="Article 1", pub_date=datetime(2005, 7, 26),
|
|
|
|
expire_date=datetime(2005, 9, 1)
|
|
|
|
)
|
|
|
|
a2 = Article.objects.create(
|
|
|
|
headline="Article 2", pub_date=datetime(2005, 7, 27),
|
|
|
|
expire_date=datetime(2005, 7, 28)
|
|
|
|
)
|
2017-06-22 04:28:16 +08:00
|
|
|
a3 = Article.objects.create(
|
2013-01-12 16:37:19 +08:00
|
|
|
headline="Article 3", pub_date=datetime(2005, 7, 28),
|
|
|
|
expire_date=datetime(2005, 8, 27)
|
|
|
|
)
|
2017-06-22 04:28:16 +08:00
|
|
|
a4 = Article.objects.create(
|
2013-01-12 16:37:19 +08:00
|
|
|
headline="Article 4", pub_date=datetime(2005, 7, 28),
|
|
|
|
expire_date=datetime(2005, 7, 30)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get the earliest Article.
|
|
|
|
self.assertEqual(Article.objects.earliest(), a1)
|
|
|
|
# Get the earliest Article that matches certain filters.
|
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.filter(pub_date__gt=datetime(2005, 7, 26)).earliest(),
|
|
|
|
a2
|
|
|
|
)
|
|
|
|
|
|
|
|
# Pass a custom field name to earliest() to change the field that's used
|
|
|
|
# to determine the earliest object.
|
|
|
|
self.assertEqual(Article.objects.earliest('expire_date'), a2)
|
|
|
|
self.assertEqual(Article.objects.filter(
|
|
|
|
pub_date__gt=datetime(2005, 7, 26)).earliest('expire_date'), a2)
|
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# earliest() overrides any other ordering specified on the query.
|
|
|
|
# Refs #11283.
|
2013-01-12 16:37:19 +08:00
|
|
|
self.assertEqual(Article.objects.order_by('id').earliest(), a1)
|
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# Error is raised if the user forgot to add a get_latest_by
|
2013-01-12 16:37:19 +08:00
|
|
|
# in the Model.Meta
|
|
|
|
Article.objects.model._meta.get_latest_by = None
|
2016-01-04 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(
|
2017-06-22 04:28:16 +08:00
|
|
|
ValueError,
|
|
|
|
"earliest() and latest() require either fields as positional "
|
|
|
|
"arguments or 'get_latest_by' in the model's Meta."
|
2016-01-04 16:50:08 +08:00
|
|
|
):
|
|
|
|
Article.objects.earliest()
|
2013-01-12 16:37:19 +08:00
|
|
|
|
2017-06-22 04:28:16 +08:00
|
|
|
# Earliest publication date, earliest expire date.
|
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.filter(pub_date=datetime(2005, 7, 28)).earliest('pub_date', 'expire_date'),
|
|
|
|
a4,
|
|
|
|
)
|
|
|
|
# Earliest publication date, latest expire date.
|
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.filter(pub_date=datetime(2005, 7, 28)).earliest('pub_date', '-expire_date'),
|
|
|
|
a3,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Meta.get_latest_by may be a tuple.
|
|
|
|
Article.objects.model._meta.get_latest_by = ('pub_date', 'expire_date')
|
|
|
|
self.assertEqual(Article.objects.filter(pub_date=datetime(2005, 7, 28)).earliest(), a4)
|
|
|
|
|
2021-03-10 16:16:28 +08:00
|
|
|
def test_earliest_sliced_queryset(self):
|
|
|
|
msg = 'Cannot change a query once a slice has been taken.'
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
Article.objects.all()[0:5].earliest()
|
|
|
|
|
2013-01-12 16:37:19 +08:00
|
|
|
def test_latest(self):
|
|
|
|
# Because no Articles exist yet, latest() raises ArticleDoesNotExist.
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(Article.DoesNotExist):
|
|
|
|
Article.objects.latest()
|
2013-01-12 16:37:19 +08:00
|
|
|
|
|
|
|
a1 = Article.objects.create(
|
|
|
|
headline="Article 1", pub_date=datetime(2005, 7, 26),
|
|
|
|
expire_date=datetime(2005, 9, 1)
|
|
|
|
)
|
2017-06-22 04:28:16 +08:00
|
|
|
a2 = Article.objects.create(
|
2013-01-12 16:37:19 +08:00
|
|
|
headline="Article 2", pub_date=datetime(2005, 7, 27),
|
|
|
|
expire_date=datetime(2005, 7, 28)
|
|
|
|
)
|
|
|
|
a3 = Article.objects.create(
|
|
|
|
headline="Article 3", pub_date=datetime(2005, 7, 27),
|
|
|
|
expire_date=datetime(2005, 8, 27)
|
|
|
|
)
|
|
|
|
a4 = Article.objects.create(
|
|
|
|
headline="Article 4", pub_date=datetime(2005, 7, 28),
|
|
|
|
expire_date=datetime(2005, 7, 30)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get the latest Article.
|
|
|
|
self.assertEqual(Article.objects.latest(), a4)
|
|
|
|
# Get the latest Article that matches certain filters.
|
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.filter(pub_date__lt=datetime(2005, 7, 27)).latest(),
|
|
|
|
a1
|
|
|
|
)
|
|
|
|
|
|
|
|
# Pass a custom field name to latest() to change the field that's used
|
|
|
|
# to determine the latest object.
|
|
|
|
self.assertEqual(Article.objects.latest('expire_date'), a1)
|
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.filter(pub_date__gt=datetime(2005, 7, 26)).latest('expire_date'),
|
|
|
|
a3,
|
|
|
|
)
|
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# latest() overrides any other ordering specified on the query (#11283).
|
2013-01-12 16:37:19 +08:00
|
|
|
self.assertEqual(Article.objects.order_by('id').latest(), a4)
|
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# Error is raised if get_latest_by isn't in Model.Meta.
|
2013-01-12 16:37:19 +08:00
|
|
|
Article.objects.model._meta.get_latest_by = None
|
2016-01-04 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(
|
2017-06-22 04:28:16 +08:00
|
|
|
ValueError,
|
|
|
|
"earliest() and latest() require either fields as positional "
|
|
|
|
"arguments or 'get_latest_by' in the model's Meta."
|
2016-02-04 19:10:50 +08:00
|
|
|
):
|
2016-01-04 16:50:08 +08:00
|
|
|
Article.objects.latest()
|
2013-01-12 16:37:19 +08:00
|
|
|
|
2017-06-22 04:28:16 +08:00
|
|
|
# Latest publication date, latest expire date.
|
|
|
|
self.assertEqual(Article.objects.filter(pub_date=datetime(2005, 7, 27)).latest('pub_date', 'expire_date'), a3)
|
|
|
|
# Latest publication date, earliest expire date.
|
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.filter(pub_date=datetime(2005, 7, 27)).latest('pub_date', '-expire_date'),
|
|
|
|
a2,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Meta.get_latest_by may be a tuple.
|
|
|
|
Article.objects.model._meta.get_latest_by = ('pub_date', 'expire_date')
|
|
|
|
self.assertEqual(Article.objects.filter(pub_date=datetime(2005, 7, 27)).latest(), a3)
|
|
|
|
|
2021-03-10 16:16:28 +08:00
|
|
|
def test_latest_sliced_queryset(self):
|
|
|
|
msg = 'Cannot change a query once a slice has been taken.'
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
Article.objects.all()[0:5].latest()
|
|
|
|
|
2013-01-12 16:37:19 +08:00
|
|
|
def test_latest_manual(self):
|
|
|
|
# You can still use latest() with a model that doesn't have
|
|
|
|
# "get_latest_by" set -- just pass in the field name manually.
|
2013-10-19 20:31:38 +08:00
|
|
|
Person.objects.create(name="Ralph", birthday=datetime(1950, 1, 1))
|
2013-01-12 16:37:19 +08:00
|
|
|
p2 = Person.objects.create(name="Stephanie", birthday=datetime(1960, 2, 3))
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = (
|
2017-06-22 04:28:16 +08:00
|
|
|
"earliest() and latest() require either fields as positional arguments "
|
|
|
|
"or 'get_latest_by' in the model's Meta."
|
2017-05-29 03:37:21 +08:00
|
|
|
)
|
2017-06-22 04:28:16 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
Person.objects.latest()
|
2013-01-12 16:37:19 +08:00
|
|
|
self.assertEqual(Person.objects.latest("birthday"), p2)
|
2013-05-21 23:35:12 +08:00
|
|
|
|
2014-10-08 23:27:17 +08:00
|
|
|
|
|
|
|
class TestFirstLast(TestCase):
|
|
|
|
|
2013-05-21 23:35:12 +08:00
|
|
|
def test_first(self):
|
|
|
|
p1 = Person.objects.create(name="Bob", birthday=datetime(1950, 1, 1))
|
|
|
|
p2 = Person.objects.create(name="Alice", birthday=datetime(1961, 2, 3))
|
2016-10-27 15:53:39 +08:00
|
|
|
self.assertEqual(Person.objects.first(), p1)
|
|
|
|
self.assertEqual(Person.objects.order_by('name').first(), p2)
|
|
|
|
self.assertEqual(Person.objects.filter(birthday__lte=datetime(1955, 1, 1)).first(), p1)
|
|
|
|
self.assertIsNone(Person.objects.filter(birthday__lte=datetime(1940, 1, 1)).first())
|
2013-05-21 23:35:12 +08:00
|
|
|
|
|
|
|
def test_last(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
p1 = Person.objects.create(name="Alice", birthday=datetime(1950, 1, 1))
|
|
|
|
p2 = Person.objects.create(name="Bob", birthday=datetime(1960, 2, 3))
|
2013-05-21 23:35:12 +08:00
|
|
|
# Note: by default PK ordering.
|
2016-10-27 15:53:39 +08:00
|
|
|
self.assertEqual(Person.objects.last(), p2)
|
|
|
|
self.assertEqual(Person.objects.order_by('-name').last(), p1)
|
|
|
|
self.assertEqual(Person.objects.filter(birthday__lte=datetime(1955, 1, 1)).last(), p1)
|
|
|
|
self.assertIsNone(Person.objects.filter(birthday__lte=datetime(1940, 1, 1)).last())
|
2014-10-08 23:27:17 +08:00
|
|
|
|
|
|
|
def test_index_error_not_suppressed(self):
|
|
|
|
"""
|
|
|
|
#23555 -- Unexpected IndexError exceptions in QuerySet iteration
|
|
|
|
shouldn't be suppressed.
|
|
|
|
"""
|
|
|
|
def check():
|
|
|
|
# We know that we've broken the __iter__ method, so the queryset
|
|
|
|
# should always raise an exception.
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IndexError):
|
2017-07-31 23:02:23 +08:00
|
|
|
IndexErrorArticle.objects.all()[:10:2]
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
IndexErrorArticle.objects.all().first()
|
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
IndexErrorArticle.objects.all().last()
|
2014-10-08 23:27:17 +08:00
|
|
|
|
|
|
|
check()
|
|
|
|
|
|
|
|
# And it does not matter if there are any records in the DB.
|
|
|
|
IndexErrorArticle.objects.create(
|
|
|
|
headline="Article 1", pub_date=datetime(2005, 7, 26),
|
|
|
|
expire_date=datetime(2005, 9, 1)
|
|
|
|
)
|
|
|
|
check()
|