Skip to content

Commit f365790

Browse files
committed
Count feature, related test and import optimisation
1 parent 60c7f0b commit f365790

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

django_async_orm/query.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ async def async_update(self, **kwargs):
6060
async def async_exists(self):
6161
return await sync_to_async(self.exists, thread_sensitive=True)()
6262

63+
async def async_count(self):
64+
return await sync_to_async(self.count, thread_sensitive=True)()
65+
6366
async def async_explain(self, *_, format=None, **options):
6467
return await sync_to_async(self.explain, thread_sensitive=True)(*_, format=format, **options)
6568

tests/test_django_async_orm.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
import pdb
2-
import asyncio
3-
41
from django.test import TestCase, tag, TransactionTestCase
5-
from django.conf import settings
62
from django.apps import apps
73
from unittest import IsolatedAsyncioTestCase
8-
import time
9-
104
from .models import TestModel
115

126

13-
147
class AppLoadingTestCase(TestCase):
158

169
@tag('ci')
1710
def test_dao_loaded(self):
1811
self.assertTrue(apps.is_installed('django_async_orm'))
1912

20-
2113
@tag('ci')
2214
def test_manager_is_async(self):
2315
manager_class_name = TestModel.objects.__class__.__name__
@@ -51,8 +43,9 @@ async def test_async_bulk_create(self):
5143
TestModel(name='bulk create 1'),
5244
TestModel(name='bulk create 2'),
5345
])
54-
55-
self.assertEqual(len(objs), 2)
46+
objs = await TestModel.objects.async_all()
47+
objs = await objs.async_count()
48+
self.assertEqual(objs, 4)
5649

5750
@tag('dev')
5851
async def test_async_bulk_update(self):
@@ -96,14 +89,15 @@ async def test_async_in_bulk(self):
9689

9790
@tag('ci')
9891
async def test_async_delete(self):
99-
10092
created = await TestModel.objects.async_create(name="to delete")
10193
all_created = await TestModel.objects.async_all()
102-
self.assertEqual(len(all_created), 3)
94+
count = await all_created.async_count()
95+
self.assertEqual(count, 3)
10396

10497
await all_created.async_delete()
10598
all_after_delete = await TestModel.objects.async_all()
106-
self.assertEqual(len(all_after_delete), 0)
99+
count = await all_created.async_count()
100+
self.assertEqual(count, 0)
107101

108102
@tag('ci')
109103
async def test_async_update(self):
@@ -130,10 +124,11 @@ async def test_async_raw(self):
130124
rs = await TestModel.objects.async_raw('SELECT * from tests_testmodel')
131125
print(list(rs))
132126

133-
@tag('dev')
127+
@tag('ci')
134128
async def test_async_count(self):
135129
result = await TestModel.objects.async_all()
136-
self.assertEqual(result.count(), 1)
130+
result = await result.async_count()
131+
self.assertEqual(result, 2)
137132

138133
@tag('ci')
139134
async def test_async_none(self):
@@ -155,7 +150,8 @@ async def test_async_fetch_all(self):
155150
@tag('ci')
156151
async def test_async_all(self):
157152
result = await TestModel.objects.async_all()
158-
self.assertEqual(len(result), 2)
153+
result = await result.async_count()
154+
self.assertEqual(result, 2)
159155

160156
@tag('ci')
161157
async def test_async_filter(self):

0 commit comments

Comments
 (0)