mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Refs #33476 -- Reformatted code with Black.
This commit is contained in:
committed by
Mariusz Felisiak
parent
f68fa8b45d
commit
9c19aff7c7
@@ -19,12 +19,12 @@ class Author(models.Model):
|
||||
|
||||
class ArticleManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().filter(authors__name__icontains='sir')
|
||||
return super().get_queryset().filter(authors__name__icontains="sir")
|
||||
|
||||
|
||||
class AttributeErrorManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
raise AttributeError('AttributeErrorManager')
|
||||
raise AttributeError("AttributeErrorManager")
|
||||
|
||||
|
||||
class Article(models.Model):
|
||||
|
||||
@@ -18,15 +18,11 @@ class GetObjectOr404Tests(TestCase):
|
||||
article = Article.objects.create(title="Run away!")
|
||||
article.authors.set([a1, a2])
|
||||
# get_object_or_404 can be passed a Model to query.
|
||||
self.assertEqual(
|
||||
get_object_or_404(Article, title__contains="Run"),
|
||||
article
|
||||
)
|
||||
self.assertEqual(get_object_or_404(Article, title__contains="Run"), article)
|
||||
|
||||
# We can also use the Article manager through an Author object.
|
||||
self.assertEqual(
|
||||
get_object_or_404(a1.article_set, title__contains="Run"),
|
||||
article
|
||||
get_object_or_404(a1.article_set, title__contains="Run"), article
|
||||
)
|
||||
|
||||
# No articles containing "Camelot". This should raise an Http404 error.
|
||||
@@ -35,14 +31,12 @@ class GetObjectOr404Tests(TestCase):
|
||||
|
||||
# Custom managers can be used too.
|
||||
self.assertEqual(
|
||||
get_object_or_404(Article.by_a_sir, title="Run away!"),
|
||||
article
|
||||
get_object_or_404(Article.by_a_sir, title="Run away!"), article
|
||||
)
|
||||
|
||||
# QuerySets can be used too.
|
||||
self.assertEqual(
|
||||
get_object_or_404(Article.objects.all(), title__contains="Run"),
|
||||
article
|
||||
get_object_or_404(Article.objects.all(), title__contains="Run"), article
|
||||
)
|
||||
|
||||
# Just as when using a get() lookup, you will get an error if more than
|
||||
@@ -57,8 +51,7 @@ class GetObjectOr404Tests(TestCase):
|
||||
|
||||
# get_list_or_404 can be used to get lists of objects
|
||||
self.assertEqual(
|
||||
get_list_or_404(a1.article_set, title__icontains="Run"),
|
||||
[article]
|
||||
get_list_or_404(a1.article_set, title__icontains="Run"), [article]
|
||||
)
|
||||
|
||||
# Http404 is returned if the list is empty.
|
||||
@@ -67,29 +60,27 @@ class GetObjectOr404Tests(TestCase):
|
||||
|
||||
# Custom managers can be used too.
|
||||
self.assertEqual(
|
||||
get_list_or_404(Article.by_a_sir, title__icontains="Run"),
|
||||
[article]
|
||||
get_list_or_404(Article.by_a_sir, title__icontains="Run"), [article]
|
||||
)
|
||||
|
||||
# QuerySets can be used too.
|
||||
self.assertEqual(
|
||||
get_list_or_404(Article.objects.all(), title__icontains="Run"),
|
||||
[article]
|
||||
get_list_or_404(Article.objects.all(), title__icontains="Run"), [article]
|
||||
)
|
||||
# Q objects.
|
||||
self.assertEqual(
|
||||
get_object_or_404(
|
||||
Article,
|
||||
Q(title__startswith='Run') | Q(title__startswith='Walk'),
|
||||
authors__name__contains='Brave',
|
||||
Q(title__startswith="Run") | Q(title__startswith="Walk"),
|
||||
authors__name__contains="Brave",
|
||||
),
|
||||
article,
|
||||
)
|
||||
self.assertEqual(
|
||||
get_list_or_404(
|
||||
Article,
|
||||
Q(title__startswith='Run') | Q(title__startswith='Walk'),
|
||||
authors__name='Patsy',
|
||||
Q(title__startswith="Run") | Q(title__startswith="Walk"),
|
||||
authors__name="Patsy",
|
||||
),
|
||||
[article],
|
||||
)
|
||||
@@ -115,10 +106,10 @@ class GetObjectOr404Tests(TestCase):
|
||||
|
||||
def test_get_object_or_404_queryset_attribute_error(self):
|
||||
"""AttributeError raised by QuerySet.get() isn't hidden."""
|
||||
with self.assertRaisesMessage(AttributeError, 'AttributeErrorManager'):
|
||||
with self.assertRaisesMessage(AttributeError, "AttributeErrorManager"):
|
||||
get_object_or_404(Article.attribute_error_objects, id=42)
|
||||
|
||||
def test_get_list_or_404_queryset_attribute_error(self):
|
||||
"""AttributeError raised by QuerySet.filter() isn't hidden."""
|
||||
with self.assertRaisesMessage(AttributeError, 'AttributeErrorManager'):
|
||||
get_list_or_404(Article.attribute_error_objects, title__icontains='Run')
|
||||
with self.assertRaisesMessage(AttributeError, "AttributeErrorManager"):
|
||||
get_list_or_404(Article.attribute_error_objects, title__icontains="Run")
|
||||
|
||||
Reference in New Issue
Block a user