mirror of
https://github.com/d3vyce/fastapi-toolsets.git
synced 2026-04-16 06:36:26 +02:00
Compare commits
1 Commits
v3.1.0
...
44eb22800a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44eb22800a
|
7
.github/workflows/ci.yml
vendored
7
.github/workflows/ci.yml
vendored
@@ -6,9 +6,6 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches: [main]
|
branches: [main]
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
|
|
||||||
concurrency:
|
concurrency:
|
||||||
group: ${{ github.workflow }}-${{ github.ref }}
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
cancel-in-progress: true
|
cancel-in-progress: true
|
||||||
@@ -96,7 +93,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Upload coverage to Codecov
|
- name: Upload coverage to Codecov
|
||||||
if: matrix.python-version == '3.14'
|
if: matrix.python-version == '3.14'
|
||||||
uses: codecov/codecov-action@v6
|
uses: codecov/codecov-action@v5
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
report_type: coverage
|
report_type: coverage
|
||||||
@@ -105,7 +102,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Upload test results to Codecov
|
- name: Upload test results to Codecov
|
||||||
if: matrix.python-version == '3.14'
|
if: matrix.python-version == '3.14'
|
||||||
uses: codecov/codecov-action@v6
|
uses: codecov/codecov-action@v5
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
report_type: test_results
|
report_type: test_results
|
||||||
|
|||||||
21
.github/workflows/docs.yml
vendored
21
.github/workflows/docs.yml
vendored
@@ -34,17 +34,26 @@ jobs:
|
|||||||
MAJOR=$(echo "$VERSION" | cut -d. -f1)
|
MAJOR=$(echo "$VERSION" | cut -d. -f1)
|
||||||
DEPLOY_VERSION="v$(echo "$VERSION" | cut -d. -f1-2)"
|
DEPLOY_VERSION="v$(echo "$VERSION" | cut -d. -f1-2)"
|
||||||
|
|
||||||
# On new major: keep only the latest feature version of the previous major
|
# On new major: consolidate previous major's feature versions into vX
|
||||||
PREV_MAJOR=$((MAJOR - 1))
|
PREV_MAJOR=$((MAJOR - 1))
|
||||||
OLD_FEATURE_VERSIONS=$(uv run mike list 2>/dev/null | grep -oE "^v${PREV_MAJOR}\.[0-9]+" || true)
|
OLD_FEATURE_VERSIONS=$(uv run mike list 2>/dev/null | grep -oE "^v${PREV_MAJOR}\.[0-9]+" || true)
|
||||||
|
|
||||||
if [ -n "$OLD_FEATURE_VERSIONS" ]; then
|
if [ -n "$OLD_FEATURE_VERSIONS" ]; then
|
||||||
LATEST_PREV=$(echo "$OLD_FEATURE_VERSIONS" | sort -t. -k2 -n | tail -1)
|
LATEST_PREV_TAG=$(git tag -l "v${PREV_MAJOR}.*" | sort -V | tail -1)
|
||||||
echo "$OLD_FEATURE_VERSIONS" | while read -r OLD_V; do
|
|
||||||
if [ "$OLD_V" != "$LATEST_PREV" ]; then
|
if [ -n "$LATEST_PREV_TAG" ]; then
|
||||||
echo "Deleting $OLD_V"
|
git checkout "$LATEST_PREV_TAG" -- docs/ src/ zensical.toml
|
||||||
uv run mike delete "$OLD_V"
|
if ! grep -q '\[project\.extra\.version\]' zensical.toml; then
|
||||||
|
printf '\n[project.extra.version]\nprovider = "mike"\ndefault = "stable"\nalias = true\n' >> zensical.toml
|
||||||
fi
|
fi
|
||||||
|
uv run mike deploy "v${PREV_MAJOR}"
|
||||||
|
git checkout HEAD -- docs/ src/ zensical.toml
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Delete old feature versions
|
||||||
|
echo "$OLD_FEATURE_VERSIONS" | while read -r OLD_V; do
|
||||||
|
echo "Deleting $OLD_V"
|
||||||
|
uv run mike delete "$OLD_V"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
@@ -48,8 +48,7 @@ uv add "fastapi-toolsets[all]"
|
|||||||
- **Database**: Session management, transaction helpers, table locking, and polling-based row change detection
|
- **Database**: Session management, transaction helpers, table locking, and polling-based row change detection
|
||||||
- **Dependencies**: FastAPI dependency factories (`PathDependency`, `BodyDependency`) for automatic DB lookups from path or body parameters
|
- **Dependencies**: FastAPI dependency factories (`PathDependency`, `BodyDependency`) for automatic DB lookups from path or body parameters
|
||||||
- **Fixtures**: Fixture system with dependency management, context support, and pytest integration
|
- **Fixtures**: Fixture system with dependency management, context support, and pytest integration
|
||||||
- **Model Mixins**: SQLAlchemy mixins for common column patterns (`UUIDMixin`, `UUIDv7Mixin`, `CreatedAtMixin`, `UpdatedAtMixin`, `TimestampMixin`)
|
- **Model Mixins**: SQLAlchemy mixins for common column patterns (`UUIDMixin`, `UUIDv7Mixin`, `CreatedAtMixin`, `UpdatedAtMixin`, `TimestampMixin`) and lifecycle callbacks (`WatchedFieldsMixin`, `@watch`) that fire after commit for insert, update, and delete events
|
||||||
- **Lifecycle Events**: Post-commit event system (`EventSession`, `listens_for`) that dispatches async/sync callbacks for insert, update, and delete operations
|
|
||||||
- **Standardized API Responses**: Consistent response format with `Response`, `ErrorResponse`, `PaginatedResponse`, `CursorPaginatedResponse` and `OffsetPaginatedResponse`.
|
- **Standardized API Responses**: Consistent response format with `Response`, `ErrorResponse`, `PaginatedResponse`, `CursorPaginatedResponse` and `OffsetPaginatedResponse`.
|
||||||
- **Exception Handling**: Structured error responses with automatic OpenAPI documentation
|
- **Exception Handling**: Structured error responses with automatic OpenAPI documentation
|
||||||
- **Logging**: Logging configuration with uvicorn integration via `configure_logging` and `get_logger`
|
- **Logging**: Logging configuration with uvicorn integration via `configure_logging` and `get_logger`
|
||||||
|
|||||||
@@ -43,16 +43,16 @@ Declare `searchable_fields`, `facet_fields`, and `order_fields` once on [`CrudFa
|
|||||||
|
|
||||||
## Routes
|
## Routes
|
||||||
|
|
||||||
```python title="routes.py:1:16"
|
```python title="routes.py:1:17"
|
||||||
--8<-- "docs_src/examples/pagination_search/routes.py:1:16"
|
--8<-- "docs_src/examples/pagination_search/routes.py:1:17"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Offset pagination
|
### Offset pagination
|
||||||
|
|
||||||
Best for admin panels or any UI that needs a total item count and numbered pages.
|
Best for admin panels or any UI that needs a total item count and numbered pages.
|
||||||
|
|
||||||
```python title="routes.py:19:37"
|
```python title="routes.py:20:40"
|
||||||
--8<-- "docs_src/examples/pagination_search/routes.py:19:37"
|
--8<-- "docs_src/examples/pagination_search/routes.py:20:40"
|
||||||
```
|
```
|
||||||
|
|
||||||
**Example request**
|
**Example request**
|
||||||
@@ -92,8 +92,8 @@ To skip the `COUNT(*)` query for better performance on large tables, pass `inclu
|
|||||||
|
|
||||||
Best for feeds, infinite scroll, or any high-throughput API where offset performance degrades.
|
Best for feeds, infinite scroll, or any high-throughput API where offset performance degrades.
|
||||||
|
|
||||||
```python title="routes.py:40:58"
|
```python title="routes.py:43:63"
|
||||||
--8<-- "docs_src/examples/pagination_search/routes.py:40:58"
|
--8<-- "docs_src/examples/pagination_search/routes.py:43:63"
|
||||||
```
|
```
|
||||||
|
|
||||||
**Example request**
|
**Example request**
|
||||||
@@ -132,8 +132,8 @@ Pass `next_cursor` as the `cursor` query parameter on the next request to advanc
|
|||||||
|
|
||||||
[`paginate()`](../module/crud.md#unified-paginate--both-strategies-on-one-endpoint) lets a single endpoint support both strategies via a `pagination_type` query parameter. The `pagination_type` field in the response acts as a discriminator for frontend tooling.
|
[`paginate()`](../module/crud.md#unified-paginate--both-strategies-on-one-endpoint) lets a single endpoint support both strategies via a `pagination_type` query parameter. The `pagination_type` field in the response acts as a discriminator for frontend tooling.
|
||||||
|
|
||||||
```python title="routes.py:61:79"
|
```python title="routes.py:66:90"
|
||||||
--8<-- "docs_src/examples/pagination_search/routes.py:61:79"
|
--8<-- "docs_src/examples/pagination_search/routes.py:66:90"
|
||||||
```
|
```
|
||||||
|
|
||||||
**Offset request** (default)
|
**Offset request** (default)
|
||||||
|
|||||||
@@ -48,8 +48,7 @@ uv add "fastapi-toolsets[all]"
|
|||||||
- **Database**: Session management, transaction helpers, table locking, and polling-based row change detection
|
- **Database**: Session management, transaction helpers, table locking, and polling-based row change detection
|
||||||
- **Dependencies**: FastAPI dependency factories (`PathDependency`, `BodyDependency`) for automatic DB lookups from path or body parameters
|
- **Dependencies**: FastAPI dependency factories (`PathDependency`, `BodyDependency`) for automatic DB lookups from path or body parameters
|
||||||
- **Fixtures**: Fixture system with dependency management, context support, and pytest integration
|
- **Fixtures**: Fixture system with dependency management, context support, and pytest integration
|
||||||
- **Model Mixins**: SQLAlchemy mixins for common column patterns (`UUIDMixin`, `UUIDv7Mixin`, `CreatedAtMixin`, `UpdatedAtMixin`, `TimestampMixin`).
|
- **Model Mixins**: SQLAlchemy mixins for common column patterns (`UUIDMixin`, `UUIDv7Mixin`, `CreatedAtMixin`, `UpdatedAtMixin`, `TimestampMixin`) and lifecycle callbacks (`WatchedFieldsMixin`) that fire after commit for insert, update, and delete events.
|
||||||
- **Lifecycle Events**: Post-commit event system (`EventSession`, `listens_for`) that dispatches async/sync callbacks for insert, update, and delete operations.
|
|
||||||
- **Standardized API Responses**: Consistent response format with `Response`, `ErrorResponse`, `PaginatedResponse`, `CursorPaginatedResponse` and `OffsetPaginatedResponse`.
|
- **Standardized API Responses**: Consistent response format with `Response`, `ErrorResponse`, `PaginatedResponse`, `CursorPaginatedResponse` and `OffsetPaginatedResponse`.
|
||||||
- **Exception Handling**: Structured error responses with automatic OpenAPI documentation
|
- **Exception Handling**: Structured error responses with automatic OpenAPI documentation
|
||||||
- **Logging**: Logging configuration with uvicorn integration via `configure_logging` and `get_logger`
|
- **Logging**: Logging configuration with uvicorn integration via `configure_logging` and `get_logger`
|
||||||
|
|||||||
@@ -1,180 +0,0 @@
|
|||||||
# Migrating to v3.0
|
|
||||||
|
|
||||||
This page covers every breaking change introduced in **v3.0** and the steps required to update your code.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## CRUD
|
|
||||||
|
|
||||||
### Facet keys now always use the full relationship chain
|
|
||||||
|
|
||||||
In `v2`, relationship facet fields used only the terminal column key (e.g. `"name"` for `Role.name`) and only prepended the relationship name when two facet fields shared the same column key. In `v3`, facet keys **always** include the full relationship chain joined by `__`, regardless of collisions.
|
|
||||||
|
|
||||||
=== "Before (`v2`)"
|
|
||||||
|
|
||||||
```
|
|
||||||
User.status -> status
|
|
||||||
(User.role, Role.name) -> name
|
|
||||||
(User.role, Role.permission, Permission.name) -> name
|
|
||||||
```
|
|
||||||
|
|
||||||
=== "Now (`v3`)"
|
|
||||||
|
|
||||||
```
|
|
||||||
User.status -> status
|
|
||||||
(User.role, Role.name) -> role__name
|
|
||||||
(User.role, Role.permission, Permission.name) -> role__permission__name
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### `*_params` dependencies consolidated into per-paginate methods
|
|
||||||
|
|
||||||
The six individual dependency methods (`offset_params`, `cursor_params`, `paginate_params`, `filter_params`, `search_params`, `order_params`) have been **removed** and replaced by three consolidated methods that bundle pagination, search, filter, and order into a single `Depends()` call.
|
|
||||||
|
|
||||||
| Removed | Replacement |
|
|
||||||
|---|---|
|
|
||||||
| `offset_params()` + `filter_params()` + `search_params()` + `order_params()` | `offset_paginate_params()` |
|
|
||||||
| `cursor_params()` + `filter_params()` + `search_params()` + `order_params()` | `cursor_paginate_params()` |
|
|
||||||
| `paginate_params()` + `filter_params()` + `search_params()` + `order_params()` | `paginate_params()` |
|
|
||||||
|
|
||||||
Each new method accepts `search`, `filter`, and `order` boolean toggles (all `True` by default) to disable features you don't need.
|
|
||||||
|
|
||||||
=== "Before (`v2`)"
|
|
||||||
|
|
||||||
```python
|
|
||||||
from fastapi_toolsets.crud import OrderByClause
|
|
||||||
|
|
||||||
@router.get("/offset")
|
|
||||||
async def list_articles_offset(
|
|
||||||
session: SessionDep,
|
|
||||||
params: Annotated[dict, Depends(ArticleCrud.offset_params(default_page_size=20))],
|
|
||||||
filter_by: Annotated[dict, Depends(ArticleCrud.filter_params())],
|
|
||||||
order_by: Annotated[OrderByClause | None, Depends(ArticleCrud.order_params(default_field=Article.created_at))],
|
|
||||||
search: str | None = None,
|
|
||||||
) -> OffsetPaginatedResponse[ArticleRead]:
|
|
||||||
return await ArticleCrud.offset_paginate(
|
|
||||||
session=session,
|
|
||||||
**params,
|
|
||||||
search=search,
|
|
||||||
filter_by=filter_by or None,
|
|
||||||
order_by=order_by,
|
|
||||||
schema=ArticleRead,
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
=== "Now (`v3`)"
|
|
||||||
|
|
||||||
```python
|
|
||||||
@router.get("/offset")
|
|
||||||
async def list_articles_offset(
|
|
||||||
session: SessionDep,
|
|
||||||
params: Annotated[
|
|
||||||
dict,
|
|
||||||
Depends(
|
|
||||||
ArticleCrud.offset_paginate_params(
|
|
||||||
default_page_size=20,
|
|
||||||
default_order_field=Article.created_at,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
],
|
|
||||||
) -> OffsetPaginatedResponse[ArticleRead]:
|
|
||||||
return await ArticleCrud.offset_paginate(session=session, **params, schema=ArticleRead)
|
|
||||||
```
|
|
||||||
|
|
||||||
The same pattern applies to `cursor_paginate_params()` and `paginate_params()`. To disable a feature, pass the toggle:
|
|
||||||
|
|
||||||
```python
|
|
||||||
# No search or ordering, only pagination + filtering
|
|
||||||
ArticleCrud.offset_paginate_params(search=False, order=False)
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Models
|
|
||||||
|
|
||||||
The lifecycle event system has been rewritten. Callbacks are now registered with a module-level [`listens_for`](../reference/models.md#fastapi_toolsets.models.listens_for) decorator and dispatched by [`EventSession`](../reference/models.md#fastapi_toolsets.models.EventSession), replacing the mixin-based approach from `v2`.
|
|
||||||
|
|
||||||
### `WatchedFieldsMixin` and `@watch` removed
|
|
||||||
|
|
||||||
Importing `WatchedFieldsMixin` or `watch` will raise `ImportError`.
|
|
||||||
|
|
||||||
Model method callbacks (`on_create`, `on_delete`, `on_update`) and the `@watch` decorator are replaced by:
|
|
||||||
|
|
||||||
1. **`__watched_fields__`** — a plain class attribute to restrict which field changes trigger `UPDATE` events (replaces `@watch`).
|
|
||||||
2. **`@listens_for`** — a module-level decorator to register callbacks for one or more [`ModelEvent`](../reference/models.md#fastapi_toolsets.models.ModelEvent) types (replaces `on_create` / `on_delete` / `on_update` methods).
|
|
||||||
|
|
||||||
=== "Before (`v2`)"
|
|
||||||
|
|
||||||
```python
|
|
||||||
from fastapi_toolsets.models import WatchedFieldsMixin, watch
|
|
||||||
|
|
||||||
@watch("status")
|
|
||||||
class Order(Base, UUIDMixin, WatchedFieldsMixin):
|
|
||||||
__tablename__ = "orders"
|
|
||||||
|
|
||||||
status: Mapped[str]
|
|
||||||
|
|
||||||
async def on_create(self):
|
|
||||||
await notify_new_order(self.id)
|
|
||||||
|
|
||||||
async def on_update(self, changes):
|
|
||||||
if "status" in changes:
|
|
||||||
await notify_status_change(self.id, changes["status"])
|
|
||||||
|
|
||||||
async def on_delete(self):
|
|
||||||
await notify_order_cancelled(self.id)
|
|
||||||
```
|
|
||||||
|
|
||||||
=== "Now (`v3`)"
|
|
||||||
|
|
||||||
```python
|
|
||||||
from fastapi_toolsets.models import ModelEvent, UUIDMixin, listens_for
|
|
||||||
|
|
||||||
class Order(Base, UUIDMixin):
|
|
||||||
__tablename__ = "orders"
|
|
||||||
__watched_fields__ = ("status",)
|
|
||||||
|
|
||||||
status: Mapped[str]
|
|
||||||
|
|
||||||
@listens_for(Order, [ModelEvent.CREATE])
|
|
||||||
async def on_order_created(order: Order, event_type: ModelEvent, changes: None):
|
|
||||||
await notify_new_order(order.id)
|
|
||||||
|
|
||||||
@listens_for(Order, [ModelEvent.UPDATE])
|
|
||||||
async def on_order_updated(order: Order, event_type: ModelEvent, changes: dict):
|
|
||||||
if "status" in changes:
|
|
||||||
await notify_status_change(order.id, changes["status"])
|
|
||||||
|
|
||||||
@listens_for(Order, [ModelEvent.DELETE])
|
|
||||||
async def on_order_deleted(order: Order, event_type: ModelEvent, changes: None):
|
|
||||||
await notify_order_cancelled(order.id)
|
|
||||||
```
|
|
||||||
|
|
||||||
### `EventSession` now required
|
|
||||||
|
|
||||||
Without `EventSession`, lifecycle callbacks will silently stop firing.
|
|
||||||
|
|
||||||
Callbacks are now dispatched inside `EventSession.commit()` rather than via background tasks. Pass it as the session class when creating your session factory:
|
|
||||||
|
|
||||||
=== "Before (`v2`)"
|
|
||||||
|
|
||||||
```python
|
|
||||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
|
||||||
|
|
||||||
engine = create_async_engine("postgresql+asyncpg://...")
|
|
||||||
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
|
|
||||||
```
|
|
||||||
|
|
||||||
=== "Now (`v3`)"
|
|
||||||
|
|
||||||
```python
|
|
||||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
|
||||||
from fastapi_toolsets.models import EventSession
|
|
||||||
|
|
||||||
engine = create_async_engine("postgresql+asyncpg://...")
|
|
||||||
SessionLocal = async_sessionmaker(engine, expire_on_commit=False, class_=EventSession)
|
|
||||||
```
|
|
||||||
|
|
||||||
!!! note
|
|
||||||
If you use `create_db_session` from `fastapi_toolsets.pytest`, the session already uses `EventSession` — no changes needed in tests.
|
|
||||||
@@ -159,15 +159,18 @@ Three pagination methods are available. All return a typed response whose `pagi
|
|||||||
### Offset pagination
|
### Offset pagination
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from typing import Annotated
|
|
||||||
from fastapi import Depends
|
|
||||||
|
|
||||||
@router.get("")
|
@router.get("")
|
||||||
async def get_users(
|
async def get_users(
|
||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
params: Annotated[dict, Depends(UserCrud.offset_paginate_params())],
|
items_per_page: int = 50,
|
||||||
|
page: int = 1,
|
||||||
) -> OffsetPaginatedResponse[UserRead]:
|
) -> OffsetPaginatedResponse[UserRead]:
|
||||||
return await UserCrud.offset_paginate(session=session, **params, schema=UserRead)
|
return await UserCrud.offset_paginate(
|
||||||
|
session=session,
|
||||||
|
items_per_page=items_per_page,
|
||||||
|
page=page,
|
||||||
|
schema=UserRead,
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
The [`offset_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.offset_paginate) method returns an [`OffsetPaginatedResponse`](../reference/schemas.md#fastapi_toolsets.schemas.OffsetPaginatedResponse):
|
The [`offset_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.offset_paginate) method returns an [`OffsetPaginatedResponse`](../reference/schemas.md#fastapi_toolsets.schemas.OffsetPaginatedResponse):
|
||||||
@@ -191,13 +194,32 @@ The [`offset_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.Async
|
|||||||
|
|
||||||
!!! info "Added in `v2.4.1`"
|
!!! info "Added in `v2.4.1`"
|
||||||
|
|
||||||
By default `offset_paginate` runs two queries: one for the page items and one `COUNT(*)` for `total_count`. On large tables the `COUNT` can be expensive. Pass `include_total=False` to `offset_paginate_params()` to skip it:
|
By default `offset_paginate` runs two queries: one for the page items and one `COUNT(*)` for `total_count`. On large tables the `COUNT` can be expensive. Pass `include_total=False` to skip it:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
result = await UserCrud.offset_paginate(
|
||||||
|
session=session,
|
||||||
|
page=page,
|
||||||
|
items_per_page=items_per_page,
|
||||||
|
include_total=False,
|
||||||
|
schema=UserRead,
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Pagination params dependency
|
||||||
|
|
||||||
|
!!! info "Added in `v2.4.1`"
|
||||||
|
|
||||||
|
Use [`offset_params()`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.offset_params) to generate a FastAPI dependency that injects `page` and `items_per_page` from query parameters with configurable defaults and a `max_page_size` cap:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from typing import Annotated
|
||||||
|
from fastapi import Depends
|
||||||
|
|
||||||
@router.get("")
|
@router.get("")
|
||||||
async def get_users(
|
async def list_users(
|
||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
params: Annotated[dict, Depends(UserCrud.offset_paginate_params(include_total=False))],
|
params: Annotated[dict, Depends(UserCrud.offset_params(default_page_size=20, max_page_size=100))],
|
||||||
) -> OffsetPaginatedResponse[UserRead]:
|
) -> OffsetPaginatedResponse[UserRead]:
|
||||||
return await UserCrud.offset_paginate(session=session, **params, schema=UserRead)
|
return await UserCrud.offset_paginate(session=session, **params, schema=UserRead)
|
||||||
```
|
```
|
||||||
@@ -208,9 +230,15 @@ async def get_users(
|
|||||||
@router.get("")
|
@router.get("")
|
||||||
async def list_users(
|
async def list_users(
|
||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
params: Annotated[dict, Depends(UserCrud.cursor_paginate_params())],
|
cursor: str | None = None,
|
||||||
|
items_per_page: int = 20,
|
||||||
) -> CursorPaginatedResponse[UserRead]:
|
) -> CursorPaginatedResponse[UserRead]:
|
||||||
return await UserCrud.cursor_paginate(session=session, **params, schema=UserRead)
|
return await UserCrud.cursor_paginate(
|
||||||
|
session=session,
|
||||||
|
cursor=cursor,
|
||||||
|
items_per_page=items_per_page,
|
||||||
|
schema=UserRead,
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
The [`cursor_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.cursor_paginate) method returns a [`CursorPaginatedResponse`](../reference/schemas.md#fastapi_toolsets.schemas.CursorPaginatedResponse):
|
The [`cursor_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.cursor_paginate) method returns a [`CursorPaginatedResponse`](../reference/schemas.md#fastapi_toolsets.schemas.CursorPaginatedResponse):
|
||||||
@@ -263,6 +291,24 @@ PostCrud = CrudFactory(model=Post, cursor_column=Post.id)
|
|||||||
PostCrud = CrudFactory(model=Post, cursor_column=Post.created_at)
|
PostCrud = CrudFactory(model=Post, cursor_column=Post.created_at)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Pagination params dependency
|
||||||
|
|
||||||
|
!!! info "Added in `v2.4.1`"
|
||||||
|
|
||||||
|
Use [`cursor_params()`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.cursor_params) to inject `cursor` and `items_per_page` from query parameters with a `max_page_size` cap:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from typing import Annotated
|
||||||
|
from fastapi import Depends
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
async def list_users(
|
||||||
|
session: SessionDep,
|
||||||
|
params: Annotated[dict, Depends(UserCrud.cursor_params(default_page_size=20, max_page_size=100))],
|
||||||
|
) -> CursorPaginatedResponse[UserRead]:
|
||||||
|
return await UserCrud.cursor_paginate(session=session, **params, schema=UserRead)
|
||||||
|
```
|
||||||
|
|
||||||
### Unified endpoint (both strategies)
|
### Unified endpoint (both strategies)
|
||||||
|
|
||||||
!!! info "Added in `v2.3.0`"
|
!!! info "Added in `v2.3.0`"
|
||||||
@@ -270,14 +316,25 @@ PostCrud = CrudFactory(model=Post, cursor_column=Post.created_at)
|
|||||||
[`paginate()`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.paginate) dispatches to `offset_paginate` or `cursor_paginate` based on a `pagination_type` query parameter, letting you expose **one endpoint** that supports both strategies. The `pagination_type` field in the response tells clients which strategy was used, enabling frontend discriminated-union typing.
|
[`paginate()`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.paginate) dispatches to `offset_paginate` or `cursor_paginate` based on a `pagination_type` query parameter, letting you expose **one endpoint** that supports both strategies. The `pagination_type` field in the response tells clients which strategy was used, enabling frontend discriminated-union typing.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
from fastapi_toolsets.crud import PaginationType
|
||||||
from fastapi_toolsets.schemas import PaginatedResponse
|
from fastapi_toolsets.schemas import PaginatedResponse
|
||||||
|
|
||||||
@router.get("")
|
@router.get("")
|
||||||
async def list_users(
|
async def list_users(
|
||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
params: Annotated[dict, Depends(UserCrud.paginate_params())],
|
pagination_type: PaginationType = PaginationType.OFFSET,
|
||||||
|
page: int = Query(1, ge=1, description="Current page (offset only)"),
|
||||||
|
cursor: str | None = Query(None, description="Cursor token (cursor only)"),
|
||||||
|
items_per_page: int = Query(20, ge=1, le=100),
|
||||||
) -> PaginatedResponse[UserRead]:
|
) -> PaginatedResponse[UserRead]:
|
||||||
return await UserCrud.paginate(session, **params, schema=UserRead)
|
return await UserCrud.paginate(
|
||||||
|
session,
|
||||||
|
pagination_type=pagination_type,
|
||||||
|
page=page,
|
||||||
|
cursor=cursor,
|
||||||
|
items_per_page=items_per_page,
|
||||||
|
schema=UserRead,
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -285,6 +342,25 @@ GET /users?pagination_type=offset&page=2&items_per_page=10
|
|||||||
GET /users?pagination_type=cursor&cursor=eyJ2YWx1ZSI6...&items_per_page=10
|
GET /users?pagination_type=cursor&cursor=eyJ2YWx1ZSI6...&items_per_page=10
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Pagination params dependency
|
||||||
|
|
||||||
|
!!! info "Added in `v2.4.1`"
|
||||||
|
|
||||||
|
Use [`paginate_params()`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.paginate_params) to inject all parameters at once with configurable defaults and a `max_page_size` cap:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from typing import Annotated
|
||||||
|
from fastapi import Depends
|
||||||
|
from fastapi_toolsets.schemas import PaginatedResponse
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
async def list_users(
|
||||||
|
session: SessionDep,
|
||||||
|
params: Annotated[dict, Depends(UserCrud.paginate_params(default_page_size=20, max_page_size=100))],
|
||||||
|
) -> PaginatedResponse[UserRead]:
|
||||||
|
return await UserCrud.paginate(session, **params, schema=UserRead)
|
||||||
|
```
|
||||||
|
|
||||||
## Search
|
## Search
|
||||||
|
|
||||||
Two search strategies are available, both compatible with [`offset_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.offset_paginate) and [`cursor_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.cursor_paginate).
|
Two search strategies are available, both compatible with [`offset_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.offset_paginate) and [`cursor_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.cursor_paginate).
|
||||||
@@ -324,63 +400,49 @@ result = await UserCrud.offset_paginate(
|
|||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
Or via the dependency to narrow which fields are exposed as query parameters:
|
|
||||||
|
|
||||||
```python
|
|
||||||
params = UserCrud.offset_paginate_params(search_fields=[Post.title])
|
|
||||||
```
|
|
||||||
|
|
||||||
This allows searching with both [`offset_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.offset_paginate) and [`cursor_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.cursor_paginate):
|
This allows searching with both [`offset_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.offset_paginate) and [`cursor_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.cursor_paginate):
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@router.get("")
|
@router.get("")
|
||||||
async def get_users(
|
async def get_users(
|
||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
params: Annotated[dict, Depends(UserCrud.offset_paginate_params())],
|
items_per_page: int = 50,
|
||||||
|
page: int = 1,
|
||||||
|
search: str | None = None,
|
||||||
) -> OffsetPaginatedResponse[UserRead]:
|
) -> OffsetPaginatedResponse[UserRead]:
|
||||||
return await UserCrud.offset_paginate(session=session, **params, schema=UserRead)
|
return await UserCrud.offset_paginate(
|
||||||
|
session=session,
|
||||||
|
items_per_page=items_per_page,
|
||||||
|
page=page,
|
||||||
|
search=search,
|
||||||
|
schema=UserRead,
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@router.get("")
|
@router.get("")
|
||||||
async def get_users(
|
async def get_users(
|
||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
params: Annotated[dict, Depends(UserCrud.cursor_paginate_params())],
|
cursor: str | None = None,
|
||||||
|
items_per_page: int = 50,
|
||||||
|
search: str | None = None,
|
||||||
) -> CursorPaginatedResponse[UserRead]:
|
) -> CursorPaginatedResponse[UserRead]:
|
||||||
return await UserCrud.cursor_paginate(session=session, **params, schema=UserRead)
|
return await UserCrud.cursor_paginate(
|
||||||
|
session=session,
|
||||||
|
items_per_page=items_per_page,
|
||||||
|
cursor=cursor,
|
||||||
|
search=search,
|
||||||
|
schema=UserRead,
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
The dependency adds two query parameters to the endpoint:
|
|
||||||
|
|
||||||
| Parameter | Type |
|
|
||||||
| --------------- | ------------- |
|
|
||||||
| `search` | `str \| null` |
|
|
||||||
| `search_column` | `str \| null` |
|
|
||||||
|
|
||||||
```
|
|
||||||
GET /posts?search=hello → search all configured columns
|
|
||||||
GET /posts?search=hello&search_column=title → search only Post.title
|
|
||||||
```
|
|
||||||
|
|
||||||
The available search column keys are returned in the `search_columns` field of [`PaginatedResponse`](../reference/schemas.md#fastapi_toolsets.schemas.PaginatedResponse). Use them to populate a column picker in the UI, or to validate `search_column` values on the client side:
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"status": "SUCCESS",
|
|
||||||
"data": ["..."],
|
|
||||||
"pagination": { "..." },
|
|
||||||
"search_columns": ["content", "author__username", "title"]
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
!!! info "Key format uses `__` as a separator for relationship chains."
|
|
||||||
A direct column `Post.title` produces `"title"`. A relationship tuple `(Post.author, User.username)` produces `"author__username"`. An unknown `search_column` value raises [`InvalidSearchColumnError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.InvalidSearchColumnError) (HTTP 422).
|
|
||||||
|
|
||||||
### Faceted search
|
### Faceted search
|
||||||
|
|
||||||
!!! info "Added in `v1.2`"
|
!!! info "Added in `v1.2`"
|
||||||
|
|
||||||
Declare `facet_fields` on the CRUD class to return distinct column values alongside paginated results. This is useful for populating filter dropdowns or building faceted search UIs. Relationship traversal is supported via tuples, using the same syntax as `searchable_fields`:
|
Declare `facet_fields` on the CRUD class to return distinct column values alongside paginated results. This is useful for populating filter dropdowns or building faceted search UIs.
|
||||||
|
|
||||||
|
Facet fields use the same syntax as `searchable_fields` — direct columns or relationship tuples:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
UserCrud = CrudFactory(
|
UserCrud = CrudFactory(
|
||||||
@@ -402,47 +464,7 @@ result = await UserCrud.offset_paginate(
|
|||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
Or via the dependency to narrow which fields are exposed as query parameters:
|
The distinct values are returned in the `filter_attributes` field of [`PaginatedResponse`](../reference/schemas.md#fastapi_toolsets.schemas.PaginatedResponse):
|
||||||
|
|
||||||
```python
|
|
||||||
params = UserCrud.offset_paginate_params(facet_fields=[User.country])
|
|
||||||
```
|
|
||||||
|
|
||||||
Facet filtering is built into the consolidated params dependencies. When `filter=True` (the default), each facet field is exposed as a query parameter and values are collected into `filter_by` automatically:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from typing import Annotated
|
|
||||||
|
|
||||||
from fastapi import Depends
|
|
||||||
|
|
||||||
@router.get("", response_model_exclude_none=True)
|
|
||||||
async def list_users(
|
|
||||||
session: SessionDep,
|
|
||||||
params: Annotated[dict, Depends(UserCrud.offset_paginate_params())],
|
|
||||||
) -> OffsetPaginatedResponse[UserRead]:
|
|
||||||
return await UserCrud.offset_paginate(session=session, **params, schema=UserRead)
|
|
||||||
```
|
|
||||||
|
|
||||||
```python
|
|
||||||
@router.get("", response_model_exclude_none=True)
|
|
||||||
async def list_users(
|
|
||||||
session: SessionDep,
|
|
||||||
params: Annotated[dict, Depends(UserCrud.cursor_paginate_params())],
|
|
||||||
) -> CursorPaginatedResponse[UserRead]:
|
|
||||||
return await UserCrud.cursor_paginate(session=session, **params, schema=UserRead)
|
|
||||||
```
|
|
||||||
|
|
||||||
Both single-value and multi-value query parameters work:
|
|
||||||
|
|
||||||
```
|
|
||||||
GET /users?status=active → filter_by={"status": ["active"]}
|
|
||||||
GET /users?status=active&country=FR → filter_by={"status": ["active"], "country": ["FR"]}
|
|
||||||
GET /users?role__name=admin&role__name=editor → filter_by={"role__name": ["admin", "editor"]} (IN clause)
|
|
||||||
```
|
|
||||||
|
|
||||||
`filter_by` and `filters` can be combined — both are applied with AND logic.
|
|
||||||
|
|
||||||
The distinct values for each facet field are returned in the `filter_attributes` field of [`PaginatedResponse`](../reference/schemas.md#fastapi_toolsets.schemas.PaginatedResponse). Use them to populate filter dropdowns in the UI, or to validate `filter_by` keys on the client side:
|
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
@@ -457,14 +479,52 @@ The distinct values for each facet field are returned in the `filter_attributes`
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! info "Key format uses `__` as a separator for relationship chains."
|
Use `filter_by` to pass the client's chosen filter values directly — no need to build SQLAlchemy conditions by hand. Any unknown key raises [`InvalidFacetFilterError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.InvalidFacetFilterError).
|
||||||
A direct column `User.status` produces `"status"`. A relationship tuple `(User.role, Role.name)` produces `"role__name"`. A deeper chain `(User.role, Role.permission, Permission.name)` produces `"role__permission__name"`. An unknown `filter_by` key raises [`InvalidFacetFilterError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.InvalidFacetFilterError) (HTTP 422).
|
|
||||||
|
!!! info "The keys in `filter_by` are the same keys the client received in `filter_attributes`."
|
||||||
|
Keys use `__` as a separator for the full relationship chain. A direct column `User.status` produces `"status"`. A relationship tuple `(User.role, Role.name)` produces `"role__name"`. A deeper chain `(User.role, Role.permission, Permission.name)` produces `"role__permission__name"`.
|
||||||
|
|
||||||
|
`filter_by` and `filters` can be combined — both are applied with AND logic.
|
||||||
|
|
||||||
|
Use [`filter_params()`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.filter_params) to generate a dict with the facet filter values from the query parameters:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
from fastapi import Depends
|
||||||
|
|
||||||
|
UserCrud = CrudFactory(
|
||||||
|
model=User,
|
||||||
|
facet_fields=[User.status, User.country, (User.role, Role.name)],
|
||||||
|
)
|
||||||
|
|
||||||
|
@router.get("", response_model_exclude_none=True)
|
||||||
|
async def list_users(
|
||||||
|
session: SessionDep,
|
||||||
|
page: int = 1,
|
||||||
|
filter_by: Annotated[dict[str, list[str]], Depends(UserCrud.filter_params())],
|
||||||
|
) -> OffsetPaginatedResponse[UserRead]:
|
||||||
|
return await UserCrud.offset_paginate(
|
||||||
|
session=session,
|
||||||
|
page=page,
|
||||||
|
filter_by=filter_by,
|
||||||
|
schema=UserRead,
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
Both single-value and multi-value query parameters work:
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /users?status=active → filter_by={"status": ["active"]}
|
||||||
|
GET /users?status=active&country=FR → filter_by={"status": ["active"], "country": ["FR"]}
|
||||||
|
GET /users?role__name=admin&role__name=editor → filter_by={"role__name": ["admin", "editor"]} (IN clause)
|
||||||
|
```
|
||||||
|
|
||||||
## Sorting
|
## Sorting
|
||||||
|
|
||||||
!!! info "Added in `v1.3`"
|
!!! info "Added in `v1.3`"
|
||||||
|
|
||||||
Declare `order_fields` on the CRUD class. Relationship traversal is supported via tuples, using the same syntax as `searchable_fields` and `facet_fields`:
|
Declare `order_fields` on the CRUD class to expose client-driven column ordering via `order_by` and `order` query parameters.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
UserCrud = CrudFactory(
|
UserCrud = CrudFactory(
|
||||||
@@ -472,80 +532,46 @@ UserCrud = CrudFactory(
|
|||||||
order_fields=[
|
order_fields=[
|
||||||
User.name,
|
User.name,
|
||||||
User.created_at,
|
User.created_at,
|
||||||
(User.role, Role.name), # sort by a related model column
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
You can override `order_fields` per call:
|
Call [`order_params()`](../reference/crud.md#fastapi_toolsets.crud.factory.AsyncCrud.order_params) to generate a FastAPI dependency that maps the query parameters to an [`OrderByClause`](../reference/crud.md#fastapi_toolsets.crud.factory.OrderByClause) expression:
|
||||||
|
|
||||||
```python
|
|
||||||
result = await UserCrud.offset_paginate(
|
|
||||||
session=session,
|
|
||||||
order_fields=[User.name],
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
Or via the dependency to narrow which fields are exposed as query parameters:
|
|
||||||
|
|
||||||
```python
|
|
||||||
params = UserCrud.offset_paginate_params(order_fields=[User.name])
|
|
||||||
```
|
|
||||||
|
|
||||||
Sorting is built into the consolidated params dependencies. When `order=True` (the default), `order_by` and `order` query parameters are exposed and resolved into an `OrderByClause` automatically:
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
|
from fastapi_toolsets.crud import OrderByClause
|
||||||
|
|
||||||
@router.get("")
|
@router.get("")
|
||||||
async def list_users(
|
async def list_users(
|
||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
params: Annotated[dict, Depends(UserCrud.offset_paginate_params())],
|
order_by: Annotated[OrderByClause | None, Depends(UserCrud.order_params())],
|
||||||
) -> OffsetPaginatedResponse[UserRead]:
|
) -> OffsetPaginatedResponse[UserRead]:
|
||||||
return await UserCrud.offset_paginate(session=session, **params, schema=UserRead)
|
return await UserCrud.offset_paginate(session=session, order_by=order_by, schema=UserRead)
|
||||||
```
|
|
||||||
|
|
||||||
```python
|
|
||||||
@router.get("")
|
|
||||||
async def list_users(
|
|
||||||
session: SessionDep,
|
|
||||||
params: Annotated[dict, Depends(UserCrud.cursor_paginate_params())],
|
|
||||||
) -> CursorPaginatedResponse[UserRead]:
|
|
||||||
return await UserCrud.cursor_paginate(session=session, **params, schema=UserRead)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The dependency adds two query parameters to the endpoint:
|
The dependency adds two query parameters to the endpoint:
|
||||||
|
|
||||||
| Parameter | Type |
|
| Parameter | Type |
|
||||||
| ---------- | --------------- |
|
| ---------- | --------------- |
|
||||||
| `order_by` | `str \| null` |
|
| `order_by` | `str | null` |
|
||||||
| `order` | `asc` or `desc` |
|
| `order` | `asc` or `desc` |
|
||||||
|
|
||||||
```
|
```
|
||||||
GET /users?order_by=name&order=asc → ORDER BY users.name ASC
|
GET /users?order_by=name&order=asc → ORDER BY users.name ASC
|
||||||
GET /users?order_by=role__name&order=desc → LEFT JOIN roles ON ... ORDER BY roles.name DESC
|
GET /users?order_by=name&order=desc → ORDER BY users.name DESC
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! info "Relationship tuples are joined automatically."
|
An unknown `order_by` value raises [`InvalidOrderFieldError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.InvalidOrderFieldError) (HTTP 422).
|
||||||
When a relation field is selected, the related table is LEFT OUTER JOINed automatically. An unknown `order_by` value raises [`InvalidOrderFieldError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.InvalidOrderFieldError) (HTTP 422).
|
|
||||||
|
|
||||||
|
You can also pass `order_fields` directly to `order_params()` to override the class-level defaults without modifying them:
|
||||||
|
|
||||||
The available sort keys are returned in the `order_columns` field of [`PaginatedResponse`](../reference/schemas.md#fastapi_toolsets.schemas.PaginatedResponse). Use them to populate a sort picker in the UI, or to validate `order_by` values on the client side:
|
```python
|
||||||
|
UserOrderParams = UserCrud.order_params(order_fields=[User.name])
|
||||||
```json
|
|
||||||
{
|
|
||||||
"status": "SUCCESS",
|
|
||||||
"data": ["..."],
|
|
||||||
"pagination": { "..." },
|
|
||||||
"order_columns": ["created_at", "name", "role__name"]
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! info "Key format uses `__` as a separator for relationship chains."
|
|
||||||
A direct column `User.name` produces `"name"`. A relationship tuple `(User.role, Role.name)` produces `"role__name"`.
|
|
||||||
|
|
||||||
## Relationship loading
|
## Relationship loading
|
||||||
|
|
||||||
!!! info "Added in `v1.1`"
|
!!! info "Added in `v1.1`"
|
||||||
@@ -630,11 +656,12 @@ async def get_user(session: SessionDep, uuid: UUID) -> Response[UserRead]:
|
|||||||
)
|
)
|
||||||
|
|
||||||
@router.get("")
|
@router.get("")
|
||||||
async def list_users(
|
async def list_users(session: SessionDep, page: int = 1) -> OffsetPaginatedResponse[UserRead]:
|
||||||
session: SessionDep,
|
return await crud.UserCrud.offset_paginate(
|
||||||
params: Annotated[dict, Depends(crud.UserCrud.offset_paginate_params())],
|
session=session,
|
||||||
) -> OffsetPaginatedResponse[UserRead]:
|
page=page,
|
||||||
return await crud.UserCrud.offset_paginate(session=session, **params, schema=UserRead)
|
schema=UserRead,
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
The schema must have `from_attributes=True` (or inherit from [`PydanticBase`](../reference/schemas.md#fastapi_toolsets.schemas.PydanticBase)) so it can be built from SQLAlchemy model instances.
|
The schema must have `from_attributes=True` (or inherit from [`PydanticBase`](../reference/schemas.md#fastapi_toolsets.schemas.PydanticBase)) so it can be built from SQLAlchemy model instances.
|
||||||
|
|||||||
@@ -118,57 +118,6 @@ async def clean(db_session):
|
|||||||
await cleanup_tables(session=db_session, base=Base)
|
await cleanup_tables(session=db_session, base=Base)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Many-to-Many helpers
|
|
||||||
|
|
||||||
SQLAlchemy's ORM collection API triggers lazy-loads when you append to a relationship inside a savepoint (e.g. inside `lock_tables` or a nested `get_transaction`). The three `m2m_*` helpers bypass the ORM collection entirely and issue direct SQL against the association table.
|
|
||||||
|
|
||||||
### `m2m_add` — insert associations
|
|
||||||
|
|
||||||
[`m2m_add`](../reference/db.md#fastapi_toolsets.db.m2m_add) inserts one or more rows into a secondary table without touching the ORM collection:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from fastapi_toolsets.db import lock_tables, m2m_add
|
|
||||||
|
|
||||||
async with lock_tables(session, [Tag]):
|
|
||||||
tag = await TagCrud.create(session, TagCreate(name="python"))
|
|
||||||
await m2m_add(session, post, Post.tags, tag)
|
|
||||||
```
|
|
||||||
|
|
||||||
Pass `ignore_conflicts=True` to silently skip associations that already exist:
|
|
||||||
|
|
||||||
```python
|
|
||||||
await m2m_add(session, post, Post.tags, tag, ignore_conflicts=True)
|
|
||||||
```
|
|
||||||
|
|
||||||
### `m2m_remove` — delete associations
|
|
||||||
|
|
||||||
[`m2m_remove`](../reference/db.md#fastapi_toolsets.db.m2m_remove) deletes specific association rows. Removing a non-existent association is a no-op:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from fastapi_toolsets.db import get_transaction, m2m_remove
|
|
||||||
|
|
||||||
async with get_transaction(session):
|
|
||||||
await m2m_remove(session, post, Post.tags, tag1, tag2)
|
|
||||||
```
|
|
||||||
|
|
||||||
### `m2m_set` — replace the full set
|
|
||||||
|
|
||||||
[`m2m_set`](../reference/db.md#fastapi_toolsets.db.m2m_set) atomically replaces all associations: it deletes every existing row for the owner instance then inserts the new set. Passing no related instances clears the association entirely:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from fastapi_toolsets.db import get_transaction, m2m_set
|
|
||||||
|
|
||||||
# Replace all tags
|
|
||||||
async with get_transaction(session):
|
|
||||||
await m2m_set(session, post, Post.tags, tag_a, tag_b)
|
|
||||||
|
|
||||||
# Clear all tags
|
|
||||||
async with get_transaction(session):
|
|
||||||
await m2m_set(session, post, Post.tags)
|
|
||||||
```
|
|
||||||
|
|
||||||
All three helpers raise `TypeError` if the relationship attribute is not a Many-to-Many (i.e. has no secondary table).
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
[:material-api: API Reference](../reference/db.md)
|
[:material-api: API Reference](../reference/db.md)
|
||||||
|
|||||||
@@ -117,118 +117,139 @@ class Article(Base, UUIDMixin, TimestampMixin):
|
|||||||
title: Mapped[str]
|
title: Mapped[str]
|
||||||
```
|
```
|
||||||
|
|
||||||
## Lifecycle events
|
### [`WatchedFieldsMixin`](../reference/models.md#fastapi_toolsets.models.WatchedFieldsMixin)
|
||||||
|
|
||||||
The event system provides lifecycle callbacks that fire **after commit**. If the transaction rolls back, no callback fires.
|
!!! info "Added in `v2.4`"
|
||||||
|
|
||||||
### Setup
|
`WatchedFieldsMixin` provides lifecycle callbacks that fire **after commit** — meaning the row is durably persisted when your callback runs. If the transaction rolls back, no callback fires.
|
||||||
|
|
||||||
Event dispatch requires [`EventSession`](../reference/models.md#fastapi_toolsets.models.EventSession). Pass it as the session class when creating your session factory:
|
Three callbacks are available, each corresponding to a [`ModelEvent`](../reference/models.md#fastapi_toolsets.models.ModelEvent) value:
|
||||||
|
|
||||||
|
| Callback | Event | Trigger |
|
||||||
|
|---|---|---|
|
||||||
|
| `on_create()` | `ModelEvent.CREATE` | After `INSERT` |
|
||||||
|
| `on_delete()` | `ModelEvent.DELETE` | After `DELETE` |
|
||||||
|
| `on_update(changes)` | `ModelEvent.UPDATE` | After `UPDATE` on a watched field |
|
||||||
|
|
||||||
|
Server-side defaults (e.g. `id`, `created_at`) are fully populated in all callbacks. All callbacks support both `async def` and plain `def`. Use `@watch` to restrict which fields trigger `on_update`:
|
||||||
|
|
||||||
|
| Decorator | `on_update` behaviour |
|
||||||
|
|---|---|
|
||||||
|
| `@watch("status", "role")` | Only fires when `status` or `role` changes |
|
||||||
|
| *(no decorator)* | Fires when **any** mapped field changes |
|
||||||
|
|
||||||
|
`@watch` is inherited through the class hierarchy. If a subclass does not declare its own `@watch`, it uses the filter from the nearest decorated parent. Applying `@watch` on the subclass overrides the parent's filter:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
@watch("status")
|
||||||
from fastapi_toolsets.models import EventSession
|
class Order(Base, UUIDMixin, WatchedFieldsMixin):
|
||||||
|
|
||||||
engine = create_async_engine("postgresql+asyncpg://...")
|
|
||||||
SessionLocal = async_sessionmaker(engine, expire_on_commit=False, class_=EventSession)
|
|
||||||
```
|
|
||||||
|
|
||||||
!!! info "Callbacks fire on `session.commit()` only — not on savepoints."
|
|
||||||
Savepoints created by [`get_transaction`](db.md) or `begin_nested()` do **not**
|
|
||||||
trigger callbacks. All events accumulated across flushes are dispatched once
|
|
||||||
when the outermost `commit()` is called.
|
|
||||||
|
|
||||||
### Events
|
|
||||||
|
|
||||||
Three event types are available, each corresponding to a [`ModelEvent`](../reference/models.md#fastapi_toolsets.models.ModelEvent) value:
|
|
||||||
|
|
||||||
| Event | Trigger |
|
|
||||||
|---|---|
|
|
||||||
| `ModelEvent.CREATE` | After `INSERT` commit |
|
|
||||||
| `ModelEvent.DELETE` | After `DELETE` commit |
|
|
||||||
| `ModelEvent.UPDATE` | After `UPDATE` commit on a watched field |
|
|
||||||
|
|
||||||
!!! warning "Callbacks fire only for ORM-level changes. Rows updated via raw SQL (`UPDATE ... SET ...`) are not detected."
|
|
||||||
|
|
||||||
### Watched fields
|
|
||||||
|
|
||||||
Set `__watched_fields__` on the model to restrict which field changes trigger `UPDATE` events. It must be a `tuple[str, ...]` — any other type raises `TypeError`:
|
|
||||||
|
|
||||||
| Class attribute | `UPDATE` behaviour |
|
|
||||||
|---|---|
|
|
||||||
| `__watched_fields__ = ("status", "role")` | Only fires when `status` or `role` changes |
|
|
||||||
| *(not set)* | Fires when **any** mapped field changes |
|
|
||||||
|
|
||||||
`__watched_fields__` is inherited through the class hierarchy via normal Python MRO. A subclass can override it:
|
|
||||||
|
|
||||||
```python
|
|
||||||
class Order(Base, UUIDMixin):
|
|
||||||
__watched_fields__ = ("status",)
|
|
||||||
...
|
...
|
||||||
|
|
||||||
class UrgentOrder(Order):
|
class UrgentOrder(Order):
|
||||||
# inherits __watched_fields__ = ("status",)
|
# inherits @watch("status") — on_update fires only for status changes
|
||||||
...
|
...
|
||||||
|
|
||||||
|
@watch("priority")
|
||||||
class PriorityOrder(Order):
|
class PriorityOrder(Order):
|
||||||
__watched_fields__ = ("priority",)
|
# overrides parent — on_update fires only for priority changes
|
||||||
# overrides parent — UPDATE fires only for priority changes
|
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
### Registering handlers
|
#### Option 1 — catch-all with `on_event`
|
||||||
|
|
||||||
Register handlers with the [`listens_for`](../reference/models.md#fastapi_toolsets.models.listens_for) decorator. Every callback receives three arguments: the model instance, the [`ModelEvent`](../reference/models.md#fastapi_toolsets.models.ModelEvent) that triggered it, and a `changes` dict (`None` for `CREATE` and `DELETE`):
|
Override `on_event` to handle all event types in one place. The specific methods delegate here by default:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from fastapi_toolsets.models import ModelEvent, UUIDMixin, listens_for
|
from fastapi_toolsets.models import ModelEvent, UUIDMixin, WatchedFieldsMixin, watch
|
||||||
|
|
||||||
class Order(Base, UUIDMixin):
|
@watch("status")
|
||||||
|
class Order(Base, UUIDMixin, WatchedFieldsMixin):
|
||||||
__tablename__ = "orders"
|
__tablename__ = "orders"
|
||||||
__watched_fields__ = ("status",)
|
|
||||||
|
|
||||||
status: Mapped[str]
|
status: Mapped[str]
|
||||||
|
|
||||||
@listens_for(Order, [ModelEvent.CREATE])
|
async def on_event(self, event: ModelEvent, changes: dict | None = None) -> None:
|
||||||
async def on_order_created(order: Order, event_type: ModelEvent, changes: None):
|
if event == ModelEvent.CREATE:
|
||||||
await notify_new_order(order.id)
|
await notify_new_order(self.id)
|
||||||
|
elif event == ModelEvent.DELETE:
|
||||||
@listens_for(Order, [ModelEvent.DELETE])
|
await notify_order_cancelled(self.id)
|
||||||
async def on_order_deleted(order: Order, event_type: ModelEvent, changes: None):
|
elif event == ModelEvent.UPDATE:
|
||||||
await notify_order_cancelled(order.id)
|
await notify_status_change(self.id, changes["status"])
|
||||||
|
|
||||||
@listens_for(Order, [ModelEvent.UPDATE])
|
|
||||||
async def on_order_updated(order: Order, event_type: ModelEvent, changes: dict):
|
|
||||||
if "status" in changes:
|
|
||||||
await notify_status_change(order.id, changes["status"])
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Multiple handlers can be registered for the same model and event. Handlers registered on a parent class also fire for subclass instances.
|
#### Option 2 — targeted overrides
|
||||||
|
|
||||||
A single handler can listen for multiple events at once. When `event_types` is omitted, the handler fires for all events:
|
Override individual methods for more focused logic:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@listens_for(Order, [ModelEvent.CREATE, ModelEvent.UPDATE])
|
@watch("status")
|
||||||
async def on_order_changed(order: Order, event_type: ModelEvent, changes: dict | None):
|
class Order(Base, UUIDMixin, WatchedFieldsMixin):
|
||||||
await invalidate_cache(order.id)
|
__tablename__ = "orders"
|
||||||
|
|
||||||
@listens_for(Order) # all events
|
status: Mapped[str]
|
||||||
async def on_any_order_event(order: Order, event_type: ModelEvent, changes: dict | None):
|
|
||||||
await audit_log(order.id, event_type)
|
async def on_create(self) -> None:
|
||||||
|
await notify_new_order(self.id)
|
||||||
|
|
||||||
|
async def on_delete(self) -> None:
|
||||||
|
await notify_order_cancelled(self.id)
|
||||||
|
|
||||||
|
async def on_update(self, changes: dict) -> None:
|
||||||
|
if "status" in changes:
|
||||||
|
old = changes["status"]["old"]
|
||||||
|
new = changes["status"]["new"]
|
||||||
|
await notify_status_change(self.id, old, new)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Field changes format
|
#### Field changes format
|
||||||
|
|
||||||
The `changes` dict maps each watched field that changed to `{"old": ..., "new": ...}`. Only fields that actually changed are included. For `CREATE` and `DELETE` events, `changes` is `None`:
|
The `changes` dict maps each watched field that changed to `{"old": ..., "new": ...}`. Only fields that actually changed are included:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# CREATE / DELETE → changes is None
|
|
||||||
# status changed → {"status": {"old": "pending", "new": "shipped"}}
|
# status changed → {"status": {"old": "pending", "new": "shipped"}}
|
||||||
# two fields changed → {"status": {...}, "assigned_to": {...}}
|
# two fields changed → {"status": {...}, "assigned_to": {...}}
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! info "Multiple flushes in one transaction are merged: the earliest `old` and latest `new` are preserved, and `on_update` fires only once per commit."
|
!!! info "Multiple flushes in one transaction are merged: the earliest `old` and latest `new` are preserved, and `on_update` fires only once per commit."
|
||||||
|
|
||||||
|
!!! warning "Callbacks fire only for ORM-level changes. Rows updated via raw SQL (`UPDATE ... SET ...`) are not detected."
|
||||||
|
|
||||||
|
!!! warning "Callbacks fire when the **outermost active context** (savepoint or transaction) commits."
|
||||||
|
If you create several related objects using `CrudFactory.create` and need
|
||||||
|
callbacks to see all of them (including associations), wrap the whole
|
||||||
|
operation in a single [`get_transaction`](db.md) or [`lock_tables`](db.md)
|
||||||
|
block. Without it, each `create` call commits its own savepoint and
|
||||||
|
`on_create` fires before the remaining objects exist.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi_toolsets.db import get_transaction
|
||||||
|
|
||||||
|
async with get_transaction(session):
|
||||||
|
order = await OrderCrud.create(session, order_data)
|
||||||
|
item = await ItemCrud.create(session, item_data)
|
||||||
|
await session.refresh(order, attribute_names=["items"])
|
||||||
|
order.items.append(item)
|
||||||
|
# on_create fires here for both order and item,
|
||||||
|
# with the full association already committed.
|
||||||
|
```
|
||||||
|
|
||||||
|
## Composing mixins
|
||||||
|
|
||||||
|
All mixins can be combined in any order. The only constraint is that exactly one primary key must be defined — either via `UUIDMixin` or directly on the model.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi_toolsets.models import UUIDMixin, TimestampMixin
|
||||||
|
|
||||||
|
class Event(Base, UUIDMixin, TimestampMixin):
|
||||||
|
__tablename__ = "events"
|
||||||
|
name: Mapped[str]
|
||||||
|
|
||||||
|
class Counter(Base, UpdatedAtMixin):
|
||||||
|
__tablename__ = "counters"
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
|
value: Mapped[int]
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
[:material-api: API Reference](../reference/models.md)
|
[:material-api: API Reference](../reference/models.md)
|
||||||
|
|||||||
@@ -79,6 +79,9 @@ The examples above are already compatible with parallel test execution with `pyt
|
|||||||
|
|
||||||
## Cleaning up tables
|
## Cleaning up tables
|
||||||
|
|
||||||
|
!!! warning
|
||||||
|
Since `V2.1.0` `cleanup_tables` now live in `fastapi_toolsets.db`. For backward compatibility the function is still available in `fastapi_toolsets.pytest`, but this will be remove in `V3.0.0`.
|
||||||
|
|
||||||
If you want to manually clean up a database you can use [`cleanup_tables`](../reference/db.md#fastapi_toolsets.db.cleanup_tables), this will truncate all tables between tests for fast isolation:
|
If you want to manually clean up a database you can use [`cleanup_tables`](../reference/db.md#fastapi_toolsets.db.cleanup_tables), this will truncate all tables between tests for fast isolation:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|||||||
@@ -13,9 +13,6 @@ from fastapi_toolsets.db import (
|
|||||||
create_db_context,
|
create_db_context,
|
||||||
get_transaction,
|
get_transaction,
|
||||||
lock_tables,
|
lock_tables,
|
||||||
m2m_add,
|
|
||||||
m2m_remove,
|
|
||||||
m2m_set,
|
|
||||||
wait_for_row_change,
|
wait_for_row_change,
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
@@ -35,9 +32,3 @@ from fastapi_toolsets.db import (
|
|||||||
## ::: fastapi_toolsets.db.create_database
|
## ::: fastapi_toolsets.db.create_database
|
||||||
|
|
||||||
## ::: fastapi_toolsets.db.cleanup_tables
|
## ::: fastapi_toolsets.db.cleanup_tables
|
||||||
|
|
||||||
## ::: fastapi_toolsets.db.m2m_add
|
|
||||||
|
|
||||||
## ::: fastapi_toolsets.db.m2m_remove
|
|
||||||
|
|
||||||
## ::: fastapi_toolsets.db.m2m_set
|
|
||||||
|
|||||||
@@ -6,19 +6,17 @@ You can import them directly from `fastapi_toolsets.models`:
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
from fastapi_toolsets.models import (
|
from fastapi_toolsets.models import (
|
||||||
EventSession,
|
|
||||||
ModelEvent,
|
ModelEvent,
|
||||||
UUIDMixin,
|
UUIDMixin,
|
||||||
UUIDv7Mixin,
|
UUIDv7Mixin,
|
||||||
CreatedAtMixin,
|
CreatedAtMixin,
|
||||||
UpdatedAtMixin,
|
UpdatedAtMixin,
|
||||||
TimestampMixin,
|
TimestampMixin,
|
||||||
listens_for,
|
WatchedFieldsMixin,
|
||||||
|
watch,
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
## ::: fastapi_toolsets.models.EventSession
|
|
||||||
|
|
||||||
## ::: fastapi_toolsets.models.ModelEvent
|
## ::: fastapi_toolsets.models.ModelEvent
|
||||||
|
|
||||||
## ::: fastapi_toolsets.models.UUIDMixin
|
## ::: fastapi_toolsets.models.UUIDMixin
|
||||||
@@ -31,4 +29,6 @@ from fastapi_toolsets.models import (
|
|||||||
|
|
||||||
## ::: fastapi_toolsets.models.TimestampMixin
|
## ::: fastapi_toolsets.models.TimestampMixin
|
||||||
|
|
||||||
## ::: fastapi_toolsets.models.listens_for
|
## ::: fastapi_toolsets.models.WatchedFieldsMixin
|
||||||
|
|
||||||
|
## ::: fastapi_toolsets.models.watch
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from typing import Annotated
|
|||||||
|
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
|
|
||||||
|
from fastapi_toolsets.crud import OrderByClause
|
||||||
from fastapi_toolsets.schemas import (
|
from fastapi_toolsets.schemas import (
|
||||||
CursorPaginatedResponse,
|
CursorPaginatedResponse,
|
||||||
OffsetPaginatedResponse,
|
OffsetPaginatedResponse,
|
||||||
@@ -21,18 +22,21 @@ async def list_articles_offset(
|
|||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
params: Annotated[
|
params: Annotated[
|
||||||
dict,
|
dict,
|
||||||
Depends(
|
Depends(ArticleCrud.offset_params(default_page_size=20, max_page_size=100)),
|
||||||
ArticleCrud.offset_paginate_params(
|
|
||||||
default_page_size=20,
|
|
||||||
max_page_size=100,
|
|
||||||
default_order_field=Article.created_at,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
|
filter_by: Annotated[dict[str, list[str]], Depends(ArticleCrud.filter_params())],
|
||||||
|
order_by: Annotated[
|
||||||
|
OrderByClause | None,
|
||||||
|
Depends(ArticleCrud.order_params(default_field=Article.created_at)),
|
||||||
|
],
|
||||||
|
search: str | None = None,
|
||||||
) -> OffsetPaginatedResponse[ArticleRead]:
|
) -> OffsetPaginatedResponse[ArticleRead]:
|
||||||
return await ArticleCrud.offset_paginate(
|
return await ArticleCrud.offset_paginate(
|
||||||
session=session,
|
session=session,
|
||||||
**params,
|
**params,
|
||||||
|
search=search,
|
||||||
|
filter_by=filter_by or None,
|
||||||
|
order_by=order_by,
|
||||||
schema=ArticleRead,
|
schema=ArticleRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,18 +46,21 @@ async def list_articles_cursor(
|
|||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
params: Annotated[
|
params: Annotated[
|
||||||
dict,
|
dict,
|
||||||
Depends(
|
Depends(ArticleCrud.cursor_params(default_page_size=20, max_page_size=100)),
|
||||||
ArticleCrud.cursor_paginate_params(
|
|
||||||
default_page_size=20,
|
|
||||||
max_page_size=100,
|
|
||||||
default_order_field=Article.created_at,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
|
filter_by: Annotated[dict[str, list[str]], Depends(ArticleCrud.filter_params())],
|
||||||
|
order_by: Annotated[
|
||||||
|
OrderByClause | None,
|
||||||
|
Depends(ArticleCrud.order_params(default_field=Article.created_at)),
|
||||||
|
],
|
||||||
|
search: str | None = None,
|
||||||
) -> CursorPaginatedResponse[ArticleRead]:
|
) -> CursorPaginatedResponse[ArticleRead]:
|
||||||
return await ArticleCrud.cursor_paginate(
|
return await ArticleCrud.cursor_paginate(
|
||||||
session=session,
|
session=session,
|
||||||
**params,
|
**params,
|
||||||
|
search=search,
|
||||||
|
filter_by=filter_by or None,
|
||||||
|
order_by=order_by,
|
||||||
schema=ArticleRead,
|
schema=ArticleRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -63,17 +70,20 @@ async def list_articles(
|
|||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
params: Annotated[
|
params: Annotated[
|
||||||
dict,
|
dict,
|
||||||
Depends(
|
Depends(ArticleCrud.paginate_params(default_page_size=20, max_page_size=100)),
|
||||||
ArticleCrud.paginate_params(
|
|
||||||
default_page_size=20,
|
|
||||||
max_page_size=100,
|
|
||||||
default_order_field=Article.created_at,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
|
filter_by: Annotated[dict[str, list[str]], Depends(ArticleCrud.filter_params())],
|
||||||
|
order_by: Annotated[
|
||||||
|
OrderByClause | None,
|
||||||
|
Depends(ArticleCrud.order_params(default_field=Article.created_at)),
|
||||||
|
],
|
||||||
|
search: str | None = None,
|
||||||
) -> PaginatedResponse[ArticleRead]:
|
) -> PaginatedResponse[ArticleRead]:
|
||||||
return await ArticleCrud.paginate(
|
return await ArticleCrud.paginate(
|
||||||
session,
|
session,
|
||||||
**params,
|
**params,
|
||||||
|
search=search,
|
||||||
|
filter_by=filter_by or None,
|
||||||
|
order_by=order_by,
|
||||||
schema=ArticleRead,
|
schema=ArticleRead,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "fastapi-toolsets"
|
name = "fastapi-toolsets"
|
||||||
version = "3.1.0"
|
version = "2.4.3"
|
||||||
description = "Production-ready utilities for FastAPI applications"
|
description = "Production-ready utilities for FastAPI applications"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|||||||
@@ -21,4 +21,4 @@ Example usage:
|
|||||||
return Response(data={"user": user.username}, message="Success")
|
return Response(data={"user": user.username}, message="Success")
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version__ = "3.1.0"
|
__version__ = "2.4.3"
|
||||||
|
|||||||
@@ -1,18 +1,12 @@
|
|||||||
"""Generic async CRUD operations for SQLAlchemy models."""
|
"""Generic async CRUD operations for SQLAlchemy models."""
|
||||||
|
|
||||||
from ..exceptions import (
|
from ..exceptions import InvalidFacetFilterError, NoSearchableFieldsError
|
||||||
InvalidFacetFilterError,
|
|
||||||
InvalidSearchColumnError,
|
|
||||||
NoSearchableFieldsError,
|
|
||||||
UnsupportedFacetTypeError,
|
|
||||||
)
|
|
||||||
from ..schemas import PaginationType
|
from ..schemas import PaginationType
|
||||||
from ..types import (
|
from ..types import (
|
||||||
FacetFieldType,
|
FacetFieldType,
|
||||||
JoinType,
|
JoinType,
|
||||||
M2MFieldType,
|
M2MFieldType,
|
||||||
OrderByClause,
|
OrderByClause,
|
||||||
OrderFieldType,
|
|
||||||
SearchFieldType,
|
SearchFieldType,
|
||||||
)
|
)
|
||||||
from .factory import AsyncCrud, CrudFactory
|
from .factory import AsyncCrud, CrudFactory
|
||||||
@@ -24,14 +18,11 @@ __all__ = [
|
|||||||
"FacetFieldType",
|
"FacetFieldType",
|
||||||
"get_searchable_fields",
|
"get_searchable_fields",
|
||||||
"InvalidFacetFilterError",
|
"InvalidFacetFilterError",
|
||||||
"InvalidSearchColumnError",
|
|
||||||
"JoinType",
|
"JoinType",
|
||||||
"M2MFieldType",
|
"M2MFieldType",
|
||||||
"NoSearchableFieldsError",
|
"NoSearchableFieldsError",
|
||||||
"OrderByClause",
|
"OrderByClause",
|
||||||
"OrderFieldType",
|
|
||||||
"PaginationType",
|
"PaginationType",
|
||||||
"SearchConfig",
|
"SearchConfig",
|
||||||
"SearchFieldType",
|
"SearchFieldType",
|
||||||
"UnsupportedFacetTypeError",
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ from ..types import (
|
|||||||
M2MFieldType,
|
M2MFieldType,
|
||||||
ModelType,
|
ModelType,
|
||||||
OrderByClause,
|
OrderByClause,
|
||||||
OrderFieldType,
|
|
||||||
SchemaType,
|
SchemaType,
|
||||||
SearchFieldType,
|
SearchFieldType,
|
||||||
)
|
)
|
||||||
@@ -48,7 +47,6 @@ from .search import (
|
|||||||
build_filter_by,
|
build_filter_by,
|
||||||
build_search_filters,
|
build_search_filters,
|
||||||
facet_keys,
|
facet_keys,
|
||||||
search_field_keys,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -117,12 +115,8 @@ def _apply_joins(q: Any, joins: JoinType | None, outer_join: bool) -> Any:
|
|||||||
|
|
||||||
def _apply_search_joins(q: Any, search_joins: list[Any]) -> Any:
|
def _apply_search_joins(q: Any, search_joins: list[Any]) -> Any:
|
||||||
"""Apply relationship-based outer joins (from search/filter_by) to a query."""
|
"""Apply relationship-based outer joins (from search/filter_by) to a query."""
|
||||||
seen: set[str] = set()
|
|
||||||
for join_rel in search_joins:
|
for join_rel in search_joins:
|
||||||
key = str(join_rel)
|
q = q.outerjoin(join_rel)
|
||||||
if key not in seen:
|
|
||||||
seen.add(key)
|
|
||||||
q = q.outerjoin(join_rel)
|
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
|
||||||
@@ -135,7 +129,7 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
model: ClassVar[type[DeclarativeBase]]
|
model: ClassVar[type[DeclarativeBase]]
|
||||||
searchable_fields: ClassVar[Sequence[SearchFieldType] | None] = None
|
searchable_fields: ClassVar[Sequence[SearchFieldType] | None] = None
|
||||||
facet_fields: ClassVar[Sequence[FacetFieldType] | None] = None
|
facet_fields: ClassVar[Sequence[FacetFieldType] | None] = None
|
||||||
order_fields: ClassVar[Sequence[OrderFieldType] | None] = None
|
order_fields: ClassVar[Sequence[QueryableAttribute[Any]] | None] = None
|
||||||
m2m_fields: ClassVar[M2MFieldType | None] = None
|
m2m_fields: ClassVar[M2MFieldType | None] = None
|
||||||
default_load_options: ClassVar[Sequence[ExecutableOption] | None] = None
|
default_load_options: ClassVar[Sequence[ExecutableOption] | None] = None
|
||||||
cursor_column: ClassVar[Any | None] = None
|
cursor_column: ClassVar[Any | None] = None
|
||||||
@@ -170,18 +164,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
return load_options
|
return load_options
|
||||||
return cls.default_load_options
|
return cls.default_load_options
|
||||||
|
|
||||||
@classmethod
|
|
||||||
async def _reload_with_options(
|
|
||||||
cls: type[Self], session: AsyncSession, instance: ModelType
|
|
||||||
) -> ModelType:
|
|
||||||
"""Re-query instance by PK with default_load_options applied."""
|
|
||||||
mapper = cls.model.__mapper__
|
|
||||||
pk_filters = [
|
|
||||||
getattr(cls.model, col.key) == getattr(instance, col.key)
|
|
||||||
for col in mapper.primary_key
|
|
||||||
]
|
|
||||||
return await cls.get(session, filters=pk_filters)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def _resolve_m2m(
|
async def _resolve_m2m(
|
||||||
cls: type[Self],
|
cls: type[Self],
|
||||||
@@ -281,304 +263,118 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _resolve_search_columns(
|
def filter_params(
|
||||||
cls: type[Self],
|
|
||||||
search_fields: Sequence[SearchFieldType] | None,
|
|
||||||
) -> list[str] | None:
|
|
||||||
"""Return search column keys, or None if no searchable fields configured."""
|
|
||||||
fields = search_fields if search_fields is not None else cls.searchable_fields
|
|
||||||
if not fields:
|
|
||||||
return None
|
|
||||||
return search_field_keys(fields)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _resolve_order_columns(
|
|
||||||
cls: type[Self],
|
|
||||||
order_fields: Sequence[OrderFieldType] | None,
|
|
||||||
) -> list[str] | None:
|
|
||||||
"""Return sort column keys, or None if no order fields configured."""
|
|
||||||
fields = order_fields if order_fields is not None else cls.order_fields
|
|
||||||
if not fields:
|
|
||||||
return None
|
|
||||||
return sorted(facet_keys(fields))
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _build_paginate_params(
|
|
||||||
cls: type[Self],
|
cls: type[Self],
|
||||||
*,
|
*,
|
||||||
pagination_params: list[inspect.Parameter],
|
facet_fields: Sequence[FacetFieldType] | None = None,
|
||||||
pagination_fixed: dict[str, Any],
|
) -> Callable[..., Awaitable[dict[str, list[str]]]]:
|
||||||
dep_name: str,
|
"""Return a FastAPI dependency that collects facet filter values from query parameters.
|
||||||
search: bool,
|
|
||||||
filter: bool,
|
|
||||||
order: bool,
|
|
||||||
search_fields: Sequence[SearchFieldType] | None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None,
|
|
||||||
order_fields: Sequence[OrderFieldType] | None,
|
|
||||||
default_order_field: QueryableAttribute[Any] | None,
|
|
||||||
default_order: Literal["asc", "desc"],
|
|
||||||
) -> Callable[..., Awaitable[dict[str, Any]]]:
|
|
||||||
"""Build a consolidated FastAPI dependency that merges pagination, search, filter, and order params."""
|
|
||||||
all_params: list[inspect.Parameter] = list(pagination_params)
|
|
||||||
pagination_param_names = tuple(p.name for p in pagination_params)
|
|
||||||
reserved_names: set[str] = set(pagination_param_names)
|
|
||||||
|
|
||||||
search_keys: list[str] | None = None
|
Args:
|
||||||
if search:
|
facet_fields: Override the facet fields for this dependency. Falls back to the
|
||||||
search_keys = cls._resolve_search_columns(search_fields)
|
class-level ``facet_fields`` if not provided.
|
||||||
if search_keys:
|
|
||||||
all_params.extend(
|
|
||||||
[
|
|
||||||
inspect.Parameter(
|
|
||||||
"search",
|
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
|
||||||
annotation=str | None,
|
|
||||||
default=Query(
|
|
||||||
default=None, description="Search query string"
|
|
||||||
),
|
|
||||||
),
|
|
||||||
inspect.Parameter(
|
|
||||||
"search_column",
|
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
|
||||||
annotation=str | None,
|
|
||||||
default=Query(
|
|
||||||
default=None,
|
|
||||||
description="Restrict search to a single column",
|
|
||||||
enum=search_keys,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
reserved_names.update({"search", "search_column"})
|
|
||||||
|
|
||||||
filter_keys: list[str] | None = None
|
Returns:
|
||||||
if filter:
|
An async dependency function named ``{Model}FilterParams`` that resolves to a
|
||||||
resolved_facets = cls._resolve_facet_fields(facet_fields)
|
``dict[str, list[str]]`` containing only the keys that were supplied in the
|
||||||
if resolved_facets:
|
request (absent/``None`` parameters are excluded).
|
||||||
filter_keys = facet_keys(resolved_facets)
|
|
||||||
for k in filter_keys:
|
|
||||||
if k in reserved_names:
|
|
||||||
raise ValueError(
|
|
||||||
f"Facet field key {k!r} conflicts with a reserved "
|
|
||||||
f"parameter name. Reserved names: {sorted(reserved_names)}"
|
|
||||||
)
|
|
||||||
all_params.extend(
|
|
||||||
inspect.Parameter(
|
|
||||||
k,
|
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
|
||||||
annotation=list[str] | None,
|
|
||||||
default=Query(default=None),
|
|
||||||
)
|
|
||||||
for k in filter_keys
|
|
||||||
)
|
|
||||||
reserved_names.update(filter_keys)
|
|
||||||
|
|
||||||
order_field_map: dict[str, OrderFieldType] | None = None
|
Raises:
|
||||||
order_valid_keys: list[str] | None = None
|
ValueError: If no facet fields are configured on this CRUD class and none are
|
||||||
if order:
|
provided via ``facet_fields``.
|
||||||
resolved_order = (
|
"""
|
||||||
order_fields if order_fields is not None else cls.order_fields
|
fields = cls._resolve_facet_fields(facet_fields)
|
||||||
|
if not fields:
|
||||||
|
raise ValueError(
|
||||||
|
f"{cls.__name__} has no facet_fields configured. "
|
||||||
|
"Pass facet_fields= or set them on CrudFactory."
|
||||||
)
|
)
|
||||||
if resolved_order:
|
keys = facet_keys(fields)
|
||||||
keys = facet_keys(resolved_order)
|
|
||||||
order_field_map = dict(zip(keys, resolved_order))
|
|
||||||
order_valid_keys = sorted(order_field_map.keys())
|
|
||||||
all_params.extend(
|
|
||||||
[
|
|
||||||
inspect.Parameter(
|
|
||||||
"order_by",
|
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
|
||||||
annotation=str | None,
|
|
||||||
default=Query(
|
|
||||||
None,
|
|
||||||
description=f"Field to order by. Valid values: {order_valid_keys}",
|
|
||||||
enum=order_valid_keys,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
inspect.Parameter(
|
|
||||||
"order",
|
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
|
||||||
annotation=Literal["asc", "desc"],
|
|
||||||
default=Query(default_order, description="Sort direction"),
|
|
||||||
),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
async def dependency(**kwargs: Any) -> dict[str, Any]:
|
async def dependency(**kwargs: Any) -> dict[str, list[str]]:
|
||||||
result: dict[str, Any] = dict(pagination_fixed)
|
return {k: v for k, v in kwargs.items() if v is not None}
|
||||||
for name in pagination_param_names:
|
|
||||||
result[name] = kwargs[name]
|
|
||||||
|
|
||||||
if search_keys is not None:
|
dependency.__name__ = f"{cls.model.__name__}FilterParams"
|
||||||
search_val = kwargs.get("search")
|
|
||||||
if search_val is not None:
|
|
||||||
result["search"] = search_val
|
|
||||||
search_col_val = kwargs.get("search_column")
|
|
||||||
if search_col_val is not None:
|
|
||||||
result["search_column"] = search_col_val
|
|
||||||
|
|
||||||
if filter_keys is not None:
|
|
||||||
filter_by = {
|
|
||||||
k: kwargs[k] for k in filter_keys if kwargs.get(k) is not None
|
|
||||||
}
|
|
||||||
result["filter_by"] = filter_by or None
|
|
||||||
|
|
||||||
if order_field_map is not None:
|
|
||||||
order_by_val = kwargs.get("order_by")
|
|
||||||
order_dir = kwargs.get("order", default_order)
|
|
||||||
if order_by_val is None:
|
|
||||||
field = default_order_field
|
|
||||||
elif order_by_val not in order_field_map:
|
|
||||||
raise InvalidOrderFieldError(order_by_val, order_valid_keys or [])
|
|
||||||
else:
|
|
||||||
field = order_field_map[order_by_val]
|
|
||||||
if field is not None:
|
|
||||||
if isinstance(field, tuple):
|
|
||||||
col = field[-1]
|
|
||||||
result["order_by"] = (
|
|
||||||
col.asc() if order_dir == "asc" else col.desc()
|
|
||||||
)
|
|
||||||
result["order_joins"] = list(field[:-1])
|
|
||||||
else:
|
|
||||||
result["order_by"] = (
|
|
||||||
field.asc() if order_dir == "asc" else field.desc()
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
result["order_by"] = None
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
dependency.__name__ = dep_name
|
|
||||||
dependency.__signature__ = inspect.Signature( # type: ignore[attr-defined] # ty:ignore[unresolved-attribute]
|
dependency.__signature__ = inspect.Signature( # type: ignore[attr-defined] # ty:ignore[unresolved-attribute]
|
||||||
parameters=all_params,
|
parameters=[
|
||||||
|
inspect.Parameter(
|
||||||
|
k,
|
||||||
|
inspect.Parameter.KEYWORD_ONLY,
|
||||||
|
annotation=list[str] | None,
|
||||||
|
default=Query(default=None),
|
||||||
|
)
|
||||||
|
for k in keys
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
return dependency
|
return dependency
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def offset_paginate_params(
|
def offset_params(
|
||||||
cls: type[Self],
|
cls: type[Self],
|
||||||
*,
|
*,
|
||||||
default_page_size: int = 20,
|
default_page_size: int = 20,
|
||||||
max_page_size: int = 100,
|
max_page_size: int = 100,
|
||||||
include_total: bool = True,
|
include_total: bool = True,
|
||||||
search: bool = True,
|
|
||||||
filter: bool = True,
|
|
||||||
order: bool = True,
|
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
|
||||||
order_fields: Sequence[OrderFieldType] | None = None,
|
|
||||||
default_order_field: QueryableAttribute[Any] | None = None,
|
|
||||||
default_order: Literal["asc", "desc"] = "asc",
|
|
||||||
) -> Callable[..., Awaitable[dict[str, Any]]]:
|
) -> Callable[..., Awaitable[dict[str, Any]]]:
|
||||||
"""Return a FastAPI dependency that collects all params for :meth:`offset_paginate`.
|
"""Return a FastAPI dependency that collects offset pagination params from query params.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
default_page_size: Default ``items_per_page`` value.
|
default_page_size: Default value for the ``items_per_page`` query parameter.
|
||||||
max_page_size: Maximum ``items_per_page`` value.
|
max_page_size: Maximum allowed value for ``items_per_page`` (enforced via
|
||||||
include_total: Whether to include total count (not a query param).
|
``le`` on the ``Query``).
|
||||||
search: Enable search query parameters.
|
include_total: Server-side flag forwarded as-is to ``include_total`` in
|
||||||
filter: Enable facet filter query parameters.
|
:meth:`offset_paginate`. Not exposed as a query parameter.
|
||||||
order: Enable order query parameters.
|
|
||||||
search_fields: Override searchable fields.
|
|
||||||
facet_fields: Override facet fields.
|
|
||||||
order_fields: Override order fields.
|
|
||||||
default_order_field: Default field to order by when ``order_by`` is absent.
|
|
||||||
default_order: Default sort direction.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
An async dependency that resolves to a dict ready to be unpacked
|
An async dependency that resolves to a dict with ``page``,
|
||||||
into :meth:`offset_paginate`.
|
``items_per_page``, and ``include_total`` keys, ready to be
|
||||||
|
unpacked into :meth:`offset_paginate`.
|
||||||
"""
|
"""
|
||||||
pagination_params = [
|
|
||||||
inspect.Parameter(
|
async def dependency(
|
||||||
"page",
|
page: int = Query(1, ge=1, description="Page number (1-indexed)"),
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
items_per_page: int = _page_size_query(default_page_size, max_page_size),
|
||||||
annotation=int,
|
) -> dict[str, Any]:
|
||||||
default=Query(1, ge=1, description="Page number (1-indexed)"),
|
return {
|
||||||
),
|
"page": page,
|
||||||
inspect.Parameter(
|
"items_per_page": items_per_page,
|
||||||
"items_per_page",
|
"include_total": include_total,
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
}
|
||||||
annotation=int,
|
|
||||||
default=_page_size_query(default_page_size, max_page_size),
|
dependency.__name__ = f"{cls.model.__name__}OffsetParams"
|
||||||
),
|
return dependency
|
||||||
]
|
|
||||||
return cls._build_paginate_params(
|
|
||||||
pagination_params=pagination_params,
|
|
||||||
pagination_fixed={"include_total": include_total},
|
|
||||||
dep_name=f"{cls.model.__name__}OffsetPaginateParams",
|
|
||||||
search=search,
|
|
||||||
filter=filter,
|
|
||||||
order=order,
|
|
||||||
search_fields=search_fields,
|
|
||||||
facet_fields=facet_fields,
|
|
||||||
order_fields=order_fields,
|
|
||||||
default_order_field=default_order_field,
|
|
||||||
default_order=default_order,
|
|
||||||
)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def cursor_paginate_params(
|
def cursor_params(
|
||||||
cls: type[Self],
|
cls: type[Self],
|
||||||
*,
|
*,
|
||||||
default_page_size: int = 20,
|
default_page_size: int = 20,
|
||||||
max_page_size: int = 100,
|
max_page_size: int = 100,
|
||||||
search: bool = True,
|
|
||||||
filter: bool = True,
|
|
||||||
order: bool = True,
|
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
|
||||||
order_fields: Sequence[OrderFieldType] | None = None,
|
|
||||||
default_order_field: QueryableAttribute[Any] | None = None,
|
|
||||||
default_order: Literal["asc", "desc"] = "asc",
|
|
||||||
) -> Callable[..., Awaitable[dict[str, Any]]]:
|
) -> Callable[..., Awaitable[dict[str, Any]]]:
|
||||||
"""Return a FastAPI dependency that collects all params for :meth:`cursor_paginate`.
|
"""Return a FastAPI dependency that collects cursor pagination params from query params.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
default_page_size: Default ``items_per_page`` value.
|
default_page_size: Default value for the ``items_per_page`` query parameter.
|
||||||
max_page_size: Maximum ``items_per_page`` value.
|
max_page_size: Maximum allowed value for ``items_per_page`` (enforced via
|
||||||
search: Enable search query parameters.
|
``le`` on the ``Query``).
|
||||||
filter: Enable facet filter query parameters.
|
|
||||||
order: Enable order query parameters.
|
|
||||||
search_fields: Override searchable fields.
|
|
||||||
facet_fields: Override facet fields.
|
|
||||||
order_fields: Override order fields.
|
|
||||||
default_order_field: Default field to order by when ``order_by`` is absent.
|
|
||||||
default_order: Default sort direction.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
An async dependency that resolves to a dict ready to be unpacked
|
An async dependency that resolves to a dict with ``cursor`` and
|
||||||
into :meth:`cursor_paginate`.
|
``items_per_page`` keys, ready to be unpacked into
|
||||||
|
:meth:`cursor_paginate`.
|
||||||
"""
|
"""
|
||||||
pagination_params = [
|
|
||||||
inspect.Parameter(
|
async def dependency(
|
||||||
"cursor",
|
cursor: str | None = Query(
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
None, description="Cursor token from a previous response"
|
||||||
annotation=str | None,
|
|
||||||
default=Query(
|
|
||||||
None, description="Cursor token from a previous response"
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
inspect.Parameter(
|
items_per_page: int = _page_size_query(default_page_size, max_page_size),
|
||||||
"items_per_page",
|
) -> dict[str, Any]:
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
return {"cursor": cursor, "items_per_page": items_per_page}
|
||||||
annotation=int,
|
|
||||||
default=_page_size_query(default_page_size, max_page_size),
|
dependency.__name__ = f"{cls.model.__name__}CursorParams"
|
||||||
),
|
return dependency
|
||||||
]
|
|
||||||
return cls._build_paginate_params(
|
|
||||||
pagination_params=pagination_params,
|
|
||||||
pagination_fixed={},
|
|
||||||
dep_name=f"{cls.model.__name__}CursorPaginateParams",
|
|
||||||
search=search,
|
|
||||||
filter=filter,
|
|
||||||
order=order,
|
|
||||||
search_fields=search_fields,
|
|
||||||
facet_fields=facet_fields,
|
|
||||||
order_fields=order_fields,
|
|
||||||
default_order_field=default_order_field,
|
|
||||||
default_order=default_order,
|
|
||||||
)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def paginate_params(
|
def paginate_params(
|
||||||
@@ -588,81 +384,102 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
max_page_size: int = 100,
|
max_page_size: int = 100,
|
||||||
default_pagination_type: PaginationType = PaginationType.OFFSET,
|
default_pagination_type: PaginationType = PaginationType.OFFSET,
|
||||||
include_total: bool = True,
|
include_total: bool = True,
|
||||||
search: bool = True,
|
|
||||||
filter: bool = True,
|
|
||||||
order: bool = True,
|
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
|
||||||
order_fields: Sequence[OrderFieldType] | None = None,
|
|
||||||
default_order_field: QueryableAttribute[Any] | None = None,
|
|
||||||
default_order: Literal["asc", "desc"] = "asc",
|
|
||||||
) -> Callable[..., Awaitable[dict[str, Any]]]:
|
) -> Callable[..., Awaitable[dict[str, Any]]]:
|
||||||
"""Return a FastAPI dependency that collects all params for :meth:`paginate`.
|
"""Return a FastAPI dependency that collects all pagination params from query params.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
default_page_size: Default ``items_per_page`` value.
|
default_page_size: Default value for the ``items_per_page`` query parameter.
|
||||||
max_page_size: Maximum ``items_per_page`` value.
|
max_page_size: Maximum allowed value for ``items_per_page`` (enforced via
|
||||||
|
``le`` on the ``Query``).
|
||||||
default_pagination_type: Default pagination strategy.
|
default_pagination_type: Default pagination strategy.
|
||||||
include_total: Whether to include total count (not a query param).
|
include_total: Server-side flag forwarded as-is to ``include_total`` in
|
||||||
search: Enable search query parameters.
|
:meth:`paginate`. Not exposed as a query parameter.
|
||||||
filter: Enable facet filter query parameters.
|
|
||||||
order: Enable order query parameters.
|
|
||||||
search_fields: Override searchable fields.
|
|
||||||
facet_fields: Override facet fields.
|
|
||||||
order_fields: Override order fields.
|
|
||||||
default_order_field: Default field to order by when ``order_by`` is absent.
|
|
||||||
default_order: Default sort direction.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
An async dependency that resolves to a dict ready to be unpacked
|
An async dependency that resolves to a dict with ``pagination_type``,
|
||||||
into :meth:`paginate`.
|
``page``, ``cursor``, ``items_per_page``, and ``include_total`` keys,
|
||||||
|
ready to be unpacked into :meth:`paginate`.
|
||||||
"""
|
"""
|
||||||
pagination_params = [
|
|
||||||
inspect.Parameter(
|
async def dependency(
|
||||||
"pagination_type",
|
pagination_type: PaginationType = Query(
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
default_pagination_type, description="Pagination strategy"
|
||||||
annotation=PaginationType,
|
|
||||||
default=Query(
|
|
||||||
default_pagination_type, description="Pagination strategy"
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
inspect.Parameter(
|
page: int = Query(
|
||||||
"page",
|
1, ge=1, description="Page number (1-indexed, offset only)"
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
|
||||||
annotation=int,
|
|
||||||
default=Query(
|
|
||||||
1, ge=1, description="Page number (1-indexed, offset only)"
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
inspect.Parameter(
|
cursor: str | None = Query(
|
||||||
"cursor",
|
None, description="Cursor token from a previous response (cursor only)"
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
|
||||||
annotation=str | None,
|
|
||||||
default=Query(
|
|
||||||
None,
|
|
||||||
description="Cursor token from a previous response (cursor only)",
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
inspect.Parameter(
|
items_per_page: int = _page_size_query(default_page_size, max_page_size),
|
||||||
"items_per_page",
|
) -> dict[str, Any]:
|
||||||
inspect.Parameter.KEYWORD_ONLY,
|
return {
|
||||||
annotation=int,
|
"pagination_type": pagination_type,
|
||||||
default=_page_size_query(default_page_size, max_page_size),
|
"page": page,
|
||||||
|
"cursor": cursor,
|
||||||
|
"items_per_page": items_per_page,
|
||||||
|
"include_total": include_total,
|
||||||
|
}
|
||||||
|
|
||||||
|
dependency.__name__ = f"{cls.model.__name__}PaginateParams"
|
||||||
|
return dependency
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def order_params(
|
||||||
|
cls: type[Self],
|
||||||
|
*,
|
||||||
|
order_fields: Sequence[QueryableAttribute[Any]] | None = None,
|
||||||
|
default_field: QueryableAttribute[Any] | None = None,
|
||||||
|
default_order: Literal["asc", "desc"] = "asc",
|
||||||
|
) -> Callable[..., Awaitable[OrderByClause | None]]:
|
||||||
|
"""Return a FastAPI dependency that resolves order query params into an order_by clause.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
order_fields: Override the allowed order fields. Falls back to the class-level
|
||||||
|
``order_fields`` if not provided.
|
||||||
|
default_field: Field to order by when ``order_by`` query param is absent.
|
||||||
|
If ``None`` and no ``order_by`` is provided, no ordering is applied.
|
||||||
|
default_order: Default order direction when ``order`` is absent
|
||||||
|
(``"asc"`` or ``"desc"``).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
An async dependency function named ``{Model}OrderParams`` that resolves to an
|
||||||
|
``OrderByClause`` (or ``None``). Pass it to ``Depends()`` in your route.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If no order fields are configured on this CRUD class and none are
|
||||||
|
provided via ``order_fields``.
|
||||||
|
InvalidOrderFieldError: When the request provides an unknown ``order_by`` value.
|
||||||
|
"""
|
||||||
|
fields = order_fields if order_fields is not None else cls.order_fields
|
||||||
|
if not fields:
|
||||||
|
raise ValueError(
|
||||||
|
f"{cls.__name__} has no order_fields configured. "
|
||||||
|
"Pass order_fields= or set them on CrudFactory."
|
||||||
|
)
|
||||||
|
field_map: dict[str, QueryableAttribute[Any]] = {f.key: f for f in fields}
|
||||||
|
valid_keys = sorted(field_map.keys())
|
||||||
|
|
||||||
|
async def dependency(
|
||||||
|
order_by: str | None = Query(
|
||||||
|
None, description=f"Field to order by. Valid values: {valid_keys}"
|
||||||
),
|
),
|
||||||
]
|
order: Literal["asc", "desc"] = Query(
|
||||||
return cls._build_paginate_params(
|
default_order, description="Sort direction"
|
||||||
pagination_params=pagination_params,
|
),
|
||||||
pagination_fixed={"include_total": include_total},
|
) -> OrderByClause | None:
|
||||||
dep_name=f"{cls.model.__name__}PaginateParams",
|
if order_by is None:
|
||||||
search=search,
|
if default_field is None:
|
||||||
filter=filter,
|
return None
|
||||||
order=order,
|
field = default_field
|
||||||
search_fields=search_fields,
|
elif order_by not in field_map:
|
||||||
facet_fields=facet_fields,
|
raise InvalidOrderFieldError(order_by, valid_keys)
|
||||||
order_fields=order_fields,
|
else:
|
||||||
default_order_field=default_order_field,
|
field = field_map[order_by]
|
||||||
default_order=default_order,
|
return field.asc() if order == "asc" else field.desc()
|
||||||
)
|
|
||||||
|
dependency.__name__ = f"{cls.model.__name__}OrderParams"
|
||||||
|
return dependency
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -717,8 +534,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
|
|
||||||
session.add(db_model)
|
session.add(db_model)
|
||||||
await session.refresh(db_model)
|
await session.refresh(db_model)
|
||||||
if cls.default_load_options:
|
|
||||||
db_model = await cls._reload_with_options(session, db_model)
|
|
||||||
result = cast(ModelType, db_model)
|
result = cast(ModelType, db_model)
|
||||||
if schema:
|
if schema:
|
||||||
return Response(data=schema.model_validate(result))
|
return Response(data=schema.model_validate(result))
|
||||||
@@ -1074,8 +889,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
for rel_attr, related_instances in m2m_resolved.items():
|
for rel_attr, related_instances in m2m_resolved.items():
|
||||||
setattr(db_model, rel_attr, related_instances)
|
setattr(db_model, rel_attr, related_instances)
|
||||||
await session.refresh(db_model)
|
await session.refresh(db_model)
|
||||||
if cls.default_load_options:
|
|
||||||
db_model = await cls._reload_with_options(session, db_model)
|
|
||||||
if schema:
|
if schema:
|
||||||
return Response(data=schema.model_validate(db_model))
|
return Response(data=schema.model_validate(db_model))
|
||||||
return db_model
|
return db_model
|
||||||
@@ -1238,14 +1051,11 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
outer_join: bool = False,
|
outer_join: bool = False,
|
||||||
load_options: Sequence[ExecutableOption] | None = None,
|
load_options: Sequence[ExecutableOption] | None = None,
|
||||||
order_by: OrderByClause | None = None,
|
order_by: OrderByClause | None = None,
|
||||||
order_joins: list[Any] | None = None,
|
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
items_per_page: int = 20,
|
items_per_page: int = 20,
|
||||||
include_total: bool = True,
|
include_total: bool = True,
|
||||||
search: str | SearchConfig | None = None,
|
search: str | SearchConfig | None = None,
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
search_fields: Sequence[SearchFieldType] | None = None,
|
||||||
search_column: str | None = None,
|
|
||||||
order_fields: Sequence[OrderFieldType] | None = None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
facet_fields: Sequence[FacetFieldType] | None = None,
|
||||||
filter_by: dict[str, Any] | BaseModel | None = None,
|
filter_by: dict[str, Any] | BaseModel | None = None,
|
||||||
schema: type[BaseModel],
|
schema: type[BaseModel],
|
||||||
@@ -1265,8 +1075,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
``pagination.total_count`` will be ``None``.
|
``pagination.total_count`` will be ``None``.
|
||||||
search: Search query string or SearchConfig object
|
search: Search query string or SearchConfig object
|
||||||
search_fields: Fields to search in (overrides class default)
|
search_fields: Fields to search in (overrides class default)
|
||||||
search_column: Restrict search to a single column key.
|
|
||||||
order_fields: Fields allowed for sorting (overrides class default).
|
|
||||||
facet_fields: Columns to compute distinct values for (overrides class default)
|
facet_fields: Columns to compute distinct values for (overrides class default)
|
||||||
filter_by: Dict of {column_key: value} to filter by declared facet fields.
|
filter_by: Dict of {column_key: value} to filter by declared facet fields.
|
||||||
Keys must match the column.key of a facet field. Scalar → equality,
|
Keys must match the column.key of a facet field. Scalar → equality,
|
||||||
@@ -1289,7 +1097,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
search,
|
search,
|
||||||
search_fields=search_fields,
|
search_fields=search_fields,
|
||||||
default_fields=cls.searchable_fields,
|
default_fields=cls.searchable_fields,
|
||||||
search_column=search_column,
|
|
||||||
)
|
)
|
||||||
filters.extend(search_filters)
|
filters.extend(search_filters)
|
||||||
search_joins.extend(new_search_joins)
|
search_joins.extend(new_search_joins)
|
||||||
@@ -1303,10 +1110,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
# Apply search joins (always outer joins for search)
|
# Apply search joins (always outer joins for search)
|
||||||
q = _apply_search_joins(q, search_joins)
|
q = _apply_search_joins(q, search_joins)
|
||||||
|
|
||||||
# Apply order joins (relation joins required for order_by field)
|
|
||||||
if order_joins:
|
|
||||||
q = _apply_search_joins(q, order_joins)
|
|
||||||
|
|
||||||
if filters:
|
if filters:
|
||||||
q = q.where(and_(*filters))
|
q = q.where(and_(*filters))
|
||||||
if resolved := cls._resolve_load_options(load_options):
|
if resolved := cls._resolve_load_options(load_options):
|
||||||
@@ -1350,8 +1153,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
filter_attributes = await cls._build_filter_attributes(
|
filter_attributes = await cls._build_filter_attributes(
|
||||||
session, facet_fields, filters, search_joins
|
session, facet_fields, filters, search_joins
|
||||||
)
|
)
|
||||||
search_columns = cls._resolve_search_columns(search_fields)
|
|
||||||
order_columns = cls._resolve_order_columns(order_fields)
|
|
||||||
|
|
||||||
return OffsetPaginatedResponse(
|
return OffsetPaginatedResponse(
|
||||||
data=items,
|
data=items,
|
||||||
@@ -1362,8 +1163,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
has_more=has_more,
|
has_more=has_more,
|
||||||
),
|
),
|
||||||
filter_attributes=filter_attributes,
|
filter_attributes=filter_attributes,
|
||||||
search_columns=search_columns,
|
|
||||||
order_columns=order_columns,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -1377,12 +1176,9 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
outer_join: bool = False,
|
outer_join: bool = False,
|
||||||
load_options: Sequence[ExecutableOption] | None = None,
|
load_options: Sequence[ExecutableOption] | None = None,
|
||||||
order_by: OrderByClause | None = None,
|
order_by: OrderByClause | None = None,
|
||||||
order_joins: list[Any] | None = None,
|
|
||||||
items_per_page: int = 20,
|
items_per_page: int = 20,
|
||||||
search: str | SearchConfig | None = None,
|
search: str | SearchConfig | None = None,
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
search_fields: Sequence[SearchFieldType] | None = None,
|
||||||
search_column: str | None = None,
|
|
||||||
order_fields: Sequence[OrderFieldType] | None = None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
facet_fields: Sequence[FacetFieldType] | None = None,
|
||||||
filter_by: dict[str, Any] | BaseModel | None = None,
|
filter_by: dict[str, Any] | BaseModel | None = None,
|
||||||
schema: type[BaseModel],
|
schema: type[BaseModel],
|
||||||
@@ -1403,8 +1199,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
items_per_page: Number of items per page (default 20).
|
items_per_page: Number of items per page (default 20).
|
||||||
search: Search query string or SearchConfig object.
|
search: Search query string or SearchConfig object.
|
||||||
search_fields: Fields to search in (overrides class default).
|
search_fields: Fields to search in (overrides class default).
|
||||||
search_column: Restrict search to a single column key.
|
|
||||||
order_fields: Fields allowed for sorting (overrides class default).
|
|
||||||
facet_fields: Columns to compute distinct values for (overrides class default).
|
facet_fields: Columns to compute distinct values for (overrides class default).
|
||||||
filter_by: Dict of {column_key: value} to filter by declared facet fields.
|
filter_by: Dict of {column_key: value} to filter by declared facet fields.
|
||||||
Keys must match the column.key of a facet field. Scalar → equality,
|
Keys must match the column.key of a facet field. Scalar → equality,
|
||||||
@@ -1444,7 +1238,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
search,
|
search,
|
||||||
search_fields=search_fields,
|
search_fields=search_fields,
|
||||||
default_fields=cls.searchable_fields,
|
default_fields=cls.searchable_fields,
|
||||||
search_column=search_column,
|
|
||||||
)
|
)
|
||||||
filters.extend(search_filters)
|
filters.extend(search_filters)
|
||||||
search_joins.extend(new_search_joins)
|
search_joins.extend(new_search_joins)
|
||||||
@@ -1458,10 +1251,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
# Apply search joins (always outer joins)
|
# Apply search joins (always outer joins)
|
||||||
q = _apply_search_joins(q, search_joins)
|
q = _apply_search_joins(q, search_joins)
|
||||||
|
|
||||||
# Apply order joins (relation joins required for order_by field)
|
|
||||||
if order_joins:
|
|
||||||
q = _apply_search_joins(q, order_joins)
|
|
||||||
|
|
||||||
if filters:
|
if filters:
|
||||||
q = q.where(and_(*filters))
|
q = q.where(and_(*filters))
|
||||||
if resolved := cls._resolve_load_options(load_options):
|
if resolved := cls._resolve_load_options(load_options):
|
||||||
@@ -1519,8 +1308,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
filter_attributes = await cls._build_filter_attributes(
|
filter_attributes = await cls._build_filter_attributes(
|
||||||
session, facet_fields, filters, search_joins
|
session, facet_fields, filters, search_joins
|
||||||
)
|
)
|
||||||
search_columns = cls._resolve_search_columns(search_fields)
|
|
||||||
order_columns = cls._resolve_order_columns(order_fields)
|
|
||||||
|
|
||||||
return CursorPaginatedResponse(
|
return CursorPaginatedResponse(
|
||||||
data=items,
|
data=items,
|
||||||
@@ -1531,8 +1318,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
has_more=has_more,
|
has_more=has_more,
|
||||||
),
|
),
|
||||||
filter_attributes=filter_attributes,
|
filter_attributes=filter_attributes,
|
||||||
search_columns=search_columns,
|
|
||||||
order_columns=order_columns,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
@@ -1547,15 +1332,12 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
outer_join: bool = ...,
|
outer_join: bool = ...,
|
||||||
load_options: Sequence[ExecutableOption] | None = ...,
|
load_options: Sequence[ExecutableOption] | None = ...,
|
||||||
order_by: OrderByClause | None = ...,
|
order_by: OrderByClause | None = ...,
|
||||||
order_joins: list[Any] | None = ...,
|
|
||||||
page: int = ...,
|
page: int = ...,
|
||||||
cursor: str | None = ...,
|
cursor: str | None = ...,
|
||||||
items_per_page: int = ...,
|
items_per_page: int = ...,
|
||||||
include_total: bool = ...,
|
include_total: bool = ...,
|
||||||
search: str | SearchConfig | None = ...,
|
search: str | SearchConfig | None = ...,
|
||||||
search_fields: Sequence[SearchFieldType] | None = ...,
|
search_fields: Sequence[SearchFieldType] | None = ...,
|
||||||
search_column: str | None = ...,
|
|
||||||
order_fields: Sequence[OrderFieldType] | None = ...,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = ...,
|
facet_fields: Sequence[FacetFieldType] | None = ...,
|
||||||
filter_by: dict[str, Any] | BaseModel | None = ...,
|
filter_by: dict[str, Any] | BaseModel | None = ...,
|
||||||
schema: type[BaseModel],
|
schema: type[BaseModel],
|
||||||
@@ -1573,15 +1355,12 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
outer_join: bool = ...,
|
outer_join: bool = ...,
|
||||||
load_options: Sequence[ExecutableOption] | None = ...,
|
load_options: Sequence[ExecutableOption] | None = ...,
|
||||||
order_by: OrderByClause | None = ...,
|
order_by: OrderByClause | None = ...,
|
||||||
order_joins: list[Any] | None = ...,
|
|
||||||
page: int = ...,
|
page: int = ...,
|
||||||
cursor: str | None = ...,
|
cursor: str | None = ...,
|
||||||
items_per_page: int = ...,
|
items_per_page: int = ...,
|
||||||
include_total: bool = ...,
|
include_total: bool = ...,
|
||||||
search: str | SearchConfig | None = ...,
|
search: str | SearchConfig | None = ...,
|
||||||
search_fields: Sequence[SearchFieldType] | None = ...,
|
search_fields: Sequence[SearchFieldType] | None = ...,
|
||||||
search_column: str | None = ...,
|
|
||||||
order_fields: Sequence[OrderFieldType] | None = ...,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = ...,
|
facet_fields: Sequence[FacetFieldType] | None = ...,
|
||||||
filter_by: dict[str, Any] | BaseModel | None = ...,
|
filter_by: dict[str, Any] | BaseModel | None = ...,
|
||||||
schema: type[BaseModel],
|
schema: type[BaseModel],
|
||||||
@@ -1598,15 +1377,12 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
outer_join: bool = False,
|
outer_join: bool = False,
|
||||||
load_options: Sequence[ExecutableOption] | None = None,
|
load_options: Sequence[ExecutableOption] | None = None,
|
||||||
order_by: OrderByClause | None = None,
|
order_by: OrderByClause | None = None,
|
||||||
order_joins: list[Any] | None = None,
|
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
cursor: str | None = None,
|
cursor: str | None = None,
|
||||||
items_per_page: int = 20,
|
items_per_page: int = 20,
|
||||||
include_total: bool = True,
|
include_total: bool = True,
|
||||||
search: str | SearchConfig | None = None,
|
search: str | SearchConfig | None = None,
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
search_fields: Sequence[SearchFieldType] | None = None,
|
||||||
search_column: str | None = None,
|
|
||||||
order_fields: Sequence[OrderFieldType] | None = None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
facet_fields: Sequence[FacetFieldType] | None = None,
|
||||||
filter_by: dict[str, Any] | BaseModel | None = None,
|
filter_by: dict[str, Any] | BaseModel | None = None,
|
||||||
schema: type[BaseModel],
|
schema: type[BaseModel],
|
||||||
@@ -1634,8 +1410,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
only applies when ``pagination_type`` is ``OFFSET``.
|
only applies when ``pagination_type`` is ``OFFSET``.
|
||||||
search: Search query string or :class:`.SearchConfig` object.
|
search: Search query string or :class:`.SearchConfig` object.
|
||||||
search_fields: Fields to search in (overrides class default).
|
search_fields: Fields to search in (overrides class default).
|
||||||
search_column: Restrict search to a single column key.
|
|
||||||
order_fields: Fields allowed for sorting (overrides class default).
|
|
||||||
facet_fields: Columns to compute distinct values for (overrides
|
facet_fields: Columns to compute distinct values for (overrides
|
||||||
class default).
|
class default).
|
||||||
filter_by: Dict of ``{column_key: value}`` to filter by declared
|
filter_by: Dict of ``{column_key: value}`` to filter by declared
|
||||||
@@ -1661,12 +1435,9 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
outer_join=outer_join,
|
outer_join=outer_join,
|
||||||
load_options=load_options,
|
load_options=load_options,
|
||||||
order_by=order_by,
|
order_by=order_by,
|
||||||
order_joins=order_joins,
|
|
||||||
items_per_page=items_per_page,
|
items_per_page=items_per_page,
|
||||||
search=search,
|
search=search,
|
||||||
search_fields=search_fields,
|
search_fields=search_fields,
|
||||||
search_column=search_column,
|
|
||||||
order_fields=order_fields,
|
|
||||||
facet_fields=facet_fields,
|
facet_fields=facet_fields,
|
||||||
filter_by=filter_by,
|
filter_by=filter_by,
|
||||||
schema=schema,
|
schema=schema,
|
||||||
@@ -1681,14 +1452,11 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
outer_join=outer_join,
|
outer_join=outer_join,
|
||||||
load_options=load_options,
|
load_options=load_options,
|
||||||
order_by=order_by,
|
order_by=order_by,
|
||||||
order_joins=order_joins,
|
|
||||||
page=page,
|
page=page,
|
||||||
items_per_page=items_per_page,
|
items_per_page=items_per_page,
|
||||||
include_total=include_total,
|
include_total=include_total,
|
||||||
search=search,
|
search=search,
|
||||||
search_fields=search_fields,
|
search_fields=search_fields,
|
||||||
search_column=search_column,
|
|
||||||
order_fields=order_fields,
|
|
||||||
facet_fields=facet_fields,
|
facet_fields=facet_fields,
|
||||||
filter_by=filter_by,
|
filter_by=filter_by,
|
||||||
schema=schema,
|
schema=schema,
|
||||||
@@ -1703,7 +1471,7 @@ def CrudFactory(
|
|||||||
base_class: type[AsyncCrud[Any]] = AsyncCrud,
|
base_class: type[AsyncCrud[Any]] = AsyncCrud,
|
||||||
searchable_fields: Sequence[SearchFieldType] | None = None,
|
searchable_fields: Sequence[SearchFieldType] | None = None,
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
facet_fields: Sequence[FacetFieldType] | None = None,
|
||||||
order_fields: Sequence[OrderFieldType] | None = None,
|
order_fields: Sequence[QueryableAttribute[Any]] | None = None,
|
||||||
m2m_fields: M2MFieldType | None = None,
|
m2m_fields: M2MFieldType | None = None,
|
||||||
default_load_options: Sequence[ExecutableOption] | None = None,
|
default_load_options: Sequence[ExecutableOption] | None = None,
|
||||||
cursor_column: Any | None = None,
|
cursor_column: Any | None = None,
|
||||||
@@ -1720,7 +1488,7 @@ def CrudFactory(
|
|||||||
responses. Supports direct columns (``User.status``) and relationship tuples
|
responses. Supports direct columns (``User.status``) and relationship tuples
|
||||||
(``(User.role, Role.name)``). Can be overridden per call.
|
(``(User.role, Role.name)``). Can be overridden per call.
|
||||||
order_fields: Optional list of model attributes that callers are allowed to order by
|
order_fields: Optional list of model attributes that callers are allowed to order by
|
||||||
via ``offset_paginate_params()``. Can be overridden per call.
|
via ``order_params()``. Can be overridden per call.
|
||||||
m2m_fields: Optional mapping for many-to-many relationships.
|
m2m_fields: Optional mapping for many-to-many relationships.
|
||||||
Maps schema field names (containing lists of IDs) to
|
Maps schema field names (containing lists of IDs) to
|
||||||
SQLAlchemy relationship attributes.
|
SQLAlchemy relationship attributes.
|
||||||
|
|||||||
@@ -6,28 +6,12 @@ from collections.abc import Sequence
|
|||||||
from dataclasses import dataclass, replace
|
from dataclasses import dataclass, replace
|
||||||
from typing import TYPE_CHECKING, Any, Literal
|
from typing import TYPE_CHECKING, Any, Literal
|
||||||
|
|
||||||
from sqlalchemy import String, and_, func, or_, select
|
from sqlalchemy import String, and_, or_, select
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.orm import DeclarativeBase
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
from sqlalchemy.orm.attributes import InstrumentedAttribute
|
from sqlalchemy.orm.attributes import InstrumentedAttribute
|
||||||
from sqlalchemy.types import (
|
|
||||||
ARRAY,
|
|
||||||
Boolean,
|
|
||||||
Date,
|
|
||||||
DateTime,
|
|
||||||
Enum,
|
|
||||||
Integer,
|
|
||||||
Numeric,
|
|
||||||
Time,
|
|
||||||
Uuid,
|
|
||||||
)
|
|
||||||
|
|
||||||
from ..exceptions import (
|
from ..exceptions import InvalidFacetFilterError, NoSearchableFieldsError
|
||||||
InvalidFacetFilterError,
|
|
||||||
InvalidSearchColumnError,
|
|
||||||
NoSearchableFieldsError,
|
|
||||||
UnsupportedFacetTypeError,
|
|
||||||
)
|
|
||||||
from ..types import FacetFieldType, SearchFieldType
|
from ..types import FacetFieldType, SearchFieldType
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -97,7 +81,6 @@ def build_search_filters(
|
|||||||
search: str | SearchConfig,
|
search: str | SearchConfig,
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
search_fields: Sequence[SearchFieldType] | None = None,
|
||||||
default_fields: Sequence[SearchFieldType] | None = None,
|
default_fields: Sequence[SearchFieldType] | None = None,
|
||||||
search_column: str | None = None,
|
|
||||||
) -> tuple[list["ColumnElement[bool]"], list[InstrumentedAttribute[Any]]]:
|
) -> tuple[list["ColumnElement[bool]"], list[InstrumentedAttribute[Any]]]:
|
||||||
"""Build SQLAlchemy filter conditions for search.
|
"""Build SQLAlchemy filter conditions for search.
|
||||||
|
|
||||||
@@ -106,8 +89,6 @@ def build_search_filters(
|
|||||||
search: Search string or SearchConfig
|
search: Search string or SearchConfig
|
||||||
search_fields: Fields specified per-call (takes priority)
|
search_fields: Fields specified per-call (takes priority)
|
||||||
default_fields: Default fields (from ClassVar)
|
default_fields: Default fields (from ClassVar)
|
||||||
search_column: Optional key to narrow search to a single field.
|
|
||||||
Must match one of the resolved search field keys.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tuple of (filter_conditions, joins_needed)
|
Tuple of (filter_conditions, joins_needed)
|
||||||
@@ -134,14 +115,6 @@ def build_search_filters(
|
|||||||
if not fields:
|
if not fields:
|
||||||
raise NoSearchableFieldsError(model)
|
raise NoSearchableFieldsError(model)
|
||||||
|
|
||||||
# Narrow to a single column when search_column is specified
|
|
||||||
if search_column is not None:
|
|
||||||
keys = search_field_keys(fields)
|
|
||||||
index = {k: f for k, f in zip(keys, fields)}
|
|
||||||
if search_column not in index:
|
|
||||||
raise InvalidSearchColumnError(search_column, sorted(index))
|
|
||||||
fields = [index[search_column]]
|
|
||||||
|
|
||||||
query = config.query.strip()
|
query = config.query.strip()
|
||||||
filters: list[ColumnElement[bool]] = []
|
filters: list[ColumnElement[bool]] = []
|
||||||
joins: list[InstrumentedAttribute[Any]] = []
|
joins: list[InstrumentedAttribute[Any]] = []
|
||||||
@@ -176,11 +149,6 @@ def build_search_filters(
|
|||||||
return filters, joins
|
return filters, joins
|
||||||
|
|
||||||
|
|
||||||
def search_field_keys(fields: Sequence[SearchFieldType]) -> list[str]:
|
|
||||||
"""Return a human-readable key for each search field."""
|
|
||||||
return facet_keys(fields)
|
|
||||||
|
|
||||||
|
|
||||||
def facet_keys(facet_fields: Sequence[FacetFieldType]) -> list[str]:
|
def facet_keys(facet_fields: Sequence[FacetFieldType]) -> list[str]:
|
||||||
"""Return a key for each facet field.
|
"""Return a key for each facet field.
|
||||||
|
|
||||||
@@ -233,47 +201,23 @@ async def build_facets(
|
|||||||
rels = ()
|
rels = ()
|
||||||
column = field
|
column = field
|
||||||
|
|
||||||
col_type = column.property.columns[0].type
|
q = select(column).select_from(model).distinct()
|
||||||
is_array = isinstance(col_type, ARRAY)
|
|
||||||
|
|
||||||
if is_array:
|
# Apply base joins (already done on main query, but needed here independently)
|
||||||
unnested = func.unnest(column).label(column.key)
|
|
||||||
q = select(unnested).select_from(model).distinct()
|
|
||||||
else:
|
|
||||||
q = select(column).select_from(model).distinct()
|
|
||||||
|
|
||||||
# Apply base joins (deduplicated) — needed here independently
|
|
||||||
seen_joins: set[str] = set()
|
|
||||||
for rel in base_joins or []:
|
for rel in base_joins or []:
|
||||||
rel_key = str(rel)
|
q = q.outerjoin(rel)
|
||||||
if rel_key not in seen_joins:
|
|
||||||
seen_joins.add(rel_key)
|
|
||||||
q = q.outerjoin(rel)
|
|
||||||
|
|
||||||
# Add any extra joins required by this facet field that aren't already applied
|
# Add any extra joins required by this facet field that aren't already in base_joins
|
||||||
for rel in rels:
|
for rel in rels:
|
||||||
rel_key = str(rel)
|
if str(rel) not in existing_join_keys:
|
||||||
if rel_key not in existing_join_keys and rel_key not in seen_joins:
|
|
||||||
seen_joins.add(rel_key)
|
|
||||||
q = q.outerjoin(rel)
|
q = q.outerjoin(rel)
|
||||||
|
|
||||||
if base_filters:
|
if base_filters:
|
||||||
q = q.where(and_(*base_filters))
|
q = q.where(and_(*base_filters))
|
||||||
|
|
||||||
if is_array:
|
q = q.order_by(column)
|
||||||
q = q.order_by(unnested)
|
|
||||||
else:
|
|
||||||
q = q.order_by(column)
|
|
||||||
result = await session.execute(q)
|
result = await session.execute(q)
|
||||||
col_type = column.property.columns[0].type
|
values = [row[0] for row in result.all() if row[0] is not None]
|
||||||
enum_class = getattr(col_type, "enum_class", None)
|
|
||||||
values = [
|
|
||||||
row[0].name
|
|
||||||
if (enum_class is not None and isinstance(row[0], enum_class))
|
|
||||||
else row[0]
|
|
||||||
for row in result.all()
|
|
||||||
if row[0] is not None
|
|
||||||
]
|
|
||||||
return key, values
|
return key, values
|
||||||
|
|
||||||
pairs = await asyncio.gather(
|
pairs = await asyncio.gather(
|
||||||
@@ -282,22 +226,6 @@ async def build_facets(
|
|||||||
return dict(pairs)
|
return dict(pairs)
|
||||||
|
|
||||||
|
|
||||||
_EQUALITY_TYPES = (String, Integer, Numeric, Date, DateTime, Time, Enum, Uuid)
|
|
||||||
"""Column types that support equality / IN filtering in build_filter_by."""
|
|
||||||
|
|
||||||
|
|
||||||
def _coerce_bool(value: Any) -> bool:
|
|
||||||
"""Coerce a string value to a Python bool for Boolean column filtering."""
|
|
||||||
if isinstance(value, bool):
|
|
||||||
return value
|
|
||||||
if isinstance(value, str):
|
|
||||||
if value.lower() == "true":
|
|
||||||
return True
|
|
||||||
if value.lower() == "false":
|
|
||||||
return False
|
|
||||||
raise ValueError(f"Cannot coerce {value!r} to bool")
|
|
||||||
|
|
||||||
|
|
||||||
def build_filter_by(
|
def build_filter_by(
|
||||||
filter_by: dict[str, Any],
|
filter_by: dict[str, Any],
|
||||||
facet_fields: Sequence[FacetFieldType],
|
facet_fields: Sequence[FacetFieldType],
|
||||||
@@ -343,42 +271,9 @@ def build_filter_by(
|
|||||||
joins.append(rel)
|
joins.append(rel)
|
||||||
added_join_keys.add(rel_key)
|
added_join_keys.add(rel_key)
|
||||||
|
|
||||||
col_type = column.property.columns[0].type
|
if isinstance(value, list):
|
||||||
if isinstance(col_type, Boolean):
|
filters.append(column.in_(value))
|
||||||
coerce = _coerce_bool
|
|
||||||
if isinstance(value, list):
|
|
||||||
filters.append(column.in_([coerce(v) for v in value]))
|
|
||||||
else:
|
|
||||||
filters.append(column == coerce(value))
|
|
||||||
elif isinstance(col_type, ARRAY):
|
|
||||||
if isinstance(value, list):
|
|
||||||
filters.append(column.overlap(value))
|
|
||||||
else:
|
|
||||||
filters.append(column.any(value))
|
|
||||||
elif isinstance(col_type, Enum):
|
|
||||||
enum_class = col_type.enum_class
|
|
||||||
if enum_class is not None:
|
|
||||||
|
|
||||||
def _coerce_enum(v: Any) -> Any:
|
|
||||||
if isinstance(v, enum_class):
|
|
||||||
return v
|
|
||||||
return enum_class[v] # lookup by name: "PENDING", "RED"
|
|
||||||
|
|
||||||
if isinstance(value, list):
|
|
||||||
filters.append(column.in_([_coerce_enum(v) for v in value]))
|
|
||||||
else:
|
|
||||||
filters.append(column == _coerce_enum(value))
|
|
||||||
else: # pragma: no cover
|
|
||||||
if isinstance(value, list):
|
|
||||||
filters.append(column.in_(value))
|
|
||||||
else:
|
|
||||||
filters.append(column == value)
|
|
||||||
elif isinstance(col_type, _EQUALITY_TYPES):
|
|
||||||
if isinstance(value, list):
|
|
||||||
filters.append(column.in_(value))
|
|
||||||
else:
|
|
||||||
filters.append(column == value)
|
|
||||||
else:
|
else:
|
||||||
raise UnsupportedFacetTypeError(key, type(col_type).__name__)
|
filters.append(column == value)
|
||||||
|
|
||||||
return filters, joins
|
return filters, joins
|
||||||
|
|||||||
@@ -4,13 +4,11 @@ import asyncio
|
|||||||
from collections.abc import AsyncGenerator, Callable
|
from collections.abc import AsyncGenerator, Callable
|
||||||
from contextlib import AbstractAsyncContextManager, asynccontextmanager
|
from contextlib import AbstractAsyncContextManager, asynccontextmanager
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, TypeVar, cast
|
from typing import Any, TypeVar
|
||||||
|
|
||||||
from sqlalchemy import Table, delete, text, tuple_
|
from sqlalchemy import text
|
||||||
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||||
from sqlalchemy.orm import DeclarativeBase, QueryableAttribute
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
from sqlalchemy.orm.relationships import RelationshipProperty
|
|
||||||
|
|
||||||
from .exceptions import NotFoundError
|
from .exceptions import NotFoundError
|
||||||
|
|
||||||
@@ -22,19 +20,13 @@ __all__ = [
|
|||||||
"create_db_dependency",
|
"create_db_dependency",
|
||||||
"get_transaction",
|
"get_transaction",
|
||||||
"lock_tables",
|
"lock_tables",
|
||||||
"m2m_add",
|
|
||||||
"m2m_remove",
|
|
||||||
"m2m_set",
|
|
||||||
"wait_for_row_change",
|
"wait_for_row_change",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
_SessionT = TypeVar("_SessionT", bound=AsyncSession)
|
|
||||||
|
|
||||||
|
|
||||||
def create_db_dependency(
|
def create_db_dependency(
|
||||||
session_maker: async_sessionmaker[_SessionT],
|
session_maker: async_sessionmaker[AsyncSession],
|
||||||
) -> Callable[[], AsyncGenerator[_SessionT, None]]:
|
) -> Callable[[], AsyncGenerator[AsyncSession, None]]:
|
||||||
"""Create a FastAPI dependency for database sessions.
|
"""Create a FastAPI dependency for database sessions.
|
||||||
|
|
||||||
Creates a dependency function that yields a session and auto-commits
|
Creates a dependency function that yields a session and auto-commits
|
||||||
@@ -62,7 +54,7 @@ def create_db_dependency(
|
|||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async def get_db() -> AsyncGenerator[_SessionT, None]:
|
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||||
async with session_maker() as session:
|
async with session_maker() as session:
|
||||||
await session.connection()
|
await session.connection()
|
||||||
yield session
|
yield session
|
||||||
@@ -73,8 +65,8 @@ def create_db_dependency(
|
|||||||
|
|
||||||
|
|
||||||
def create_db_context(
|
def create_db_context(
|
||||||
session_maker: async_sessionmaker[_SessionT],
|
session_maker: async_sessionmaker[AsyncSession],
|
||||||
) -> Callable[[], AbstractAsyncContextManager[_SessionT]]:
|
) -> Callable[[], AbstractAsyncContextManager[AsyncSession]]:
|
||||||
"""Create a context manager for database sessions.
|
"""Create a context manager for database sessions.
|
||||||
|
|
||||||
Creates a context manager for use outside of FastAPI request handlers,
|
Creates a context manager for use outside of FastAPI request handlers,
|
||||||
@@ -344,140 +336,3 @@ async def wait_for_row_change(
|
|||||||
current = {col: getattr(instance, col) for col in watch_cols}
|
current = {col: getattr(instance, col) for col in watch_cols}
|
||||||
if current != initial:
|
if current != initial:
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
def _m2m_prop(rel_attr: QueryableAttribute) -> RelationshipProperty: # type: ignore[type-arg]
|
|
||||||
"""Return the validated M2M RelationshipProperty for *rel_attr*.
|
|
||||||
|
|
||||||
Raises TypeError if *rel_attr* is not a Many-to-Many relationship.
|
|
||||||
"""
|
|
||||||
prop = rel_attr.property
|
|
||||||
if not isinstance(prop, RelationshipProperty) or prop.secondary is None:
|
|
||||||
raise TypeError(
|
|
||||||
f"m2m helpers require a Many-to-Many relationship attribute, "
|
|
||||||
f"got {rel_attr!r}. Use a relationship with a secondary table."
|
|
||||||
)
|
|
||||||
return prop
|
|
||||||
|
|
||||||
|
|
||||||
async def m2m_add(
|
|
||||||
session: AsyncSession,
|
|
||||||
instance: DeclarativeBase,
|
|
||||||
rel_attr: QueryableAttribute,
|
|
||||||
*related: DeclarativeBase,
|
|
||||||
ignore_conflicts: bool = False,
|
|
||||||
) -> None:
|
|
||||||
"""Insert rows into a Many-to-Many association table without loading the ORM collection.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
session: DB async session.
|
|
||||||
instance: The "owner" side model instance (e.g. the ``A`` in ``A.b_list``).
|
|
||||||
rel_attr: The M2M relationship attribute on the model class (e.g. ``A.b_list``).
|
|
||||||
*related: One or more related instances to associate with ``instance``.
|
|
||||||
ignore_conflicts: When ``True``, silently skip rows that already exist
|
|
||||||
in the association table (``ON CONFLICT DO NOTHING``).
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
TypeError: If ``rel_attr`` is not a Many-to-Many relationship.
|
|
||||||
"""
|
|
||||||
prop = _m2m_prop(rel_attr)
|
|
||||||
if not related:
|
|
||||||
return
|
|
||||||
|
|
||||||
secondary = cast(Table, prop.secondary)
|
|
||||||
assert secondary is not None # guaranteed by _m2m_prop
|
|
||||||
sync_pairs = prop.secondary_synchronize_pairs
|
|
||||||
assert sync_pairs is not None # set whenever secondary is set
|
|
||||||
|
|
||||||
# synchronize_pairs: [(parent_col, assoc_col), ...]
|
|
||||||
# secondary_synchronize_pairs: [(related_col, assoc_col), ...]
|
|
||||||
rows: list[dict[str, Any]] = []
|
|
||||||
for rel_instance in related:
|
|
||||||
row: dict[str, Any] = {}
|
|
||||||
for parent_col, assoc_col in prop.synchronize_pairs:
|
|
||||||
row[assoc_col.name] = getattr(instance, cast(str, parent_col.key))
|
|
||||||
for related_col, assoc_col in sync_pairs:
|
|
||||||
row[assoc_col.name] = getattr(rel_instance, cast(str, related_col.key))
|
|
||||||
rows.append(row)
|
|
||||||
|
|
||||||
stmt = pg_insert(secondary).values(rows)
|
|
||||||
if ignore_conflicts:
|
|
||||||
stmt = stmt.on_conflict_do_nothing()
|
|
||||||
await session.execute(stmt)
|
|
||||||
|
|
||||||
|
|
||||||
async def m2m_remove(
|
|
||||||
session: AsyncSession,
|
|
||||||
instance: DeclarativeBase,
|
|
||||||
rel_attr: QueryableAttribute,
|
|
||||||
*related: DeclarativeBase,
|
|
||||||
) -> None:
|
|
||||||
"""Remove rows from a Many-to-Many association table without loading the ORM collection.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
session: DB async session.
|
|
||||||
instance: The "owner" side model instance (e.g. the ``A`` in ``A.b_list``).
|
|
||||||
rel_attr: The M2M relationship attribute on the model class (e.g. ``A.b_list``).
|
|
||||||
*related: One or more related instances to disassociate from ``instance``.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
TypeError: If ``rel_attr`` is not a Many-to-Many relationship.
|
|
||||||
"""
|
|
||||||
prop = _m2m_prop(rel_attr)
|
|
||||||
if not related:
|
|
||||||
return
|
|
||||||
|
|
||||||
secondary = cast(Table, prop.secondary)
|
|
||||||
assert secondary is not None # guaranteed by _m2m_prop
|
|
||||||
related_pairs = prop.secondary_synchronize_pairs
|
|
||||||
assert related_pairs is not None # set whenever secondary is set
|
|
||||||
|
|
||||||
parent_where = [
|
|
||||||
assoc_col == getattr(instance, cast(str, parent_col.key))
|
|
||||||
for parent_col, assoc_col in prop.synchronize_pairs
|
|
||||||
]
|
|
||||||
|
|
||||||
if len(related_pairs) == 1:
|
|
||||||
related_col, assoc_col = related_pairs[0]
|
|
||||||
related_values = [getattr(r, cast(str, related_col.key)) for r in related]
|
|
||||||
related_where = assoc_col.in_(related_values)
|
|
||||||
else:
|
|
||||||
assoc_cols = [ac for _, ac in related_pairs]
|
|
||||||
rel_cols = [rc for rc, _ in related_pairs]
|
|
||||||
related_values_t = [
|
|
||||||
tuple(getattr(r, cast(str, rc.key)) for rc in rel_cols) for r in related
|
|
||||||
]
|
|
||||||
related_where = tuple_(*assoc_cols).in_(related_values_t)
|
|
||||||
|
|
||||||
await session.execute(delete(secondary).where(*parent_where, related_where))
|
|
||||||
|
|
||||||
|
|
||||||
async def m2m_set(
|
|
||||||
session: AsyncSession,
|
|
||||||
instance: DeclarativeBase,
|
|
||||||
rel_attr: QueryableAttribute,
|
|
||||||
*related: DeclarativeBase,
|
|
||||||
) -> None:
|
|
||||||
"""Replace the entire Many-to-Many association set atomically.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
session: DB async session.
|
|
||||||
instance: The "owner" side model instance (e.g. the ``A`` in ``A.b_list``).
|
|
||||||
rel_attr: The M2M relationship attribute on the model class (e.g. ``A.b_list``).
|
|
||||||
*related: The new complete set of related instances.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
TypeError: If ``rel_attr`` is not a Many-to-Many relationship.
|
|
||||||
"""
|
|
||||||
prop = _m2m_prop(rel_attr)
|
|
||||||
secondary = cast(Table, prop.secondary)
|
|
||||||
assert secondary is not None # guaranteed by _m2m_prop
|
|
||||||
|
|
||||||
parent_where = [
|
|
||||||
assoc_col == getattr(instance, cast(str, parent_col.key))
|
|
||||||
for parent_col, assoc_col in prop.synchronize_pairs
|
|
||||||
]
|
|
||||||
await session.execute(delete(secondary).where(*parent_where))
|
|
||||||
|
|
||||||
if related:
|
|
||||||
await m2m_add(session, instance, rel_attr, *related)
|
|
||||||
|
|||||||
@@ -7,11 +7,9 @@ from .exceptions import (
|
|||||||
ForbiddenError,
|
ForbiddenError,
|
||||||
InvalidFacetFilterError,
|
InvalidFacetFilterError,
|
||||||
InvalidOrderFieldError,
|
InvalidOrderFieldError,
|
||||||
InvalidSearchColumnError,
|
|
||||||
NoSearchableFieldsError,
|
NoSearchableFieldsError,
|
||||||
NotFoundError,
|
NotFoundError,
|
||||||
UnauthorizedError,
|
UnauthorizedError,
|
||||||
UnsupportedFacetTypeError,
|
|
||||||
generate_error_responses,
|
generate_error_responses,
|
||||||
)
|
)
|
||||||
from .handler import init_exceptions_handlers
|
from .handler import init_exceptions_handlers
|
||||||
@@ -25,9 +23,7 @@ __all__ = [
|
|||||||
"init_exceptions_handlers",
|
"init_exceptions_handlers",
|
||||||
"InvalidFacetFilterError",
|
"InvalidFacetFilterError",
|
||||||
"InvalidOrderFieldError",
|
"InvalidOrderFieldError",
|
||||||
"InvalidSearchColumnError",
|
|
||||||
"NoSearchableFieldsError",
|
"NoSearchableFieldsError",
|
||||||
"NotFoundError",
|
"NotFoundError",
|
||||||
"UnauthorizedError",
|
"UnauthorizedError",
|
||||||
"UnsupportedFacetTypeError",
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -144,61 +144,6 @@ class InvalidFacetFilterError(ApiException):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class UnsupportedFacetTypeError(ApiException):
|
|
||||||
"""Raised when a facet field has a column type not supported by filter_by."""
|
|
||||||
|
|
||||||
api_error = ApiError(
|
|
||||||
code=400,
|
|
||||||
msg="Unsupported Facet Type",
|
|
||||||
desc="The column type is not supported for facet filtering.",
|
|
||||||
err_code="FACET-TYPE-400",
|
|
||||||
)
|
|
||||||
|
|
||||||
def __init__(self, key: str, col_type: str) -> None:
|
|
||||||
"""Initialize the exception.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
key: The facet field key.
|
|
||||||
col_type: The unsupported column type name.
|
|
||||||
"""
|
|
||||||
self.key = key
|
|
||||||
self.col_type = col_type
|
|
||||||
super().__init__(
|
|
||||||
desc=(
|
|
||||||
f"Facet field '{key}' has unsupported column type '{col_type}'. "
|
|
||||||
f"Supported types: String, Integer, Numeric, Boolean, "
|
|
||||||
f"Date, DateTime, Time, Enum, Uuid, ARRAY."
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidSearchColumnError(ApiException):
|
|
||||||
"""Raised when search_column is not one of the configured searchable fields."""
|
|
||||||
|
|
||||||
api_error = ApiError(
|
|
||||||
code=400,
|
|
||||||
msg="Invalid Search Column",
|
|
||||||
desc="The requested search column is not a configured searchable field.",
|
|
||||||
err_code="SEARCH-COL-400",
|
|
||||||
)
|
|
||||||
|
|
||||||
def __init__(self, column: str, valid_columns: list[str]) -> None:
|
|
||||||
"""Initialize the exception.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
column: The unknown search column provided by the caller.
|
|
||||||
valid_columns: List of valid search column keys.
|
|
||||||
"""
|
|
||||||
self.column = column
|
|
||||||
self.valid_columns = valid_columns
|
|
||||||
super().__init__(
|
|
||||||
desc=(
|
|
||||||
f"'{column}' is not a searchable column. "
|
|
||||||
f"Valid columns: {valid_columns}."
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidOrderFieldError(ApiException):
|
class InvalidOrderFieldError(ApiException):
|
||||||
"""Raised when order_by contains a field not in the allowed order fields."""
|
"""Raised when order_by contains a field not in the allowed order fields."""
|
||||||
|
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ def _format_validation_error(
|
|||||||
)
|
)
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||||
content=error_response.model_dump(),
|
content=error_response.model_dump(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -2,18 +2,12 @@
|
|||||||
|
|
||||||
from .enum import LoadStrategy
|
from .enum import LoadStrategy
|
||||||
from .registry import Context, FixtureRegistry
|
from .registry import Context, FixtureRegistry
|
||||||
from .utils import (
|
from .utils import get_obj_by_attr, load_fixtures, load_fixtures_by_context
|
||||||
get_field_by_attr,
|
|
||||||
get_obj_by_attr,
|
|
||||||
load_fixtures,
|
|
||||||
load_fixtures_by_context,
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"Context",
|
"Context",
|
||||||
"FixtureRegistry",
|
"FixtureRegistry",
|
||||||
"LoadStrategy",
|
"LoadStrategy",
|
||||||
"get_field_by_attr",
|
|
||||||
"get_obj_by_attr",
|
"get_obj_by_attr",
|
||||||
"load_fixtures",
|
"load_fixtures",
|
||||||
"load_fixtures_by_context",
|
"load_fixtures_by_context",
|
||||||
|
|||||||
@@ -30,16 +30,20 @@ def _instance_to_dict(instance: DeclarativeBase) -> dict[str, Any]:
|
|||||||
if val is None:
|
if val is None:
|
||||||
col = prop.columns[0]
|
col = prop.columns[0]
|
||||||
|
|
||||||
if (
|
if col.server_default is not None or (
|
||||||
col.server_default is not None
|
col.default is not None and col.default.is_callable
|
||||||
or (col.default is not None and col.default.is_callable)
|
|
||||||
or col.autoincrement is True
|
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
result[prop.key] = val
|
result[prop.key] = val
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_rows(dicts: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||||
|
"""Ensure all row dicts share the same key set."""
|
||||||
|
all_keys: set[str] = set().union(*dicts)
|
||||||
|
return [{k: d.get(k) for k in all_keys} for d in dicts]
|
||||||
|
|
||||||
|
|
||||||
def _group_by_type(
|
def _group_by_type(
|
||||||
instances: list[DeclarativeBase],
|
instances: list[DeclarativeBase],
|
||||||
) -> list[tuple[type[DeclarativeBase], list[DeclarativeBase]]]:
|
) -> list[tuple[type[DeclarativeBase], list[DeclarativeBase]]]:
|
||||||
@@ -50,32 +54,14 @@ def _group_by_type(
|
|||||||
return list(groups.items())
|
return list(groups.items())
|
||||||
|
|
||||||
|
|
||||||
def _group_by_column_set(
|
|
||||||
dicts: list[dict[str, Any]],
|
|
||||||
instances: list[DeclarativeBase],
|
|
||||||
) -> list[tuple[list[dict[str, Any]], list[DeclarativeBase]]]:
|
|
||||||
"""Group (dict, instance) pairs by their dict key sets."""
|
|
||||||
groups: dict[
|
|
||||||
frozenset[str], tuple[list[dict[str, Any]], list[DeclarativeBase]]
|
|
||||||
] = {}
|
|
||||||
for d, inst in zip(dicts, instances):
|
|
||||||
key = frozenset(d)
|
|
||||||
if key not in groups:
|
|
||||||
groups[key] = ([], [])
|
|
||||||
groups[key][0].append(d)
|
|
||||||
groups[key][1].append(inst)
|
|
||||||
return list(groups.values())
|
|
||||||
|
|
||||||
|
|
||||||
async def _batch_insert(
|
async def _batch_insert(
|
||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
model_cls: type[DeclarativeBase],
|
model_cls: type[DeclarativeBase],
|
||||||
instances: list[DeclarativeBase],
|
instances: list[DeclarativeBase],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""INSERT all instances — raises on conflict (no duplicate handling)."""
|
"""INSERT all instances — raises on conflict (no duplicate handling)."""
|
||||||
dicts = [_instance_to_dict(i) for i in instances]
|
dicts = _normalize_rows([_instance_to_dict(i) for i in instances])
|
||||||
for group_dicts, _ in _group_by_column_set(dicts, instances):
|
await session.execute(pg_insert(model_cls).values(dicts))
|
||||||
await session.execute(pg_insert(model_cls).values(group_dicts))
|
|
||||||
|
|
||||||
|
|
||||||
async def _batch_merge(
|
async def _batch_merge(
|
||||||
@@ -93,22 +79,21 @@ async def _batch_merge(
|
|||||||
if not any(col.name in pk_names_set for col in prop.columns)
|
if not any(col.name in pk_names_set for col in prop.columns)
|
||||||
]
|
]
|
||||||
|
|
||||||
dicts = [_instance_to_dict(i) for i in instances]
|
dicts = _normalize_rows([_instance_to_dict(i) for i in instances])
|
||||||
for group_dicts, _ in _group_by_column_set(dicts, instances):
|
stmt = pg_insert(model_cls).values(dicts)
|
||||||
stmt = pg_insert(model_cls).values(group_dicts)
|
|
||||||
|
|
||||||
inserted_keys = set(group_dicts[0])
|
inserted_keys = set(dicts[0]) if dicts else set()
|
||||||
update_cols = [col for col in non_pk_cols if col in inserted_keys]
|
update_cols = [col for col in non_pk_cols if col in inserted_keys]
|
||||||
|
|
||||||
if update_cols:
|
if update_cols:
|
||||||
stmt = stmt.on_conflict_do_update(
|
stmt = stmt.on_conflict_do_update(
|
||||||
index_elements=pk_names,
|
index_elements=pk_names,
|
||||||
set_={col: stmt.excluded[col] for col in update_cols},
|
set_={col: stmt.excluded[col] for col in update_cols},
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
stmt = stmt.on_conflict_do_nothing(index_elements=pk_names)
|
stmt = stmt.on_conflict_do_nothing(index_elements=pk_names)
|
||||||
|
|
||||||
await session.execute(stmt)
|
await session.execute(stmt)
|
||||||
|
|
||||||
|
|
||||||
async def _batch_skip_existing(
|
async def _batch_skip_existing(
|
||||||
@@ -131,30 +116,22 @@ async def _batch_skip_existing(
|
|||||||
|
|
||||||
loaded: list[DeclarativeBase] = list(no_pk)
|
loaded: list[DeclarativeBase] = list(no_pk)
|
||||||
if no_pk:
|
if no_pk:
|
||||||
no_pk_dicts = [_instance_to_dict(i) for i in no_pk]
|
await session.execute(
|
||||||
for group_dicts, _ in _group_by_column_set(no_pk_dicts, no_pk):
|
pg_insert(model_cls).values(
|
||||||
await session.execute(pg_insert(model_cls).values(group_dicts))
|
_normalize_rows([_instance_to_dict(i) for i in no_pk])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
if with_pk_pairs:
|
if with_pk_pairs:
|
||||||
with_pk = [i for i, _ in with_pk_pairs]
|
with_pk = [i for i, _ in with_pk_pairs]
|
||||||
with_pk_dicts = [_instance_to_dict(i) for i in with_pk]
|
stmt = (
|
||||||
for group_dicts, group_insts in _group_by_column_set(with_pk_dicts, with_pk):
|
pg_insert(model_cls)
|
||||||
stmt = (
|
.values(_normalize_rows([_instance_to_dict(i) for i in with_pk]))
|
||||||
pg_insert(model_cls)
|
.on_conflict_do_nothing(index_elements=pk_names)
|
||||||
.values(group_dicts)
|
)
|
||||||
.on_conflict_do_nothing(index_elements=pk_names)
|
result = await session.execute(stmt.returning(*mapper.primary_key))
|
||||||
)
|
inserted_pks = {row[0] if len(pk_names) == 1 else tuple(row) for row in result}
|
||||||
result = await session.execute(stmt.returning(*mapper.primary_key))
|
loaded.extend(inst for inst, pk in with_pk_pairs if pk in inserted_pks)
|
||||||
inserted_pks = {
|
|
||||||
row[0] if len(pk_names) == 1 else tuple(row) for row in result
|
|
||||||
}
|
|
||||||
loaded.extend(
|
|
||||||
inst
|
|
||||||
for inst, pk in zip(
|
|
||||||
group_insts, [_get_primary_key(i) for i in group_insts]
|
|
||||||
)
|
|
||||||
if pk in inserted_pks
|
|
||||||
)
|
|
||||||
|
|
||||||
return loaded
|
return loaded
|
||||||
|
|
||||||
@@ -166,7 +143,12 @@ async def _load_ordered(
|
|||||||
strategy: LoadStrategy,
|
strategy: LoadStrategy,
|
||||||
contexts: tuple[str, ...] | None = None,
|
contexts: tuple[str, ...] | None = None,
|
||||||
) -> dict[str, list[DeclarativeBase]]:
|
) -> dict[str, list[DeclarativeBase]]:
|
||||||
"""Load fixtures in order using batch Core INSERT statements."""
|
"""Load fixtures in order using batch Core INSERT statements.
|
||||||
|
|
||||||
|
When *contexts* is provided only variants whose context set intersects with
|
||||||
|
*contexts* are called for each name; their instances are concatenated.
|
||||||
|
When *contexts* is ``None`` all variants of each name are loaded.
|
||||||
|
"""
|
||||||
results: dict[str, list[DeclarativeBase]] = {}
|
results: dict[str, list[DeclarativeBase]] = {}
|
||||||
|
|
||||||
for name in ordered_names:
|
for name in ordered_names:
|
||||||
@@ -176,6 +158,10 @@ async def _load_ordered(
|
|||||||
else registry.get_variants(name)
|
else registry.get_variants(name)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Cross-context dependency fallback: if we're loading by context but
|
||||||
|
# no variant matches (e.g. a "base"-only fixture required by a
|
||||||
|
# "testing" fixture), load all available variants so the dependency
|
||||||
|
# is satisfied.
|
||||||
if contexts is not None and not variants:
|
if contexts is not None and not variants:
|
||||||
variants = registry.get_variants(name)
|
variants = registry.get_variants(name)
|
||||||
|
|
||||||
@@ -250,31 +236,6 @@ def get_obj_by_attr(
|
|||||||
) from None
|
) from None
|
||||||
|
|
||||||
|
|
||||||
def get_field_by_attr(
|
|
||||||
fixtures: Callable[[], Sequence[ModelType]],
|
|
||||||
attr_name: str,
|
|
||||||
value: Any,
|
|
||||||
*,
|
|
||||||
field: str = "id",
|
|
||||||
) -> Any:
|
|
||||||
"""Get a single field value from a fixture object matched by an attribute.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
fixtures: A fixture function registered via ``@registry.register``
|
|
||||||
that returns a sequence of SQLAlchemy model instances.
|
|
||||||
attr_name: Name of the attribute to match against.
|
|
||||||
value: Value to match.
|
|
||||||
field: Attribute name to return from the matched object (default: ``"id"``).
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The value of ``field`` on the first matching model instance.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
StopIteration: If no matching object is found in the fixture group.
|
|
||||||
"""
|
|
||||||
return getattr(get_obj_by_attr(fixtures, attr_name, value), field)
|
|
||||||
|
|
||||||
|
|
||||||
async def load_fixtures(
|
async def load_fixtures(
|
||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
registry: FixtureRegistry,
|
registry: FixtureRegistry,
|
||||||
@@ -306,6 +267,10 @@ async def load_fixtures_by_context(
|
|||||||
) -> dict[str, list[DeclarativeBase]]:
|
) -> dict[str, list[DeclarativeBase]]:
|
||||||
"""Load all fixtures for specific contexts.
|
"""Load all fixtures for specific contexts.
|
||||||
|
|
||||||
|
For each fixture name, only the variants whose context set intersects with
|
||||||
|
*contexts* are loaded. When a name has variants in multiple of the
|
||||||
|
requested contexts, their instances are merged before being inserted.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
session: Database session
|
session: Database session
|
||||||
registry: Fixture registry
|
registry: Fixture registry
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"""Prometheus metrics endpoint for FastAPI applications."""
|
"""Prometheus metrics endpoint for FastAPI applications."""
|
||||||
|
|
||||||
import inspect
|
import asyncio
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
@@ -55,10 +55,10 @@ def init_metrics(
|
|||||||
|
|
||||||
# Partition collectors and cache env check at startup — both are stable for the app lifetime.
|
# Partition collectors and cache env check at startup — both are stable for the app lifetime.
|
||||||
async_collectors = [
|
async_collectors = [
|
||||||
c for c in registry.get_collectors() if inspect.iscoroutinefunction(c.func)
|
c for c in registry.get_collectors() if asyncio.iscoroutinefunction(c.func)
|
||||||
]
|
]
|
||||||
sync_collectors = [
|
sync_collectors = [
|
||||||
c for c in registry.get_collectors() if not inspect.iscoroutinefunction(c.func)
|
c for c in registry.get_collectors() if not asyncio.iscoroutinefunction(c.func)
|
||||||
]
|
]
|
||||||
multiprocess_mode = _is_multiprocess()
|
multiprocess_mode = _is_multiprocess()
|
||||||
|
|
||||||
|
|||||||
@@ -7,15 +7,15 @@ from .columns import (
|
|||||||
UUIDv7Mixin,
|
UUIDv7Mixin,
|
||||||
UpdatedAtMixin,
|
UpdatedAtMixin,
|
||||||
)
|
)
|
||||||
from .watched import EventSession, ModelEvent, listens_for
|
from .watched import ModelEvent, WatchedFieldsMixin, watch
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"EventSession",
|
|
||||||
"ModelEvent",
|
"ModelEvent",
|
||||||
"UUIDMixin",
|
"UUIDMixin",
|
||||||
"UUIDv7Mixin",
|
"UUIDv7Mixin",
|
||||||
"CreatedAtMixin",
|
"CreatedAtMixin",
|
||||||
"UpdatedAtMixin",
|
"UpdatedAtMixin",
|
||||||
"TimestampMixin",
|
"TimestampMixin",
|
||||||
"listens_for",
|
"WatchedFieldsMixin",
|
||||||
|
"watch",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -6,6 +6,14 @@ from datetime import datetime
|
|||||||
from sqlalchemy import DateTime, Uuid, text
|
from sqlalchemy import DateTime, Uuid, text
|
||||||
from sqlalchemy.orm import Mapped, mapped_column
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"UUIDMixin",
|
||||||
|
"UUIDv7Mixin",
|
||||||
|
"CreatedAtMixin",
|
||||||
|
"UpdatedAtMixin",
|
||||||
|
"TimestampMixin",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class UUIDMixin:
|
class UUIDMixin:
|
||||||
"""Mixin that adds a UUID primary key auto-generated by the database."""
|
"""Mixin that adds a UUID primary key auto-generated by the database."""
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
"""Field-change monitoring via SQLAlchemy session events."""
|
"""Field-change monitoring via SQLAlchemy session events."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import inspect
|
import inspect
|
||||||
from collections.abc import Callable
|
import weakref
|
||||||
|
from collections.abc import Awaitable
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any
|
from typing import Any, TypeVar
|
||||||
|
|
||||||
from sqlalchemy import event
|
from sqlalchemy import event
|
||||||
from sqlalchemy import inspect as sa_inspect
|
from sqlalchemy import inspect as sa_inspect
|
||||||
@@ -12,81 +14,49 @@ from sqlalchemy.orm.attributes import set_committed_value as _sa_set_committed_v
|
|||||||
|
|
||||||
from ..logger import get_logger
|
from ..logger import get_logger
|
||||||
|
|
||||||
|
__all__ = ["ModelEvent", "WatchedFieldsMixin", "watch"]
|
||||||
|
|
||||||
_logger = get_logger()
|
_logger = get_logger()
|
||||||
|
_T = TypeVar("_T")
|
||||||
|
_CALLBACK_ERROR_MSG = "WatchedFieldsMixin callback raised an unhandled exception"
|
||||||
|
_WATCHED_FIELDS: weakref.WeakKeyDictionary[type, list[str]] = (
|
||||||
|
weakref.WeakKeyDictionary()
|
||||||
|
)
|
||||||
|
_SESSION_PENDING_NEW = "_ft_pending_new"
|
||||||
|
_SESSION_CREATES = "_ft_creates"
|
||||||
|
_SESSION_DELETES = "_ft_deletes"
|
||||||
|
_SESSION_UPDATES = "_ft_updates"
|
||||||
|
_SESSION_SAVEPOINT_DEPTH = "_ft_sp_depth"
|
||||||
|
_DEFERRED_STRATEGY_KEY = (("deferred", True), ("instrument", True))
|
||||||
|
|
||||||
|
|
||||||
class ModelEvent(str, Enum):
|
class ModelEvent(str, Enum):
|
||||||
"""Event types dispatched by :class:`EventSession`."""
|
"""Event types emitted by :class:`WatchedFieldsMixin`."""
|
||||||
|
|
||||||
CREATE = "create"
|
CREATE = "create"
|
||||||
DELETE = "delete"
|
DELETE = "delete"
|
||||||
UPDATE = "update"
|
UPDATE = "update"
|
||||||
|
|
||||||
|
|
||||||
_CALLBACK_ERROR_MSG = "Event callback raised an unhandled exception"
|
def watch(*fields: str) -> Any:
|
||||||
_SESSION_CREATES = "_ft_creates"
|
"""Class decorator to filter which fields trigger ``on_update``.
|
||||||
_SESSION_DELETES = "_ft_deletes"
|
|
||||||
_SESSION_UPDATES = "_ft_updates"
|
|
||||||
_DEFERRED_STRATEGY_KEY = (("deferred", True), ("instrument", True))
|
|
||||||
_EVENT_HANDLERS: dict[tuple[type, ModelEvent], list[Callable[..., Any]]] = {}
|
|
||||||
_WATCHED_MODELS: set[type] = set()
|
|
||||||
_WATCHED_CACHE: dict[type, bool] = {}
|
|
||||||
_HANDLER_CACHE: dict[tuple[type, ModelEvent], list[Callable[..., Any]]] = {}
|
|
||||||
|
|
||||||
|
|
||||||
def _invalidate_caches() -> None:
|
|
||||||
"""Clear lookup caches after handler registration."""
|
|
||||||
_WATCHED_CACHE.clear()
|
|
||||||
_HANDLER_CACHE.clear()
|
|
||||||
|
|
||||||
|
|
||||||
def listens_for(
|
|
||||||
model_class: type,
|
|
||||||
event_types: list[ModelEvent] | None = None,
|
|
||||||
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
|
|
||||||
"""Register a callback for one or more model lifecycle events.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
model_class: The SQLAlchemy model class to listen on.
|
*fields: One or more field names to watch. At least one name is required.
|
||||||
event_types: List of :class:`ModelEvent` values to listen for.
|
|
||||||
Defaults to all event types.
|
|
||||||
"""
|
|
||||||
evs = event_types if event_types is not None else list(ModelEvent)
|
|
||||||
|
|
||||||
def decorator(fn: Callable[..., Any]) -> Callable[..., Any]:
|
Raises:
|
||||||
for ev in evs:
|
ValueError: If called with no field names.
|
||||||
_EVENT_HANDLERS.setdefault((model_class, ev), []).append(fn)
|
"""
|
||||||
_WATCHED_MODELS.add(model_class)
|
if not fields:
|
||||||
_invalidate_caches()
|
raise ValueError("@watch requires at least one field name.")
|
||||||
return fn
|
|
||||||
|
def decorator(cls: type[_T]) -> type[_T]:
|
||||||
|
_WATCHED_FIELDS[cls] = list(fields)
|
||||||
|
return cls
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
def _is_watched(obj: Any) -> bool:
|
|
||||||
"""Return True if *obj*'s type (or any ancestor) has registered handlers."""
|
|
||||||
cls = type(obj)
|
|
||||||
try:
|
|
||||||
return _WATCHED_CACHE[cls]
|
|
||||||
except KeyError:
|
|
||||||
result = any(klass in _WATCHED_MODELS for klass in cls.__mro__)
|
|
||||||
_WATCHED_CACHE[cls] = result
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def _get_handlers(cls: type, ev: ModelEvent) -> list[Callable[..., Any]]:
|
|
||||||
"""Return registered handlers for *cls* and *ev*, walking the MRO."""
|
|
||||||
key = (cls, ev)
|
|
||||||
try:
|
|
||||||
return _HANDLER_CACHE[key]
|
|
||||||
except KeyError:
|
|
||||||
handlers: list[Callable[..., Any]] = []
|
|
||||||
for klass in cls.__mro__:
|
|
||||||
handlers.extend(_EVENT_HANDLERS.get((klass, ev), []))
|
|
||||||
_HANDLER_CACHE[key] = handlers
|
|
||||||
return handlers
|
|
||||||
|
|
||||||
|
|
||||||
def _snapshot_column_attrs(obj: Any) -> dict[str, Any]:
|
def _snapshot_column_attrs(obj: Any) -> dict[str, Any]:
|
||||||
"""Read currently-loaded column values into a plain dict."""
|
"""Read currently-loaded column values into a plain dict."""
|
||||||
state = sa_inspect(obj) # InstanceState
|
state = sa_inspect(obj) # InstanceState
|
||||||
@@ -95,7 +65,7 @@ def _snapshot_column_attrs(obj: Any) -> dict[str, Any]:
|
|||||||
for prop in state.mapper.column_attrs:
|
for prop in state.mapper.column_attrs:
|
||||||
if prop.key in state_dict:
|
if prop.key in state_dict:
|
||||||
snapshot[prop.key] = state_dict[prop.key]
|
snapshot[prop.key] = state_dict[prop.key]
|
||||||
elif ( # pragma: no cover
|
elif (
|
||||||
not state.expired
|
not state.expired
|
||||||
and prop.strategy_key != _DEFERRED_STRATEGY_KEY
|
and prop.strategy_key != _DEFERRED_STRATEGY_KEY
|
||||||
and all(
|
and all(
|
||||||
@@ -109,17 +79,12 @@ def _snapshot_column_attrs(obj: Any) -> dict[str, Any]:
|
|||||||
return snapshot
|
return snapshot
|
||||||
|
|
||||||
|
|
||||||
def _get_watched_fields(cls: type) -> tuple[str, ...] | None:
|
def _get_watched_fields(cls: type) -> list[str] | None:
|
||||||
"""Return the watched fields for *cls*."""
|
"""Return the watched fields for *cls*, walking the MRO to inherit from parents."""
|
||||||
fields = getattr(cls, "__watched_fields__", None)
|
for klass in cls.__mro__:
|
||||||
if fields is not None and (
|
if klass in _WATCHED_FIELDS:
|
||||||
not isinstance(fields, tuple) or not all(isinstance(f, str) for f in fields)
|
return _WATCHED_FIELDS[klass]
|
||||||
):
|
return None
|
||||||
raise TypeError(
|
|
||||||
f"{cls.__name__}.__watched_fields__ must be a tuple[str, ...], "
|
|
||||||
f"got {type(fields).__name__}"
|
|
||||||
)
|
|
||||||
return fields
|
|
||||||
|
|
||||||
|
|
||||||
def _upsert_changes(
|
def _upsert_changes(
|
||||||
@@ -140,32 +105,50 @@ def _upsert_changes(
|
|||||||
pending[key] = (obj, changes)
|
pending[key] = (obj, changes)
|
||||||
|
|
||||||
|
|
||||||
|
@event.listens_for(AsyncSession.sync_session_class, "after_transaction_create")
|
||||||
|
def _after_transaction_create(session: Any, transaction: Any) -> None:
|
||||||
|
if transaction.nested:
|
||||||
|
session.info[_SESSION_SAVEPOINT_DEPTH] = (
|
||||||
|
session.info.get(_SESSION_SAVEPOINT_DEPTH, 0) + 1
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@event.listens_for(AsyncSession.sync_session_class, "after_transaction_end")
|
||||||
|
def _after_transaction_end(session: Any, transaction: Any) -> None:
|
||||||
|
if transaction.nested:
|
||||||
|
depth = session.info.get(_SESSION_SAVEPOINT_DEPTH, 0)
|
||||||
|
if depth > 0: # pragma: no branch
|
||||||
|
session.info[_SESSION_SAVEPOINT_DEPTH] = depth - 1
|
||||||
|
|
||||||
|
|
||||||
@event.listens_for(AsyncSession.sync_session_class, "after_flush")
|
@event.listens_for(AsyncSession.sync_session_class, "after_flush")
|
||||||
def _after_flush(session: Any, flush_context: Any) -> None:
|
def _after_flush(session: Any, flush_context: Any) -> None:
|
||||||
# New objects: capture reference. Attributes will be refreshed after commit.
|
# New objects: capture references while session.new is still populated.
|
||||||
|
# Values are read in _after_flush_postexec once RETURNING has been processed.
|
||||||
for obj in session.new:
|
for obj in session.new:
|
||||||
if _is_watched(obj):
|
if isinstance(obj, WatchedFieldsMixin):
|
||||||
session.info.setdefault(_SESSION_CREATES, []).append(obj)
|
session.info.setdefault(_SESSION_PENDING_NEW, []).append(obj)
|
||||||
|
|
||||||
# Deleted objects: snapshot now while attributes are still loaded.
|
# Deleted objects: capture before they leave the identity map.
|
||||||
for obj in session.deleted:
|
for obj in session.deleted:
|
||||||
if _is_watched(obj):
|
if isinstance(obj, WatchedFieldsMixin):
|
||||||
snapshot = _snapshot_column_attrs(obj)
|
session.info.setdefault(_SESSION_DELETES, []).append(obj)
|
||||||
session.info.setdefault(_SESSION_DELETES, []).append((obj, snapshot))
|
|
||||||
|
|
||||||
# Dirty objects: read old/new from SQLAlchemy attribute history.
|
# Dirty objects: read old/new from SQLAlchemy attribute history.
|
||||||
for obj in session.dirty:
|
for obj in session.dirty:
|
||||||
if not _is_watched(obj):
|
if not isinstance(obj, WatchedFieldsMixin):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# None = not in dict = watch all fields; list = specific fields only
|
||||||
watched = _get_watched_fields(type(obj))
|
watched = _get_watched_fields(type(obj))
|
||||||
changes: dict[str, dict[str, Any]] = {}
|
changes: dict[str, dict[str, Any]] = {}
|
||||||
|
|
||||||
inst_attrs = sa_inspect(obj).attrs
|
|
||||||
attrs = (
|
attrs = (
|
||||||
((field, inst_attrs[field]) for field in watched)
|
# Specific fields
|
||||||
|
((field, sa_inspect(obj).attrs[field]) for field in watched)
|
||||||
if watched is not None
|
if watched is not None
|
||||||
else ((s.key, s) for s in inst_attrs)
|
# All mapped fields
|
||||||
|
else ((s.key, s) for s in sa_inspect(obj).attrs)
|
||||||
)
|
)
|
||||||
for field, attr_state in attrs:
|
for field, attr_state in attrs:
|
||||||
history = attr_state.history
|
history = attr_state.history
|
||||||
@@ -183,108 +166,116 @@ def _after_flush(session: Any, flush_context: Any) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@event.listens_for(AsyncSession.sync_session_class, "after_flush_postexec")
|
||||||
|
def _after_flush_postexec(session: Any, flush_context: Any) -> None:
|
||||||
|
# New objects are now persistent and RETURNING values have been applied,
|
||||||
|
# so server defaults (id, created_at, …) are available via getattr.
|
||||||
|
pending_new: list[Any] = session.info.pop(_SESSION_PENDING_NEW, [])
|
||||||
|
if not pending_new:
|
||||||
|
return
|
||||||
|
session.info.setdefault(_SESSION_CREATES, []).extend(pending_new)
|
||||||
|
|
||||||
|
|
||||||
@event.listens_for(AsyncSession.sync_session_class, "after_rollback")
|
@event.listens_for(AsyncSession.sync_session_class, "after_rollback")
|
||||||
def _after_rollback(session: Any) -> None:
|
def _after_rollback(session: Any) -> None:
|
||||||
if session.in_transaction():
|
session.info.pop(_SESSION_PENDING_NEW, None)
|
||||||
return
|
|
||||||
session.info.pop(_SESSION_CREATES, None)
|
session.info.pop(_SESSION_CREATES, None)
|
||||||
session.info.pop(_SESSION_DELETES, None)
|
session.info.pop(_SESSION_DELETES, None)
|
||||||
session.info.pop(_SESSION_UPDATES, None)
|
session.info.pop(_SESSION_UPDATES, None)
|
||||||
|
|
||||||
|
|
||||||
async def _invoke_callback(
|
def _task_error_handler(task: asyncio.Task[Any]) -> None:
|
||||||
fn: Callable[..., Any],
|
if not task.cancelled() and (exc := task.exception()):
|
||||||
obj: Any,
|
_logger.error(_CALLBACK_ERROR_MSG, exc_info=exc)
|
||||||
event_type: ModelEvent,
|
|
||||||
changes: dict[str, dict[str, Any]] | None,
|
|
||||||
|
def _schedule_with_snapshot(
|
||||||
|
loop: asyncio.AbstractEventLoop, obj: Any, fn: Any, *args: Any
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Call *fn* and await the result if it is awaitable."""
|
"""Snapshot *obj*'s column attrs now (before expire_on_commit wipes them),
|
||||||
result = fn(obj, event_type, changes)
|
then schedule a coroutine that restores the snapshot and calls *fn*.
|
||||||
if inspect.isawaitable(result):
|
"""
|
||||||
await result
|
snapshot = _snapshot_column_attrs(obj)
|
||||||
|
|
||||||
|
async def _run(
|
||||||
|
obj: Any = obj,
|
||||||
|
fn: Any = fn,
|
||||||
|
snapshot: dict[str, Any] = snapshot,
|
||||||
|
args: tuple = args,
|
||||||
|
) -> None:
|
||||||
|
for key, value in snapshot.items():
|
||||||
|
_sa_set_committed_value(obj, key, value)
|
||||||
|
try:
|
||||||
|
result = fn(*args)
|
||||||
|
if inspect.isawaitable(result):
|
||||||
|
await result
|
||||||
|
except Exception as exc:
|
||||||
|
_logger.error(_CALLBACK_ERROR_MSG, exc_info=exc)
|
||||||
|
|
||||||
|
task = loop.create_task(_run())
|
||||||
|
task.add_done_callback(_task_error_handler)
|
||||||
|
|
||||||
|
|
||||||
class EventSession(AsyncSession):
|
@event.listens_for(AsyncSession.sync_session_class, "after_commit")
|
||||||
"""AsyncSession subclass that dispatches lifecycle callbacks after commit."""
|
def _after_commit(session: Any) -> None:
|
||||||
|
if session.info.get(_SESSION_SAVEPOINT_DEPTH, 0) > 0:
|
||||||
|
return
|
||||||
|
|
||||||
async def commit(self) -> None: # noqa: C901
|
creates: list[Any] = session.info.pop(_SESSION_CREATES, [])
|
||||||
await super().commit()
|
deletes: list[Any] = session.info.pop(_SESSION_DELETES, [])
|
||||||
|
field_changes: dict[int, tuple[Any, dict[str, dict[str, Any]]]] = session.info.pop(
|
||||||
|
_SESSION_UPDATES, {}
|
||||||
|
)
|
||||||
|
|
||||||
creates: list[Any] = self.info.pop(_SESSION_CREATES, [])
|
if creates and deletes:
|
||||||
deletes: list[tuple[Any, dict[str, Any]]] = self.info.pop(_SESSION_DELETES, [])
|
transient_ids = {id(o) for o in creates} & {id(o) for o in deletes}
|
||||||
field_changes: dict[int, tuple[Any, dict[str, dict[str, Any]]]] = self.info.pop(
|
if transient_ids:
|
||||||
_SESSION_UPDATES, {}
|
creates = [o for o in creates if id(o) not in transient_ids]
|
||||||
)
|
deletes = [o for o in deletes if id(o) not in transient_ids]
|
||||||
|
|
||||||
if not creates and not deletes and not field_changes:
|
|
||||||
return
|
|
||||||
|
|
||||||
# Suppress transient objects (created + deleted in same transaction).
|
|
||||||
if creates and deletes:
|
|
||||||
created_ids = {id(o) for o in creates}
|
|
||||||
deleted_ids = {id(o) for o, _ in deletes}
|
|
||||||
transient_ids = created_ids & deleted_ids
|
|
||||||
if transient_ids:
|
|
||||||
creates = [o for o in creates if id(o) not in transient_ids]
|
|
||||||
deletes = [(o, s) for o, s in deletes if id(o) not in transient_ids]
|
|
||||||
field_changes = {
|
|
||||||
k: v for k, v in field_changes.items() if k not in transient_ids
|
|
||||||
}
|
|
||||||
|
|
||||||
# Suppress updates for deleted objects (row is gone, refresh would fail).
|
|
||||||
if deletes and field_changes:
|
|
||||||
deleted_ids = {id(o) for o, _ in deletes}
|
|
||||||
field_changes = {
|
field_changes = {
|
||||||
k: v for k, v in field_changes.items() if k not in deleted_ids
|
k: v for k, v in field_changes.items() if k not in transient_ids
|
||||||
}
|
}
|
||||||
|
|
||||||
# Suppress updates for newly created objects (CREATE-only semantics).
|
if not creates and not deletes and not field_changes:
|
||||||
if creates and field_changes:
|
return
|
||||||
create_ids = {id(o) for o in creates}
|
|
||||||
field_changes = {
|
|
||||||
k: v for k, v in field_changes.items() if k not in create_ids
|
|
||||||
}
|
|
||||||
|
|
||||||
# Dispatch CREATE callbacks.
|
try:
|
||||||
for obj in creates:
|
loop = asyncio.get_running_loop()
|
||||||
try:
|
except RuntimeError:
|
||||||
state = sa_inspect(obj, raiseerr=False)
|
return
|
||||||
if (
|
|
||||||
state is None or state.detached or state.transient
|
|
||||||
): # pragma: no cover
|
|
||||||
continue
|
|
||||||
await self.refresh(obj)
|
|
||||||
for handler in _get_handlers(type(obj), ModelEvent.CREATE):
|
|
||||||
await _invoke_callback(handler, obj, ModelEvent.CREATE, None)
|
|
||||||
except Exception as exc:
|
|
||||||
_logger.error(_CALLBACK_ERROR_MSG, exc_info=exc)
|
|
||||||
|
|
||||||
# Dispatch DELETE callbacks (restore snapshot; row is gone).
|
for obj in creates:
|
||||||
for obj, snapshot in deletes:
|
_schedule_with_snapshot(loop, obj, obj.on_create)
|
||||||
try:
|
|
||||||
for key, value in snapshot.items():
|
|
||||||
_sa_set_committed_value(obj, key, value)
|
|
||||||
for handler in _get_handlers(type(obj), ModelEvent.DELETE):
|
|
||||||
await _invoke_callback(handler, obj, ModelEvent.DELETE, None)
|
|
||||||
except Exception as exc:
|
|
||||||
_logger.error(_CALLBACK_ERROR_MSG, exc_info=exc)
|
|
||||||
|
|
||||||
# Dispatch UPDATE callbacks.
|
for obj in deletes:
|
||||||
for obj, changes in field_changes.values():
|
_schedule_with_snapshot(loop, obj, obj.on_delete)
|
||||||
try:
|
|
||||||
state = sa_inspect(obj, raiseerr=False)
|
|
||||||
if (
|
|
||||||
state is None or state.detached or state.transient
|
|
||||||
): # pragma: no cover
|
|
||||||
continue
|
|
||||||
await self.refresh(obj)
|
|
||||||
for handler in _get_handlers(type(obj), ModelEvent.UPDATE):
|
|
||||||
await _invoke_callback(handler, obj, ModelEvent.UPDATE, changes)
|
|
||||||
except Exception as exc:
|
|
||||||
_logger.error(_CALLBACK_ERROR_MSG, exc_info=exc)
|
|
||||||
|
|
||||||
async def rollback(self) -> None:
|
for obj, changes in field_changes.values():
|
||||||
await super().rollback()
|
_schedule_with_snapshot(loop, obj, obj.on_update, changes)
|
||||||
self.info.pop(_SESSION_CREATES, None)
|
|
||||||
self.info.pop(_SESSION_DELETES, None)
|
|
||||||
self.info.pop(_SESSION_UPDATES, None)
|
class WatchedFieldsMixin:
|
||||||
|
"""Mixin that enables lifecycle callbacks for SQLAlchemy models."""
|
||||||
|
|
||||||
|
def on_event(
|
||||||
|
self, event: ModelEvent, changes: dict[str, dict[str, Any]] | None = None
|
||||||
|
) -> Awaitable[None] | None:
|
||||||
|
"""Catch-all callback fired for every lifecycle event.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
event: The event type (:attr:`ModelEvent.CREATE`, :attr:`ModelEvent.DELETE`,
|
||||||
|
or :attr:`ModelEvent.UPDATE`).
|
||||||
|
changes: Field changes for :attr:`ModelEvent.UPDATE`, ``None`` otherwise.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def on_create(self) -> Awaitable[None] | None:
|
||||||
|
"""Called after INSERT commit."""
|
||||||
|
return self.on_event(ModelEvent.CREATE)
|
||||||
|
|
||||||
|
def on_delete(self) -> Awaitable[None] | None:
|
||||||
|
"""Called after DELETE commit."""
|
||||||
|
return self.on_event(ModelEvent.DELETE)
|
||||||
|
|
||||||
|
def on_update(self, changes: dict[str, dict[str, Any]]) -> Awaitable[None] | None:
|
||||||
|
"""Called after UPDATE commit when watched fields change."""
|
||||||
|
return self.on_event(ModelEvent.UPDATE, changes=changes)
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
"""Pytest plugin for using FixtureRegistry fixtures in tests."""
|
"""Pytest plugin for using FixtureRegistry fixtures in tests."""
|
||||||
|
|
||||||
from collections.abc import Callable, Sequence
|
from collections.abc import Callable, Sequence
|
||||||
from typing import Any, cast
|
from typing import Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy import select
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.orm import DeclarativeBase, selectinload
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
from sqlalchemy.orm.interfaces import ExecutableOption, ORMOption
|
|
||||||
|
|
||||||
from ..db import get_transaction
|
from ..db import get_transaction
|
||||||
from ..fixtures import FixtureRegistry, LoadStrategy
|
from ..fixtures import FixtureRegistry, LoadStrategy
|
||||||
@@ -114,7 +112,7 @@ def _create_fixture_function(
|
|||||||
elif strategy == LoadStrategy.MERGE:
|
elif strategy == LoadStrategy.MERGE:
|
||||||
merged = await session.merge(instance)
|
merged = await session.merge(instance)
|
||||||
loaded.append(merged)
|
loaded.append(merged)
|
||||||
elif strategy == LoadStrategy.SKIP_EXISTING: # pragma: no branch
|
elif strategy == LoadStrategy.SKIP_EXISTING:
|
||||||
pk = _get_primary_key(instance)
|
pk = _get_primary_key(instance)
|
||||||
if pk is not None:
|
if pk is not None:
|
||||||
existing = await session.get(type(instance), pk)
|
existing = await session.get(type(instance), pk)
|
||||||
@@ -127,11 +125,6 @@ def _create_fixture_function(
|
|||||||
session.add(instance)
|
session.add(instance)
|
||||||
loaded.append(instance)
|
loaded.append(instance)
|
||||||
|
|
||||||
if loaded: # pragma: no branch
|
|
||||||
load_options = _relationship_load_options(type(loaded[0]))
|
|
||||||
if load_options:
|
|
||||||
return await _reload_with_relationships(session, loaded, load_options)
|
|
||||||
|
|
||||||
return loaded
|
return loaded
|
||||||
|
|
||||||
# Update function signature to include dependencies
|
# Update function signature to include dependencies
|
||||||
@@ -148,54 +141,6 @@ def _create_fixture_function(
|
|||||||
return created_func
|
return created_func
|
||||||
|
|
||||||
|
|
||||||
def _relationship_load_options(model: type[DeclarativeBase]) -> list[ExecutableOption]:
|
|
||||||
"""Build selectinload options for all direct relationships on a model."""
|
|
||||||
return [
|
|
||||||
selectinload(getattr(model, rel.key)) for rel in model.__mapper__.relationships
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
async def _reload_with_relationships(
|
|
||||||
session: AsyncSession,
|
|
||||||
instances: list[DeclarativeBase],
|
|
||||||
load_options: list[ExecutableOption],
|
|
||||||
) -> list[DeclarativeBase]:
|
|
||||||
"""Reload instances in a single bulk query with relationship eager-loading.
|
|
||||||
|
|
||||||
Uses one SELECT … WHERE pk IN (…) so selectinload can batch all relationship
|
|
||||||
queries — 1 + N_relationships round-trips regardless of how many instances
|
|
||||||
there are, instead of one session.get() per instance.
|
|
||||||
|
|
||||||
Preserves the original insertion order.
|
|
||||||
"""
|
|
||||||
model = type(instances[0])
|
|
||||||
mapper = model.__mapper__
|
|
||||||
pk_cols = mapper.primary_key
|
|
||||||
|
|
||||||
if len(pk_cols) == 1:
|
|
||||||
pk_attr = getattr(model, pk_cols[0].key)
|
|
||||||
pks = [getattr(inst, pk_cols[0].key) for inst in instances]
|
|
||||||
result = await session.execute(
|
|
||||||
select(model).where(pk_attr.in_(pks)).options(*load_options)
|
|
||||||
)
|
|
||||||
by_pk = {getattr(row, pk_cols[0].key): row for row in result.unique().scalars()}
|
|
||||||
return [by_pk[pk] for pk in pks]
|
|
||||||
|
|
||||||
# Composite PK: fall back to per-instance reload
|
|
||||||
reloaded: list[DeclarativeBase] = []
|
|
||||||
for instance in instances:
|
|
||||||
pk = _get_primary_key(instance)
|
|
||||||
refreshed = await session.get(
|
|
||||||
model,
|
|
||||||
pk,
|
|
||||||
options=cast(list[ORMOption], load_options),
|
|
||||||
populate_existing=True,
|
|
||||||
)
|
|
||||||
if refreshed is not None: # pragma: no branch
|
|
||||||
reloaded.append(refreshed)
|
|
||||||
return reloaded
|
|
||||||
|
|
||||||
|
|
||||||
def _get_primary_key(instance: DeclarativeBase) -> Any | None:
|
def _get_primary_key(instance: DeclarativeBase) -> Any | None:
|
||||||
"""Get the primary key value of a model instance."""
|
"""Get the primary key value of a model instance."""
|
||||||
mapper = instance.__class__.__mapper__
|
mapper = instance.__class__.__mapper__
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"""Pytest helper utilities for FastAPI testing."""
|
"""Pytest helper utilities for FastAPI testing."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import warnings
|
||||||
from collections.abc import AsyncGenerator, Callable
|
from collections.abc import AsyncGenerator, Callable
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@@ -15,8 +16,28 @@ from sqlalchemy.ext.asyncio import (
|
|||||||
)
|
)
|
||||||
from sqlalchemy.orm import DeclarativeBase
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
|
|
||||||
from ..db import cleanup_tables, create_database
|
from ..db import cleanup_tables as _cleanup_tables
|
||||||
from ..models.watched import EventSession
|
from ..db import create_database
|
||||||
|
|
||||||
|
|
||||||
|
async def cleanup_tables(
|
||||||
|
session: AsyncSession,
|
||||||
|
base: type[DeclarativeBase],
|
||||||
|
) -> None:
|
||||||
|
"""Truncate all tables for fast between-test cleanup.
|
||||||
|
|
||||||
|
.. deprecated::
|
||||||
|
Import ``cleanup_tables`` from ``fastapi_toolsets.db`` instead.
|
||||||
|
This re-export will be removed in v3.0.0.
|
||||||
|
"""
|
||||||
|
warnings.warn(
|
||||||
|
"Importing cleanup_tables from fastapi_toolsets.pytest is deprecated "
|
||||||
|
"and will be removed in v3.0.0. "
|
||||||
|
"Use 'from fastapi_toolsets.db import cleanup_tables' instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
await _cleanup_tables(session=session, base=base)
|
||||||
|
|
||||||
|
|
||||||
def _get_xdist_worker(default_test_db: str) -> str:
|
def _get_xdist_worker(default_test_db: str) -> str:
|
||||||
@@ -244,14 +265,12 @@ async def create_db_session(
|
|||||||
async with engine.begin() as conn:
|
async with engine.begin() as conn:
|
||||||
await conn.run_sync(base.metadata.create_all)
|
await conn.run_sync(base.metadata.create_all)
|
||||||
|
|
||||||
session_maker = async_sessionmaker(
|
session_maker = async_sessionmaker(engine, expire_on_commit=expire_on_commit)
|
||||||
engine, expire_on_commit=expire_on_commit, class_=EventSession
|
|
||||||
)
|
|
||||||
async with session_maker() as session:
|
async with session_maker() as session:
|
||||||
yield session
|
yield session
|
||||||
|
|
||||||
if cleanup:
|
if cleanup:
|
||||||
await cleanup_tables(session=session, base=base)
|
await _cleanup_tables(session=session, base=base)
|
||||||
|
|
||||||
if drop_tables:
|
if drop_tables:
|
||||||
async with engine.begin() as conn:
|
async with engine.begin() as conn:
|
||||||
|
|||||||
@@ -162,8 +162,6 @@ class PaginatedResponse(BaseResponse, Generic[DataT]):
|
|||||||
pagination: OffsetPagination | CursorPagination
|
pagination: OffsetPagination | CursorPagination
|
||||||
pagination_type: PaginationType | None = None
|
pagination_type: PaginationType | None = None
|
||||||
filter_attributes: dict[str, list[Any]] | None = None
|
filter_attributes: dict[str, list[Any]] | None = None
|
||||||
search_columns: list[str] | None = None
|
|
||||||
order_columns: list[str] | None = None
|
|
||||||
|
|
||||||
_discriminated_union_cache: ClassVar[dict[Any, Any]] = {}
|
_discriminated_union_cache: ClassVar[dict[Any, Any]] = {}
|
||||||
|
|
||||||
|
|||||||
@@ -15,14 +15,13 @@ ModelType = TypeVar("ModelType", bound=DeclarativeBase)
|
|||||||
SchemaType = TypeVar("SchemaType", bound=BaseModel)
|
SchemaType = TypeVar("SchemaType", bound=BaseModel)
|
||||||
|
|
||||||
# CRUD type aliases
|
# CRUD type aliases
|
||||||
JoinType = list[tuple[type[DeclarativeBase] | Any, Any]]
|
JoinType = list[tuple[type[DeclarativeBase], Any]]
|
||||||
M2MFieldType = Mapping[str, QueryableAttribute[Any]]
|
M2MFieldType = Mapping[str, QueryableAttribute[Any]]
|
||||||
OrderByClause = ColumnElement[Any] | QueryableAttribute[Any]
|
OrderByClause = ColumnElement[Any] | QueryableAttribute[Any]
|
||||||
|
|
||||||
# Search / facet / order type aliases
|
# Search / facet type aliases
|
||||||
SearchFieldType = InstrumentedAttribute[Any] | tuple[InstrumentedAttribute[Any], ...]
|
SearchFieldType = InstrumentedAttribute[Any] | tuple[InstrumentedAttribute[Any], ...]
|
||||||
FacetFieldType = SearchFieldType
|
FacetFieldType = SearchFieldType
|
||||||
OrderFieldType = SearchFieldType
|
|
||||||
|
|
||||||
# Dependency type aliases
|
# Dependency type aliases
|
||||||
SessionDependency = Callable[[], AsyncGenerator[AsyncSession, None]] | Any
|
SessionDependency = Callable[[], AsyncGenerator[AsyncSession, None]] | Any
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
from enum import Enum
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -13,16 +12,13 @@ from sqlalchemy import (
|
|||||||
Column,
|
Column,
|
||||||
Date,
|
Date,
|
||||||
DateTime,
|
DateTime,
|
||||||
Enum as SAEnum,
|
|
||||||
ForeignKey,
|
ForeignKey,
|
||||||
Integer,
|
Integer,
|
||||||
JSON,
|
|
||||||
Numeric,
|
Numeric,
|
||||||
String,
|
String,
|
||||||
Table,
|
Table,
|
||||||
Uuid,
|
Uuid,
|
||||||
)
|
)
|
||||||
from sqlalchemy.dialects.postgresql import ARRAY
|
|
||||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||||
|
|
||||||
@@ -141,57 +137,6 @@ class Post(Base):
|
|||||||
tags: Mapped[list[Tag]] = relationship(secondary=post_tags)
|
tags: Mapped[list[Tag]] = relationship(secondary=post_tags)
|
||||||
|
|
||||||
|
|
||||||
class OrderStatus(int, Enum):
|
|
||||||
"""Integer-backed enum for order status."""
|
|
||||||
|
|
||||||
PENDING = 1
|
|
||||||
PROCESSING = 2
|
|
||||||
SHIPPED = 3
|
|
||||||
CANCELLED = 4
|
|
||||||
|
|
||||||
|
|
||||||
class Color(str, Enum):
|
|
||||||
"""String-backed enum for color."""
|
|
||||||
|
|
||||||
RED = "red"
|
|
||||||
GREEN = "green"
|
|
||||||
BLUE = "blue"
|
|
||||||
|
|
||||||
|
|
||||||
class Order(Base):
|
|
||||||
"""Test model with an IntEnum column (Enum(int, Enum)) and a raw Integer column."""
|
|
||||||
|
|
||||||
__tablename__ = "orders"
|
|
||||||
|
|
||||||
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
|
|
||||||
name: Mapped[str] = mapped_column(String(100))
|
|
||||||
status: Mapped[OrderStatus] = mapped_column(SAEnum(OrderStatus))
|
|
||||||
priority: Mapped[int] = mapped_column(Integer)
|
|
||||||
color: Mapped[Color] = mapped_column(SAEnum(Color))
|
|
||||||
|
|
||||||
|
|
||||||
class Transfer(Base):
|
|
||||||
"""Test model with two FKs to the same table (users)."""
|
|
||||||
|
|
||||||
__tablename__ = "transfers"
|
|
||||||
|
|
||||||
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
|
|
||||||
amount: Mapped[str] = mapped_column(String(50))
|
|
||||||
sender_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"))
|
|
||||||
receiver_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"))
|
|
||||||
|
|
||||||
|
|
||||||
class Article(Base):
|
|
||||||
"""Test article model with ARRAY and JSON columns."""
|
|
||||||
|
|
||||||
__tablename__ = "articles"
|
|
||||||
|
|
||||||
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
|
|
||||||
title: Mapped[str] = mapped_column(String(200))
|
|
||||||
labels: Mapped[list[str]] = mapped_column(ARRAY(String))
|
|
||||||
metadata_: Mapped[dict | None] = mapped_column("metadata", JSON, nullable=True)
|
|
||||||
|
|
||||||
|
|
||||||
class RoleCreate(BaseModel):
|
class RoleCreate(BaseModel):
|
||||||
"""Schema for creating a role."""
|
"""Schema for creating a role."""
|
||||||
|
|
||||||
@@ -326,61 +271,6 @@ class ProductCreate(BaseModel):
|
|||||||
price: decimal.Decimal
|
price: decimal.Decimal
|
||||||
|
|
||||||
|
|
||||||
class ArticleCreate(BaseModel):
|
|
||||||
"""Schema for creating an article."""
|
|
||||||
|
|
||||||
id: uuid.UUID | None = None
|
|
||||||
title: str
|
|
||||||
labels: list[str] = []
|
|
||||||
|
|
||||||
|
|
||||||
class ArticleRead(PydanticBase):
|
|
||||||
"""Schema for reading an article."""
|
|
||||||
|
|
||||||
id: uuid.UUID
|
|
||||||
title: str
|
|
||||||
labels: list[str]
|
|
||||||
|
|
||||||
|
|
||||||
class OrderCreate(BaseModel):
|
|
||||||
"""Schema for creating an order."""
|
|
||||||
|
|
||||||
id: uuid.UUID | None = None
|
|
||||||
name: str
|
|
||||||
status: OrderStatus
|
|
||||||
priority: int = 0
|
|
||||||
color: Color = Color.RED
|
|
||||||
|
|
||||||
|
|
||||||
class OrderRead(PydanticBase):
|
|
||||||
"""Schema for reading an order."""
|
|
||||||
|
|
||||||
id: uuid.UUID
|
|
||||||
name: str
|
|
||||||
status: OrderStatus
|
|
||||||
priority: int
|
|
||||||
color: Color
|
|
||||||
|
|
||||||
|
|
||||||
class TransferCreate(BaseModel):
|
|
||||||
"""Schema for creating a transfer."""
|
|
||||||
|
|
||||||
id: uuid.UUID | None = None
|
|
||||||
amount: str
|
|
||||||
sender_id: uuid.UUID
|
|
||||||
receiver_id: uuid.UUID
|
|
||||||
|
|
||||||
|
|
||||||
class TransferRead(PydanticBase):
|
|
||||||
"""Schema for reading a transfer."""
|
|
||||||
|
|
||||||
id: uuid.UUID
|
|
||||||
amount: str
|
|
||||||
|
|
||||||
|
|
||||||
OrderCrud = CrudFactory(Order)
|
|
||||||
TransferCrud = CrudFactory(Transfer)
|
|
||||||
ArticleCrud = CrudFactory(Article)
|
|
||||||
RoleCrud = CrudFactory(Role)
|
RoleCrud = CrudFactory(Role)
|
||||||
RoleCursorCrud = CrudFactory(Role, cursor_column=Role.id)
|
RoleCursorCrud = CrudFactory(Role, cursor_column=Role.id)
|
||||||
IntRoleCursorCrud = CrudFactory(IntRole, cursor_column=IntRole.id)
|
IntRoleCursorCrud = CrudFactory(IntRole, cursor_column=IntRole.id)
|
||||||
|
|||||||
@@ -38,10 +38,6 @@ from .conftest import (
|
|||||||
Tag,
|
Tag,
|
||||||
TagCreate,
|
TagCreate,
|
||||||
TagCrud,
|
TagCrud,
|
||||||
Transfer,
|
|
||||||
TransferCreate,
|
|
||||||
TransferCrud,
|
|
||||||
TransferRead,
|
|
||||||
User,
|
User,
|
||||||
UserCreate,
|
UserCreate,
|
||||||
UserCrud,
|
UserCrud,
|
||||||
@@ -215,92 +211,6 @@ class TestResolveLoadOptions:
|
|||||||
assert crud._resolve_load_options([]) == []
|
assert crud._resolve_load_options([]) == []
|
||||||
|
|
||||||
|
|
||||||
class TestResolveSearchColumns:
|
|
||||||
"""Tests for _resolve_search_columns logic."""
|
|
||||||
|
|
||||||
def test_returns_none_when_no_searchable_fields(self):
|
|
||||||
"""Returns None when cls.searchable_fields is None and no search_fields passed."""
|
|
||||||
|
|
||||||
class AbstractCrud(AsyncCrud[User]):
|
|
||||||
pass
|
|
||||||
|
|
||||||
assert AbstractCrud._resolve_search_columns(None) is None
|
|
||||||
|
|
||||||
def test_returns_none_when_empty_search_fields_passed(self):
|
|
||||||
"""Returns None when an empty list is passed explicitly."""
|
|
||||||
crud = CrudFactory(User)
|
|
||||||
assert crud._resolve_search_columns([]) is None
|
|
||||||
|
|
||||||
def test_returns_keys_from_class_searchable_fields(self):
|
|
||||||
"""Returns column keys from cls.searchable_fields when no override passed."""
|
|
||||||
crud = CrudFactory(User, searchable_fields=[User.username])
|
|
||||||
result = crud._resolve_search_columns(None)
|
|
||||||
assert result is not None
|
|
||||||
assert "username" in result
|
|
||||||
|
|
||||||
def test_search_fields_override_takes_priority(self):
|
|
||||||
"""Explicit search_fields override cls.searchable_fields."""
|
|
||||||
crud = CrudFactory(User, searchable_fields=[User.username])
|
|
||||||
result = crud._resolve_search_columns([User.email])
|
|
||||||
assert result is not None
|
|
||||||
assert "email" in result
|
|
||||||
assert "username" not in result
|
|
||||||
|
|
||||||
|
|
||||||
class TestResolveOrderColumns:
|
|
||||||
"""Tests for _resolve_order_columns logic."""
|
|
||||||
|
|
||||||
def test_returns_none_when_no_order_fields(self):
|
|
||||||
"""Returns None when cls.order_fields is None and no order_fields passed."""
|
|
||||||
|
|
||||||
class AbstractCrud(AsyncCrud[User]):
|
|
||||||
pass
|
|
||||||
|
|
||||||
assert AbstractCrud._resolve_order_columns(None) is None
|
|
||||||
|
|
||||||
def test_returns_none_when_empty_order_fields_passed(self):
|
|
||||||
"""Returns None when an empty list is passed explicitly."""
|
|
||||||
crud = CrudFactory(User)
|
|
||||||
assert crud._resolve_order_columns([]) is None
|
|
||||||
|
|
||||||
def test_returns_keys_from_class_order_fields(self):
|
|
||||||
"""Returns sorted column keys from cls.order_fields when no override passed."""
|
|
||||||
crud = CrudFactory(User, order_fields=[User.username])
|
|
||||||
result = crud._resolve_order_columns(None)
|
|
||||||
assert result is not None
|
|
||||||
assert "username" in result
|
|
||||||
|
|
||||||
def test_order_fields_override_takes_priority(self):
|
|
||||||
"""Explicit order_fields override cls.order_fields."""
|
|
||||||
crud = CrudFactory(User, order_fields=[User.username])
|
|
||||||
result = crud._resolve_order_columns([User.email])
|
|
||||||
assert result is not None
|
|
||||||
assert "email" in result
|
|
||||||
assert "username" not in result
|
|
||||||
|
|
||||||
def test_returns_sorted_keys(self):
|
|
||||||
"""Keys are returned in sorted order."""
|
|
||||||
crud = CrudFactory(User, order_fields=[User.email, User.username])
|
|
||||||
result = crud._resolve_order_columns(None)
|
|
||||||
assert result is not None
|
|
||||||
assert result == sorted(result)
|
|
||||||
|
|
||||||
def test_relation_tuple_produces_dunder_key(self):
|
|
||||||
"""A (rel, column) tuple produces a 'rel__column' key."""
|
|
||||||
crud = CrudFactory(User, order_fields=[(User.role, Role.name)])
|
|
||||||
result = crud._resolve_order_columns(None)
|
|
||||||
assert result == ["role__name"]
|
|
||||||
|
|
||||||
def test_mixed_flat_and_relation_fields(self):
|
|
||||||
"""Flat and relation fields can be mixed; keys are sorted."""
|
|
||||||
crud = CrudFactory(User, order_fields=[User.username, (User.role, Role.name)])
|
|
||||||
result = crud._resolve_order_columns(None)
|
|
||||||
assert result is not None
|
|
||||||
assert "username" in result
|
|
||||||
assert "role__name" in result
|
|
||||||
assert result == sorted(result)
|
|
||||||
|
|
||||||
|
|
||||||
class TestDefaultLoadOptionsIntegration:
|
class TestDefaultLoadOptionsIntegration:
|
||||||
"""Integration tests for default_load_options with real DB queries."""
|
"""Integration tests for default_load_options with real DB queries."""
|
||||||
|
|
||||||
@@ -380,43 +290,6 @@ class TestDefaultLoadOptionsIntegration:
|
|||||||
assert result.data[0].role is not None
|
assert result.data[0].role is not None
|
||||||
assert result.data[0].role.name == "admin"
|
assert result.data[0].role.name == "admin"
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_default_load_options_applied_to_create(
|
|
||||||
self, db_session: AsyncSession
|
|
||||||
):
|
|
||||||
"""default_load_options loads relationships after create()."""
|
|
||||||
UserWithDefaultLoad = CrudFactory(
|
|
||||||
User, default_load_options=[selectinload(User.role)]
|
|
||||||
)
|
|
||||||
role = await RoleCrud.create(db_session, RoleCreate(name="admin"))
|
|
||||||
user = await UserWithDefaultLoad.create(
|
|
||||||
db_session,
|
|
||||||
UserCreate(username="alice", email="alice@test.com", role_id=role.id),
|
|
||||||
)
|
|
||||||
assert user.role is not None
|
|
||||||
assert user.role.name == "admin"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_default_load_options_applied_to_update(
|
|
||||||
self, db_session: AsyncSession
|
|
||||||
):
|
|
||||||
"""default_load_options loads relationships after update()."""
|
|
||||||
UserWithDefaultLoad = CrudFactory(
|
|
||||||
User, default_load_options=[selectinload(User.role)]
|
|
||||||
)
|
|
||||||
role = await RoleCrud.create(db_session, RoleCreate(name="admin"))
|
|
||||||
user = await UserCrud.create(
|
|
||||||
db_session,
|
|
||||||
UserCreate(username="alice", email="alice@test.com"),
|
|
||||||
)
|
|
||||||
updated = await UserWithDefaultLoad.update(
|
|
||||||
db_session,
|
|
||||||
UserUpdate(role_id=role.id),
|
|
||||||
filters=[User.id == user.id],
|
|
||||||
)
|
|
||||||
assert updated.role is not None
|
|
||||||
assert updated.role.name == "admin"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_load_options_overrides_default_load_options(
|
async def test_load_options_overrides_default_load_options(
|
||||||
self, db_session: AsyncSession
|
self, db_session: AsyncSession
|
||||||
@@ -1377,128 +1250,6 @@ class TestCrudJoins:
|
|||||||
assert users[0].username == "multi_join"
|
assert users[0].username == "multi_join"
|
||||||
|
|
||||||
|
|
||||||
class TestCrudAliasedJoins:
|
|
||||||
"""Tests for CRUD operations with aliased joins (same table joined twice)."""
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_get_multi_with_aliased_joins(self, db_session: AsyncSession):
|
|
||||||
"""Aliased joins allow joining the same table twice."""
|
|
||||||
from sqlalchemy.orm import aliased
|
|
||||||
|
|
||||||
alice = await UserCrud.create(
|
|
||||||
db_session, UserCreate(username="alice", email="alice@test.com")
|
|
||||||
)
|
|
||||||
bob = await UserCrud.create(
|
|
||||||
db_session, UserCreate(username="bob", email="bob@test.com")
|
|
||||||
)
|
|
||||||
await TransferCrud.create(
|
|
||||||
db_session,
|
|
||||||
TransferCreate(amount="100", sender_id=alice.id, receiver_id=bob.id),
|
|
||||||
)
|
|
||||||
|
|
||||||
Sender = aliased(User)
|
|
||||||
Receiver = aliased(User)
|
|
||||||
|
|
||||||
results = await TransferCrud.get_multi(
|
|
||||||
db_session,
|
|
||||||
joins=[
|
|
||||||
(Sender, Transfer.sender_id == Sender.id),
|
|
||||||
(Receiver, Transfer.receiver_id == Receiver.id),
|
|
||||||
],
|
|
||||||
filters=[Sender.username == "alice", Receiver.username == "bob"],
|
|
||||||
)
|
|
||||||
assert len(results) == 1
|
|
||||||
assert results[0].amount == "100"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_get_multi_aliased_no_match(self, db_session: AsyncSession):
|
|
||||||
"""Aliased joins correctly filter out non-matching rows."""
|
|
||||||
from sqlalchemy.orm import aliased
|
|
||||||
|
|
||||||
alice = await UserCrud.create(
|
|
||||||
db_session, UserCreate(username="alice", email="alice@test.com")
|
|
||||||
)
|
|
||||||
bob = await UserCrud.create(
|
|
||||||
db_session, UserCreate(username="bob", email="bob@test.com")
|
|
||||||
)
|
|
||||||
await TransferCrud.create(
|
|
||||||
db_session,
|
|
||||||
TransferCreate(amount="100", sender_id=alice.id, receiver_id=bob.id),
|
|
||||||
)
|
|
||||||
|
|
||||||
Sender = aliased(User)
|
|
||||||
Receiver = aliased(User)
|
|
||||||
|
|
||||||
# bob is receiver, not sender — should return nothing
|
|
||||||
results = await TransferCrud.get_multi(
|
|
||||||
db_session,
|
|
||||||
joins=[
|
|
||||||
(Sender, Transfer.sender_id == Sender.id),
|
|
||||||
(Receiver, Transfer.receiver_id == Receiver.id),
|
|
||||||
],
|
|
||||||
filters=[Sender.username == "bob", Receiver.username == "alice"],
|
|
||||||
)
|
|
||||||
assert len(results) == 0
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_paginate_with_aliased_joins(self, db_session: AsyncSession):
|
|
||||||
"""Aliased joins work with offset_paginate."""
|
|
||||||
from sqlalchemy.orm import aliased
|
|
||||||
|
|
||||||
alice = await UserCrud.create(
|
|
||||||
db_session, UserCreate(username="alice", email="alice@test.com")
|
|
||||||
)
|
|
||||||
bob = await UserCrud.create(
|
|
||||||
db_session, UserCreate(username="bob", email="bob@test.com")
|
|
||||||
)
|
|
||||||
await TransferCrud.create(
|
|
||||||
db_session,
|
|
||||||
TransferCreate(amount="50", sender_id=alice.id, receiver_id=bob.id),
|
|
||||||
)
|
|
||||||
await TransferCrud.create(
|
|
||||||
db_session,
|
|
||||||
TransferCreate(amount="75", sender_id=bob.id, receiver_id=alice.id),
|
|
||||||
)
|
|
||||||
|
|
||||||
Sender = aliased(User)
|
|
||||||
result = await TransferCrud.offset_paginate(
|
|
||||||
db_session,
|
|
||||||
joins=[(Sender, Transfer.sender_id == Sender.id)],
|
|
||||||
filters=[Sender.username == "alice"],
|
|
||||||
schema=TransferRead,
|
|
||||||
)
|
|
||||||
assert result.pagination.total_count == 1
|
|
||||||
assert result.data[0].amount == "50"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_count_with_aliased_join(self, db_session: AsyncSession):
|
|
||||||
"""Aliased joins work with count."""
|
|
||||||
from sqlalchemy.orm import aliased
|
|
||||||
|
|
||||||
alice = await UserCrud.create(
|
|
||||||
db_session, UserCreate(username="alice", email="alice@test.com")
|
|
||||||
)
|
|
||||||
bob = await UserCrud.create(
|
|
||||||
db_session, UserCreate(username="bob", email="bob@test.com")
|
|
||||||
)
|
|
||||||
await TransferCrud.create(
|
|
||||||
db_session,
|
|
||||||
TransferCreate(amount="10", sender_id=alice.id, receiver_id=bob.id),
|
|
||||||
)
|
|
||||||
await TransferCrud.create(
|
|
||||||
db_session,
|
|
||||||
TransferCreate(amount="20", sender_id=alice.id, receiver_id=bob.id),
|
|
||||||
)
|
|
||||||
|
|
||||||
Sender = aliased(User)
|
|
||||||
count = await TransferCrud.count(
|
|
||||||
db_session,
|
|
||||||
joins=[(Sender, Transfer.sender_id == Sender.id)],
|
|
||||||
filters=[Sender.username == "alice"],
|
|
||||||
)
|
|
||||||
assert count == 2
|
|
||||||
|
|
||||||
|
|
||||||
class TestCrudFactoryM2M:
|
class TestCrudFactoryM2M:
|
||||||
"""Tests for CrudFactory with m2m_fields parameter."""
|
"""Tests for CrudFactory with m2m_fields parameter."""
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
454
tests/test_db.py
454
tests/test_db.py
@@ -4,26 +4,10 @@ import asyncio
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy import (
|
from sqlalchemy import text
|
||||||
Column,
|
|
||||||
ForeignKey,
|
|
||||||
ForeignKeyConstraint,
|
|
||||||
String,
|
|
||||||
Table,
|
|
||||||
Uuid,
|
|
||||||
select,
|
|
||||||
text,
|
|
||||||
)
|
|
||||||
from sqlalchemy.engine import make_url
|
from sqlalchemy.engine import make_url
|
||||||
from sqlalchemy.exc import IntegrityError
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||||
from sqlalchemy.orm import (
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
DeclarativeBase,
|
|
||||||
Mapped,
|
|
||||||
mapped_column,
|
|
||||||
relationship,
|
|
||||||
selectinload,
|
|
||||||
)
|
|
||||||
|
|
||||||
from fastapi_toolsets.db import (
|
from fastapi_toolsets.db import (
|
||||||
LockMode,
|
LockMode,
|
||||||
@@ -33,15 +17,12 @@ from fastapi_toolsets.db import (
|
|||||||
create_db_dependency,
|
create_db_dependency,
|
||||||
get_transaction,
|
get_transaction,
|
||||||
lock_tables,
|
lock_tables,
|
||||||
m2m_add,
|
|
||||||
m2m_remove,
|
|
||||||
m2m_set,
|
|
||||||
wait_for_row_change,
|
wait_for_row_change,
|
||||||
)
|
)
|
||||||
from fastapi_toolsets.exceptions import NotFoundError
|
from fastapi_toolsets.exceptions import NotFoundError
|
||||||
from fastapi_toolsets.pytest import create_db_session
|
from fastapi_toolsets.pytest import create_db_session
|
||||||
|
|
||||||
from .conftest import DATABASE_URL, Base, Post, Role, RoleCrud, Tag, User, UserCrud
|
from .conftest import DATABASE_URL, Base, Role, RoleCrud, User, UserCrud
|
||||||
|
|
||||||
|
|
||||||
class TestCreateDbDependency:
|
class TestCreateDbDependency:
|
||||||
@@ -100,21 +81,6 @@ class TestCreateDbDependency:
|
|||||||
|
|
||||||
await engine.dispose()
|
await engine.dispose()
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_no_commit_when_not_in_transaction(self):
|
|
||||||
"""Dependency skips commit if the session is no longer in a transaction on exit."""
|
|
||||||
engine = create_async_engine(DATABASE_URL, echo=False)
|
|
||||||
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
|
||||||
get_db = create_db_dependency(session_factory)
|
|
||||||
|
|
||||||
async for session in get_db():
|
|
||||||
# Manually commit — session exits the transaction
|
|
||||||
await session.commit()
|
|
||||||
assert not session.in_transaction()
|
|
||||||
# The dependency's post-yield path must not call commit again (no error)
|
|
||||||
|
|
||||||
await engine.dispose()
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_update_after_lock_tables_is_persisted(self):
|
async def test_update_after_lock_tables_is_persisted(self):
|
||||||
"""Changes made after lock_tables exits (before endpoint returns) are committed.
|
"""Changes made after lock_tables exits (before endpoint returns) are committed.
|
||||||
@@ -514,417 +480,3 @@ class TestCleanupTables:
|
|||||||
async with create_db_session(DATABASE_URL, Base, drop_tables=True) as session:
|
async with create_db_session(DATABASE_URL, Base, drop_tables=True) as session:
|
||||||
# Should not raise
|
# Should not raise
|
||||||
await cleanup_tables(session, EmptyBase)
|
await cleanup_tables(session, EmptyBase)
|
||||||
|
|
||||||
|
|
||||||
class TestM2MAdd:
|
|
||||||
"""Tests for m2m_add helper."""
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_adds_single_related(self, db_session: AsyncSession):
|
|
||||||
"""Associates one related instance via the secondary table."""
|
|
||||||
user = User(username="m2m_author", email="m2m@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
post = Post(title="Post A", author_id=user.id)
|
|
||||||
tag = Tag(name="python")
|
|
||||||
db_session.add_all([post, tag])
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_add(db_session, post, Post.tags, tag)
|
|
||||||
|
|
||||||
result = await db_session.execute(
|
|
||||||
select(Post).where(Post.id == post.id).options(selectinload(Post.tags))
|
|
||||||
)
|
|
||||||
loaded = result.scalar_one()
|
|
||||||
assert len(loaded.tags) == 1
|
|
||||||
assert loaded.tags[0].id == tag.id
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_adds_multiple_related(self, db_session: AsyncSession):
|
|
||||||
"""Associates multiple related instances in a single call."""
|
|
||||||
user = User(username="m2m_author2", email="m2m2@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
post = Post(title="Post B", author_id=user.id)
|
|
||||||
tag1 = Tag(name="web")
|
|
||||||
tag2 = Tag(name="api")
|
|
||||||
tag3 = Tag(name="async")
|
|
||||||
db_session.add_all([post, tag1, tag2, tag3])
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_add(db_session, post, Post.tags, tag1, tag2, tag3)
|
|
||||||
|
|
||||||
result = await db_session.execute(
|
|
||||||
select(Post).where(Post.id == post.id).options(selectinload(Post.tags))
|
|
||||||
)
|
|
||||||
loaded = result.scalar_one()
|
|
||||||
assert {t.id for t in loaded.tags} == {tag1.id, tag2.id, tag3.id}
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_noop_for_empty_related(self, db_session: AsyncSession):
|
|
||||||
"""Calling with no related instances is a no-op."""
|
|
||||||
user = User(username="m2m_author3", email="m2m3@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
post = Post(title="Post C", author_id=user.id)
|
|
||||||
db_session.add(post)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_add(db_session, post, Post.tags) # no related instances
|
|
||||||
|
|
||||||
result = await db_session.execute(
|
|
||||||
select(Post).where(Post.id == post.id).options(selectinload(Post.tags))
|
|
||||||
)
|
|
||||||
loaded = result.scalar_one()
|
|
||||||
assert loaded.tags == []
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_ignore_conflicts_true(self, db_session: AsyncSession):
|
|
||||||
"""Duplicate inserts are silently skipped when ignore_conflicts=True."""
|
|
||||||
user = User(username="m2m_author4", email="m2m4@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
post = Post(title="Post D", author_id=user.id)
|
|
||||||
tag = Tag(name="duplicate_tag")
|
|
||||||
db_session.add_all([post, tag])
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_add(db_session, post, Post.tags, tag)
|
|
||||||
|
|
||||||
# Second call with ignore_conflicts=True must not raise
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_add(db_session, post, Post.tags, tag, ignore_conflicts=True)
|
|
||||||
|
|
||||||
result = await db_session.execute(
|
|
||||||
select(Post).where(Post.id == post.id).options(selectinload(Post.tags))
|
|
||||||
)
|
|
||||||
loaded = result.scalar_one()
|
|
||||||
assert len(loaded.tags) == 1
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_ignore_conflicts_false_raises(self, db_session: AsyncSession):
|
|
||||||
"""Duplicate inserts raise IntegrityError when ignore_conflicts=False (default)."""
|
|
||||||
user = User(username="m2m_author5", email="m2m5@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
post = Post(title="Post E", author_id=user.id)
|
|
||||||
tag = Tag(name="conflict_tag")
|
|
||||||
db_session.add_all([post, tag])
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_add(db_session, post, Post.tags, tag)
|
|
||||||
|
|
||||||
with pytest.raises(IntegrityError):
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_add(db_session, post, Post.tags, tag)
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_non_m2m_raises_type_error(self, db_session: AsyncSession):
|
|
||||||
"""Passing a non-M2M relationship attribute raises TypeError."""
|
|
||||||
user = User(username="m2m_author6", email="m2m6@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
role = Role(name="type_err_role")
|
|
||||||
db_session.add(role)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
with pytest.raises(TypeError, match="Many-to-Many"):
|
|
||||||
await m2m_add(db_session, user, User.role, role)
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_works_inside_lock_tables(self, db_session: AsyncSession):
|
|
||||||
"""m2m_add works correctly inside a lock_tables nested transaction."""
|
|
||||||
user = User(username="m2m_lock_author", email="m2m_lock@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
async with lock_tables(db_session, [Tag]):
|
|
||||||
tag = Tag(name="locked_tag")
|
|
||||||
db_session.add(tag)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
post = Post(title="Post Lock", author_id=user.id)
|
|
||||||
db_session.add(post)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
await m2m_add(db_session, post, Post.tags, tag)
|
|
||||||
|
|
||||||
result = await db_session.execute(
|
|
||||||
select(Post).where(Post.id == post.id).options(selectinload(Post.tags))
|
|
||||||
)
|
|
||||||
loaded = result.scalar_one()
|
|
||||||
assert len(loaded.tags) == 1
|
|
||||||
assert loaded.tags[0].name == "locked_tag"
|
|
||||||
|
|
||||||
|
|
||||||
class _LocalBase(DeclarativeBase):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
_comp_assoc = Table(
|
|
||||||
"_comp_assoc",
|
|
||||||
_LocalBase.metadata,
|
|
||||||
Column("owner_id", Uuid, ForeignKey("_comp_owners.id"), primary_key=True),
|
|
||||||
Column("item_group", String(50), primary_key=True),
|
|
||||||
Column("item_code", String(50), primary_key=True),
|
|
||||||
ForeignKeyConstraint(
|
|
||||||
["item_group", "item_code"],
|
|
||||||
["_comp_items.group_id", "_comp_items.item_code"],
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class _CompOwner(_LocalBase):
|
|
||||||
__tablename__ = "_comp_owners"
|
|
||||||
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
|
|
||||||
items: Mapped[list["_CompItem"]] = relationship(secondary=_comp_assoc)
|
|
||||||
|
|
||||||
|
|
||||||
class _CompItem(_LocalBase):
|
|
||||||
__tablename__ = "_comp_items"
|
|
||||||
group_id: Mapped[str] = mapped_column(String(50), primary_key=True)
|
|
||||||
item_code: Mapped[str] = mapped_column(String(50), primary_key=True)
|
|
||||||
|
|
||||||
|
|
||||||
class TestM2MRemove:
|
|
||||||
"""Tests for m2m_remove helper."""
|
|
||||||
|
|
||||||
async def _setup(
|
|
||||||
self, session: AsyncSession, username: str, email: str, *tag_names: str
|
|
||||||
):
|
|
||||||
"""Create a user, post, and tags; associate all tags with the post."""
|
|
||||||
user = User(username=username, email=email)
|
|
||||||
session.add(user)
|
|
||||||
await session.flush()
|
|
||||||
|
|
||||||
post = Post(title=f"Post {username}", author_id=user.id)
|
|
||||||
tags = [Tag(name=n) for n in tag_names]
|
|
||||||
session.add(post)
|
|
||||||
session.add_all(tags)
|
|
||||||
await session.flush()
|
|
||||||
|
|
||||||
async with get_transaction(session):
|
|
||||||
await m2m_add(session, post, Post.tags, *tags)
|
|
||||||
|
|
||||||
return post, tags
|
|
||||||
|
|
||||||
async def _load_tags(self, session: AsyncSession, post: Post) -> list[Tag]:
|
|
||||||
result = await session.execute(
|
|
||||||
select(Post).where(Post.id == post.id).options(selectinload(Post.tags))
|
|
||||||
)
|
|
||||||
return result.scalar_one().tags
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_removes_single(self, db_session: AsyncSession):
|
|
||||||
"""Removes one association, leaving others intact."""
|
|
||||||
post, (tag1, tag2) = await self._setup(
|
|
||||||
db_session, "rm_author1", "rm1@test.com", "tag_rm_a", "tag_rm_b"
|
|
||||||
)
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_remove(db_session, post, Post.tags, tag1)
|
|
||||||
|
|
||||||
remaining = await self._load_tags(db_session, post)
|
|
||||||
assert len(remaining) == 1
|
|
||||||
assert remaining[0].id == tag2.id
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_removes_multiple(self, db_session: AsyncSession):
|
|
||||||
"""Removes multiple associations in one call."""
|
|
||||||
post, (tag1, tag2, tag3) = await self._setup(
|
|
||||||
db_session, "rm_author2", "rm2@test.com", "tag_rm_c", "tag_rm_d", "tag_rm_e"
|
|
||||||
)
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_remove(db_session, post, Post.tags, tag1, tag3)
|
|
||||||
|
|
||||||
remaining = await self._load_tags(db_session, post)
|
|
||||||
assert len(remaining) == 1
|
|
||||||
assert remaining[0].id == tag2.id
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_noop_for_empty_related(self, db_session: AsyncSession):
|
|
||||||
"""Calling with no related instances is a no-op."""
|
|
||||||
post, (tag,) = await self._setup(
|
|
||||||
db_session, "rm_author3", "rm3@test.com", "tag_rm_f"
|
|
||||||
)
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_remove(db_session, post, Post.tags)
|
|
||||||
|
|
||||||
remaining = await self._load_tags(db_session, post)
|
|
||||||
assert len(remaining) == 1
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_idempotent_for_missing_association(self, db_session: AsyncSession):
|
|
||||||
"""Removing a non-existent association does not raise."""
|
|
||||||
post, (tag1,) = await self._setup(
|
|
||||||
db_session, "rm_author4", "rm4@test.com", "tag_rm_g"
|
|
||||||
)
|
|
||||||
tag2 = Tag(name="tag_rm_h")
|
|
||||||
db_session.add(tag2)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
# tag2 was never associated — should not raise
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_remove(db_session, post, Post.tags, tag2)
|
|
||||||
|
|
||||||
remaining = await self._load_tags(db_session, post)
|
|
||||||
assert len(remaining) == 1
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_non_m2m_raises_type_error(self, db_session: AsyncSession):
|
|
||||||
"""Passing a non-M2M relationship attribute raises TypeError."""
|
|
||||||
user = User(username="rm_author5", email="rm5@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
role = Role(name="rm_type_err_role")
|
|
||||||
db_session.add(role)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
with pytest.raises(TypeError, match="Many-to-Many"):
|
|
||||||
await m2m_remove(db_session, user, User.role, role)
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_removes_composite_pk_related(self):
|
|
||||||
"""Composite-PK branch: DELETE uses tuple IN when related side has multi-col PK."""
|
|
||||||
engine = create_async_engine(DATABASE_URL, echo=False)
|
|
||||||
async with engine.begin() as conn:
|
|
||||||
await conn.run_sync(_LocalBase.metadata.create_all)
|
|
||||||
|
|
||||||
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
|
||||||
try:
|
|
||||||
async with session_factory() as session:
|
|
||||||
owner = _CompOwner()
|
|
||||||
item1 = _CompItem(group_id="g1", item_code="c1")
|
|
||||||
item2 = _CompItem(group_id="g1", item_code="c2")
|
|
||||||
session.add_all([owner, item1, item2])
|
|
||||||
await session.flush()
|
|
||||||
|
|
||||||
async with get_transaction(session):
|
|
||||||
await m2m_add(session, owner, _CompOwner.items, item1, item2)
|
|
||||||
|
|
||||||
async with get_transaction(session):
|
|
||||||
await m2m_remove(session, owner, _CompOwner.items, item1)
|
|
||||||
|
|
||||||
await session.commit()
|
|
||||||
|
|
||||||
async with session_factory() as verify:
|
|
||||||
from sqlalchemy import select
|
|
||||||
from sqlalchemy.orm import selectinload
|
|
||||||
|
|
||||||
result = await verify.execute(
|
|
||||||
select(_CompOwner)
|
|
||||||
.where(_CompOwner.id == owner.id)
|
|
||||||
.options(selectinload(_CompOwner.items))
|
|
||||||
)
|
|
||||||
loaded = result.scalar_one()
|
|
||||||
assert len(loaded.items) == 1
|
|
||||||
assert (loaded.items[0].group_id, loaded.items[0].item_code) == (
|
|
||||||
"g1",
|
|
||||||
"c2",
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
async with engine.begin() as conn:
|
|
||||||
await conn.run_sync(_LocalBase.metadata.drop_all)
|
|
||||||
await engine.dispose()
|
|
||||||
|
|
||||||
|
|
||||||
class TestM2MSet:
|
|
||||||
"""Tests for m2m_set helper."""
|
|
||||||
|
|
||||||
async def _load_tags(self, session: AsyncSession, post: Post) -> list[Tag]:
|
|
||||||
result = await session.execute(
|
|
||||||
select(Post).where(Post.id == post.id).options(selectinload(Post.tags))
|
|
||||||
)
|
|
||||||
return result.scalar_one().tags
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_replaces_existing_set(self, db_session: AsyncSession):
|
|
||||||
"""Replaces the full association set atomically."""
|
|
||||||
user = User(username="set_author1", email="set1@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
post = Post(title="Post Set A", author_id=user.id)
|
|
||||||
tag1 = Tag(name="tag_set_a")
|
|
||||||
tag2 = Tag(name="tag_set_b")
|
|
||||||
tag3 = Tag(name="tag_set_c")
|
|
||||||
db_session.add_all([post, tag1, tag2, tag3])
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_add(db_session, post, Post.tags, tag1, tag2)
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_set(db_session, post, Post.tags, tag3)
|
|
||||||
|
|
||||||
remaining = await self._load_tags(db_session, post)
|
|
||||||
assert len(remaining) == 1
|
|
||||||
assert remaining[0].id == tag3.id
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_clears_all_when_no_related(self, db_session: AsyncSession):
|
|
||||||
"""Passing no related instances clears all associations."""
|
|
||||||
user = User(username="set_author2", email="set2@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
post = Post(title="Post Set B", author_id=user.id)
|
|
||||||
tag = Tag(name="tag_set_d")
|
|
||||||
db_session.add_all([post, tag])
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_add(db_session, post, Post.tags, tag)
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_set(db_session, post, Post.tags)
|
|
||||||
|
|
||||||
remaining = await self._load_tags(db_session, post)
|
|
||||||
assert remaining == []
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_set_on_empty_then_populate(self, db_session: AsyncSession):
|
|
||||||
"""m2m_set works on a post with no existing associations."""
|
|
||||||
user = User(username="set_author3", email="set3@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
post = Post(title="Post Set C", author_id=user.id)
|
|
||||||
tag1 = Tag(name="tag_set_e")
|
|
||||||
tag2 = Tag(name="tag_set_f")
|
|
||||||
db_session.add_all([post, tag1, tag2])
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
async with get_transaction(db_session):
|
|
||||||
await m2m_set(db_session, post, Post.tags, tag1, tag2)
|
|
||||||
|
|
||||||
remaining = await self._load_tags(db_session, post)
|
|
||||||
assert {t.id for t in remaining} == {tag1.id, tag2.id}
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_non_m2m_raises_type_error(self, db_session: AsyncSession):
|
|
||||||
"""Passing a non-M2M relationship attribute raises TypeError."""
|
|
||||||
user = User(username="set_author4", email="set4@test.com")
|
|
||||||
db_session.add(user)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
role = Role(name="set_type_err_role")
|
|
||||||
db_session.add(role)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
with pytest.raises(TypeError, match="Many-to-Many"):
|
|
||||||
await m2m_set(db_session, user, User.role, role)
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ from fastapi_toolsets.fixtures import (
|
|||||||
Context,
|
Context,
|
||||||
FixtureRegistry,
|
FixtureRegistry,
|
||||||
LoadStrategy,
|
LoadStrategy,
|
||||||
get_field_by_attr,
|
|
||||||
get_obj_by_attr,
|
get_obj_by_attr,
|
||||||
load_fixtures,
|
load_fixtures,
|
||||||
load_fixtures_by_context,
|
load_fixtures_by_context,
|
||||||
@@ -952,41 +951,6 @@ class TestGetObjByAttr:
|
|||||||
get_obj_by_attr(self.roles, "id", "not-a-uuid")
|
get_obj_by_attr(self.roles, "id", "not-a-uuid")
|
||||||
|
|
||||||
|
|
||||||
class TestGetFieldByAttr:
|
|
||||||
"""Tests for get_field_by_attr helper function."""
|
|
||||||
|
|
||||||
def setup_method(self):
|
|
||||||
self.registry = FixtureRegistry()
|
|
||||||
self.role_id_1 = uuid.uuid4()
|
|
||||||
self.role_id_2 = uuid.uuid4()
|
|
||||||
role_id_1 = self.role_id_1
|
|
||||||
role_id_2 = self.role_id_2
|
|
||||||
|
|
||||||
@self.registry.register
|
|
||||||
def roles() -> list[Role]:
|
|
||||||
return [
|
|
||||||
Role(id=role_id_1, name="admin"),
|
|
||||||
Role(id=role_id_2, name="user"),
|
|
||||||
]
|
|
||||||
|
|
||||||
self.roles = roles
|
|
||||||
|
|
||||||
def test_returns_id_by_default(self):
|
|
||||||
"""Returns the id field when no field is specified."""
|
|
||||||
result = get_field_by_attr(self.roles, "name", "admin")
|
|
||||||
assert result == self.role_id_1
|
|
||||||
|
|
||||||
def test_returns_specified_field(self):
|
|
||||||
"""Returns the requested field instead of id."""
|
|
||||||
result = get_field_by_attr(self.roles, "id", self.role_id_2, field="name")
|
|
||||||
assert result == "user"
|
|
||||||
|
|
||||||
def test_no_match_raises_stop_iteration(self):
|
|
||||||
"""Propagates StopIteration from get_obj_by_attr when no match found."""
|
|
||||||
with pytest.raises(StopIteration, match="No object with name=missing"):
|
|
||||||
get_field_by_attr(self.roles, "name", "missing")
|
|
||||||
|
|
||||||
|
|
||||||
class TestGetPrimaryKey:
|
class TestGetPrimaryKey:
|
||||||
"""Unit tests for the _get_primary_key helper (composite PK paths)."""
|
"""Unit tests for the _get_primary_key helper (composite PK paths)."""
|
||||||
|
|
||||||
@@ -1047,14 +1011,6 @@ class TestInstanceToDict:
|
|||||||
assert "id" not in d
|
assert "id" not in d
|
||||||
assert d["name"] == "admin"
|
assert d["name"] == "admin"
|
||||||
|
|
||||||
def test_autoincrement_none_excluded(self):
|
|
||||||
"""A column whose value is None but has autoincrement=True is excluded
|
|
||||||
so the DB generates the value via its sequence."""
|
|
||||||
instance = IntRole(id=None, name="admin")
|
|
||||||
d = _instance_to_dict(instance)
|
|
||||||
assert "id" not in d
|
|
||||||
assert d["name"] == "admin"
|
|
||||||
|
|
||||||
def test_nullable_none_included(self):
|
def test_nullable_none_included(self):
|
||||||
"""None on a nullable column with no default is kept (explicit NULL)."""
|
"""None on a nullable column with no default is kept (explicit NULL)."""
|
||||||
instance = User(id=uuid.uuid4(), username="u", email="e@e.com", role_id=None)
|
instance = User(id=uuid.uuid4(), username="u", email="e@e.com", role_id=None)
|
||||||
@@ -1151,361 +1107,3 @@ class TestBatchMergeNonPkColumns:
|
|||||||
db_session, registry, "permissions", strategy=LoadStrategy.MERGE
|
db_session, registry, "permissions", strategy=LoadStrategy.MERGE
|
||||||
)
|
)
|
||||||
assert len(result2["permissions"]) == 2
|
assert len(result2["permissions"]) == 2
|
||||||
|
|
||||||
|
|
||||||
class TestBatchNullableColumnEdgeCases:
|
|
||||||
"""Deep tests for nullable column handling during batch import."""
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_insert_batch_mixed_nullable_fk(self, db_session: AsyncSession):
|
|
||||||
"""INSERT batch where some rows set a nullable FK and others don't.
|
|
||||||
|
|
||||||
After normalization the omitted role_id becomes None. For INSERT this
|
|
||||||
is acceptable — both rows should insert successfully with the correct
|
|
||||||
values (one with FK, one with NULL).
|
|
||||||
"""
|
|
||||||
registry = FixtureRegistry()
|
|
||||||
admin = await RoleCrud.create(db_session, RoleCreate(name="admin"))
|
|
||||||
uid1 = uuid.uuid4()
|
|
||||||
uid2 = uuid.uuid4()
|
|
||||||
|
|
||||||
@registry.register
|
|
||||||
def users():
|
|
||||||
return [
|
|
||||||
User(
|
|
||||||
id=uid1, username="with_role", email="a@test.com", role_id=admin.id
|
|
||||||
),
|
|
||||||
User(id=uid2, username="no_role", email="b@test.com"),
|
|
||||||
]
|
|
||||||
|
|
||||||
await load_fixtures(db_session, registry, "users", strategy=LoadStrategy.INSERT)
|
|
||||||
|
|
||||||
from sqlalchemy import select
|
|
||||||
|
|
||||||
rows = {
|
|
||||||
r.username: r
|
|
||||||
for r in (await db_session.execute(select(User))).scalars().all()
|
|
||||||
}
|
|
||||||
assert rows["with_role"].role_id == admin.id
|
|
||||||
assert rows["no_role"].role_id is None
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_insert_batch_mixed_nullable_notes(self, db_session: AsyncSession):
|
|
||||||
"""INSERT batch where some rows have notes and others don't.
|
|
||||||
|
|
||||||
Ensures normalization doesn't break the insert and that each row gets
|
|
||||||
the intended value.
|
|
||||||
"""
|
|
||||||
registry = FixtureRegistry()
|
|
||||||
uid1 = uuid.uuid4()
|
|
||||||
uid2 = uuid.uuid4()
|
|
||||||
uid3 = uuid.uuid4()
|
|
||||||
|
|
||||||
@registry.register
|
|
||||||
def users():
|
|
||||||
return [
|
|
||||||
User(
|
|
||||||
id=uid1,
|
|
||||||
username="has_notes",
|
|
||||||
email="a@test.com",
|
|
||||||
notes="important",
|
|
||||||
),
|
|
||||||
User(id=uid2, username="no_notes", email="b@test.com"),
|
|
||||||
User(id=uid3, username="null_notes", email="c@test.com", notes=None),
|
|
||||||
]
|
|
||||||
|
|
||||||
await load_fixtures(db_session, registry, "users", strategy=LoadStrategy.INSERT)
|
|
||||||
|
|
||||||
from sqlalchemy import select
|
|
||||||
|
|
||||||
rows = {
|
|
||||||
r.username: r
|
|
||||||
for r in (await db_session.execute(select(User))).scalars().all()
|
|
||||||
}
|
|
||||||
assert rows["has_notes"].notes == "important"
|
|
||||||
assert rows["no_notes"].notes is None
|
|
||||||
assert rows["null_notes"].notes is None
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_merge_batch_mixed_nullable_does_not_overwrite(
|
|
||||||
self, db_session: AsyncSession
|
|
||||||
):
|
|
||||||
"""MERGE batch where one row sets a nullable column and another omits it.
|
|
||||||
|
|
||||||
If both rows already exist in DB, the row that omits the column must
|
|
||||||
NOT have its existing value overwritten with NULL.
|
|
||||||
|
|
||||||
This is the core normalization bug: _normalize_rows fills missing keys
|
|
||||||
with None, and then MERGE's SET clause includes that column for ALL rows.
|
|
||||||
"""
|
|
||||||
from sqlalchemy import select
|
|
||||||
|
|
||||||
admin = await RoleCrud.create(db_session, RoleCreate(name="admin"))
|
|
||||||
uid1 = uuid.uuid4()
|
|
||||||
uid2 = uuid.uuid4()
|
|
||||||
|
|
||||||
# Pre-populate: both users have role_id and notes
|
|
||||||
registry_initial = FixtureRegistry()
|
|
||||||
|
|
||||||
@registry_initial.register
|
|
||||||
def users():
|
|
||||||
return [
|
|
||||||
User(
|
|
||||||
id=uid1,
|
|
||||||
username="alice",
|
|
||||||
email="a@test.com",
|
|
||||||
role_id=admin.id,
|
|
||||||
notes="alice notes",
|
|
||||||
),
|
|
||||||
User(
|
|
||||||
id=uid2,
|
|
||||||
username="bob",
|
|
||||||
email="b@test.com",
|
|
||||||
role_id=admin.id,
|
|
||||||
notes="bob notes",
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
await load_fixtures(
|
|
||||||
db_session, registry_initial, "users", strategy=LoadStrategy.INSERT
|
|
||||||
)
|
|
||||||
|
|
||||||
# Re-merge: alice updates notes, bob omits notes entirely
|
|
||||||
registry_merge = FixtureRegistry()
|
|
||||||
|
|
||||||
@registry_merge.register
|
|
||||||
def users(): # noqa: F811
|
|
||||||
return [
|
|
||||||
User(
|
|
||||||
id=uid1,
|
|
||||||
username="alice",
|
|
||||||
email="a@test.com",
|
|
||||||
role_id=admin.id,
|
|
||||||
notes="updated",
|
|
||||||
),
|
|
||||||
User(
|
|
||||||
id=uid2,
|
|
||||||
username="bob",
|
|
||||||
email="b@test.com",
|
|
||||||
role_id=admin.id,
|
|
||||||
), # notes omitted
|
|
||||||
]
|
|
||||||
|
|
||||||
await load_fixtures(
|
|
||||||
db_session, registry_merge, "users", strategy=LoadStrategy.MERGE
|
|
||||||
)
|
|
||||||
|
|
||||||
rows = {
|
|
||||||
r.username: r
|
|
||||||
for r in (await db_session.execute(select(User))).scalars().all()
|
|
||||||
}
|
|
||||||
assert rows["alice"].notes == "updated"
|
|
||||||
# Bob's notes must be preserved, NOT overwritten with NULL
|
|
||||||
assert rows["bob"].notes == "bob notes"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_merge_batch_mixed_nullable_fk_preserves_existing(
|
|
||||||
self, db_session: AsyncSession
|
|
||||||
):
|
|
||||||
"""MERGE batch where one row sets role_id and another omits it.
|
|
||||||
|
|
||||||
The row that omits role_id must keep its existing DB value.
|
|
||||||
"""
|
|
||||||
from sqlalchemy import select
|
|
||||||
|
|
||||||
admin = await RoleCrud.create(db_session, RoleCreate(name="admin"))
|
|
||||||
editor = await RoleCrud.create(db_session, RoleCreate(name="editor"))
|
|
||||||
uid1 = uuid.uuid4()
|
|
||||||
uid2 = uuid.uuid4()
|
|
||||||
|
|
||||||
# Pre-populate
|
|
||||||
registry_initial = FixtureRegistry()
|
|
||||||
|
|
||||||
@registry_initial.register
|
|
||||||
def users():
|
|
||||||
return [
|
|
||||||
User(
|
|
||||||
id=uid1,
|
|
||||||
username="alice",
|
|
||||||
email="a@test.com",
|
|
||||||
role_id=admin.id,
|
|
||||||
),
|
|
||||||
User(
|
|
||||||
id=uid2,
|
|
||||||
username="bob",
|
|
||||||
email="b@test.com",
|
|
||||||
role_id=editor.id,
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
await load_fixtures(
|
|
||||||
db_session, registry_initial, "users", strategy=LoadStrategy.INSERT
|
|
||||||
)
|
|
||||||
|
|
||||||
# Re-merge: alice changes role, bob omits role_id
|
|
||||||
registry_merge = FixtureRegistry()
|
|
||||||
|
|
||||||
@registry_merge.register
|
|
||||||
def users(): # noqa: F811
|
|
||||||
return [
|
|
||||||
User(
|
|
||||||
id=uid1,
|
|
||||||
username="alice",
|
|
||||||
email="a@test.com",
|
|
||||||
role_id=editor.id,
|
|
||||||
),
|
|
||||||
User(id=uid2, username="bob", email="b@test.com"), # role_id omitted
|
|
||||||
]
|
|
||||||
|
|
||||||
await load_fixtures(
|
|
||||||
db_session, registry_merge, "users", strategy=LoadStrategy.MERGE
|
|
||||||
)
|
|
||||||
|
|
||||||
rows = {
|
|
||||||
r.username: r
|
|
||||||
for r in (await db_session.execute(select(User))).scalars().all()
|
|
||||||
}
|
|
||||||
assert rows["alice"].role_id == editor.id # updated
|
|
||||||
assert rows["bob"].role_id == editor.id # must be preserved, NOT NULL
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_insert_batch_mixed_pk_presence(self, db_session: AsyncSession):
|
|
||||||
"""INSERT batch where some rows have explicit PK and others rely on
|
|
||||||
the callable default (uuid.uuid4).
|
|
||||||
|
|
||||||
Normalization adds the PK key with None to rows that omitted it,
|
|
||||||
which can cause NOT NULL violations on the PK column.
|
|
||||||
"""
|
|
||||||
registry = FixtureRegistry()
|
|
||||||
explicit_id = uuid.uuid4()
|
|
||||||
|
|
||||||
@registry.register
|
|
||||||
def roles():
|
|
||||||
return [
|
|
||||||
Role(id=explicit_id, name="admin"),
|
|
||||||
Role(name="user"), # PK omitted, relies on default=uuid.uuid4
|
|
||||||
]
|
|
||||||
|
|
||||||
await load_fixtures(db_session, registry, "roles", strategy=LoadStrategy.INSERT)
|
|
||||||
|
|
||||||
from sqlalchemy import select
|
|
||||||
|
|
||||||
rows = (await db_session.execute(select(Role))).scalars().all()
|
|
||||||
assert len(rows) == 2
|
|
||||||
names = {r.name for r in rows}
|
|
||||||
assert names == {"admin", "user"}
|
|
||||||
# The "admin" row must have the explicit ID
|
|
||||||
admin = next(r for r in rows if r.name == "admin")
|
|
||||||
assert admin.id == explicit_id
|
|
||||||
# The "user" row must have a generated UUID (not None)
|
|
||||||
user = next(r for r in rows if r.name == "user")
|
|
||||||
assert user.id is not None
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_skip_existing_batch_mixed_nullable(self, db_session: AsyncSession):
|
|
||||||
"""SKIP_EXISTING with mixed nullable columns inserts correctly.
|
|
||||||
|
|
||||||
Only new rows are inserted; existing rows are untouched regardless of
|
|
||||||
which columns the fixture provides.
|
|
||||||
"""
|
|
||||||
from sqlalchemy import select
|
|
||||||
|
|
||||||
admin = await RoleCrud.create(db_session, RoleCreate(name="admin"))
|
|
||||||
uid1 = uuid.uuid4()
|
|
||||||
uid2 = uuid.uuid4()
|
|
||||||
|
|
||||||
# Pre-populate uid1 with notes
|
|
||||||
registry_initial = FixtureRegistry()
|
|
||||||
|
|
||||||
@registry_initial.register
|
|
||||||
def users():
|
|
||||||
return [
|
|
||||||
User(
|
|
||||||
id=uid1,
|
|
||||||
username="alice",
|
|
||||||
email="a@test.com",
|
|
||||||
role_id=admin.id,
|
|
||||||
notes="keep me",
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
await load_fixtures(
|
|
||||||
db_session, registry_initial, "users", strategy=LoadStrategy.INSERT
|
|
||||||
)
|
|
||||||
|
|
||||||
# Load again with SKIP_EXISTING: uid1 already exists, uid2 is new
|
|
||||||
registry_skip = FixtureRegistry()
|
|
||||||
|
|
||||||
@registry_skip.register
|
|
||||||
def users(): # noqa: F811
|
|
||||||
return [
|
|
||||||
User(id=uid1, username="alice-updated", email="a@test.com"), # exists
|
|
||||||
User(
|
|
||||||
id=uid2,
|
|
||||||
username="bob",
|
|
||||||
email="b@test.com",
|
|
||||||
notes="new user",
|
|
||||||
), # new
|
|
||||||
]
|
|
||||||
|
|
||||||
result = await load_fixtures(
|
|
||||||
db_session, registry_skip, "users", strategy=LoadStrategy.SKIP_EXISTING
|
|
||||||
)
|
|
||||||
assert len(result["users"]) == 1 # only bob inserted
|
|
||||||
|
|
||||||
rows = {
|
|
||||||
r.username: r
|
|
||||||
for r in (await db_session.execute(select(User))).scalars().all()
|
|
||||||
}
|
|
||||||
# alice untouched
|
|
||||||
assert rows["alice"].role_id == admin.id
|
|
||||||
assert rows["alice"].notes == "keep me"
|
|
||||||
# bob inserted correctly
|
|
||||||
assert rows["bob"].notes == "new user"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_insert_batch_every_row_different_nullable_columns(
|
|
||||||
self, db_session: AsyncSession
|
|
||||||
):
|
|
||||||
"""Each row in the batch sets a different combination of nullable columns.
|
|
||||||
|
|
||||||
Tests that normalization produces valid SQL for all rows.
|
|
||||||
"""
|
|
||||||
registry = FixtureRegistry()
|
|
||||||
admin = await RoleCrud.create(db_session, RoleCreate(name="admin"))
|
|
||||||
uid1 = uuid.uuid4()
|
|
||||||
uid2 = uuid.uuid4()
|
|
||||||
uid3 = uuid.uuid4()
|
|
||||||
|
|
||||||
@registry.register
|
|
||||||
def users():
|
|
||||||
return [
|
|
||||||
User(
|
|
||||||
id=uid1,
|
|
||||||
username="all_set",
|
|
||||||
email="a@test.com",
|
|
||||||
role_id=admin.id,
|
|
||||||
notes="full",
|
|
||||||
),
|
|
||||||
User(
|
|
||||||
id=uid2, username="only_role", email="b@test.com", role_id=admin.id
|
|
||||||
),
|
|
||||||
User(
|
|
||||||
id=uid3, username="only_notes", email="c@test.com", notes="partial"
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
await load_fixtures(db_session, registry, "users", strategy=LoadStrategy.INSERT)
|
|
||||||
|
|
||||||
from sqlalchemy import select
|
|
||||||
|
|
||||||
rows = {
|
|
||||||
r.username: r
|
|
||||||
for r in (await db_session.execute(select(User))).scalars().all()
|
|
||||||
}
|
|
||||||
assert rows["all_set"].role_id == admin.id
|
|
||||||
assert rows["all_set"].notes == "full"
|
|
||||||
assert rows["only_role"].role_id == admin.id
|
|
||||||
assert rows["only_role"].notes is None
|
|
||||||
assert rows["only_notes"].role_id is None
|
|
||||||
assert rows["only_notes"].notes == "partial"
|
|
||||||
|
|||||||
1196
tests/test_models.py
1196
tests/test_models.py
File diff suppressed because it is too large
Load Diff
@@ -1,18 +1,17 @@
|
|||||||
"""Tests for fastapi_toolsets.pytest module."""
|
"""Tests for fastapi_toolsets.pytest module."""
|
||||||
|
|
||||||
import uuid
|
import uuid
|
||||||
from typing import cast
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi import Depends, FastAPI
|
from fastapi import Depends, FastAPI
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
from sqlalchemy import ForeignKey, String, select, text
|
from sqlalchemy import select, text
|
||||||
from sqlalchemy.engine import make_url
|
from sqlalchemy.engine import make_url
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
from sqlalchemy.orm import selectinload
|
||||||
|
|
||||||
from fastapi_toolsets.db import get_transaction
|
from fastapi_toolsets.db import get_transaction
|
||||||
from fastapi_toolsets.fixtures import Context, FixtureRegistry, LoadStrategy
|
from fastapi_toolsets.fixtures import Context, FixtureRegistry
|
||||||
from fastapi_toolsets.pytest import (
|
from fastapi_toolsets.pytest import (
|
||||||
create_async_client,
|
create_async_client,
|
||||||
create_db_session,
|
create_db_session,
|
||||||
@@ -20,23 +19,9 @@ from fastapi_toolsets.pytest import (
|
|||||||
register_fixtures,
|
register_fixtures,
|
||||||
worker_database_url,
|
worker_database_url,
|
||||||
)
|
)
|
||||||
from fastapi_toolsets.pytest.plugin import (
|
|
||||||
_get_primary_key,
|
|
||||||
_relationship_load_options,
|
|
||||||
_reload_with_relationships,
|
|
||||||
)
|
|
||||||
from fastapi_toolsets.pytest.utils import _get_xdist_worker
|
from fastapi_toolsets.pytest.utils import _get_xdist_worker
|
||||||
|
|
||||||
from .conftest import (
|
from .conftest import DATABASE_URL, Base, Role, RoleCrud, User, UserCrud
|
||||||
DATABASE_URL,
|
|
||||||
Base,
|
|
||||||
IntRole,
|
|
||||||
Permission,
|
|
||||||
Role,
|
|
||||||
RoleCrud,
|
|
||||||
User,
|
|
||||||
UserCrud,
|
|
||||||
)
|
|
||||||
|
|
||||||
test_registry = FixtureRegistry()
|
test_registry = FixtureRegistry()
|
||||||
|
|
||||||
@@ -151,8 +136,14 @@ class TestGeneratedFixtures:
|
|||||||
async def test_fixture_relationships_work(
|
async def test_fixture_relationships_work(
|
||||||
self, db_session: AsyncSession, fixture_users: list[User]
|
self, db_session: AsyncSession, fixture_users: list[User]
|
||||||
):
|
):
|
||||||
"""Loaded fixtures have working relationships directly accessible."""
|
"""Loaded fixtures have working relationships."""
|
||||||
user = next(u for u in fixture_users if u.id == USER_ADMIN_ID)
|
# Load user with role relationship
|
||||||
|
user = await UserCrud.get(
|
||||||
|
db_session,
|
||||||
|
[User.id == USER_ADMIN_ID],
|
||||||
|
load_options=[selectinload(User.role)],
|
||||||
|
)
|
||||||
|
|
||||||
assert user.role is not None
|
assert user.role is not None
|
||||||
assert user.role.name == "plugin_admin"
|
assert user.role.name == "plugin_admin"
|
||||||
|
|
||||||
@@ -186,15 +177,6 @@ class TestGeneratedFixtures:
|
|||||||
assert users[0].username == "plugin_admin"
|
assert users[0].username == "plugin_admin"
|
||||||
assert users[1].username == "plugin_user"
|
assert users[1].username == "plugin_user"
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_fixture_auto_loads_relationships(
|
|
||||||
self, db_session: AsyncSession, fixture_users: list[User]
|
|
||||||
):
|
|
||||||
"""Fixtures automatically eager-load all direct relationships."""
|
|
||||||
user = next(u for u in fixture_users if u.username == "plugin_admin")
|
|
||||||
assert user.role is not None
|
|
||||||
assert user.role.name == "plugin_admin"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_multiple_fixtures_in_same_test(
|
async def test_multiple_fixtures_in_same_test(
|
||||||
self,
|
self,
|
||||||
@@ -392,6 +374,19 @@ class TestCreateDbSession:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TestDeprecatedCleanupTables:
|
||||||
|
"""Tests for the deprecated cleanup_tables re-export in fastapi_toolsets.pytest."""
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_emits_deprecation_warning(self):
|
||||||
|
"""cleanup_tables imported from fastapi_toolsets.pytest emits DeprecationWarning."""
|
||||||
|
from fastapi_toolsets.pytest.utils import cleanup_tables
|
||||||
|
|
||||||
|
async with create_db_session(DATABASE_URL, Base, drop_tables=True) as session:
|
||||||
|
with pytest.warns(DeprecationWarning, match="fastapi_toolsets.db"):
|
||||||
|
await cleanup_tables(session, Base)
|
||||||
|
|
||||||
|
|
||||||
class TestGetXdistWorker:
|
class TestGetXdistWorker:
|
||||||
"""Tests for _get_xdist_worker helper."""
|
"""Tests for _get_xdist_worker helper."""
|
||||||
|
|
||||||
@@ -534,192 +529,3 @@ class TestCreateWorkerDatabase:
|
|||||||
)
|
)
|
||||||
assert result.scalar() is None
|
assert result.scalar() is None
|
||||||
await engine.dispose()
|
await engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
class _LocalBase(DeclarativeBase):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class _Group(_LocalBase):
|
|
||||||
__tablename__ = "_test_groups"
|
|
||||||
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
|
||||||
name: Mapped[str] = mapped_column(String(50))
|
|
||||||
|
|
||||||
|
|
||||||
class _CompositeItem(_LocalBase):
|
|
||||||
"""Model with composite PK and a relationship — exercises the fallback path."""
|
|
||||||
|
|
||||||
__tablename__ = "_test_composite_items"
|
|
||||||
group_id: Mapped[uuid.UUID] = mapped_column(
|
|
||||||
ForeignKey("_test_groups.id"), primary_key=True
|
|
||||||
)
|
|
||||||
item_code: Mapped[str] = mapped_column(String(50), primary_key=True)
|
|
||||||
group: Mapped["_Group"] = relationship()
|
|
||||||
|
|
||||||
|
|
||||||
class TestGetPrimaryKey:
|
|
||||||
"""Unit tests for _get_primary_key — no DB needed."""
|
|
||||||
|
|
||||||
def test_single_pk_returns_value(self):
|
|
||||||
rid = uuid.UUID("00000000-0000-0000-0000-000000000001")
|
|
||||||
role = Role(id=rid, name="x")
|
|
||||||
assert _get_primary_key(role) == rid
|
|
||||||
|
|
||||||
def test_composite_pk_all_set_returns_tuple(self):
|
|
||||||
perm = Permission(subject="posts", action="read")
|
|
||||||
assert _get_primary_key(perm) == ("posts", "read")
|
|
||||||
|
|
||||||
def test_composite_pk_partial_none_returns_none(self):
|
|
||||||
perm = Permission(subject=None, action="read")
|
|
||||||
assert _get_primary_key(perm) is None
|
|
||||||
|
|
||||||
def test_composite_pk_all_none_returns_none(self):
|
|
||||||
perm = Permission(subject=None, action=None)
|
|
||||||
assert _get_primary_key(perm) is None
|
|
||||||
|
|
||||||
|
|
||||||
class TestRelationshipLoadOptions:
|
|
||||||
"""Unit tests for _relationship_load_options — no DB needed."""
|
|
||||||
|
|
||||||
def test_empty_for_model_with_no_relationships(self):
|
|
||||||
assert _relationship_load_options(IntRole) == []
|
|
||||||
|
|
||||||
def test_returns_options_for_model_with_relationships(self):
|
|
||||||
opts = _relationship_load_options(User)
|
|
||||||
assert len(opts) >= 1
|
|
||||||
|
|
||||||
|
|
||||||
class TestFixtureStrategies:
|
|
||||||
"""Integration tests covering INSERT, SKIP_EXISTING, empty fixture, no-rels model."""
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_empty_fixture_returns_empty_list(self, db_session: AsyncSession):
|
|
||||||
"""Fixture function returning [] produces an empty list."""
|
|
||||||
registry = FixtureRegistry()
|
|
||||||
|
|
||||||
@registry.register()
|
|
||||||
def empty() -> list[Role]:
|
|
||||||
return []
|
|
||||||
|
|
||||||
local_ns: dict = {}
|
|
||||||
register_fixtures(registry, local_ns, session_fixture="db_session")
|
|
||||||
inner = local_ns["fixture_empty"].__wrapped__ # type: ignore[attr-defined]
|
|
||||||
result = await inner(db_session=db_session)
|
|
||||||
assert result == []
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_insert_strategy_no_relationships(self, db_session: AsyncSession):
|
|
||||||
"""INSERT strategy adds instances; model with no rels skips reload (line 135)."""
|
|
||||||
registry = FixtureRegistry()
|
|
||||||
|
|
||||||
@registry.register()
|
|
||||||
def int_roles() -> list[IntRole]:
|
|
||||||
return [IntRole(name="insert_role")]
|
|
||||||
|
|
||||||
local_ns: dict = {}
|
|
||||||
register_fixtures(
|
|
||||||
registry,
|
|
||||||
local_ns,
|
|
||||||
session_fixture="db_session",
|
|
||||||
strategy=LoadStrategy.INSERT,
|
|
||||||
)
|
|
||||||
inner = local_ns["fixture_int_roles"].__wrapped__ # type: ignore[attr-defined]
|
|
||||||
result = await inner(db_session=db_session)
|
|
||||||
assert len(result) == 1
|
|
||||||
assert result[0].name == "insert_role"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_skip_existing_inserts_new_record(self, db_session: AsyncSession):
|
|
||||||
"""SKIP_EXISTING inserts when the record does not yet exist."""
|
|
||||||
registry = FixtureRegistry()
|
|
||||||
role_id = uuid.uuid4()
|
|
||||||
|
|
||||||
@registry.register()
|
|
||||||
def new_roles() -> list[Role]:
|
|
||||||
return [Role(id=role_id, name="skip_new")]
|
|
||||||
|
|
||||||
local_ns: dict = {}
|
|
||||||
register_fixtures(
|
|
||||||
registry,
|
|
||||||
local_ns,
|
|
||||||
session_fixture="db_session",
|
|
||||||
strategy=LoadStrategy.SKIP_EXISTING,
|
|
||||||
)
|
|
||||||
inner = local_ns["fixture_new_roles"].__wrapped__ # type: ignore[attr-defined]
|
|
||||||
result = await inner(db_session=db_session)
|
|
||||||
assert len(result) == 1
|
|
||||||
assert result[0].id == role_id
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_skip_existing_returns_existing_record(
|
|
||||||
self, db_session: AsyncSession
|
|
||||||
):
|
|
||||||
"""SKIP_EXISTING returns the existing DB record when PK already present."""
|
|
||||||
role_id = uuid.uuid4()
|
|
||||||
existing = Role(id=role_id, name="already_there")
|
|
||||||
db_session.add(existing)
|
|
||||||
await db_session.flush()
|
|
||||||
|
|
||||||
registry = FixtureRegistry()
|
|
||||||
|
|
||||||
@registry.register()
|
|
||||||
def dup_roles() -> list[Role]:
|
|
||||||
return [Role(id=role_id, name="should_not_overwrite")]
|
|
||||||
|
|
||||||
local_ns: dict = {}
|
|
||||||
register_fixtures(
|
|
||||||
registry,
|
|
||||||
local_ns,
|
|
||||||
session_fixture="db_session",
|
|
||||||
strategy=LoadStrategy.SKIP_EXISTING,
|
|
||||||
)
|
|
||||||
inner = local_ns["fixture_dup_roles"].__wrapped__ # type: ignore[attr-defined]
|
|
||||||
result = await inner(db_session=db_session)
|
|
||||||
assert len(result) == 1
|
|
||||||
assert result[0].name == "already_there"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_skip_existing_null_pk_inserts(self, db_session: AsyncSession):
|
|
||||||
"""SKIP_EXISTING with null PK (auto-increment) falls through to session.add()."""
|
|
||||||
registry = FixtureRegistry()
|
|
||||||
|
|
||||||
@registry.register()
|
|
||||||
def auto_roles() -> list[IntRole]:
|
|
||||||
return [IntRole(name="auto_int")]
|
|
||||||
|
|
||||||
local_ns: dict = {}
|
|
||||||
register_fixtures(
|
|
||||||
registry,
|
|
||||||
local_ns,
|
|
||||||
session_fixture="db_session",
|
|
||||||
strategy=LoadStrategy.SKIP_EXISTING,
|
|
||||||
)
|
|
||||||
inner = local_ns["fixture_auto_roles"].__wrapped__ # type: ignore[attr-defined]
|
|
||||||
result = await inner(db_session=db_session)
|
|
||||||
assert len(result) == 1
|
|
||||||
assert result[0].name == "auto_int"
|
|
||||||
|
|
||||||
|
|
||||||
class TestReloadWithRelationshipsCompositePK:
|
|
||||||
"""Integration test for _reload_with_relationships composite-PK fallback."""
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_composite_pk_fallback_loads_relationships(self):
|
|
||||||
"""Models with composite PKs are reloaded per-instance via session.get()."""
|
|
||||||
async with create_db_session(DATABASE_URL, _LocalBase) as session:
|
|
||||||
group = _Group(id=uuid.uuid4(), name="g1")
|
|
||||||
session.add(group)
|
|
||||||
await session.flush()
|
|
||||||
|
|
||||||
item = _CompositeItem(group_id=group.id, item_code="A")
|
|
||||||
session.add(item)
|
|
||||||
await session.flush()
|
|
||||||
|
|
||||||
load_opts = _relationship_load_options(_CompositeItem)
|
|
||||||
assert load_opts # _CompositeItem has 'group' relationship
|
|
||||||
|
|
||||||
reloaded = await _reload_with_relationships(session, [item], load_opts)
|
|
||||||
assert len(reloaded) == 1
|
|
||||||
reloaded_item = cast(_CompositeItem, reloaded[0])
|
|
||||||
assert reloaded_item.group is not None
|
|
||||||
assert reloaded_item.group.name == "g1"
|
|
||||||
|
|||||||
128
uv.lock
generated
128
uv.lock
generated
@@ -235,7 +235,7 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastapi"
|
name = "fastapi"
|
||||||
version = "0.135.3"
|
version = "0.135.1"
|
||||||
source = { registry = "https://pypi.org/simple" }
|
source = { registry = "https://pypi.org/simple" }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "annotated-doc" },
|
{ name = "annotated-doc" },
|
||||||
@@ -244,14 +244,14 @@ dependencies = [
|
|||||||
{ name = "typing-extensions" },
|
{ name = "typing-extensions" },
|
||||||
{ name = "typing-inspection" },
|
{ name = "typing-inspection" },
|
||||||
]
|
]
|
||||||
sdist = { url = "https://files.pythonhosted.org/packages/f7/e6/7adb4c5fa231e82c35b8f5741a9f2d055f520c29af5546fd70d3e8e1cd2e/fastapi-0.135.3.tar.gz", hash = "sha256:bd6d7caf1a2bdd8d676843cdcd2287729572a1ef524fc4d65c17ae002a1be654", size = 396524, upload-time = "2026-04-01T16:23:58.188Z" }
|
sdist = { url = "https://files.pythonhosted.org/packages/e7/7b/f8e0211e9380f7195ba3f3d40c292594fd81ba8ec4629e3854c353aaca45/fastapi-0.135.1.tar.gz", hash = "sha256:d04115b508d936d254cea545b7312ecaa58a7b3a0f84952535b4c9afae7668cd", size = 394962, upload-time = "2026-03-01T18:18:29.369Z" }
|
||||||
wheels = [
|
wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/84/a4/5caa2de7f917a04ada20018eccf60d6cc6145b0199d55ca3711b0fc08312/fastapi-0.135.3-py3-none-any.whl", hash = "sha256:9b0f590c813acd13d0ab43dd8494138eb58e484bfac405db1f3187cfc5810d98", size = 117734, upload-time = "2026-04-01T16:23:59.328Z" },
|
{ url = "https://files.pythonhosted.org/packages/e4/72/42e900510195b23a56bde950d26a51f8b723846bfcaa0286e90287f0422b/fastapi-0.135.1-py3-none-any.whl", hash = "sha256:46e2fc5745924b7c840f71ddd277382af29ce1cdb7d5eab5bf697e3fb9999c9e", size = 116999, upload-time = "2026-03-01T18:18:30.831Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastapi-toolsets"
|
name = "fastapi-toolsets"
|
||||||
version = "3.1.0"
|
version = "2.4.3"
|
||||||
source = { editable = "." }
|
source = { editable = "." }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "asyncpg" },
|
{ name = "asyncpg" },
|
||||||
@@ -885,24 +885,24 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pygments"
|
name = "pygments"
|
||||||
version = "2.20.0"
|
version = "2.19.2"
|
||||||
source = { registry = "https://pypi.org/simple" }
|
source = { registry = "https://pypi.org/simple" }
|
||||||
sdist = { url = "https://files.pythonhosted.org/packages/c3/b2/bc9c9196916376152d655522fdcebac55e66de6603a76a02bca1b6414f6c/pygments-2.20.0.tar.gz", hash = "sha256:6757cd03768053ff99f3039c1a36d6c0aa0b263438fcab17520b30a303a82b5f", size = 4955991, upload-time = "2026-03-29T13:29:33.898Z" }
|
sdist = { url = "https://files.pythonhosted.org/packages/b0/77/a5b8c569bf593b0140bde72ea885a803b82086995367bf2037de0159d924/pygments-2.19.2.tar.gz", hash = "sha256:636cb2477cec7f8952536970bc533bc43743542f70392ae026374600add5b887", size = 4968631, upload-time = "2025-06-21T13:39:12.283Z" }
|
||||||
wheels = [
|
wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/f4/7e/a72dd26f3b0f4f2bf1dd8923c85f7ceb43172af56d63c7383eb62b332364/pygments-2.20.0-py3-none-any.whl", hash = "sha256:81a9e26dd42fd28a23a2d169d86d7ac03b46e2f8b59ed4698fb4785f946d0176", size = 1231151, upload-time = "2026-03-29T13:29:30.038Z" },
|
{ url = "https://files.pythonhosted.org/packages/c7/21/705964c7812476f378728bdf590ca4b771ec72385c533964653c68e86bdc/pygments-2.19.2-py3-none-any.whl", hash = "sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b", size = 1225217, upload-time = "2025-06-21T13:39:07.939Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pymdown-extensions"
|
name = "pymdown-extensions"
|
||||||
version = "10.21.2"
|
version = "10.21"
|
||||||
source = { registry = "https://pypi.org/simple" }
|
source = { registry = "https://pypi.org/simple" }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "markdown" },
|
{ name = "markdown" },
|
||||||
{ name = "pyyaml" },
|
{ name = "pyyaml" },
|
||||||
]
|
]
|
||||||
sdist = { url = "https://files.pythonhosted.org/packages/df/08/f1c908c581fd11913da4711ea7ba32c0eee40b0190000996bb863b0c9349/pymdown_extensions-10.21.2.tar.gz", hash = "sha256:c3f55a5b8a1d0edf6699e35dcbea71d978d34ff3fa79f3d807b8a5b3fa90fbdc", size = 853922, upload-time = "2026-03-29T15:01:55.233Z" }
|
sdist = { url = "https://files.pythonhosted.org/packages/ba/63/06673d1eb6d8f83c0ea1f677d770e12565fb516928b4109c9e2055656a9e/pymdown_extensions-10.21.tar.gz", hash = "sha256:39f4a020f40773f6b2ff31d2cd2546c2c04d0a6498c31d9c688d2be07e1767d5", size = 853363, upload-time = "2026-02-15T20:44:06.748Z" }
|
||||||
wheels = [
|
wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/f7/27/a2fc51a4a122dfd1015e921ae9d22fee3d20b0b8080d9a704578bf9deece/pymdown_extensions-10.21.2-py3-none-any.whl", hash = "sha256:5c0fd2a2bea14eb39af8ff284f1066d898ab2187d81b889b75d46d4348c01638", size = 268901, upload-time = "2026-03-29T15:01:53.244Z" },
|
{ url = "https://files.pythonhosted.org/packages/6f/2c/5b079febdc65e1c3fb2729bf958d18b45be7113828528e8a0b5850dd819a/pymdown_extensions-10.21-py3-none-any.whl", hash = "sha256:91b879f9f864d49794c2d9534372b10150e6141096c3908a455e45ca72ad9d3f", size = 268877, upload-time = "2026-02-15T20:44:05.464Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -916,7 +916,7 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pytest"
|
name = "pytest"
|
||||||
version = "9.0.3"
|
version = "9.0.2"
|
||||||
source = { registry = "https://pypi.org/simple" }
|
source = { registry = "https://pypi.org/simple" }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "colorama", marker = "sys_platform == 'win32'" },
|
{ name = "colorama", marker = "sys_platform == 'win32'" },
|
||||||
@@ -925,9 +925,9 @@ dependencies = [
|
|||||||
{ name = "pluggy" },
|
{ name = "pluggy" },
|
||||||
{ name = "pygments" },
|
{ name = "pygments" },
|
||||||
]
|
]
|
||||||
sdist = { url = "https://files.pythonhosted.org/packages/7d/0d/549bd94f1a0a402dc8cf64563a117c0f3765662e2e668477624baeec44d5/pytest-9.0.3.tar.gz", hash = "sha256:b86ada508af81d19edeb213c681b1d48246c1a91d304c6c81a427674c17eb91c", size = 1572165, upload-time = "2026-04-07T17:16:18.027Z" }
|
sdist = { url = "https://files.pythonhosted.org/packages/d1/db/7ef3487e0fb0049ddb5ce41d3a49c235bf9ad299b6a25d5780a89f19230f/pytest-9.0.2.tar.gz", hash = "sha256:75186651a92bd89611d1d9fc20f0b4345fd827c41ccd5c299a868a05d70edf11", size = 1568901, upload-time = "2025-12-06T21:30:51.014Z" }
|
||||||
wheels = [
|
wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/d4/24/a372aaf5c9b7208e7112038812994107bc65a84cd00e0354a88c2c77a617/pytest-9.0.3-py3-none-any.whl", hash = "sha256:2c5efc453d45394fdd706ade797c0a81091eccd1d6e4bccfcd476e2b8e0ab5d9", size = 375249, upload-time = "2026-04-07T17:16:16.13Z" },
|
{ url = "https://files.pythonhosted.org/packages/3b/ab/b3226f0bd7cdcf710fbede2b3548584366da3b19b5021e74f5bde2a8fa3f/pytest-9.0.2-py3-none-any.whl", hash = "sha256:711ffd45bf766d5264d487b917733b453d917afd2b0ad65223959f59089f875b", size = 374801, upload-time = "2025-12-06T21:30:49.154Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1064,27 +1064,27 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruff"
|
name = "ruff"
|
||||||
version = "0.15.9"
|
version = "0.15.7"
|
||||||
source = { registry = "https://pypi.org/simple" }
|
source = { registry = "https://pypi.org/simple" }
|
||||||
sdist = { url = "https://files.pythonhosted.org/packages/e6/97/e9f1ca355108ef7194e38c812ef40ba98c7208f47b13ad78d023caa583da/ruff-0.15.9.tar.gz", hash = "sha256:29cbb1255a9797903f6dde5ba0188c707907ff44a9006eb273b5a17bfa0739a2", size = 4617361, upload-time = "2026-04-02T18:17:20.829Z" }
|
sdist = { url = "https://files.pythonhosted.org/packages/a1/22/9e4f66ee588588dc6c9af6a994e12d26e19efbe874d1a909d09a6dac7a59/ruff-0.15.7.tar.gz", hash = "sha256:04f1ae61fc20fe0b148617c324d9d009b5f63412c0b16474f3d5f1a1a665f7ac", size = 4601277, upload-time = "2026-03-19T16:26:22.605Z" }
|
||||||
wheels = [
|
wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/0b/1f/9cdfd0ac4b9d1e5a6cf09bedabdf0b56306ab5e333c85c87281273e7b041/ruff-0.15.9-py3-none-linux_armv6l.whl", hash = "sha256:6efbe303983441c51975c243e26dff328aca11f94b70992f35b093c2e71801e1", size = 10511206, upload-time = "2026-04-02T18:16:41.574Z" },
|
{ url = "https://files.pythonhosted.org/packages/41/2f/0b08ced94412af091807b6119ca03755d651d3d93a242682bf020189db94/ruff-0.15.7-py3-none-linux_armv6l.whl", hash = "sha256:a81cc5b6910fb7dfc7c32d20652e50fa05963f6e13ead3c5915c41ac5d16668e", size = 10489037, upload-time = "2026-03-19T16:26:32.47Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/3d/f6/32bfe3e9c136b35f02e489778d94384118bb80fd92c6d92e7ccd97db12ce/ruff-0.15.9-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:4965bac6ac9ea86772f4e23587746f0b7a395eccabb823eb8bfacc3fa06069f7", size = 10923307, upload-time = "2026-04-02T18:17:08.645Z" },
|
{ url = "https://files.pythonhosted.org/packages/91/4a/82e0fa632e5c8b1eba5ee86ecd929e8ff327bbdbfb3c6ac5d81631bef605/ruff-0.15.7-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:722d165bd52403f3bdabc0ce9e41fc47070ac56d7a91b4e0d097b516a53a3477", size = 10955433, upload-time = "2026-03-19T16:27:00.205Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/ca/25/de55f52ab5535d12e7aaba1de37a84be6179fb20bddcbe71ec091b4a3243/ruff-0.15.9-py3-none-macosx_11_0_arm64.whl", hash = "sha256:eaf05aad70ca5b5a0a4b0e080df3a6b699803916d88f006efd1f5b46302daab8", size = 10316722, upload-time = "2026-04-02T18:16:44.206Z" },
|
{ url = "https://files.pythonhosted.org/packages/ab/10/12586735d0ff42526ad78c049bf51d7428618c8b5c467e72508c694119df/ruff-0.15.7-py3-none-macosx_11_0_arm64.whl", hash = "sha256:7fbc2448094262552146cbe1b9643a92f66559d3761f1ad0656d4991491af49e", size = 10269302, upload-time = "2026-03-19T16:26:26.183Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/48/11/690d75f3fd6278fe55fff7c9eb429c92d207e14b25d1cae4064a32677029/ruff-0.15.9-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:9439a342adb8725f32f92732e2bafb6d5246bd7a5021101166b223d312e8fc59", size = 10623674, upload-time = "2026-04-02T18:16:50.951Z" },
|
{ url = "https://files.pythonhosted.org/packages/eb/5d/32b5c44ccf149a26623671df49cbfbd0a0ae511ff3df9d9d2426966a8d57/ruff-0.15.7-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6b39329b60eba44156d138275323cc726bbfbddcec3063da57caa8a8b1d50adf", size = 10607625, upload-time = "2026-03-19T16:27:03.263Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/bd/ec/176f6987be248fc5404199255522f57af1b4a5a1b57727e942479fec98ad/ruff-0.15.9-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:9c5e6faf9d97c8edc43877c3f406f47446fc48c40e1442d58cfcdaba2acea745", size = 10351516, upload-time = "2026-04-02T18:16:57.206Z" },
|
{ url = "https://files.pythonhosted.org/packages/5d/f1/f0001cabe86173aaacb6eb9bb734aa0605f9a6aa6fa7d43cb49cbc4af9c9/ruff-0.15.7-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:87768c151808505f2bfc93ae44e5f9e7c8518943e5074f76ac21558ef5627c85", size = 10324743, upload-time = "2026-03-19T16:27:09.791Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/b2/fc/51cffbd2b3f240accc380171d51446a32aa2ea43a40d4a45ada67368fbd2/ruff-0.15.9-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:7b34a9766aeec27a222373d0b055722900fbc0582b24f39661aa96f3fe6ad901", size = 11150202, upload-time = "2026-04-02T18:17:06.452Z" },
|
{ url = "https://files.pythonhosted.org/packages/7a/87/b8a8f3d56b8d848008559e7c9d8bf367934d5367f6d932ba779456e2f73b/ruff-0.15.7-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:fb0511670002c6c529ec66c0e30641c976c8963de26a113f3a30456b702468b0", size = 11138536, upload-time = "2026-03-19T16:27:06.101Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/d6/d4/25292a6dfc125f6b6528fe6af31f5e996e19bf73ca8e3ce6eb7fa5b95885/ruff-0.15.9-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:89dd695bc72ae76ff484ae54b7e8b0f6b50f49046e198355e44ea656e521fef9", size = 11988891, upload-time = "2026-04-02T18:17:18.575Z" },
|
{ url = "https://files.pythonhosted.org/packages/e4/f2/4fd0d05aab0c5934b2e1464784f85ba2eab9d54bffc53fb5430d1ed8b829/ruff-0.15.7-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e0d19644f801849229db8345180a71bee5407b429dd217f853ec515e968a6912", size = 11994292, upload-time = "2026-03-19T16:26:48.718Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/13/e1/1eebcb885c10e19f969dcb93d8413dfee8172578709d7ee933640f5e7147/ruff-0.15.9-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:ce187224ef1de1bd225bc9a152ac7102a6171107f026e81f317e4257052916d5", size = 11480576, upload-time = "2026-04-02T18:16:52.986Z" },
|
{ url = "https://files.pythonhosted.org/packages/64/22/fc4483871e767e5e95d1622ad83dad5ebb830f762ed0420fde7dfa9d9b08/ruff-0.15.7-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:4806d8e09ef5e84eb19ba833d0442f7e300b23fe3f0981cae159a248a10f0036", size = 11398981, upload-time = "2026-03-19T16:26:54.513Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/ff/6b/a1548ac378a78332a4c3dcf4a134c2475a36d2a22ddfa272acd574140b50/ruff-0.15.9-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2b0c7c341f68adb01c488c3b7d4b49aa8ea97409eae6462d860a79cf55f431b6", size = 11254525, upload-time = "2026-04-02T18:17:02.041Z" },
|
{ url = "https://files.pythonhosted.org/packages/b0/99/66f0343176d5eab02c3f7fcd2de7a8e0dd7a41f0d982bee56cd1c24db62b/ruff-0.15.7-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dce0896488562f09a27b9c91b1f58a097457143931f3c4d519690dea54e624c5", size = 11242422, upload-time = "2026-03-19T16:26:29.277Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/42/aa/4bb3af8e61acd9b1281db2ab77e8b2c3c5e5599bf2a29d4a942f1c62b8d6/ruff-0.15.9-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:55cc15eee27dc0eebdfcb0d185a6153420efbedc15eb1d38fe5e685657b0f840", size = 11204072, upload-time = "2026-04-02T18:17:13.581Z" },
|
{ url = "https://files.pythonhosted.org/packages/5d/3a/a7060f145bfdcce4c987ea27788b30c60e2c81d6e9a65157ca8afe646328/ruff-0.15.7-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:1852ce241d2bc89e5dc823e03cff4ce73d816b5c6cdadd27dbfe7b03217d2a12", size = 11232158, upload-time = "2026-03-19T16:26:42.321Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/69/48/d550dc2aa6e423ea0bcc1d0ff0699325ffe8a811e2dba156bd80750b86dc/ruff-0.15.9-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:a6537f6eed5cda688c81073d46ffdfb962a5f29ecb6f7e770b2dc920598997ed", size = 10594998, upload-time = "2026-04-02T18:16:46.369Z" },
|
{ url = "https://files.pythonhosted.org/packages/a7/53/90fbb9e08b29c048c403558d3cdd0adf2668b02ce9d50602452e187cd4af/ruff-0.15.7-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:5f3e4b221fb4bd293f79912fc5e93a9063ebd6d0dcbd528f91b89172a9b8436c", size = 10577861, upload-time = "2026-03-19T16:26:57.459Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/63/47/321167e17f5344ed5ec6b0aa2cff64efef5f9e985af8f5622cfa6536043f/ruff-0.15.9-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:6d3fcbca7388b066139c523bda744c822258ebdcfbba7d24410c3f454cc9af71", size = 10359769, upload-time = "2026-04-02T18:17:10.994Z" },
|
{ url = "https://files.pythonhosted.org/packages/2f/aa/5f486226538fe4d0f0439e2da1716e1acf895e2a232b26f2459c55f8ddad/ruff-0.15.7-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:b15e48602c9c1d9bdc504b472e90b90c97dc7d46c7028011ae67f3861ceba7b4", size = 10327310, upload-time = "2026-03-19T16:26:35.909Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/67/5e/074f00b9785d1d2c6f8c22a21e023d0c2c1817838cfca4c8243200a1fa87/ruff-0.15.9-py3-none-musllinux_1_2_i686.whl", hash = "sha256:058d8e99e1bfe79d8a0def0b481c56059ee6716214f7e425d8e737e412d69677", size = 10850236, upload-time = "2026-04-02T18:16:48.749Z" },
|
{ url = "https://files.pythonhosted.org/packages/99/9e/271afdffb81fe7bfc8c43ba079e9d96238f674380099457a74ccb3863857/ruff-0.15.7-py3-none-musllinux_1_2_i686.whl", hash = "sha256:1b4705e0e85cedc74b0a23cf6a179dbb3df184cb227761979cc76c0440b5ab0d", size = 10840752, upload-time = "2026-03-19T16:26:45.723Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/76/37/804c4135a2a2caf042925d30d5f68181bdbd4461fd0d7739da28305df593/ruff-0.15.9-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:8e1ddb11dbd61d5983fa2d7d6370ef3eb210951e443cace19594c01c72abab4c", size = 11358343, upload-time = "2026-04-02T18:16:55.068Z" },
|
{ url = "https://files.pythonhosted.org/packages/bf/29/a4ae78394f76c7759953c47884eb44de271b03a66634148d9f7d11e721bd/ruff-0.15.7-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:112c1fa316a558bb34319282c1200a8bf0495f1b735aeb78bfcb2991e6087580", size = 11336961, upload-time = "2026-03-19T16:26:39.076Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/88/3d/1364fcde8656962782aa9ea93c92d98682b1ecec2f184e625a965ad3b4a6/ruff-0.15.9-py3-none-win32.whl", hash = "sha256:bde6ff36eaf72b700f32b7196088970bf8fdb2b917b7accd8c371bfc0fd573ec", size = 10583382, upload-time = "2026-04-02T18:17:04.261Z" },
|
{ url = "https://files.pythonhosted.org/packages/26/6b/8786ba5736562220d588a2f6653e6c17e90c59ced34a2d7b512ef8956103/ruff-0.15.7-py3-none-win32.whl", hash = "sha256:6d39e2d3505b082323352f733599f28169d12e891f7dd407f2d4f54b4c2886de", size = 10582538, upload-time = "2026-03-19T16:26:15.992Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/4c/56/5c7084299bd2cacaa07ae63a91c6f4ba66edc08bf28f356b24f6b717c799/ruff-0.15.9-py3-none-win_amd64.whl", hash = "sha256:45a70921b80e1c10cf0b734ef09421f71b5aa11d27404edc89d7e8a69505e43d", size = 11744969, upload-time = "2026-04-02T18:16:59.611Z" },
|
{ url = "https://files.pythonhosted.org/packages/2b/e9/346d4d3fffc6871125e877dae8d9a1966b254fbd92a50f8561078b88b099/ruff-0.15.7-py3-none-win_amd64.whl", hash = "sha256:4d53d712ddebcd7dace1bc395367aec12c057aacfe9adbb6d832302575f4d3a1", size = 11755839, upload-time = "2026-03-19T16:26:19.897Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/03/36/76704c4f312257d6dbaae3c959add2a622f63fcca9d864659ce6d8d97d3d/ruff-0.15.9-py3-none-win_arm64.whl", hash = "sha256:0694e601c028fd97dc5c6ee244675bc241aeefced7ef80cd9c6935a871078f53", size = 11005870, upload-time = "2026-04-02T18:17:15.773Z" },
|
{ url = "https://files.pythonhosted.org/packages/8f/e8/726643a3ea68c727da31570bde48c7a10f1aa60eddd628d94078fec586ff/ruff-0.15.7-py3-none-win_arm64.whl", hash = "sha256:18e8d73f1c3fdf27931497972250340f92e8c861722161a9caeb89a58ead6ed2", size = 11023304, upload-time = "2026-03-19T16:26:51.669Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1228,26 +1228,26 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ty"
|
name = "ty"
|
||||||
version = "0.0.29"
|
version = "0.0.25"
|
||||||
source = { registry = "https://pypi.org/simple" }
|
source = { registry = "https://pypi.org/simple" }
|
||||||
sdist = { url = "https://files.pythonhosted.org/packages/47/d5/853561de49fae38c519e905b2d8da9c531219608f1fccc47a0fc2c896980/ty-0.0.29.tar.gz", hash = "sha256:e7936cca2f691eeda631876c92809688dbbab68687c3473f526cd83b6a9228d8", size = 5469221, upload-time = "2026-04-05T15:01:21.328Z" }
|
sdist = { url = "https://files.pythonhosted.org/packages/12/bf/3c3147c7237277b0e8a911ff89de7183408be96b31fb42b38edb666d287f/ty-0.0.25.tar.gz", hash = "sha256:8ae3891be17dfb6acab51a2df3a8f8f6c551eb60ea674c10946dc92aae8d4401", size = 5375500, upload-time = "2026-03-24T22:32:34.608Z" }
|
||||||
wheels = [
|
wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/03/b7/911f9962115acfa24e3b2ec9d4992dd994c38e8769e1b1d7680bb4d28a51/ty-0.0.29-py3-none-linux_armv6l.whl", hash = "sha256:b8a40955f7660d3eaceb0d964affc81b790c0765e7052921a5f861ff8a471c30", size = 10568206, upload-time = "2026-04-05T15:01:19.165Z" },
|
{ url = "https://files.pythonhosted.org/packages/97/a4/6c289cbd1474285223124a4ffb55c078dbe9ae1d925d0b6a948643c7f115/ty-0.0.25-py3-none-linux_armv6l.whl", hash = "sha256:26d6d5aede5d54fb055779460f896d9c1473c6fb996716bd11cb90f027d8fee7", size = 10452747, upload-time = "2026-03-24T22:32:32.662Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/fe/c3/fcae2167d4c77a97269f92f11d1b43b03617f81de1283d5d05b43432110c/ty-0.0.29-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:6b6849adae15b00bbe2d3c5b078967dcb62eba37d38936b8eeb4c81a82d2e3b8", size = 10442530, upload-time = "2026-04-05T15:01:28.471Z" },
|
{ url = "https://files.pythonhosted.org/packages/00/13/74cb9de356b9ceb3f281ab048f8c4ac2207122161b0ac0066886ce129abe/ty-0.0.25-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:aedcfbc7b6b96dbc55b0da78fa02bd049373ff3d8a827f613dadd8bd17d10758", size = 10271349, upload-time = "2026-03-24T22:32:13.041Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/97/33/5a6bfa240cfcb9c36046ae2459fa9ea23238d20130d8656ff5ac4d6c012a/ty-0.0.29-py3-none-macosx_11_0_arm64.whl", hash = "sha256:dcdd9b17209788152f7b7ea815eda07989152325052fe690013537cc7904ce49", size = 9915735, upload-time = "2026-04-05T15:01:10.365Z" },
|
{ url = "https://files.pythonhosted.org/packages/0e/93/ffc5a20cc9e14fa9b32b0c54884864bede30d144ce2ae013805bce0c86d0/ty-0.0.25-py3-none-macosx_11_0_arm64.whl", hash = "sha256:0a8fb3c1e28f73618941811e2568dca195178a1a6314651d4ee97086a4497253", size = 9730308, upload-time = "2026-03-24T22:32:19.24Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/b3/1e/318f45fae232118e81a6306c30f50de42c509c412128d5bd231eab699ffb/ty-0.0.29-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:9d8ed4789bae78ffaf94462c0d25589a734cab0366b86f2bbcb1bb90e1a7a169", size = 10419748, upload-time = "2026-04-05T15:01:32.375Z" },
|
{ url = "https://files.pythonhosted.org/packages/6d/78/52e05ef32a5f172fce70633a4e19d8e04364271a4322ae12382c7344b0de/ty-0.0.25-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:814870b7f347b5d0276304cddb98a0958f08de183bf159abc920ebe321247ad4", size = 10247664, upload-time = "2026-03-24T22:32:08.669Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/a9/a8/5687872e2ab5a0f7dd4fd8456eac31e9381ad4dc74961f6f29965ad4dd91/ty-0.0.29-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:91ec374b8565e0ad0900011c24641ebbef2da51adbd4fb69ff3280c8a7eceb02", size = 10394738, upload-time = "2026-04-05T15:01:06.473Z" },
|
{ url = "https://files.pythonhosted.org/packages/c2/64/0d0a47ed0aa1d634c666c2cc15d3b0af4b95d0fd3dbb796032bd493f3433/ty-0.0.25-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:781150e23825dc110cd5e1f50ca3d61664f7a5db5b4a55d5dbf7d3b1e246b917", size = 10261961, upload-time = "2026-03-24T22:32:43.935Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/de/68/015d118097eeb95e6a44c4abce4c0a28b7b9dfb3085b7f0ee48e4f099633/ty-0.0.29-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:298a8d5faa2502d3810bbbb47a030b9455495b9921594206043c785dd61548cf", size = 10910613, upload-time = "2026-04-05T15:01:17.17Z" },
|
{ url = "https://files.pythonhosted.org/packages/3e/ba/4666b96f0499465efb97c244554107c541d74a1add393e62276b3de9b54f/ty-0.0.25-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:ffc81ff2a0143911321251dc81d1c259fa5cdc56d043019a733c845d55409e2a", size = 10746076, upload-time = "2026-03-24T22:32:26.37Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/1c/01/47ce3c6c53e0670eadbe80756b167bf80ed6681d1ba57cfde2e8065a13d1/ty-0.0.29-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:3c8fba1a3524c6109d1e020d92301c79d41bf442fa8d335b9fa366239339cb70", size = 11475750, upload-time = "2026-04-05T15:01:30.461Z" },
|
{ url = "https://files.pythonhosted.org/packages/e7/ed/aa958ccbcd85cc206600e48fbf0a1c27aef54b4b90112d9a73f69ed0c739/ty-0.0.25-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f03c5c5b5c10355ea030cbe3cd93b2e759b9492c66688288ea03a68086069f2e", size = 11287331, upload-time = "2026-03-24T22:32:21.607Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/c4/cf/e361845b1081c9264ad5b7c963231bab03f2666865a9f2a115c4233f2137/ty-0.0.29-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:4c48adf88a70d264128c39ee922ed14a947817fced1e93c08c1a89c9244edcde", size = 11190055, upload-time = "2026-04-05T15:01:12.369Z" },
|
{ url = "https://files.pythonhosted.org/packages/26/e4/f4a004e1952e6042f5bfeeb7d09cffb379270ef009d9f8568471863e86e6/ty-0.0.25-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:7fc1ef49cd6262eb9223ccf6e258ac899aaa53e7dc2151ba65a2c9fa248dfa75", size = 11028804, upload-time = "2026-03-24T22:32:39.088Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/79/12/0fb0857e9a62cb11586e9a712103877bbf717f5fb570d16634408cfdefee/ty-0.0.29-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2ce0a7a0e96bc7b42518cd3a1a6a6298ef64ff40ca4614355c1aa807059b5c6f", size = 11020539, upload-time = "2026-04-05T15:01:37.022Z" },
|
{ url = "https://files.pythonhosted.org/packages/56/32/5c15bb8ea20ed54d43c734f253a2a5da95d41474caecf4ef3682df9f68f5/ty-0.0.25-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7ad98da1393161096235a387cc36abecd31861060c68416761eccdb7c1bc326b", size = 10845246, upload-time = "2026-03-24T22:32:41.33Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/20/36/5a26753802083f80cd125db6c4348ad42b3c982ec36e718e0bf4c18f75e5/ty-0.0.29-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:a6ac86a05b4a3731d45365ab97780acc7b8146fa62fccb3cbe94fe6546c67a97", size = 10396399, upload-time = "2026-04-05T15:01:26.167Z" },
|
{ url = "https://files.pythonhosted.org/packages/6f/fe/4ddd83e810c8682fcfada0d1c9d38936a34a024d32d7736075c1e53a038e/ty-0.0.25-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:2d4336aa5381eb4eab107c3dec75fe22943a648ef6646f5a8431ef1c8cdabb66", size = 10233515, upload-time = "2026-03-24T22:32:17.012Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/00/e6/b4e75b5752239ab3ab400f19faef4dbef81d05aab5d3419fda0c062a3765/ty-0.0.29-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:6bbbf53141af0f3150bf288d716263f1a3550054e4b3551ca866d38192ba9891", size = 10421461, upload-time = "2026-04-05T15:01:08.367Z" },
|
{ url = "https://files.pythonhosted.org/packages/ad/db/9fe54f6fb952e5b218f2e661e64ed656512edf2046cfbb9c159558e255db/ty-0.0.25-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:e10ed39564227de2b7bd89398250b65daaedbef15a25cef8eee70078f5d9e0b2", size = 10275289, upload-time = "2026-03-24T22:32:28.21Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/c0/21/1084b5b609f9abed62070ec0b31c283a403832a6310c8bbc208bd45ee1e6/ty-0.0.29-py3-none-musllinux_1_2_i686.whl", hash = "sha256:1c9e06b770c1d0ff5efc51e34312390db31d53fcf3088163f413030b42b74f84", size = 10599187, upload-time = "2026-04-05T15:01:23.52Z" },
|
{ url = "https://files.pythonhosted.org/packages/b1/e0/090d7b33791b42bc7ec29463ac6a634738e16b289e027608ebe542682773/ty-0.0.25-py3-none-musllinux_1_2_i686.whl", hash = "sha256:aca04e9ed9b61c706064a1c0b71a247c3f92f373d0222103f3bc54b649421796", size = 10461195, upload-time = "2026-03-24T22:32:24.252Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/ab/a1/ce19a2ca717bbcc1ee11378aba52ef70b6ce5b87245162a729d9fdc2360f/ty-0.0.29-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:0307fe37e3f000ef1a4ae230bbaf511508a78d24a5e51b40902a21b09d5e6037", size = 11121198, upload-time = "2026-04-05T15:01:15.22Z" },
|
{ url = "https://files.pythonhosted.org/packages/42/31/5bf12bce01b80b72a7a4e627380779b41510e730f6000862a1d078e423f7/ty-0.0.25-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:18a5443e4ef339c1bd8c57fc13112c22080617ea582bfc22b497d82d65361325", size = 10931471, upload-time = "2026-03-24T22:32:14.985Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/6b/6b/f1430b279af704321566ce7ec2725d3d8258c2f815ebd93e474c64cd4543/ty-0.0.29-py3-none-win32.whl", hash = "sha256:7a2a898217960a825f8bc0087e1fdbaf379606175e98f9807187221d53a4a8ed", size = 9995331, upload-time = "2026-04-05T15:01:01.32Z" },
|
{ url = "https://files.pythonhosted.org/packages/6a/5e/ab60c11f8a6dd2a0ae96daac83458ef2e9be1ae70481d1ad9c59d3eaf20f/ty-0.0.25-py3-none-win32.whl", hash = "sha256:a685b9a611b69195b5a557e05dbb7ebcd12815f6c32fb27fdf15edeb1fa33d8f", size = 9835974, upload-time = "2026-03-24T22:32:36.86Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/d2/ef/3ef01c17785ff9a69378465c7d0faccd48a07b163554db0995e5d65a5a23/ty-0.0.29-py3-none-win_amd64.whl", hash = "sha256:fc1294200226b91615acbf34e0a9ad81caf98c081e9c6a912a31b0a7b603bc3f", size = 11023644, upload-time = "2026-04-05T15:01:04.432Z" },
|
{ url = "https://files.pythonhosted.org/packages/41/55/625acc2ef34646268bc2baa8fdd6e22fb47cd5965e2acd3be92c687fb6b0/ty-0.0.25-py3-none-win_amd64.whl", hash = "sha256:0d4d37a1f1ab7f2669c941c38c65144ff223eb51ececd7ccfc0d623afbc0f729", size = 10815449, upload-time = "2026-03-24T22:32:11.031Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/2c/55/87280a994d6a2d2647c65e12abbc997ed49835794366153c04c4d9304d76/ty-0.0.29-py3-none-win_arm64.whl", hash = "sha256:f9794bbd1bb3ce13f78c191d0c89ae4c63f52c12b6daa0c6fe220b90d019d12c", size = 10428165, upload-time = "2026-04-05T15:01:34.665Z" },
|
{ url = "https://files.pythonhosted.org/packages/82/c7/0147bfb543df97740b45b222c54ff79ef20fa57f14b9d2c1dab3cd7d3faa/ty-0.0.25-py3-none-win_arm64.whl", hash = "sha256:d80b8cd965cbacbfd887ac2d985f5b6da09b7aa3569371e2894e0b30b26b89cd", size = 10225494, upload-time = "2026-03-24T22:32:30.611Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1324,7 +1324,7 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zensical"
|
name = "zensical"
|
||||||
version = "0.0.32"
|
version = "0.0.30"
|
||||||
source = { registry = "https://pypi.org/simple" }
|
source = { registry = "https://pypi.org/simple" }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "click" },
|
{ name = "click" },
|
||||||
@@ -1334,18 +1334,18 @@ dependencies = [
|
|||||||
{ name = "pymdown-extensions" },
|
{ name = "pymdown-extensions" },
|
||||||
{ name = "pyyaml" },
|
{ name = "pyyaml" },
|
||||||
]
|
]
|
||||||
sdist = { url = "https://files.pythonhosted.org/packages/7a/94/4a49ca9329136445f4111fda60e4bfcbe68d95e18e9aa02e4606fba5df4a/zensical-0.0.32.tar.gz", hash = "sha256:0f857b09a2b10c99202b3712e1ffc4d1d1ffa4c7c2f1aa0fafb1346b2d8df604", size = 3891955, upload-time = "2026-04-07T11:41:29.203Z" }
|
sdist = { url = "https://files.pythonhosted.org/packages/1d/53/5e551f8912718816733a75adcb53a0787b2d2edca5869c156325aaf82e24/zensical-0.0.30.tar.gz", hash = "sha256:408b531683f6bcb6cc5ab928146d2c68afbc16fac4eda87ae3dd20af1498180f", size = 3844287, upload-time = "2026-03-28T17:55:52.836Z" }
|
||||||
wheels = [
|
wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/73/e1/dd03762447f1c2a4c8aff08e8f047ec17c73421714a0600ef71c361a5934/zensical-0.0.32-cp310-abi3-macosx_10_12_x86_64.whl", hash = "sha256:7ed181c76c03fec4c2dd5db207810044bf9c3fa87097fbdbabd633661e20fc70", size = 12416474, upload-time = "2026-04-07T11:40:55.888Z" },
|
{ url = "https://files.pythonhosted.org/packages/1b/e3/ac0eb77a8a7f793613813de68bde26776d0da68d8041fa9eb8d0b986a449/zensical-0.0.30-cp310-abi3-macosx_10_12_x86_64.whl", hash = "sha256:b67fca8bfcd71c94b331045a591bf6e24fe123a66fba94587aa3379faf521a16", size = 12313786, upload-time = "2026-03-28T17:55:18.839Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/f5/a6/2f1babb00842c6efa5ae755b3ab414e4688ae8e47bdd2e785c0c37ef625d/zensical-0.0.32-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:8cde82bf256408f75ae2b07bffcaac7d080b6aad5f7acf210c438cb7413c3081", size = 12292801, upload-time = "2026-04-07T11:40:59.648Z" },
|
{ url = "https://files.pythonhosted.org/packages/a5/6a/73e461dfa27d3bc415e48396f83a3287b43df2fd3361e25146bc86360aab/zensical-0.0.30-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:8ceadfece1153edc26506e8ddf68d9818afe8517cf3bcdb6bfe4cb2793ae247b", size = 12186136, upload-time = "2026-03-28T17:55:21.836Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/2d/f1/d32706de06fd30fb07ae514222a79dd17d4578cd1634e5b692e0c790a61e/zensical-0.0.32-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:60e60e2358249b2a2c5e1c5c04586d8dbba27e577441cc9dd32fe8d879c6951e", size = 12658847, upload-time = "2026-04-07T11:41:02.347Z" },
|
{ url = "https://files.pythonhosted.org/packages/a3/bc/9022156b4c28c1b95209acb64319b1e5cd0af2e97035bdd461e58408cb46/zensical-0.0.30-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e100b2b654337ac5306ba12818f3c5336c66d0d34c593ef05e316c124a5819cb", size = 12556115, upload-time = "2026-03-28T17:55:24.849Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/e7/42/a3daf4047c86382749a59795c4e7acd59952b4f6f37f329cd2d41cc37a0f/zensical-0.0.32-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:ec79b4304009138e7a38ebe24e8a8e9dbc15d38922185f8a84470a7757d7b73f", size = 12604777, upload-time = "2026-04-07T11:41:05.227Z" },
|
{ url = "https://files.pythonhosted.org/packages/0b/29/9e8f5bd6d33b35f4c368ae8b13d431dc42b2de17ea6eccbd71d48122eba6/zensical-0.0.30-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:bdf641ffddaf21c6971b91a4426b81cd76271c5b1adb7176afcce3f1508328b1", size = 12498121, upload-time = "2026-03-28T17:55:27.637Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/59/11/4af61d3fb07713cd3f77981c1b3017a60c2b210b36f1b04353f9116d03ca/zensical-0.0.32-cp310-abi3-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:fc92fa7d0860ec6d95426a5f545cfc5493c60f8ab44fcc11611a4251f34f1b70", size = 12956242, upload-time = "2026-04-07T11:41:07.58Z" },
|
{ url = "https://files.pythonhosted.org/packages/c4/e1/b8dfa0769050e62cd731358145fdeb67af35e322197bd7e7727250596e7b/zensical-0.0.30-cp310-abi3-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1fd909a0c2116e26190c7f3ec4fb55837c417b7a8d99ebf4f3deb26b07b97e49", size = 12854142, upload-time = "2026-03-28T17:55:30.54Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/8c/34/e9b5f4376bbf460f8c07a77af59bd169c7c68ed719a074e6667ba41109f8/zensical-0.0.32-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:07f69019396060e310c9c3b18747ce8982ad56d67fbab269b61e74a6a5bdcb4a", size = 12701954, upload-time = "2026-04-07T11:41:10.532Z" },
|
{ url = "https://files.pythonhosted.org/packages/04/11/62a36cfb81522b6108db8f9e96d36da8cccb306b02c15ad19e1b333fa7c8/zensical-0.0.30-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:16fd2da09fe4e5cbec2ca74f31abc70f32f7330d56593b647e0a114bb329171a", size = 12598341, upload-time = "2026-03-28T17:55:32.988Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/d2/43/a52e5dcb324f38a1d22f7fafd4eec273385d04de52a7ab5ac7b444cf2bdc/zensical-0.0.32-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:d096c9ed20a48e5ff095eca218eef94f67e739cdf0abf7e1f7e232e78f6d980c", size = 12835464, upload-time = "2026-04-07T11:41:13.152Z" },
|
{ url = "https://files.pythonhosted.org/packages/a7/a4/8c7a6725fb226aa71d19209403d974e45f39d757e725f9558c6ed8d350a5/zensical-0.0.30-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:896b36eaef7fed5f8fc6f2c8264b2751aad63c2d66d3d8650e38481b6b4f6f7b", size = 12732307, upload-time = "2026-03-28T17:55:35.618Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/a7/95/bede89ecb4932bbd29db7b61bf530a962aed09d3a8d5aa71a64af1d4920f/zensical-0.0.32-cp310-abi3-musllinux_1_2_armv7l.whl", hash = "sha256:bf5576b7154bde18cebd9a7b065d3ab8b334c6e73d5b2e83abe2b17f9d00a992", size = 12876574, upload-time = "2026-04-07T11:41:16.085Z" },
|
{ url = "https://files.pythonhosted.org/packages/5e/a1/7858fb3f6ac67d7d24a8acbe834cbe26851d6bd151ece6fba3fc88b0f878/zensical-0.0.30-cp310-abi3-musllinux_1_2_armv7l.whl", hash = "sha256:a1f515ec67a0d0250e53846327bf0c69635a1f39749da3b04feb68431188d3c6", size = 12770962, upload-time = "2026-03-28T17:55:38.627Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/9e/e8/9b25fda22bf729ca2598cc42cefe9b20e751d12d23e35c70ea0c7939d20a/zensical-0.0.32-cp310-abi3-musllinux_1_2_i686.whl", hash = "sha256:f33905a1e0b03a2ad548554a157b7f7c398e6f41012d1e755105ae2bc60eab8a", size = 13022702, upload-time = "2026-04-07T11:41:18.947Z" },
|
{ url = "https://files.pythonhosted.org/packages/49/b7/228298112a69d0b74e6e93041bffcf1fc96d03cf252be94a354f277d4789/zensical-0.0.30-cp310-abi3-musllinux_1_2_i686.whl", hash = "sha256:ce33d1002438838a35fa43358a1f43d74f874586596d3d116999d3756cded00e", size = 12919256, upload-time = "2026-03-28T17:55:41.413Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/f6/35/0c6d0b57187bd470a05e8a391c0edd1d690eb429e12b9755c99cf60a370e/zensical-0.0.32-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:0a73a53b1dd41fd239875a3cb57c4284747989c45b6933f18e9b51f1b5f3d8ef", size = 12975593, upload-time = "2026-04-07T11:41:21.436Z" },
|
{ url = "https://files.pythonhosted.org/packages/de/c7/5b4ea036f7f7d84abf907f7f7a3e8420b054c89279c5273ca248d3bc9f48/zensical-0.0.30-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:029dad561568f4ae3056dde16a81012efd92c426d4eb7101f960f448c1168196", size = 12869760, upload-time = "2026-03-28T17:55:44.474Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/ee/2d/4e88bcefc33b7af22f0637fd002d3cf5384e8354f0a7f8a9dbfcd40cfa24/zensical-0.0.32-cp310-abi3-win32.whl", hash = "sha256:f8cb579bdb9b56f1704b93f4e17b42895c8cb466e8eec933fbe0153b5b1e3459", size = 12012163, upload-time = "2026-04-07T11:41:23.975Z" },
|
{ url = "https://files.pythonhosted.org/packages/36/b4/77bef2132e43108db718ae014a5961fc511e88fc446c11f1c3483def429e/zensical-0.0.30-cp310-abi3-win32.whl", hash = "sha256:0105672850f053c326fba9fdd95adf60e9f90308f8cc1c08e3a00e15a8d5e90f", size = 11905658, upload-time = "2026-03-28T17:55:47.416Z" },
|
||||||
{ url = "https://files.pythonhosted.org/packages/8a/ae/a80a2f15fd10201fe3dfd6b5cdf85351165f820cf5b29e3c3b24092c158c/zensical-0.0.32-cp310-abi3-win_amd64.whl", hash = "sha256:6d662f42b5d0eadfac6d281e9d86574bc7a9f812f1ed496335d15f2d581d4b28", size = 12205948, upload-time = "2026-04-07T11:41:27.056Z" },
|
{ url = "https://files.pythonhosted.org/packages/a1/59/23b6c7ff062e2b299cc60e333095e853f9d38d1b5abe743c7b94c4ac432c/zensical-0.0.30-cp310-abi3-win_amd64.whl", hash = "sha256:b879dbf4c69d3ea41694bae33e1b948847e635dcbcd6ec8c522920833379dd48", size = 12101867, upload-time = "2026-03-28T17:55:50.083Z" },
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -2,15 +2,10 @@
|
|||||||
site_name = "FastAPI Toolsets"
|
site_name = "FastAPI Toolsets"
|
||||||
site_description = "Production-ready utilities for FastAPI applications."
|
site_description = "Production-ready utilities for FastAPI applications."
|
||||||
site_author = "d3vyce"
|
site_author = "d3vyce"
|
||||||
site_url = "https://fastapi-toolsets.d3vyce.fr/"
|
site_url = "https://fastapi-toolsets.d3vyce.fr"
|
||||||
copyright = "Copyright © 2026 d3vyce"
|
copyright = "Copyright © 2026 d3vyce"
|
||||||
repo_url = "https://github.com/d3vyce/fastapi-toolsets"
|
repo_url = "https://github.com/d3vyce/fastapi-toolsets"
|
||||||
|
|
||||||
[project.extra.version]
|
|
||||||
provider = "mike"
|
|
||||||
default = "stable"
|
|
||||||
alias = true
|
|
||||||
|
|
||||||
[project.theme]
|
[project.theme]
|
||||||
custom_dir = "docs/overrides"
|
custom_dir = "docs/overrides"
|
||||||
language = "en"
|
language = "en"
|
||||||
@@ -145,7 +140,6 @@ Examples = [
|
|||||||
|
|
||||||
[[project.nav]]
|
[[project.nav]]
|
||||||
Migration = [
|
Migration = [
|
||||||
{"v3.0" = "migration/v3.md"},
|
|
||||||
{"v2.0" = "migration/v2.md"},
|
{"v2.0" = "migration/v2.md"},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user