Skip to content

Commit bc6bc52

Browse files
committed
add aio_scalar test
1 parent 89506c8 commit bc6bc52

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

peewee_async.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -707,13 +707,11 @@ async def aio_scalar(self, database, as_tuple=False):
707707
:return: result is the same as after sync ``query.scalar()`` call
708708
"""
709709
async def fetch_results(cursor):
710-
row = await cursor.fetchone()
711-
if row and not as_tuple:
712-
return row[0]
713-
else:
714-
return row
710+
return await cursor.fetchone()
711+
712+
rows = await database.aio_execute(self, fetch_results=fetch_results)
715713

716-
return await database.aio_execute(self, fetch_results=fetch_results)
714+
return rows[0] if rows and not as_tuple else rows
717715

718716
async def aio_get(self, database=None):
719717
clone = self.paginate(1, 1)

tests/aio_model/test_shortcuts.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import pytest
2+
from peewee import fn
23

34
from tests.conftest import dbs_all
4-
from tests.models import TestModel
5+
from tests.models import TestModel, IntegerTestModel
56

67

78
@dbs_all
@@ -28,3 +29,17 @@ async def test_aio_get_or_none(db):
2829

2930
result = await TestModel.aio_get_or_none(TestModel.text == "unknown")
3031
assert result is None
32+
33+
34+
@dbs_all
35+
async def test_aio_scalar(db):
36+
await IntegerTestModel.aio_create(num=1)
37+
await IntegerTestModel.aio_create(num=2)
38+
39+
assert await IntegerTestModel.select(fn.MAX(IntegerTestModel.num)).aio_scalar() == 2
40+
41+
assert await IntegerTestModel.select(
42+
fn.MAX(IntegerTestModel.num),fn.Min(IntegerTestModel.num)
43+
).aio_scalar(as_tuple=True) == (2, 1)
44+
45+
assert await TestModel.select().aio_scalar() is None

tests/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ class Meta:
6565
primary_key = peewee.CompositeKey('uuid', 'alpha')
6666

6767

68+
class IntegerTestModel(peewee_async.AioModel):
69+
__test__ = False # disable pytest warnings
70+
num = peewee.IntegerField()
71+
72+
6873
ALL_MODELS = (
6974
TestModel, UUIDTestModel, TestModelAlpha, TestModelBeta, TestModelGamma,
70-
CompatTestModel, CompositeTestModel
75+
CompatTestModel, CompositeTestModel, IntegerTestModel
7176
)

0 commit comments

Comments
 (0)