From f14a81b0cad1807d42f1f429c03737a5acef96b4 Mon Sep 17 00:00:00 2001 From: Konstantin Fadeev Date: Fri, 27 Mar 2026 11:57:55 +0300 Subject: [PATCH 1/2] Fix count for paginate. --- src/db_first/dbal/paginate.py | 14 +++++++------- tests/dbal/test_dbal.py | 4 +--- tests/test_pagination_mixin.py | 12 ++++++++++++ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/db_first/dbal/paginate.py b/src/db_first/dbal/paginate.py index 8554dc3..8388ae0 100644 --- a/src/db_first/dbal/paginate.py +++ b/src/db_first/dbal/paginate.py @@ -1,7 +1,6 @@ from math import ceil from typing import Any -import sqlalchemy as sa from db_first.dbal.exceptions import DBALPaginateException from db_first.statement_maker import StatementMaker from sqlalchemy import func @@ -56,10 +55,6 @@ def query_string_to_sql_json( return sql_as_json - def run_query(self, **data: dict[str, Any]): - stmt = StatementMaker(self._model, **data).make_stmt() - return self._session.scalars(stmt).all() - def paginate( self, ids: list[str] | None = None, @@ -77,13 +72,18 @@ def paginate( sql_as_json = self.query_string_to_sql_json(ids=ids, page=page, per_page=per_page, **data) - items = self.run_query(**sql_as_json) + statement = StatementMaker(self._model, **sql_as_json).make_stmt() + items = self._session.scalars(statement).all() result = {'items': items} if include_metadata: total = self._session.scalar( - sa.select(func.count()).select_from(self._model).order_by(None) + statement.with_only_columns(func.count()) + .select_from(self._model) + .order_by(None) + .limit(None) + .offset(None) ) pages = ceil(total / per_page) diff --git a/tests/dbal/test_dbal.py b/tests/dbal/test_dbal.py index b587591..3e21996 100644 --- a/tests/dbal/test_dbal.py +++ b/tests/dbal/test_dbal.py @@ -20,9 +20,7 @@ def test_dbal__bulk_create(fx_db, fx_parent_dbal): data = [{'first': next(UNIQUE_STRING)}] fx_parent_dbal(session_db).bulk_create(data) - result = fx_parent_dbal(session_db).run_query( - where={'and': [{'col': 'first', 'opr': 'eq', 'value': data[0]['first']}]} - ) + result = fx_parent_dbal(session_db).read_filtered_list(first=data[0]['first']) assert result[0].id assert result[0].first == data[0]['first'] diff --git a/tests/test_pagination_mixin.py b/tests/test_pagination_mixin.py index 328bbe5..c781918 100644 --- a/tests/test_pagination_mixin.py +++ b/tests/test_pagination_mixin.py @@ -139,6 +139,18 @@ def test_pagination__get_pages(fx_parent__create, fx_parent__paginate, page, per assert len(items['items']) == per_page +def test_pagination__count_pages_total(fx_parent__create, fx_parent__paginate): + item_1 = fx_parent__create({'first': next(UNIQUE_STRING)}) + fx_parent__create({'first': next(UNIQUE_STRING)}) + + data = {'eq__id': item_1.id, 'include_metadata': 'enable'} + items = fx_parent__paginate(data) + + assert items['_metadata']['pagination']['pages'] == 1 + assert items['_metadata']['pagination']['total'] == 1 + assert len(items['items']) == 1 + + def test_pagination__without_meta_pagination(fx_db, fx_parent__create, fx_parent__paginate): fx_parent__create({'first': next(UNIQUE_STRING)}) fx_parent__create({'first': next(UNIQUE_STRING)}) From 235e78d6fa750595e52a230058fc11a9d95f6a25 Mon Sep 17 00:00:00 2001 From: Konstantin Fadeev Date: Fri, 27 Mar 2026 12:04:18 +0300 Subject: [PATCH 2/2] Fix count for paginate. --- CHANGES.md | 4 ++++ pyproject.toml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 6d5ccef..891553a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +## Version 5.2.1 + +* Fix count for paginate. + ## Version 5.2.0 * Remove actions classes. diff --git a/pyproject.toml b/pyproject.toml index 205cbcc..f3d515f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ license = {file = "LICENSE"} name = "DB-First" readme = "README.md" requires-python = ">=3.11" -version = "5.2.0" +version = "5.2.1" [project.optional-dependencies] dev = [