diff --git a/peewee_async.py b/peewee_async.py index 898c9f1..de4aa87 100644 --- a/peewee_async.py +++ b/peewee_async.py @@ -707,13 +707,11 @@ async def aio_scalar(self, database, as_tuple=False): :return: result is the same as after sync ``query.scalar()`` call """ async def fetch_results(cursor): - row = await cursor.fetchone() - if row and not as_tuple: - return row[0] - else: - return row + return await cursor.fetchone() + + rows = await database.aio_execute(self, fetch_results=fetch_results) - return await database.aio_execute(self, fetch_results=fetch_results) + return rows[0] if rows and not as_tuple else rows async def aio_get(self, database=None): clone = self.paginate(1, 1) diff --git a/tests/aio_model/test_shortcuts.py b/tests/aio_model/test_shortcuts.py index 40e6c6d..0ec756b 100644 --- a/tests/aio_model/test_shortcuts.py +++ b/tests/aio_model/test_shortcuts.py @@ -1,7 +1,8 @@ import pytest +from peewee import fn from tests.conftest import dbs_all -from tests.models import TestModel +from tests.models import TestModel, IntegerTestModel @dbs_all @@ -28,3 +29,17 @@ async def test_aio_get_or_none(db): result = await TestModel.aio_get_or_none(TestModel.text == "unknown") assert result is None + + +@dbs_all +async def test_aio_scalar(db): + await IntegerTestModel.aio_create(num=1) + await IntegerTestModel.aio_create(num=2) + + assert await IntegerTestModel.select(fn.MAX(IntegerTestModel.num)).aio_scalar() == 2 + + assert await IntegerTestModel.select( + fn.MAX(IntegerTestModel.num),fn.Min(IntegerTestModel.num) + ).aio_scalar(as_tuple=True) == (2, 1) + + assert await TestModel.select().aio_scalar() is None diff --git a/tests/models.py b/tests/models.py index daab497..5013221 100644 --- a/tests/models.py +++ b/tests/models.py @@ -65,7 +65,12 @@ class Meta: primary_key = peewee.CompositeKey('uuid', 'alpha') +class IntegerTestModel(peewee_async.AioModel): + __test__ = False # disable pytest warnings + num = peewee.IntegerField() + + ALL_MODELS = ( TestModel, UUIDTestModel, TestModelAlpha, TestModelBeta, TestModelGamma, - CompatTestModel, CompositeTestModel + CompatTestModel, CompositeTestModel, IntegerTestModel )