mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Used assertRaisesMessage() to test Django's error messages.
This commit is contained in:
@@ -199,14 +199,22 @@ class PrefetchRelatedTests(TestCase):
|
||||
|
||||
def test_attribute_error(self):
|
||||
qs = Reader.objects.all().prefetch_related('books_read__xyz')
|
||||
with self.assertRaises(AttributeError) as cm:
|
||||
msg = (
|
||||
"Cannot find 'xyz' on Book object, 'books_read__xyz' "
|
||||
"is an invalid parameter to prefetch_related()"
|
||||
)
|
||||
with self.assertRaisesMessage(AttributeError, msg) as cm:
|
||||
list(qs)
|
||||
|
||||
self.assertIn('prefetch_related', str(cm.exception))
|
||||
|
||||
def test_invalid_final_lookup(self):
|
||||
qs = Book.objects.prefetch_related('authors__name')
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
msg = (
|
||||
"'authors__name' does not resolve to an item that supports "
|
||||
"prefetching - this is an invalid parameter to prefetch_related()."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg) as cm:
|
||||
list(qs)
|
||||
|
||||
self.assertIn('prefetch_related', str(cm.exception))
|
||||
@@ -337,14 +345,22 @@ class CustomPrefetchTests(TestCase):
|
||||
|
||||
def test_ambiguous(self):
|
||||
# Ambiguous: Lookup was already seen with a different queryset.
|
||||
with self.assertRaises(ValueError):
|
||||
msg = (
|
||||
"'houses' lookup was already seen with a different queryset. You "
|
||||
"may need to adjust the ordering of your lookups."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
self.traverse_qs(
|
||||
Person.objects.prefetch_related('houses__rooms', Prefetch('houses', queryset=House.objects.all())),
|
||||
[['houses', 'rooms']]
|
||||
)
|
||||
|
||||
# Ambiguous: Lookup houses_lst doesn't yet exist when performing houses_lst__rooms.
|
||||
with self.assertRaises(AttributeError):
|
||||
msg = (
|
||||
"Cannot find 'houses_lst' on Person object, 'houses_lst__rooms' is "
|
||||
"an invalid parameter to prefetch_related()"
|
||||
)
|
||||
with self.assertRaisesMessage(AttributeError, msg):
|
||||
self.traverse_qs(
|
||||
Person.objects.prefetch_related(
|
||||
'houses_lst__rooms',
|
||||
|
||||
Reference in New Issue
Block a user