Skip to content

Commit

Permalink
fix(sqlalchemy): updates to documentation based on upstream changes (#…
Browse files Browse the repository at this point in the history
…3917)

* fix(sqlalchemy): updates to documentation based on upstream changes
  • Loading branch information
cofin authored Jan 3, 2025
1 parent ebdb6e5 commit 1f8acc7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from litestar import Litestar, get
from litestar.contrib.sqlalchemy.base import UUIDAuditBase, UUIDBase
from litestar.contrib.sqlalchemy.plugins import AsyncSessionConfig, SQLAlchemyAsyncConfig, SQLAlchemyPlugin
from litestar.plugins.sqlalchemy import AsyncSessionConfig, SQLAlchemyAsyncConfig, SQLAlchemyPlugin, base


# The SQLAlchemy base includes a declarative model for you to use in your models.
# The `UUIDBase` class includes a `UUID` based primary key (`id`)
class Author(UUIDBase):
class Author(base.UUIDBase):
__tablename__ = "author"
name: Mapped[str]
dob: Mapped[date]
books: Mapped[List[Book]] = relationship(back_populates="author", lazy="selectin")
Expand All @@ -25,7 +25,8 @@ class Author(UUIDBase):
# The `UUIDAuditBase` class includes the same UUID` based primary key (`id`) and 2
# additional columns: `created_at` and `updated_at`. `created_at` is a timestamp of when the
# record created, and `updated_at` is the last time the record was modified.
class Book(UUIDAuditBase):
class Book(base.UUIDAuditBase):
__tablename__ = "book"
title: Mapped[str]
author_id: Mapped[UUID] = mapped_column(ForeignKey("author.id"))
author: Mapped[Author] = relationship(lazy="joined", innerjoin=True, viewonly=True)
Expand All @@ -37,7 +38,7 @@ class Book(UUIDAuditBase):
) # Create 'async_session' dependency.


async def on_startup() -> None:
async def on_startup(app: Litestar) -> None:
"""Adds some dummy data if no data is present."""
async with sqlalchemy_config.get_session() as session:
statement = select(func.count()).select_from(Author)
Expand Down
16 changes: 10 additions & 6 deletions litestar/contrib/sqlalchemy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@ def __getattr__(attr_name: str) -> object:
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,
)
from advanced_alchemy.mixins import ( # pyright: ignore[reportMissingImports]
AuditColumns,
BigIntPrimaryKey,
UUIDPrimaryKey,
)

warn_deprecation(
deprecated_name=f"litestar.contrib.sqlalchemy.base.{attr_name}",
Expand All @@ -78,15 +80,17 @@ def __getattr__(attr_name: str) -> object:
from advanced_alchemy.base import touch_updated_timestamp # type: ignore[no-redef,attr-defined]

from advanced_alchemy.base import ( # pyright: ignore[reportMissingImports]
AuditColumns,
BigIntAuditBase,
BigIntBase,
BigIntPrimaryKey,
CommonTableAttributes,
ModelProtocol,
UUIDAuditBase,
UUIDBase,
UUIDPrimaryKey,
create_registry,
orm_registry,
)
from advanced_alchemy.mixins import ( # pyright: ignore[reportMissingImports]
AuditColumns,
BigIntPrimaryKey,
UUIDPrimaryKey,
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
from pathlib import Path

import pytest
from pytest import MonkeyPatch
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.pool import NullPool

from litestar.plugins.sqlalchemy import AsyncSessionConfig, SQLAlchemyAsyncConfig
from litestar.testing import TestClient

pytestmark = pytest.mark.xdist_group("sqlalchemy_examples")


def test_sqlalchemy_declarative_models() -> None:
from docs.examples.contrib.sqlalchemy.sqlalchemy_declarative_models import app
async def test_sqlalchemy_declarative_models(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
engine = create_async_engine("sqlite+aiosqlite:///test.sqlite", poolclass=NullPool)

session_config = AsyncSessionConfig(expire_on_commit=False)
sqlalchemy_config = SQLAlchemyAsyncConfig(
session_config=session_config,
create_all=True,
engine_instance=engine,
) # Create 'async_session' dependency.
from docs.examples.contrib.sqlalchemy import sqlalchemy_declarative_models

with TestClient(app) as client:
monkeypatch.setattr(sqlalchemy_declarative_models, "sqlalchemy_config", sqlalchemy_config)
async with engine.begin() as connection:
await connection.run_sync(sqlalchemy_declarative_models.Author.metadata.create_all)
await connection.commit()
with TestClient(sqlalchemy_declarative_models.app) as client:
response = client.get("/authors")
assert response.status_code == 200
assert len(response.json()) > 0
20 changes: 11 additions & 9 deletions tests/unit/test_plugins/test_sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from advanced_alchemy.extensions import litestar as sa_litestar
from advanced_alchemy.extensions.litestar import base as sa_base
from advanced_alchemy.extensions.litestar import exceptions as sa_exceptions
Expand Down Expand Up @@ -38,12 +39,13 @@ def test_re_exports() -> None:
assert sqlalchemy.SyncSessionConfig is sa_litestar.SyncSessionConfig

# deprecated, to be removed later
assert sqlalchemy.AuditColumns is sa_base.AuditColumns
assert sqlalchemy.BigIntAuditBase is sa_base.BigIntAuditBase
assert sqlalchemy.BigIntBase is sa_base.BigIntBase
assert sqlalchemy.BigIntPrimaryKey is sa_base.BigIntPrimaryKey
assert sqlalchemy.CommonTableAttributes is sa_base.CommonTableAttributes
assert sqlalchemy.UUIDAuditBase is sa_base.UUIDAuditBase
assert sqlalchemy.UUIDBase is sa_base.UUIDBase
assert sqlalchemy.UUIDPrimaryKey is sa_base.UUIDPrimaryKey
assert sqlalchemy.orm_registry is sa_base.orm_registry
with pytest.warns(DeprecationWarning):
assert sqlalchemy.AuditColumns is sa_base.AuditColumns
assert sqlalchemy.BigIntAuditBase is sa_base.BigIntAuditBase
assert sqlalchemy.BigIntBase is sa_base.BigIntBase
assert sqlalchemy.BigIntPrimaryKey is sa_base.BigIntPrimaryKey
assert sqlalchemy.CommonTableAttributes is sa_base.CommonTableAttributes
assert sqlalchemy.UUIDAuditBase is sa_base.UUIDAuditBase
assert sqlalchemy.UUIDBase is sa_base.UUIDBase
assert sqlalchemy.UUIDPrimaryKey is sa_base.UUIDPrimaryKey
assert sqlalchemy.orm_registry is sa_base.orm_registry
8 changes: 3 additions & 5 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f8acc7

Please sign in to comment.