|
2 | 2 |
|
3 | 3 | import random |
4 | 4 | from typing import TYPE_CHECKING |
5 | | -from unittest.mock import MagicMock |
| 5 | +from unittest.mock import MagicMock, patch |
6 | 6 |
|
7 | 7 | from litestar import Litestar, get |
8 | 8 | from litestar.testing import create_test_client |
9 | 9 | from litestar.types.asgi_types import HTTPResponseStartEvent |
10 | 10 | from litestar.utils import set_litestar_scope_state |
| 11 | +from pytest import MonkeyPatch |
11 | 12 | from sqlalchemy.orm import Session |
12 | 13 |
|
13 | 14 | from advanced_alchemy.extensions.litestar.plugins import ( |
@@ -45,6 +46,30 @@ def test_handler(db_session: Session, scope: Scope) -> None: |
45 | 46 | assert config.session_dependency_key not in captured_scope_state |
46 | 47 |
|
47 | 48 |
|
| 49 | +def test_create_all_default(monkeypatch: MonkeyPatch) -> None: |
| 50 | + """Test default_before_send_handler.""" |
| 51 | + |
| 52 | + config = SQLAlchemySyncConfig(connection_string="sqlite+aiosqlite://") |
| 53 | + plugin = SQLAlchemyInitPlugin(config=config) |
| 54 | + with patch.object( |
| 55 | + config, |
| 56 | + "create_all_metadata", |
| 57 | + ) as create_all_metadata_mock, create_test_client(route_handlers=[], plugins=[plugin]) as _client: |
| 58 | + create_all_metadata_mock.assert_not_called() |
| 59 | + |
| 60 | + |
| 61 | +def test_create_all(monkeypatch: MonkeyPatch) -> None: |
| 62 | + """Test default_before_send_handler.""" |
| 63 | + |
| 64 | + config = SQLAlchemySyncConfig(connection_string="sqlite+aiosqlite://", create_all=True) |
| 65 | + plugin = SQLAlchemyInitPlugin(config=config) |
| 66 | + with patch.object( |
| 67 | + config, |
| 68 | + "create_all_metadata", |
| 69 | + ) as create_all_metadata_mock, create_test_client(route_handlers=[], plugins=[plugin]) as _client: |
| 70 | + create_all_metadata_mock.assert_called_once() |
| 71 | + |
| 72 | + |
48 | 73 | def test_before_send_handler_success_response(create_scope: Callable[..., Scope]) -> None: |
49 | 74 | """Test that the session is committed given a success response.""" |
50 | 75 | config = SQLAlchemySyncConfig(connection_string="sqlite://", before_send_handler=autocommit_before_send_handler) |
|
0 commit comments