-
-
Notifications
You must be signed in to change notification settings - Fork 69
fix: register session listeners in framework extension configs (#709) #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hasansezertasan
wants to merge
27
commits into
litestar-org:main
Choose a base branch
from
hasansezertasan:fix/extension-create-session-maker-listeners
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
83fda8b
test: add failing listener-registration tests for Litestar async config
hasansezertasan c536111
test: add failing listener-registration tests for Litestar sync config
hasansezertasan b27febe
test: add failing listener-registration tests for Starlette config
hasansezertasan 4a0122d
test: add failing listener-registration tests for Sanic config
hasansezertasan 76f56f0
test: add failing listener-registration tests for Flask config
hasansezertasan d2d46b4
fix(litestar): delegate create_session_maker to super for listener wi…
hasansezertasan a81b484
fix(litestar): delegate sync create_session_maker to super for listen…
hasansezertasan b08c820
fix(starlette): delegate async create_session_maker to super for list…
hasansezertasan 3ffc3eb
fix(starlette): delegate sync create_session_maker to super for liste…
hasansezertasan f33ea90
fix(sanic): delegate async create_session_maker to super for listener…
hasansezertasan 07b034d
fix(sanic): delegate sync create_session_maker to super for listener …
hasansezertasan 103b6f3
fix(flask): delegate sync create_session_maker to super for listener …
hasansezertasan 1259ceb
fix(flask): delegate async create_session_maker to super for listener…
hasansezertasan 7207763
test: pre-populate engine_instance to isolate listener-count assertions
hasansezertasan 7568fbd
fix(starlette): restore engine_instance pre-step in create_session_maker
hasansezertasan 5880d37
fix(sanic): restore engine_instance pre-step in create_session_maker
hasansezertasan bd22f74
fix(flask): restore engine_instance pre-step in create_session_maker
hasansezertasan efe8ef8
test: strengthen Starlette/Sanic/Flask async listener assertions
hasansezertasan a393376
test: extract shared listener-contract helpers for extension tests
hasansezertasan 71d2651
test: pin FastAPI listener-registration contract (#709)
hasansezertasan 46badd7
Update tests/unit/test_extensions/_listener_contract.py
hasansezertasan 751a86a
Update advanced_alchemy/extensions/flask/config.py
hasansezertasan 65a2d17
Update advanced_alchemy/extensions/litestar/plugins/init/config/async…
hasansezertasan f169d58
fix(ci): stabilize uv exclude-newer timestamp and drop blank line
hasansezertasan 2f3ab18
fix(routing): close routing session-maker engines on extension teardown
hasansezertasan 0dd2999
fix(starlette): wrap dispose_session_maker for run_in_threadpool typing
hasansezertasan 993fa4d
Merge branch 'main' into fix/extension-create-session-maker-listeners
hasansezertasan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| """Shared helpers for extension listener-registration contract tests. | ||
|
|
||
| Used by ``test_flask_listeners.py``, ``test_sanic_listeners.py``, and | ||
| ``test_starlette_listeners.py`` to eliminate duplication. Each extension's | ||
| async/sync config overrides ``create_session_maker``; these helpers patch | ||
| the base class's ``create_session_maker`` and ``sqlalchemy.event.listen``, | ||
| then assert the middle-layer listener-registration contract per subclass. | ||
|
|
||
| Regression helpers for | ||
| https://github.com/litestar-org/advanced-alchemy/issues/709. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
hasansezertasan marked this conversation as resolved.
Outdated
|
||
|
|
||
| from typing import Any | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from sqlalchemy.ext.asyncio import async_sessionmaker | ||
| from sqlalchemy.orm import sessionmaker | ||
|
|
||
| from advanced_alchemy.config.common import GenericSQLAlchemyConfig | ||
|
|
||
| __all__ = ( | ||
| "assert_async_file_object_listener_disabled", | ||
| "assert_async_registers_all_listeners", | ||
| "assert_async_timestamp_listener_disabled", | ||
| "assert_sync_file_object_listener_disabled", | ||
| "assert_sync_registers_all_listeners", | ||
| "assert_sync_timestamp_listener_disabled", | ||
| ) | ||
|
|
||
| _ASYNC_URL = "sqlite+aiosqlite:///" | ||
| _SYNC_URL = "sqlite:///" | ||
|
|
||
|
|
||
| def _make_async_config(config_cls: type, **kwargs: Any) -> Any: | ||
| """Build an async config with ``engine_instance`` pre-populated. | ||
|
|
||
| Pre-populating ``engine_instance`` isolates listener-count assertions | ||
| from the aiosqlite dialect's internal ``event.listen`` calls that | ||
| would otherwise fire during ``get_engine()``. | ||
| """ | ||
| config = config_cls(connection_string=_ASYNC_URL, **kwargs) | ||
| config.engine_instance = MagicMock() | ||
| return config | ||
|
|
||
|
|
||
| def _make_sync_config(config_cls: type, **kwargs: Any) -> Any: | ||
| """Build a sync config with ``engine_instance`` pre-populated.""" | ||
| config = config_cls(connection_string=_SYNC_URL, **kwargs) | ||
| config.engine_instance = MagicMock() | ||
| return config | ||
|
|
||
|
|
||
| def assert_async_registers_all_listeners(config_cls: type) -> None: | ||
| """Default async config registers 6 listeners on the synthetic sync_maker.""" | ||
| mock_session_maker = MagicMock(spec=async_sessionmaker) | ||
| config = _make_async_config(config_cls) | ||
|
|
||
| with ( | ||
| patch.object( | ||
| GenericSQLAlchemyConfig, | ||
| "create_session_maker", | ||
| return_value=mock_session_maker, | ||
| ), | ||
| patch("sqlalchemy.event.listen") as mock_listen, | ||
| patch("advanced_alchemy.config.asyncio.sync_sessionmaker") as mock_sync_factory, | ||
| ): | ||
| mock_sync_maker = MagicMock() | ||
| mock_sync_factory.return_value = mock_sync_maker | ||
| result = config.create_session_maker() | ||
|
|
||
| assert result is mock_session_maker | ||
| assert mock_listen.call_count == 6 | ||
| mock_session_maker.configure.assert_called_once_with(sync_session_class=mock_sync_maker) | ||
| for call in mock_listen.call_args_list: | ||
| assert call.args[0] is mock_sync_maker | ||
| listener_events = {c.args[1] for c in mock_listen.call_args_list} | ||
| assert {"before_flush", "after_commit", "after_rollback"} <= listener_events | ||
|
|
||
|
|
||
| def assert_async_file_object_listener_disabled(config_cls: type) -> None: | ||
| """With file-object listener disabled, only timestamp + cache listeners register (3).""" | ||
| mock_session_maker = MagicMock(spec=async_sessionmaker) | ||
| config = _make_async_config(config_cls, enable_file_object_listener=False) | ||
|
|
||
| with ( | ||
| patch.object( | ||
| GenericSQLAlchemyConfig, | ||
| "create_session_maker", | ||
| return_value=mock_session_maker, | ||
| ), | ||
| patch("sqlalchemy.event.listen") as mock_listen, | ||
| patch("advanced_alchemy.config.asyncio.sync_sessionmaker"), | ||
| ): | ||
| config.create_session_maker() | ||
|
|
||
| assert mock_listen.call_count == 3 | ||
|
|
||
|
|
||
| def assert_async_timestamp_listener_disabled(config_cls: type) -> None: | ||
| """With timestamp listener disabled, only file-object + cache listeners register (5).""" | ||
| mock_session_maker = MagicMock(spec=async_sessionmaker) | ||
| config = _make_async_config(config_cls, enable_touch_updated_timestamp_listener=False) | ||
|
|
||
| with ( | ||
| patch.object( | ||
| GenericSQLAlchemyConfig, | ||
| "create_session_maker", | ||
| return_value=mock_session_maker, | ||
| ), | ||
| patch("sqlalchemy.event.listen") as mock_listen, | ||
| patch("advanced_alchemy.config.asyncio.sync_sessionmaker"), | ||
| ): | ||
| config.create_session_maker() | ||
|
|
||
| assert mock_listen.call_count == 5 | ||
|
|
||
|
|
||
| def assert_sync_registers_all_listeners(config_cls: type) -> None: | ||
| """Default sync config registers 6 listeners directly on the session_maker.""" | ||
| mock_session_maker = MagicMock(spec=sessionmaker) | ||
| config = _make_sync_config(config_cls) | ||
|
|
||
| with ( | ||
| patch.object( | ||
| GenericSQLAlchemyConfig, | ||
| "create_session_maker", | ||
| return_value=mock_session_maker, | ||
| ), | ||
| patch("sqlalchemy.event.listen") as mock_listen, | ||
| ): | ||
| result = config.create_session_maker() | ||
|
|
||
| assert result is mock_session_maker | ||
| assert mock_listen.call_count == 6 | ||
|
|
||
|
|
||
| def assert_sync_file_object_listener_disabled(config_cls: type) -> None: | ||
| """With file-object listener disabled, only timestamp + cache listeners register (3).""" | ||
| mock_session_maker = MagicMock(spec=sessionmaker) | ||
| config = _make_sync_config(config_cls, enable_file_object_listener=False) | ||
|
|
||
| with ( | ||
| patch.object( | ||
| GenericSQLAlchemyConfig, | ||
| "create_session_maker", | ||
| return_value=mock_session_maker, | ||
| ), | ||
| patch("sqlalchemy.event.listen") as mock_listen, | ||
| ): | ||
| config.create_session_maker() | ||
|
|
||
| assert mock_listen.call_count == 3 | ||
|
|
||
|
|
||
| def assert_sync_timestamp_listener_disabled(config_cls: type) -> None: | ||
| """With timestamp listener disabled, only file-object + cache listeners register (5).""" | ||
| mock_session_maker = MagicMock(spec=sessionmaker) | ||
| config = _make_sync_config(config_cls, enable_touch_updated_timestamp_listener=False) | ||
|
|
||
| with ( | ||
| patch.object( | ||
| GenericSQLAlchemyConfig, | ||
| "create_session_maker", | ||
| return_value=mock_session_maker, | ||
| ), | ||
| patch("sqlalchemy.event.listen") as mock_listen, | ||
| ): | ||
| config.create_session_maker() | ||
|
|
||
| assert mock_listen.call_count == 5 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.