Skip to content

Commit 92d82d8

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents ff3fef0 + b33a2e7 commit 92d82d8

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ This is an on going projects, not all model methods are ported.
7676

7777
### Manager:
7878

79-
| methods | supported | comments |
80-
|----------------------------|------------|----------|
79+
| methods | supported | comments |
80+
|------------------------------------------|------------|----------|
8181
| `Model.objects.async_get` || |
8282
| `Model.objects.async_create` || |
83+
| `Model.objects.async_count` || |
84+
| `Model.objects.async_none` || |
8385
| `Model.objects.async_bulk_create` || |
8486
| `Model.objects.async_bulk_update` || |
8587
| `Model.objects.async_get_or_create` || |

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: 40 additions & 24 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,16 +43,28 @@ 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):
5952
self.assertTrue(False, "Not Implemented")
6053

61-
@tag('dev')
54+
@tag('ci')
6255
async def test_async_get_or_create(self):
63-
self.assertTrue(False, "Not Implemented")
56+
57+
async def test_async_get_or_create_on_obj_get(self):
58+
obj = await TestModel.objects.async_get_or_create(name="setup 1")
59+
self.assertEqual(obj[1], False)
60+
61+
async def test_async_get_or_create_on_obj_create(self):
62+
obj = await TestModel.objects.async_get_or_create(name="setup 3")
63+
self.assertEqual(obj[0].name, "setup 3")
64+
self.assertEqual(obj[1], True)
65+
66+
await test_async_get_or_create_on_obj_get(self)
67+
await test_async_get_or_create_on_obj_create(self)
6468

6569
@tag('dev')
6670
async def test_async_update_or_create(self):
@@ -96,29 +100,29 @@ async def test_async_in_bulk(self):
96100

97101
@tag('ci')
98102
async def test_async_delete(self):
99-
100103
created = await TestModel.objects.async_create(name="to delete")
101104
all_created = await TestModel.objects.async_all()
102-
self.assertEqual(len(all_created), 3)
105+
count = await all_created.async_count()
106+
self.assertEqual(count, 3)
103107

104108
await all_created.async_delete()
105109
all_after_delete = await TestModel.objects.async_all()
106-
self.assertEqual(len(all_after_delete), 0)
110+
count = await all_after_delete.async_count()
111+
self.assertEqual(count, 0)
107112

108113
@tag('ci')
109114
async def test_async_update(self):
110115
created = await TestModel.objects.async_create(name="to update")
111116
qs = await TestModel.objects.async_filter(name="to update")
112117
updated = await qs.async_update(name="updated")
113-
114118
self.assertEqual(updated, 1)
115119

116120
@tag('ci')
117121
async def test_async_exists(self):
118122
qs = await TestModel.objects.async_filter(name='setup 1')
119123
exists = await qs.async_exists()
120124
self.assertTrue(exists)
121-
125+
122126
@tag('ci')
123127
async def test_async_explain(self):
124128
explained = await (await TestModel.objects.async_filter(name="setup 1")).async_explain()
@@ -129,11 +133,12 @@ async def test_async_explain(self):
129133
async def test_async_raw(self):
130134
rs = await TestModel.objects.async_raw('SELECT * from tests_testmodel')
131135
print(list(rs))
132-
133-
@tag('dev')
136+
137+
@tag('ci')
134138
async def test_async_count(self):
135139
result = await TestModel.objects.async_all()
136-
self.assertEqual(result.count(), 1)
140+
result = await result.async_count()
141+
self.assertEqual(result, 2)
137142

138143
@tag('ci')
139144
async def test_async_none(self):
@@ -155,7 +160,8 @@ async def test_async_fetch_all(self):
155160
@tag('ci')
156161
async def test_async_all(self):
157162
result = await TestModel.objects.async_all()
158-
self.assertEqual(len(result), 2)
163+
result = await result.async_count()
164+
self.assertEqual(result, 2)
159165

160166
@tag('ci')
161167
async def test_async_filter(self):
@@ -198,9 +204,19 @@ async def test_async_prefetch_related(self):
198204
async def test_async_annotate(self):
199205
self.assertTrue(False, "Not Implemented")
200206

201-
@tag('dev')
202-
async def test_async_order_by(self):
203-
self.assertTrue(False, "Not Implemented")
207+
@tag('ci')
208+
async def test_async_order_by_ascending(self):
209+
qs = await TestModel.objects.async_all()
210+
qs = await qs.async_order_by('name')
211+
qs = await qs.async_first()
212+
self.assertEqual(qs.name, "setup 1")
213+
214+
@tag('ci')
215+
async def test_async_order_by_descending(self):
216+
qs = await TestModel.objects.async_all()
217+
qs = await qs.async_order_by('-name')
218+
qs = await qs.async_first()
219+
self.assertEqual(qs.name, "setup 2")
204220

205221
@tag('dev')
206222
async def test_async_distinct(self):

0 commit comments

Comments
 (0)