Skip to content

Commit 3c2aea6

Browse files
authored
Merge branch 'main' into feature/sort-nested-relationships
2 parents 100a875 + 4ed7bee commit 3c2aea6

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

fastcrud/crud/fast_crud.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,12 +2225,7 @@ async def get_multi_by_cursor(
22252225
result = await db.execute(stmt)
22262226
data = [dict(row) for row in result.mappings()]
22272227

2228-
next_cursor = None
2229-
if len(data) == limit:
2230-
if sort_order == "asc":
2231-
next_cursor = data[-1][sort_column]
2232-
else:
2233-
data[0][sort_column]
2228+
next_cursor = data[-1][sort_column] if len(data) == limit else None
22342229

22352230
return {"data": data, "next_cursor": next_cursor}
22362231

tests/sqlalchemy/crud/test_get_multi_by_cursor.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,47 @@ async def test_get_multi_by_cursor_desc_with_cursor_filter(async_session, test_d
169169
assert (
170170
record["id"] < first_page_last_id
171171
), "Each ID in the second page should be less than the last ID of the first page"
172+
173+
174+
@pytest.mark.asyncio
175+
async def test_get_multi_by_cursor_descending_order(async_session, test_data):
176+
for item in test_data:
177+
async_session.add(ModelTest(**item))
178+
await async_session.commit()
179+
180+
crud = FastCRUD(ModelTest)
181+
182+
first_page = await crud.get_multi_by_cursor(
183+
db=async_session,
184+
limit=2,
185+
sort_column="id",
186+
sort_order="desc"
187+
)
188+
next_cursor = first_page["next_cursor"]
189+
item1, item2 = first_page["data"][0]["id"], first_page["data"][1]["id"]
190+
191+
assert len(first_page["data"]) == 2
192+
assert first_page["data"][0]["id"] == 11 # Should start with highest ID
193+
assert first_page["data"][1]["id"] == 10
194+
assert first_page["next_cursor"] == 10 # Next cursor should be the last ID in the result
195+
196+
while next_cursor is not None:
197+
next_page = await crud.get_multi_by_cursor(
198+
db=async_session,
199+
cursor=next_cursor,
200+
limit=2,
201+
sort_column="id",
202+
sort_order="desc"
203+
)
204+
next_cursor = next_page["next_cursor"]
205+
if len(next_page["data"]) == 2:
206+
assert next_page["data"][0]["id"] == item1 - 2
207+
assert next_page["data"][1]["id"] == item2 - 2
208+
assert next_cursor == item2 - 2
209+
item1 -= 2
210+
item2 -= 2
211+
else:
212+
assert next_page["data"][0]["id"] == item1 - 2
213+
assert next_cursor is None
214+
215+

0 commit comments

Comments
 (0)