Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 5.2.1

* Fix count for paginate.

## Version 5.2.0

* Remove actions classes.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
14 changes: 7 additions & 7 deletions src/db_first/dbal/paginate.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions tests/dbal/test_dbal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pagination_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand Down
Loading