Skip to content

Commit d8ecdb4

Browse files
authored
fix(repo): logic correction for window function (#421)
Corrects the logic for using a count with a window function.
1 parent d363adf commit d8ecdb4

3 files changed

Lines changed: 5 additions & 7 deletions

File tree

advanced_alchemy/repository/_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,15 +1503,15 @@ async def list_and_count(
15031503
Returns:
15041504
Count of records returned by query, ignoring pagination.
15051505
"""
1506-
self.count_with_window_function = (
1506+
count_with_window_function = (
15071507
count_with_window_function if count_with_window_function is not None else self.count_with_window_function
15081508
)
15091509
self.uniquify = self._get_uniquify(uniquify)
15101510
error_messages = self._get_error_messages(
15111511
error_messages=error_messages,
15121512
default_messages=self.error_messages,
15131513
)
1514-
if self._dialect.name in {"spanner", "spanner+spanner"} or count_with_window_function:
1514+
if self._dialect.name in {"spanner", "spanner+spanner"} or not count_with_window_function:
15151515
return await self._list_and_count_basic(
15161516
*filters,
15171517
auto_expunge=auto_expunge,

advanced_alchemy/repository/_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,15 +1504,15 @@ def list_and_count(
15041504
Returns:
15051505
Count of records returned by query, ignoring pagination.
15061506
"""
1507-
self.count_with_window_function = (
1507+
count_with_window_function = (
15081508
count_with_window_function if count_with_window_function is not None else self.count_with_window_function
15091509
)
15101510
self.uniquify = self._get_uniquify(uniquify)
15111511
error_messages = self._get_error_messages(
15121512
error_messages=error_messages,
15131513
default_messages=self.error_messages,
15141514
)
1515-
if self._dialect.name in {"spanner", "spanner+spanner"} or count_with_window_function:
1515+
if self._dialect.name in {"spanner", "spanner+spanner"} or not count_with_window_function:
15161516
return self._list_and_count_basic(
15171517
*filters,
15181518
auto_expunge=auto_expunge,

tests/unit/test_repository.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,6 @@ async def test_sqlalchemy_repo_list_and_count(mock_repo: SQLAlchemyAsyncReposito
527527
"""Test expected method calls for list operation."""
528528
mock_instances = [MagicMock(), MagicMock()]
529529
mock_count = len(mock_instances)
530-
mocker.patch.object(mock_repo, "_list_and_count_basic", return_value=(mock_instances, mock_count))
531530
mocker.patch.object(mock_repo, "_list_and_count_window", return_value=(mock_instances, mock_count))
532531

533532
instances, instance_count = await maybe_async(mock_repo.list_and_count())
@@ -546,9 +545,8 @@ async def test_sqlalchemy_repo_list_and_count_basic(
546545
mock_instances = [MagicMock(), MagicMock()]
547546
mock_count = len(mock_instances)
548547
mocker.patch.object(mock_repo, "_list_and_count_basic", return_value=(mock_instances, mock_count))
549-
mocker.patch.object(mock_repo, "_list_and_count_window", return_value=(mock_instances, mock_count))
550548

551-
instances, instance_count = await maybe_async(mock_repo.list_and_count(count_with_window_function=True))
549+
instances, instance_count = await maybe_async(mock_repo.list_and_count(count_with_window_function=False))
552550

553551
assert instances == mock_instances
554552
assert instance_count == mock_count

0 commit comments

Comments
 (0)