mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
[4.1.x] Fixed #34139 -- Fixed acreate(), aget_or_create(), and aupdate_or_create() methods for related managers.
Bug in58b27e0dbb. Backport of7b94847e38from main
This commit is contained in:
committed by
Mariusz Felisiak
parent
8740d2f452
commit
9fb57fcc70
@@ -45,6 +45,10 @@ class GenericRelationsTests(TestCase):
|
||||
# Original list of tags:
|
||||
return obj.tag, obj.content_type.model_class(), obj.object_id
|
||||
|
||||
async def test_generic_async_acreate(self):
|
||||
await self.bacon.tags.acreate(tag="orange")
|
||||
self.assertEqual(await self.bacon.tags.acount(), 3)
|
||||
|
||||
def test_generic_update_or_create_when_created(self):
|
||||
"""
|
||||
Should be able to use update_or_create from the generic related manager
|
||||
@@ -70,6 +74,18 @@ class GenericRelationsTests(TestCase):
|
||||
self.assertEqual(count + 1, self.bacon.tags.count())
|
||||
self.assertEqual(tag.tag, "juicy")
|
||||
|
||||
async def test_generic_async_aupdate_or_create(self):
|
||||
tag, created = await self.bacon.tags.aupdate_or_create(
|
||||
id=self.fatty.id, defaults={"tag": "orange"}
|
||||
)
|
||||
self.assertIs(created, False)
|
||||
self.assertEqual(tag.tag, "orange")
|
||||
self.assertEqual(await self.bacon.tags.acount(), 2)
|
||||
tag, created = await self.bacon.tags.aupdate_or_create(tag="pink")
|
||||
self.assertIs(created, True)
|
||||
self.assertEqual(await self.bacon.tags.acount(), 3)
|
||||
self.assertEqual(tag.tag, "pink")
|
||||
|
||||
def test_generic_get_or_create_when_created(self):
|
||||
"""
|
||||
Should be able to use get_or_create from the generic related manager
|
||||
@@ -96,6 +112,18 @@ class GenericRelationsTests(TestCase):
|
||||
# shouldn't had changed the tag
|
||||
self.assertEqual(tag.tag, "stinky")
|
||||
|
||||
async def test_generic_async_aget_or_create(self):
|
||||
tag, created = await self.bacon.tags.aget_or_create(
|
||||
id=self.fatty.id, defaults={"tag": "orange"}
|
||||
)
|
||||
self.assertIs(created, False)
|
||||
self.assertEqual(tag.tag, "fatty")
|
||||
self.assertEqual(await self.bacon.tags.acount(), 2)
|
||||
tag, created = await self.bacon.tags.aget_or_create(tag="orange")
|
||||
self.assertIs(created, True)
|
||||
self.assertEqual(await self.bacon.tags.acount(), 3)
|
||||
self.assertEqual(tag.tag, "orange")
|
||||
|
||||
def test_generic_relations_m2m_mimic(self):
|
||||
"""
|
||||
Objects with declared GenericRelations can be tagged directly -- the
|
||||
|
||||
Reference in New Issue
Block a user