Fix Limit 0 QuerySet Empty List (#767)

* fix: debug condition check not none limit count

* test: write a test for check zero limit empty list

* fix: debug assert check empty list with is keyword
This commit is contained in:
Sepehr Bazyar
2022-07-29 19:53:10 +04:30
committed by GitHub
parent 8450c90420
commit e923513681
3 changed files with 16 additions and 2 deletions

View File

@ -20,6 +20,8 @@ class LimitQuery:
:return: modified query :return: modified query
:rtype: sqlalchemy.sql.selectable.Select :rtype: sqlalchemy.sql.selectable.Select
""" """
if self.limit_count:
if self.limit_count is not None:
expr = expr.limit(self.limit_count) expr = expr.limit(self.limit_count)
return expr return expr

View File

@ -282,10 +282,10 @@ class QuerySet(Generic[T]):
filter_clauses=self.filter_clauses, filter_clauses=self.filter_clauses,
exclude_clauses=self.exclude_clauses, exclude_clauses=self.exclude_clauses,
offset=offset or self.query_offset, offset=offset or self.query_offset,
limit_count=limit or self.limit_count,
excludable=self._excludable, excludable=self._excludable,
order_bys=order_bys or self.order_bys, order_bys=order_bys or self.order_bys,
limit_raw_sql=self.limit_sql_raw, limit_raw_sql=self.limit_sql_raw,
limit_count=limit if limit is not None else self.limit_count,
) )
exp = qry.build_select_expression() exp = qry.build_select_expression()
# print("\n", exp.compile(compile_kwargs={"literal_binds": True})) # print("\n", exp.compile(compile_kwargs={"literal_binds": True}))

View File

@ -47,6 +47,18 @@ def create_test_database():
metadata.drop_all(engine) metadata.drop_all(engine)
@pytest.mark.asyncio
async def test_limit_zero():
async with database:
async with database.transaction(force_rollback=True):
for i in range(5):
await Car(name=f"{i}").save()
cars = await Car.objects.limit(0).all()
assert cars == []
assert len(cars) == 0
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_pagination_errors(): async def test_pagination_errors():
async with database: async with database: