Skip to content

Commit

Permalink
feat: adds additional coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin committed Sep 22, 2024
1 parent e693b2b commit 4f12367
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 28 deletions.
43 changes: 27 additions & 16 deletions litestar/contrib/sqlalchemy/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ruff: noqa: TCH004, F401
# pyright: reportUnusedImport=false
"""Application ORM configuration."""

# ruff: noqa: TCH004, F401
from __future__ import annotations

from typing import TYPE_CHECKING
Expand All @@ -24,36 +25,46 @@


def __getattr__(attr_name: str) -> object:
from advanced_alchemy import base # pyright: ignore[reportMissingImports,reportUnusedImport]

try:
# v0.6.0+
from advanced_alchemy._listeners import touch_updated_timestamp # pyright: ignore
except ImportError:
from advanced_alchemy.base import touch_updated_timestamp # type: ignore[no-redef,attr-defined]

if attr_name in __all__:
if attr_name == "touch_updated_timestamp":
try:
# v0.6.0+
from advanced_alchemy._listeners import touch_updated_timestamp # pyright: ignore
except ImportError:
from advanced_alchemy.base import touch_updated_timestamp # type: ignore[no-redef,attr-defined]
warn_deprecation(
deprecated_name=f"litestar.contrib.sqlalchemy.{attr_name}",
deprecated_name=f"litestar.contrib.sqlalchemy.base.{attr_name}",
version="2.12",
kind="import",
removal_in="3.0",
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy' is deprecated, please"
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.base' is deprecated, please"
f"see the 'Advanced Alchemy' documentation for more details on how to use '{attr_name}' instead",
)
value = globals()[attr_name] = touch_updated_timestamp # pyright: ignore[reportUnknownVariableType]
value = globals()[attr_name] = locals()[attr_name] # pyright: ignore[reportUnknownVariableType]
return value # pyright: ignore[reportUnknownVariableType]
from advanced_alchemy.base import ( # pyright: ignore[reportMissingImports]
AuditColumns,
BigIntAuditBase,
BigIntBase,
BigIntPrimaryKey,
CommonTableAttributes,
ModelProtocol,
UUIDAuditBase,
UUIDBase,
UUIDPrimaryKey,
create_registry,
orm_registry,
)

warn_deprecation(
deprecated_name=f"litestar.contrib.sqlalchemy.{attr_name}",
deprecated_name=f"litestar.contrib.sqlalchemy.base.{attr_name}",
version="2.12",
kind="import",
removal_in="3.0",
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy' is deprecated, please"
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.base' is deprecated, please"
f"import it from 'litestar.plugins.sqlalchemy.base.{attr_name}' instead",
)

value = globals()[attr_name] = getattr(base, attr_name)
value = globals()[attr_name] = locals()[attr_name] # pyright: ignore[reportUnknownVariableType]
return value

raise AttributeError(f"module {__name__!r} has no attribute {attr_name!r}")
Expand Down
13 changes: 7 additions & 6 deletions litestar/contrib/sqlalchemy/dto.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ruff: noqa: TCH004, F401
# pyright: reportUnusedImport=false
"""SQLAlchemy DTO configuration."""

# ruff: noqa: TCH004, F401
from __future__ import annotations

from typing import TYPE_CHECKING
Expand All @@ -13,16 +14,16 @@
def __getattr__(attr_name: str) -> object:
if attr_name in __all__:
from advanced_alchemy.extensions.litestar.dto import (
SQLAlchemyDTO, # pyright: ignore[reportMissingImports,reportUnusedImport]
SQLAlchemyDTOConfig, # pyright: ignore[reportMissingImports,reportUnusedImport]
SQLAlchemyDTO, # pyright: ignore[reportMissingImports]
SQLAlchemyDTOConfig, # pyright: ignore[reportMissingImports]
)

warn_deprecation(
deprecated_name=f"litestar.contrib.sqlalchemy.{attr_name}",
version="2.12",
kind="import",
removal_in="3.0",
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy' is deprecated, please "
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.dto' is deprecated, please "
f"import it from 'litestar.plugins.sqlalchemy.dto' instead",
)
value = globals()[attr_name] = locals()[attr_name]
Expand All @@ -33,6 +34,6 @@ def __getattr__(attr_name: str) -> object:

if TYPE_CHECKING:
from advanced_alchemy.extensions.litestar.dto import (
SQLAlchemyDTO, # pyright: ignore[reportMissingImports,reportUnusedImport]
SQLAlchemyDTOConfig, # pyright: ignore[reportMissingImports,reportUnusedImport]
SQLAlchemyDTO, # pyright: ignore[reportMissingImports]
SQLAlchemyDTOConfig, # pyright: ignore[reportMissingImports]
)
5 changes: 3 additions & 2 deletions litestar/contrib/sqlalchemy/plugins/init/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# ruff: noqa: TCH004
# ruff: noqa: TCH004, F401
# pyright: reportUnusedImport=false
from __future__ import annotations

from typing import TYPE_CHECKING
Expand All @@ -20,7 +21,7 @@ def __getattr__(attr_name: str) -> object:
)
from advanced_alchemy.extensions.litestar import SQLAlchemyInitPlugin

value = globals()[attr_name] = SQLAlchemyInitPlugin
value = globals()[attr_name] = locals()[attr_name]
return value

raise AttributeError(f"module {__name__!r} has no attribute {attr_name!r}")
Expand Down
4 changes: 2 additions & 2 deletions litestar/contrib/sqlalchemy/plugins/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
def __getattr__(attr_name: str) -> object:
if attr_name in __all__:
warn_deprecation(
deprecated_name=f"litestar.contrib.sqlalchemy.plugins.{attr_name}",
deprecated_name=f"litestar.contrib.sqlalchemy.plugins.serialization.{attr_name}",
version="2.12",
kind="import",
removal_in="3.0",
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.plugins' is deprecated, please "
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.plugins.serialization' is deprecated, please "
f"import it from 'litstar.plugins.sqlalchemy' instead",
)
from advanced_alchemy.extensions.litestar import SQLAlchemySerializationPlugin
Expand Down
2 changes: 1 addition & 1 deletion litestar/contrib/sqlalchemy/repository/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __getattr__(attr_name: str) -> object:
version="2.12",
kind="import",
removal_in="3.0",
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.repository' is deprecated, please "
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.repository._async' is deprecated, please "
f"import it from 'litestar.plugins.sqlalchemy.repository' instead",
)
value = globals()[attr_name] = locals()[attr_name]
Expand Down
2 changes: 1 addition & 1 deletion litestar/contrib/sqlalchemy/repository/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __getattr__(attr_name: str) -> object:
version="2.12",
kind="import",
removal_in="3.0",
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.repository' is deprecated, please "
info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.repository._sync' is deprecated, please "
f"import it from 'litestar.plugins.sqlalchemy.repository' instead",
)
value = globals()[attr_name] = locals()[attr_name]
Expand Down
108 changes: 108 additions & 0 deletions tests/unit/test_contrib/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,111 @@ def test_deprecated_sqlalchemy_plugins_init_config_asyncio_imports() -> None:
match="importing AsyncSessionConfig from 'litestar.contrib.sqlalchemy.plugins.init.config.asyncio' is deprecated",
):
from litestar.contrib.sqlalchemy.plugins.init.config.asyncio import AsyncSessionConfig


def test_deprecated_sqlalchemy_plugins_init_config_engine_imports() -> None:
purge_module(["litestar.contrib.sqlalchemy.plugins.init.config.engine"], __file__)
with pytest.warns(
DeprecationWarning,
match="importing EngineConfig from 'litestar.contrib.sqlalchemy.plugins.init.config.engine' is deprecated",
):
from litestar.contrib.sqlalchemy.plugins.init.config.engine import EngineConfig


def test_deprecated_sqlalchemy_dto_imports() -> None:
purge_module(["litestar.contrib.sqlalchemy.dto"], __file__)
with pytest.warns(
DeprecationWarning,
match="importing SQLAlchemyDTOConfig from 'litestar.contrib.sqlalchemy.dto' is deprecated",
):
from litestar.contrib.sqlalchemy.dto import SQLAlchemyDTOConfig


def test_deprecated_sqlalchemy_plugins_init_plugin_imports() -> None:
purge_module(["litestar.contrib.sqlalchemy.plugins.init.plugin"], __file__)
with pytest.warns(
DeprecationWarning,
match="importing SQLAlchemyInitPlugin from 'litestar.contrib.sqlalchemy.plugins.init' is deprecated",
):
from litestar.contrib.sqlalchemy.plugins.init.plugin import SQLAlchemyInitPlugin


def test_deprecated_sqlalchemy_plugins_serialization_imports() -> None:
purge_module(["litestar.contrib.sqlalchemy.plugins.serialization"], __file__)
with pytest.warns(
DeprecationWarning,
match="importing SQLAlchemySerializationPlugin from 'litestar.contrib.sqlalchemy.plugins.serialization' is deprecated",
):
from litestar.contrib.sqlalchemy.plugins.serialization import SQLAlchemySerializationPlugin


def test_deprecated_sqlalchemy_repository_async_imports() -> None:
purge_module(["litestar.contrib.sqlalchemy.repository._async"], __file__)
with pytest.warns(
DeprecationWarning,
match="importing SQLAlchemyAsyncRepository from 'litestar.contrib.sqlalchemy.repository._async' is deprecated",
):
from litestar.contrib.sqlalchemy.repository._async import SQLAlchemyAsyncRepository


def test_deprecated_sqlalchemy_repository_sync_imports() -> None:
purge_module(["litestar.contrib.sqlalchemy.repository._sync"], __file__)
with pytest.warns(
DeprecationWarning,
match="importing SQLAlchemySyncRepository from 'litestar.contrib.sqlalchemy.repository._sync' is deprecated",
):
from litestar.contrib.sqlalchemy.repository._sync import SQLAlchemySyncRepository


def test_deprecated_sqlalchemy_base_imports() -> None:
purge_module(["litestar.contrib.sqlalchemy.base"], __file__)
with pytest.warns(
DeprecationWarning,
match="from 'litestar.contrib.sqlalchemy.base' is deprecated",
):
from litestar.contrib.sqlalchemy.base import (
AuditColumns,
BigIntAuditBase,
BigIntBase,
BigIntPrimaryKey,
CommonTableAttributes,
ModelProtocol,
UUIDAuditBase,
UUIDBase,
UUIDPrimaryKey,
create_registry,
orm_registry,
touch_updated_timestamp,
)


def test_deprecated_sqlalchemy_plugins_init_config_asyncio_handlers() -> None:
purge_module(["litestar.contrib.sqlalchemy.plugins.init.config.asyncio"], __file__)
with pytest.warns(
DeprecationWarning,
match="importing default_before_send_handler from 'litestar.contrib.sqlalchemy.plugins.init.config.asyncio' is deprecated",
):
from litestar.contrib.sqlalchemy.plugins.init.config.asyncio import default_before_send_handler

purge_module(["litestar.contrib.sqlalchemy.plugins.init.config.asyncio"], __file__)
with pytest.warns(
DeprecationWarning,
match="importing autocommit_before_send_handler from 'litestar.contrib.sqlalchemy.plugins.init.config.asyncio' is deprecated",
):
from litestar.contrib.sqlalchemy.plugins.init.config.asyncio import autocommit_before_send_handler


def test_deprecated_sqlalchemy_plugins_init_config_sync_handlers() -> None:
purge_module(["litestar.contrib.sqlalchemy.plugins.init.config.sync"], __file__)
with pytest.warns(
DeprecationWarning,
match="importing default_before_send_handler from 'litestar.contrib.sqlalchemy.plugins.init.config.sync' is deprecated",
):
from litestar.contrib.sqlalchemy.plugins.init.config.sync import default_before_send_handler

purge_module(["litestar.contrib.sqlalchemy.plugins.init.config.sync"], __file__)
with pytest.warns(
DeprecationWarning,
match="importing autocommit_before_send_handler from 'litestar.contrib.sqlalchemy.plugins.init.config.sync' is deprecated",
):
from litestar.contrib.sqlalchemy.plugins.init.config.sync import autocommit_before_send_handler

0 comments on commit 4f12367

Please sign in to comment.