mirror of
https://github.com/django/django.git
synced 2024-11-24 02:47:35 +01:00
f92641a636
The from_queryset parameter can be used to: - use a custom Manager - lock the row until the end of transaction - select additional related objects
37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from django.test import TestCase
|
|
|
|
from .models import SimpleModel
|
|
|
|
|
|
class AsyncModelOperationTest(TestCase):
|
|
@classmethod
|
|
def setUpTestData(cls):
|
|
cls.s1 = SimpleModel.objects.create(field=0)
|
|
|
|
async def test_asave(self):
|
|
self.s1.field = 10
|
|
await self.s1.asave()
|
|
refetched = await SimpleModel.objects.aget()
|
|
self.assertEqual(refetched.field, 10)
|
|
|
|
async def test_adelete(self):
|
|
await self.s1.adelete()
|
|
count = await SimpleModel.objects.acount()
|
|
self.assertEqual(count, 0)
|
|
|
|
async def test_arefresh_from_db(self):
|
|
await SimpleModel.objects.filter(pk=self.s1.pk).aupdate(field=20)
|
|
await self.s1.arefresh_from_db()
|
|
self.assertEqual(self.s1.field, 20)
|
|
|
|
async def test_arefresh_from_db_from_queryset(self):
|
|
await SimpleModel.objects.filter(pk=self.s1.pk).aupdate(field=20)
|
|
with self.assertRaises(SimpleModel.DoesNotExist):
|
|
await self.s1.arefresh_from_db(
|
|
from_queryset=SimpleModel.objects.filter(field=0)
|
|
)
|
|
await self.s1.arefresh_from_db(
|
|
from_queryset=SimpleModel.objects.filter(field__gt=0)
|
|
)
|
|
self.assertEqual(self.s1.field, 20)
|