summaryrefslogtreecommitdiff
path: root/tests/async
diff options
context:
space:
mode:
authorOlivier Tabone <olivier.tabone@ripplemotion.fr>2023-07-20 17:50:06 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-07-24 07:37:54 +0200
commitb9473cac65190822e7c94f695f1f7b4d5b49502a (patch)
tree67c6bd94f6ea36dc842f6311a3f3fdac515514e1 /tests/async
parentf05cc5e3d2ae7663dbd248029bcb74500cf1029f (diff)
Fixed #34714 -- Added aget_object_or_404()/aget_list_or_404() shortcuts.
Diffstat (limited to 'tests/async')
-rw-r--r--tests/async/test_async_shortcuts.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/async/test_async_shortcuts.py b/tests/async/test_async_shortcuts.py
new file mode 100644
index 0000000000..09c76873fa
--- /dev/null
+++ b/tests/async/test_async_shortcuts.py
@@ -0,0 +1,58 @@
+from django.db.models import Q
+from django.http import Http404
+from django.shortcuts import aget_list_or_404, aget_object_or_404
+from django.test import TestCase
+
+from .models import RelatedModel, SimpleModel
+
+
+class GetListObjectOr404Test(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.s1 = SimpleModel.objects.create(field=0)
+ cls.s2 = SimpleModel.objects.create(field=1)
+ cls.r1 = RelatedModel.objects.create(simple=cls.s1)
+
+ async def test_aget_object_or_404(self):
+ self.assertEqual(await aget_object_or_404(SimpleModel, field=1), self.s2)
+ self.assertEqual(await aget_object_or_404(SimpleModel, Q(field=0)), self.s1)
+ self.assertEqual(
+ await aget_object_or_404(SimpleModel.objects.all(), field=1), self.s2
+ )
+ self.assertEqual(
+ await aget_object_or_404(self.s1.relatedmodel_set, pk=self.r1.pk), self.r1
+ )
+ # Http404 is returned if the list is empty.
+ msg = "No SimpleModel matches the given query."
+ with self.assertRaisesMessage(Http404, msg):
+ await aget_object_or_404(SimpleModel, field=2)
+
+ async def test_get_list_or_404(self):
+ self.assertEqual(await aget_list_or_404(SimpleModel, field=1), [self.s2])
+ self.assertEqual(await aget_list_or_404(SimpleModel, Q(field=0)), [self.s1])
+ self.assertEqual(
+ await aget_list_or_404(SimpleModel.objects.all(), field=1), [self.s2]
+ )
+ self.assertEqual(
+ await aget_list_or_404(self.s1.relatedmodel_set, pk=self.r1.pk), [self.r1]
+ )
+ # Http404 is returned if the list is empty.
+ msg = "No SimpleModel matches the given query."
+ with self.assertRaisesMessage(Http404, msg):
+ await aget_list_or_404(SimpleModel, field=2)
+
+ async def test_get_object_or_404_bad_class(self):
+ msg = (
+ "First argument to aget_object_or_404() must be a Model, Manager, or "
+ "QuerySet, not 'str'."
+ )
+ with self.assertRaisesMessage(ValueError, msg):
+ await aget_object_or_404("SimpleModel", field=0)
+
+ async def test_get_list_or_404_bad_class(self):
+ msg = (
+ "First argument to aget_list_or_404() must be a Model, Manager, or "
+ "QuerySet, not 'list'."
+ )
+ with self.assertRaisesMessage(ValueError, msg):
+ await aget_list_or_404([SimpleModel], field=1)