summaryrefslogtreecommitdiff
path: root/tests/get_earliest_or_latest
diff options
context:
space:
mode:
authorArtem Rizhov <artem.rizhov@gmail.com>2014-10-08 18:27:17 +0300
committerTim Graham <timograham@gmail.com>2014-10-08 14:28:38 -0400
commitca61195827efdf6ec54e8280e4cfd9a34f07b8b4 (patch)
tree2692b2634d6700923dd9910ac61253478bf0ec72 /tests/get_earliest_or_latest
parent9e2e4cb6dda5fa1d4d6b047adbcc215daf4d6ff1 (diff)
downloaddjango-ca61195827efdf6ec54e8280e4cfd9a34f07b8b4.tar.gz
Fixed #23555 -- Avoided suppressing IndexError in QuerySet.first() and .last()
Diffstat (limited to 'tests/get_earliest_or_latest')
-rw-r--r--tests/get_earliest_or_latest/models.py15
-rw-r--r--tests/get_earliest_or_latest/tests.py26
2 files changed, 40 insertions, 1 deletions
diff --git a/tests/get_earliest_or_latest/models.py b/tests/get_earliest_or_latest/models.py
index cd62cf0104..55096f5a2c 100644
--- a/tests/get_earliest_or_latest/models.py
+++ b/tests/get_earliest_or_latest/models.py
@@ -14,3 +14,18 @@ class Person(models.Model):
name = models.CharField(max_length=30)
birthday = models.DateField()
# Note that this model doesn't have "get_latest_by" set.
+
+
+# Ticket #23555 - model with an intentionally broken QuerySet.__iter__ method.
+
+class IndexErrorQuerySet(models.QuerySet):
+ """
+ Emulates the case when some internal code raises an unexpected
+ IndexError.
+ """
+ def __iter__(self):
+ raise IndexError
+
+
+class IndexErrorArticle(Article):
+ objects = IndexErrorQuerySet.as_manager()
diff --git a/tests/get_earliest_or_latest/tests.py b/tests/get_earliest_or_latest/tests.py
index 742c70bc41..de3307b1a6 100644
--- a/tests/get_earliest_or_latest/tests.py
+++ b/tests/get_earliest_or_latest/tests.py
@@ -4,7 +4,7 @@ from datetime import datetime
from django.test import TestCase
-from .models import Article, Person
+from .models import Article, Person, IndexErrorArticle
class EarliestOrLatestTests(TestCase):
@@ -122,6 +122,9 @@ class EarliestOrLatestTests(TestCase):
self.assertRaises(AssertionError, Person.objects.latest)
self.assertEqual(Person.objects.latest("birthday"), p2)
+
+class TestFirstLast(TestCase):
+
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))
@@ -152,3 +155,24 @@ class EarliestOrLatestTests(TestCase):
self.assertIs(
Person.objects.filter(birthday__lte=datetime(1940, 1, 1)).last(),
None)
+
+ 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.
+ self.assertRaises(IndexError, lambda: IndexErrorArticle.objects.all()[0])
+ self.assertRaises(IndexError, IndexErrorArticle.objects.all().first)
+ self.assertRaises(IndexError, IndexErrorArticle.objects.all().last)
+
+ 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()