Fixed #34135 -- Added async-compatible interface to related managers.

This commit is contained in:
Jon Janzen 2022-11-03 19:57:33 +01:00 committed by Mariusz Felisiak
parent 41e8931c2c
commit 321ecb40f4
7 changed files with 188 additions and 8 deletions

View file

@ -1,6 +1,6 @@
from django.test import TestCase
from .models import ManyToManyModel, SimpleModel
from .models import ManyToManyModel, RelatedModel, SimpleModel
class AsyncRelatedManagersOperationTest(TestCase):
@ -8,6 +8,8 @@ class AsyncRelatedManagersOperationTest(TestCase):
def setUpTestData(cls):
cls.mtm1 = ManyToManyModel.objects.create()
cls.s1 = SimpleModel.objects.create(field=0)
cls.mtm2 = ManyToManyModel.objects.create()
cls.mtm2.simples.set([cls.s1])
async def test_acreate(self):
await self.mtm1.simples.acreate(field=2)
@ -54,3 +56,51 @@ class AsyncRelatedManagersOperationTest(TestCase):
self.assertIs(created, True)
self.assertEqual(await self.s1.relatedmodel_set.acount(), 1)
self.assertEqual(new_relatedmodel.simple, self.s1)
async def test_aadd(self):
await self.mtm1.simples.aadd(self.s1)
self.assertEqual(await self.mtm1.simples.aget(), self.s1)
async def test_aadd_reverse(self):
r1 = await RelatedModel.objects.acreate()
await self.s1.relatedmodel_set.aadd(r1, bulk=False)
self.assertEqual(await self.s1.relatedmodel_set.aget(), r1)
async def test_aremove(self):
self.assertEqual(await self.mtm2.simples.acount(), 1)
await self.mtm2.simples.aremove(self.s1)
self.assertEqual(await self.mtm2.simples.acount(), 0)
async def test_aremove_reverse(self):
r1 = await RelatedModel.objects.acreate(simple=self.s1)
self.assertEqual(await self.s1.relatedmodel_set.acount(), 1)
await self.s1.relatedmodel_set.aremove(r1)
self.assertEqual(await self.s1.relatedmodel_set.acount(), 0)
async def test_aset(self):
await self.mtm1.simples.aset([self.s1])
self.assertEqual(await self.mtm1.simples.aget(), self.s1)
await self.mtm1.simples.aset([])
self.assertEqual(await self.mtm1.simples.acount(), 0)
await self.mtm1.simples.aset([self.s1], clear=True)
self.assertEqual(await self.mtm1.simples.aget(), self.s1)
async def test_aset_reverse(self):
r1 = await RelatedModel.objects.acreate()
await self.s1.relatedmodel_set.aset([r1])
self.assertEqual(await self.s1.relatedmodel_set.aget(), r1)
await self.s1.relatedmodel_set.aset([])
self.assertEqual(await self.s1.relatedmodel_set.acount(), 0)
await self.s1.relatedmodel_set.aset([r1], bulk=False, clear=True)
self.assertEqual(await self.s1.relatedmodel_set.aget(), r1)
async def test_aclear(self):
self.assertEqual(await self.mtm2.simples.acount(), 1)
await self.mtm2.simples.aclear()
self.assertEqual(await self.mtm2.simples.acount(), 0)
async def test_aclear_reverse(self):
await RelatedModel.objects.acreate(simple=self.s1)
self.assertEqual(await self.s1.relatedmodel_set.acount(), 1)
await self.s1.relatedmodel_set.aclear(bulk=False)
self.assertEqual(await self.s1.relatedmodel_set.acount(), 0)