mirror of
https://github.com/d3vyce/fastapi-toolsets.git
synced 2026-04-16 14:46:24 +02:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
c4c760484b
|
|||
|
|
432e0722e0 | ||
|
|
e732e54518 | ||
|
|
05b5a2c876 | ||
|
|
4a020c56d1 | ||
|
56d365d14b
|
|||
|
|
a257d85d45 | ||
|
117675d02f
|
|||
|
|
d7ad7308c5 |
11
README.md
11
README.md
@@ -20,7 +20,7 @@ A modular collection of production-ready utilities for FastAPI. Install only wha
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
The base package includes the core modules (CRUD, database, schemas, exceptions, fixtures, dependencies, logging):
|
The base package includes the core modules (CRUD, database, schemas, exceptions, fixtures, dependencies, model mixins, logging):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
uv add fastapi-toolsets
|
uv add fastapi-toolsets
|
||||||
@@ -29,9 +29,9 @@ uv add fastapi-toolsets
|
|||||||
Install only the extras you need:
|
Install only the extras you need:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
uv add "fastapi-toolsets[cli]" # CLI (typer)
|
uv add "fastapi-toolsets[cli]"
|
||||||
uv add "fastapi-toolsets[metrics]" # Prometheus metrics (prometheus_client)
|
uv add "fastapi-toolsets[metrics]"
|
||||||
uv add "fastapi-toolsets[pytest]" # Pytest helpers (httpx, pytest-xdist)
|
uv add "fastapi-toolsets[pytest]"
|
||||||
```
|
```
|
||||||
|
|
||||||
Or install everything:
|
Or install everything:
|
||||||
@@ -44,10 +44,11 @@ uv add "fastapi-toolsets[all]"
|
|||||||
|
|
||||||
### Core
|
### Core
|
||||||
|
|
||||||
- **CRUD**: Generic async CRUD operations with `CrudFactory`, built-in search with relationship traversal
|
- **CRUD**: Generic async CRUD operations with `CrudFactory`, built-in full-text/faceted search and Offset/Cursor pagination.
|
||||||
- **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`, `CreatedAtMixin`, `UpdatedAtMixin`, `TimestampMixin`)
|
||||||
- **Standardized API Responses**: Consistent response format with `Response`, `PaginatedResponse`, and `PydanticBase`
|
- **Standardized API Responses**: Consistent response format with `Response`, `PaginatedResponse`, and `PydanticBase`
|
||||||
- **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`
|
||||||
|
|||||||
134
docs/examples/pagination-search.md
Normal file
134
docs/examples/pagination-search.md
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
# Pagination & search
|
||||||
|
|
||||||
|
This example builds an articles listing endpoint that supports **offset pagination**, **cursor pagination**, **full-text search**, **faceted filtering**, and **sorting** — all from a single `CrudFactory` definition.
|
||||||
|
|
||||||
|
## Models
|
||||||
|
|
||||||
|
```python title="models.py"
|
||||||
|
--8<-- "docs_src/examples/pagination_search/models.py"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Schemas
|
||||||
|
|
||||||
|
```python title="schemas.py"
|
||||||
|
--8<-- "docs_src/examples/pagination_search/schemas.py"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Crud
|
||||||
|
|
||||||
|
Declare `searchable_fields`, `facet_fields`, and `order_fields` once on [`CrudFactory`](../reference/crud.md#fastapi_toolsets.crud.factory.CrudFactory). All endpoints built from this class share the same defaults and can override them per call.
|
||||||
|
|
||||||
|
```python title="crud.py"
|
||||||
|
--8<-- "docs_src/examples/pagination_search/crud.py"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Session dependency
|
||||||
|
|
||||||
|
```python title="db.py"
|
||||||
|
--8<-- "docs_src/examples/pagination_search/db.py"
|
||||||
|
```
|
||||||
|
|
||||||
|
!!! info "Deploy a Postgres DB with docker"
|
||||||
|
```bash
|
||||||
|
docker run -d --name postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -p 5432:5432 postgres:18-alpine
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## App
|
||||||
|
|
||||||
|
```python title="app.py"
|
||||||
|
--8<-- "docs_src/examples/pagination_search/app.py"
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Routes
|
||||||
|
### Offset pagination
|
||||||
|
|
||||||
|
Best for admin panels or any UI that needs a total item count and numbered pages.
|
||||||
|
|
||||||
|
```python title="routes.py:1:36"
|
||||||
|
--8<-- "docs_src/examples/pagination_search/routes.py:1:36"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example request**
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /articles/offset?page=2&items_per_page=10&search=fastapi&status=published&order_by=title&order=asc
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example response**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": "SUCCESS",
|
||||||
|
"data": [
|
||||||
|
{ "id": "3f47ac69-...", "title": "FastAPI tips", "status": "published", ... }
|
||||||
|
],
|
||||||
|
"pagination": {
|
||||||
|
"total_count": 42,
|
||||||
|
"page": 2,
|
||||||
|
"items_per_page": 10,
|
||||||
|
"has_more": true
|
||||||
|
},
|
||||||
|
"filter_attributes": {
|
||||||
|
"status": ["archived", "draft", "published"],
|
||||||
|
"name": ["backend", "frontend", "python"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`filter_attributes` always reflects the values visible **after** applying the active filters. Use it to populate filter dropdowns on the client.
|
||||||
|
|
||||||
|
### Cursor pagination
|
||||||
|
|
||||||
|
Best for feeds, infinite scroll, or any high-throughput API where offset performance degrades.
|
||||||
|
|
||||||
|
```python title="routes.py:39:59"
|
||||||
|
--8<-- "docs_src/examples/pagination_search/routes.py:39:59"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example request**
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /articles/cursor?items_per_page=10&status=published&order_by=created_at&order=desc
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example response**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": "SUCCESS",
|
||||||
|
"data": [
|
||||||
|
{ "id": "3f47ac69-...", "title": "FastAPI tips", "status": "published", ... }
|
||||||
|
],
|
||||||
|
"pagination": {
|
||||||
|
"next_cursor": "eyJ2YWx1ZSI6ICIzZjQ3YWM2OS0uLi4ifQ==",
|
||||||
|
"prev_cursor": null,
|
||||||
|
"items_per_page": 10,
|
||||||
|
"has_more": true
|
||||||
|
},
|
||||||
|
"filter_attributes": {
|
||||||
|
"status": ["published"],
|
||||||
|
"name": ["backend", "python"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Pass `next_cursor` as the `cursor` query parameter on the next request to advance to the next page.
|
||||||
|
|
||||||
|
## Search behaviour
|
||||||
|
|
||||||
|
Both endpoints inherit the same `searchable_fields` declared on `ArticleCrud`:
|
||||||
|
|
||||||
|
Search is **case-insensitive** and uses a `LIKE %query%` pattern. Pass a [`SearchConfig`](../reference/crud.md#fastapi_toolsets.crud.search.SearchConfig) instead of a plain string to control case sensitivity or switch to `match_mode="all"` (AND across all fields instead of OR).
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi_toolsets.crud import SearchConfig
|
||||||
|
|
||||||
|
# Both title AND body must contain "fastapi"
|
||||||
|
result = await ArticleCrud.offset_paginate(
|
||||||
|
session,
|
||||||
|
search=SearchConfig(query="fastapi", case_sensitive=True, match_mode="all"),
|
||||||
|
search_fields=[Article.title, Article.body],
|
||||||
|
)
|
||||||
|
```
|
||||||
@@ -20,7 +20,7 @@ A modular collection of production-ready utilities for FastAPI. Install only wha
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
The base package includes the core modules (CRUD, database, schemas, exceptions, fixtures, dependencies, logging):
|
The base package includes the core modules (CRUD, database, schemas, exceptions, fixtures, dependencies, model mixins, logging):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
uv add fastapi-toolsets
|
uv add fastapi-toolsets
|
||||||
@@ -29,9 +29,9 @@ uv add fastapi-toolsets
|
|||||||
Install only the extras you need:
|
Install only the extras you need:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
uv add "fastapi-toolsets[cli]" # CLI (typer)
|
uv add "fastapi-toolsets[cli]"
|
||||||
uv add "fastapi-toolsets[metrics]" # Prometheus metrics (prometheus_client)
|
uv add "fastapi-toolsets[metrics]"
|
||||||
uv add "fastapi-toolsets[pytest]" # Pytest helpers (httpx, pytest-xdist)
|
uv add "fastapi-toolsets[pytest]"
|
||||||
```
|
```
|
||||||
|
|
||||||
Or install everything:
|
Or install everything:
|
||||||
@@ -44,10 +44,11 @@ uv add "fastapi-toolsets[all]"
|
|||||||
|
|
||||||
### Core
|
### Core
|
||||||
|
|
||||||
- **CRUD**: Generic async CRUD operations with `CrudFactory`, built-in search with relationship traversal
|
- **CRUD**: Generic async CRUD operations with `CrudFactory`, built-in full-text/faceted search and Offset/Cursor pagination.
|
||||||
- **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`, `CreatedAtMixin`, `UpdatedAtMixin`, `TimestampMixin`)
|
||||||
- **Standardized API Responses**: Consistent response format with `Response`, `PaginatedResponse`, and `PydanticBase`
|
- **Standardized API Responses**: Consistent response format with `Response`, `PaginatedResponse`, and `PydanticBase`
|
||||||
- **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`
|
||||||
|
|||||||
137
docs/migration/v2.md
Normal file
137
docs/migration/v2.md
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
# Migrating to v2.0
|
||||||
|
|
||||||
|
This page covers every breaking change introduced in **v2.0** and the steps required to update your code.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## CRUD
|
||||||
|
|
||||||
|
### `schema` is now required in `offset_paginate()` and `cursor_paginate()`
|
||||||
|
|
||||||
|
Calls that omit `schema` will now raise a `TypeError` at runtime.
|
||||||
|
|
||||||
|
Previously `schema` was optional; omitting it returned raw SQLAlchemy model instances inside the response. It is now a required keyword argument and the response always contains serialized schema instances.
|
||||||
|
|
||||||
|
=== "Before (`v1`)"
|
||||||
|
|
||||||
|
```python
|
||||||
|
# schema omitted — returned raw model instances
|
||||||
|
result = await UserCrud.offset_paginate(session=session, page=1)
|
||||||
|
result = await UserCrud.cursor_paginate(session=session, cursor=token)
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Now (`v2`)"
|
||||||
|
|
||||||
|
```python
|
||||||
|
result = await UserCrud.offset_paginate(session=session, page=1, schema=UserRead)
|
||||||
|
result = await UserCrud.cursor_paginate(session=session, cursor=token, schema=UserRead)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `as_response` removed from `create()`, `get()`, and `update()`
|
||||||
|
|
||||||
|
Passing `as_response` to these methods will raise a `TypeError` at runtime.
|
||||||
|
|
||||||
|
The `as_response=True` shorthand is replaced by passing a `schema` directly. The return value is a `Response[schema]` when `schema` is provided, or the raw model instance when it is not.
|
||||||
|
|
||||||
|
=== "Before (`v1`)"
|
||||||
|
|
||||||
|
```python
|
||||||
|
user = await UserCrud.create(session=session, obj=data, as_response=True)
|
||||||
|
user = await UserCrud.get(session=session, filters=filters, as_response=True)
|
||||||
|
user = await UserCrud.update(session=session, obj=data, filters, as_response=True)
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Now (`v2`)"
|
||||||
|
|
||||||
|
```python
|
||||||
|
user = await UserCrud.create(session=session, obj=data, schema=UserRead)
|
||||||
|
user = await UserCrud.get(session=session, filters=filters, schema=UserRead)
|
||||||
|
user = await UserCrud.update(session=session, obj=data, filters, schema=UserRead)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `delete()`: `as_response` renamed and return type changed
|
||||||
|
|
||||||
|
`as_response` is gone, and the plain (non-response) call no longer returns `True`.
|
||||||
|
|
||||||
|
Two changes were made to `delete()`:
|
||||||
|
|
||||||
|
1. The `as_response` parameter is renamed to `return_response`.
|
||||||
|
2. When called without `return_response=True`, the method now returns `None` on success instead of `True`.
|
||||||
|
|
||||||
|
=== "Before (`v1`)"
|
||||||
|
|
||||||
|
```python
|
||||||
|
ok = await UserCrud.delete(session=session, filters=filters)
|
||||||
|
if ok: # True on success
|
||||||
|
...
|
||||||
|
|
||||||
|
response = await UserCrud.delete(session=session, filters=filters, as_response=True)
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Now (`v2`)"
|
||||||
|
|
||||||
|
```python
|
||||||
|
await UserCrud.delete(session=session, filters=filters) # returns None
|
||||||
|
|
||||||
|
response = await UserCrud.delete(session=session, filters=filters, return_response=True)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `paginate()` alias removed
|
||||||
|
|
||||||
|
Any call to `crud.paginate(...)` will raise `AttributeError` at runtime.
|
||||||
|
|
||||||
|
The `paginate` shorthand was an alias for `offset_paginate`. It has been removed; call `offset_paginate` directly.
|
||||||
|
|
||||||
|
=== "Before (`v1`)"
|
||||||
|
|
||||||
|
```python
|
||||||
|
result = await UserCrud.paginate(session=session, page=2, items_per_page=20, schema=UserRead)
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Now (`v2`)"
|
||||||
|
|
||||||
|
```python
|
||||||
|
result = await UserCrud.offset_paginate(session=session, page=2, items_per_page=20, schema=UserRead)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Exceptions
|
||||||
|
|
||||||
|
### Missing `api_error` raises `TypeError` at class definition time
|
||||||
|
|
||||||
|
Unfinished or stub exception subclasses that previously compiled fine will now fail on import.
|
||||||
|
|
||||||
|
In `v1`, a subclass without `api_error` would only fail when the exception was raised. In `v2`, `__init_subclass__` validates this at class definition time.
|
||||||
|
|
||||||
|
=== "Before (`v1`)"
|
||||||
|
|
||||||
|
```python
|
||||||
|
class MyError(ApiException):
|
||||||
|
pass # fine until raised
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Now (`v2`)"
|
||||||
|
|
||||||
|
```python
|
||||||
|
class MyError(ApiException):
|
||||||
|
pass # TypeError: MyError must define an 'api_error' class attribute.
|
||||||
|
```
|
||||||
|
|
||||||
|
For shared base classes that are not meant to be raised directly, use `abstract=True`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class BillingError(ApiException, abstract=True):
|
||||||
|
"""Base for all billing-related errors — not raised directly."""
|
||||||
|
|
||||||
|
class PaymentRequiredError(BillingError):
|
||||||
|
api_error = ApiError(code=402, msg="Payment Required", desc="...", err_code="BILLING-402")
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Schemas
|
||||||
|
|
||||||
|
### `Pagination` alias removed
|
||||||
|
|
||||||
|
`Pagination` was already deprecated in `v1` and is fully removed in `v2`, you now need to use [`OffsetPagination`](../reference/schemas.md#fastapi_toolsets.schemas.OffsetPagination) or [`CursorPagination`](../reference/schemas.md#fastapi_toolsets.schemas.CursorPagination).
|
||||||
@@ -95,9 +95,6 @@ The [`offset_paginate`](../reference/crud.md#fastapi_toolsets.crud.factory.Async
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! warning "Deprecated: `paginate`"
|
|
||||||
The `paginate` function is a backward-compatible alias for `offset_paginate`. This function is **deprecated** and will be removed in **v2.0**.
|
|
||||||
|
|
||||||
### Cursor pagination
|
### Cursor pagination
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -170,7 +167,7 @@ PostCrud = CrudFactory(model=Post, cursor_column=Post.created_at)
|
|||||||
|
|
||||||
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).
|
||||||
|
|
||||||
| | Full-text search | Filter attributes |
|
| | Full-text search | Faceted search |
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
| Input | Free-text string | Exact column values |
|
| Input | Free-text string | Exact column values |
|
||||||
| Relationship support | Yes | Yes |
|
| Relationship support | Yes | Yes |
|
||||||
@@ -242,7 +239,7 @@ async def get_users(
|
|||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Filter attributes
|
### Faceted search
|
||||||
|
|
||||||
!!! info "Added in `v1.2`"
|
!!! info "Added in `v1.2`"
|
||||||
|
|
||||||
@@ -295,6 +292,8 @@ Use `filter_by` to pass the client's chosen filter values directly — no need t
|
|||||||
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:
|
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
|
```python
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
|
|
||||||
UserCrud = CrudFactory(
|
UserCrud = CrudFactory(
|
||||||
@@ -306,7 +305,7 @@ UserCrud = CrudFactory(
|
|||||||
async def list_users(
|
async def list_users(
|
||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
filter_by: dict[str, list[str]] = Depends(UserCrud.filter_params()),
|
filter_by: Annotated[dict[str, list[str]], Depends(UserCrud.filter_params())],
|
||||||
) -> PaginatedResponse[UserRead]:
|
) -> PaginatedResponse[UserRead]:
|
||||||
return await UserCrud.offset_paginate(
|
return await UserCrud.offset_paginate(
|
||||||
session=session,
|
session=session,
|
||||||
@@ -323,6 +322,58 @@ GET /users?status=active&country=FR → filter_by={"status": ["active"], "coun
|
|||||||
GET /users?role=admin&role=editor → filter_by={"role": ["admin", "editor"]} (IN clause)
|
GET /users?role=admin&role=editor → filter_by={"role": ["admin", "editor"]} (IN clause)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Sorting
|
||||||
|
|
||||||
|
!!! info "Added in `v1.3`"
|
||||||
|
|
||||||
|
Declare `order_fields` on the CRUD class to expose client-driven column ordering via `order_by` and `order` query parameters.
|
||||||
|
|
||||||
|
```python
|
||||||
|
UserCrud = CrudFactory(
|
||||||
|
model=User,
|
||||||
|
order_fields=[
|
||||||
|
User.name,
|
||||||
|
User.created_at,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
from fastapi import Depends
|
||||||
|
from fastapi_toolsets.crud import OrderByClause
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
async def list_users(
|
||||||
|
session: SessionDep,
|
||||||
|
order_by: Annotated[OrderByClause | None, Depends(UserCrud.order_params())],
|
||||||
|
) -> PaginatedResponse[UserRead]:
|
||||||
|
return await UserCrud.offset_paginate(session=session, order_by=order_by)
|
||||||
|
```
|
||||||
|
|
||||||
|
The dependency adds two query parameters to the endpoint:
|
||||||
|
|
||||||
|
| Parameter | Type |
|
||||||
|
| ---------- | --------------- |
|
||||||
|
| `order_by` | `str | null` |
|
||||||
|
| `order` | `asc` or `desc` |
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /users?order_by=name&order=asc → ORDER BY users.name ASC
|
||||||
|
GET /users?order_by=name&order=desc → ORDER BY users.name DESC
|
||||||
|
```
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
```python
|
||||||
|
UserOrderParams = UserCrud.order_params(order_fields=[User.name])
|
||||||
|
```
|
||||||
|
|
||||||
## Relationship loading
|
## Relationship loading
|
||||||
|
|
||||||
!!! info "Added in `v1.1`"
|
!!! info "Added in `v1.1`"
|
||||||
@@ -384,7 +435,7 @@ await UserCrud.upsert(
|
|||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
## `schema` — typed response serialization
|
## Response serialization
|
||||||
|
|
||||||
!!! info "Added in `v1.1`"
|
!!! info "Added in `v1.1`"
|
||||||
|
|
||||||
@@ -417,9 +468,6 @@ async def list_users(session: SessionDep, page: int = 1) -> PaginatedResponse[Us
|
|||||||
|
|
||||||
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.
|
||||||
|
|
||||||
!!! warning "Deprecated: `as_response`"
|
|
||||||
The `as_response=True` parameter is **deprecated** and will be removed in **v2.0**. Replace it with `schema=YourSchema`.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
[:material-api: API Reference](../reference/crud.md)
|
[:material-api: API Reference](../reference/crud.md)
|
||||||
|
|||||||
@@ -21,30 +21,37 @@ init_exceptions_handlers(app=app)
|
|||||||
This registers handlers for:
|
This registers handlers for:
|
||||||
|
|
||||||
- [`ApiException`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.ApiException) — all custom exceptions below
|
- [`ApiException`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.ApiException) — all custom exceptions below
|
||||||
|
- `HTTPException` — Starlette/FastAPI HTTP errors
|
||||||
- `RequestValidationError` — Pydantic request validation (422)
|
- `RequestValidationError` — Pydantic request validation (422)
|
||||||
- `ResponseValidationError` — Pydantic response validation (422)
|
- `ResponseValidationError` — Pydantic response validation (422)
|
||||||
- `Exception` — unhandled errors (500)
|
- `Exception` — unhandled errors (500)
|
||||||
|
|
||||||
|
It also patches `app.openapi()` to replace the default Pydantic 422 schema with a structured example matching the `ErrorResponse` format.
|
||||||
|
|
||||||
## Built-in exceptions
|
## Built-in exceptions
|
||||||
|
|
||||||
| Exception | Status | Default message |
|
| Exception | Status | Default message |
|
||||||
|-----------|--------|-----------------|
|
|-----------|--------|-----------------|
|
||||||
| [`UnauthorizedError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.UnauthorizedError) | 401 | Unauthorized |
|
| [`UnauthorizedError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.UnauthorizedError) | 401 | Unauthorized |
|
||||||
| [`ForbiddenError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.ForbiddenError) | 403 | Forbidden |
|
| [`ForbiddenError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.ForbiddenError) | 403 | Forbidden |
|
||||||
| [`NotFoundError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.NotFoundError) | 404 | Not found |
|
| [`NotFoundError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.NotFoundError) | 404 | Not Found |
|
||||||
| [`ConflictError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.ConflictError) | 409 | Conflict |
|
| [`ConflictError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.ConflictError) | 409 | Conflict |
|
||||||
| [`NoSearchableFieldsError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.NoSearchableFieldsError) | 400 | No searchable fields |
|
| [`NoSearchableFieldsError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.NoSearchableFieldsError) | 400 | No Searchable Fields |
|
||||||
| [`InvalidFacetFilterError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.InvalidFacetFilterError) | 400 | Invalid facet filter |
|
| [`InvalidFacetFilterError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.InvalidFacetFilterError) | 400 | Invalid Facet Filter |
|
||||||
|
| [`InvalidOrderFieldError`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.InvalidOrderFieldError) | 422 | Invalid Order Field |
|
||||||
|
|
||||||
|
### Per-instance overrides
|
||||||
|
|
||||||
|
All built-in exceptions accept optional keyword arguments to customise the response for a specific raise site without changing the class defaults:
|
||||||
|
|
||||||
|
| Argument | Effect |
|
||||||
|
|----------|--------|
|
||||||
|
| `detail` | Overrides both `str(exc)` (log output) and the `message` field in the response body |
|
||||||
|
| `desc` | Overrides the `description` field |
|
||||||
|
| `data` | Overrides the `data` field |
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from fastapi_toolsets.exceptions import NotFoundError
|
raise NotFoundError(detail="User 42 not found", desc="No user with that ID exists in the database.")
|
||||||
|
|
||||||
@router.get("/users/{id}")
|
|
||||||
async def get_user(id: int, session: AsyncSession = Depends(get_db)):
|
|
||||||
user = await UserCrud.first(session=session, filters=[User.id == id])
|
|
||||||
if not user:
|
|
||||||
raise NotFoundError
|
|
||||||
return user
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Custom exceptions
|
## Custom exceptions
|
||||||
@@ -58,12 +65,51 @@ from fastapi_toolsets.schemas import ApiError
|
|||||||
class PaymentRequiredError(ApiException):
|
class PaymentRequiredError(ApiException):
|
||||||
api_error = ApiError(
|
api_error = ApiError(
|
||||||
code=402,
|
code=402,
|
||||||
msg="Payment required",
|
msg="Payment Required",
|
||||||
desc="Your subscription has expired.",
|
desc="Your subscription has expired.",
|
||||||
err_code="PAYMENT_REQUIRED",
|
err_code="BILLING-402",
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
!!! warning
|
||||||
|
Subclasses that do not define `api_error` raise a `TypeError` at **class creation time**, not at raise time.
|
||||||
|
|
||||||
|
### Custom `__init__`
|
||||||
|
|
||||||
|
Override `__init__` to compute `detail`, `desc`, or `data` dynamically, then delegate to `super().__init__()`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class OrderValidationError(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=422,
|
||||||
|
msg="Order Validation Failed",
|
||||||
|
desc="One or more order fields are invalid.",
|
||||||
|
err_code="ORDER-422",
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, *field_errors: str) -> None:
|
||||||
|
super().__init__(
|
||||||
|
f"{len(field_errors)} validation error(s)",
|
||||||
|
desc=", ".join(field_errors),
|
||||||
|
data={"errors": [{"message": e} for e in field_errors]},
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Intermediate base classes
|
||||||
|
|
||||||
|
Use `abstract=True` when creating a shared base that is not meant to be raised directly:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class BillingError(ApiException, abstract=True):
|
||||||
|
"""Base for all billing-related errors."""
|
||||||
|
|
||||||
|
class PaymentRequiredError(BillingError):
|
||||||
|
api_error = ApiError(code=402, msg="Payment Required", desc="...", err_code="BILLING-402")
|
||||||
|
|
||||||
|
class SubscriptionExpiredError(BillingError):
|
||||||
|
api_error = ApiError(code=402, msg="Subscription Expired", desc="...", err_code="BILLING-402-EXP")
|
||||||
|
```
|
||||||
|
|
||||||
## OpenAPI response documentation
|
## OpenAPI response documentation
|
||||||
|
|
||||||
Use [`generate_error_responses`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.generate_error_responses) to add error schemas to your endpoint's OpenAPI spec:
|
Use [`generate_error_responses`](../reference/exceptions.md#fastapi_toolsets.exceptions.exceptions.generate_error_responses) to add error schemas to your endpoint's OpenAPI spec:
|
||||||
@@ -78,8 +124,7 @@ from fastapi_toolsets.exceptions import generate_error_responses, NotFoundError,
|
|||||||
async def get_user(...): ...
|
async def get_user(...): ...
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! info
|
Multiple exceptions sharing the same HTTP status code are grouped under one entry, each appearing as a named example keyed by its `err_code`. This keeps the OpenAPI UI readable when several error variants map to the same status.
|
||||||
The pydantic validation error is automatically added by FastAPI.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
113
docs/module/models.md
Normal file
113
docs/module/models.md
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
# Models
|
||||||
|
|
||||||
|
!!! info "Added in `v2.0`"
|
||||||
|
|
||||||
|
Reusable SQLAlchemy 2.0 mixins for common column patterns, designed to be composed freely on any `DeclarativeBase` model.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The `models` module provides mixins that each add a single, well-defined column behaviour. They work with standard SQLAlchemy 2.0 declarative syntax and are fully compatible with `AsyncSession`.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi_toolsets.models import UUIDMixin, TimestampMixin
|
||||||
|
|
||||||
|
class Article(Base, UUIDMixin, TimestampMixin):
|
||||||
|
__tablename__ = "articles"
|
||||||
|
|
||||||
|
title: Mapped[str]
|
||||||
|
content: Mapped[str]
|
||||||
|
```
|
||||||
|
|
||||||
|
All timestamp columns are timezone-aware (`TIMESTAMPTZ`). All defaults are server-side, so they are also applied when inserting rows via raw SQL outside the ORM.
|
||||||
|
|
||||||
|
## Mixins
|
||||||
|
|
||||||
|
### [`UUIDMixin`](../reference/models.md#fastapi_toolsets.models.UUIDMixin)
|
||||||
|
|
||||||
|
Adds a `id: UUID` primary key generated server-side by PostgreSQL using `gen_random_uuid()` (requires PostgreSQL 13+). The value is retrieved via `RETURNING` after insert, so it is available on the Python object immediately after `flush()`.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi_toolsets.models import UUIDMixin
|
||||||
|
|
||||||
|
class User(Base, UUIDMixin):
|
||||||
|
__tablename__ = "users"
|
||||||
|
|
||||||
|
username: Mapped[str]
|
||||||
|
|
||||||
|
# id is None before flush
|
||||||
|
user = User(username="alice")
|
||||||
|
await session.flush()
|
||||||
|
print(user.id) # UUID('...')
|
||||||
|
```
|
||||||
|
|
||||||
|
### [`CreatedAtMixin`](../reference/models.md#fastapi_toolsets.models.CreatedAtMixin)
|
||||||
|
|
||||||
|
Adds a `created_at: datetime` column set to `NOW()` on insert. The column has no `onupdate` hook — it is intentionally immutable after the row is created.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi_toolsets.models import UUIDMixin, CreatedAtMixin
|
||||||
|
|
||||||
|
class Order(Base, UUIDMixin, CreatedAtMixin):
|
||||||
|
__tablename__ = "orders"
|
||||||
|
|
||||||
|
total: Mapped[float]
|
||||||
|
```
|
||||||
|
|
||||||
|
### [`UpdatedAtMixin`](../reference/models.md#fastapi_toolsets.models.UpdatedAtMixin)
|
||||||
|
|
||||||
|
Adds an `updated_at: datetime` column set to `NOW()` on insert and automatically updated to `NOW()` on every ORM-level update (via SQLAlchemy's `onupdate` hook).
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi_toolsets.models import UUIDMixin, UpdatedAtMixin
|
||||||
|
|
||||||
|
class Post(Base, UUIDMixin, UpdatedAtMixin):
|
||||||
|
__tablename__ = "posts"
|
||||||
|
|
||||||
|
title: Mapped[str]
|
||||||
|
|
||||||
|
post = Post(title="Hello")
|
||||||
|
await session.flush()
|
||||||
|
await session.refresh(post)
|
||||||
|
|
||||||
|
post.title = "Hello World"
|
||||||
|
await session.flush()
|
||||||
|
await session.refresh(post)
|
||||||
|
print(post.updated_at)
|
||||||
|
```
|
||||||
|
|
||||||
|
!!! note
|
||||||
|
`updated_at` is updated by SQLAlchemy at ORM flush time. If you update rows via raw SQL (e.g. `UPDATE posts SET ...`), the column will **not** be updated automatically — use a database trigger if you need that guarantee.
|
||||||
|
|
||||||
|
### [`TimestampMixin`](../reference/models.md#fastapi_toolsets.models.TimestampMixin)
|
||||||
|
|
||||||
|
Convenience mixin that combines [`CreatedAtMixin`](../reference/models.md#fastapi_toolsets.models.CreatedAtMixin) and [`UpdatedAtMixin`](../reference/models.md#fastapi_toolsets.models.UpdatedAtMixin). Equivalent to inheriting both.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi_toolsets.models import UUIDMixin, TimestampMixin
|
||||||
|
|
||||||
|
class Article(Base, UUIDMixin, TimestampMixin):
|
||||||
|
__tablename__ = "articles"
|
||||||
|
|
||||||
|
title: Mapped[str]
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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)
|
||||||
@@ -22,16 +22,20 @@ async def get_user(user: User = UserDep) -> Response[UserSchema]:
|
|||||||
|
|
||||||
### [`PaginatedResponse[T]`](../reference/schemas.md#fastapi_toolsets.schemas.PaginatedResponse)
|
### [`PaginatedResponse[T]`](../reference/schemas.md#fastapi_toolsets.schemas.PaginatedResponse)
|
||||||
|
|
||||||
Wraps a list of items with pagination metadata and optional facet values.
|
Wraps a list of items with pagination metadata and optional facet values. The `pagination` field accepts either [`OffsetPagination`](../reference/schemas.md#fastapi_toolsets.schemas.OffsetPagination) or [`CursorPagination`](../reference/schemas.md#fastapi_toolsets.schemas.CursorPagination) depending on the strategy used.
|
||||||
|
|
||||||
|
#### [`OffsetPagination`](../reference/schemas.md#fastapi_toolsets.schemas.OffsetPagination)
|
||||||
|
|
||||||
|
Page-number based. Requires `total_count` so clients can compute the total number of pages.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from fastapi_toolsets.schemas import PaginatedResponse, Pagination
|
from fastapi_toolsets.schemas import PaginatedResponse, OffsetPagination
|
||||||
|
|
||||||
@router.get("/users")
|
@router.get("/users")
|
||||||
async def list_users() -> PaginatedResponse[UserSchema]:
|
async def list_users() -> PaginatedResponse[UserSchema]:
|
||||||
return PaginatedResponse(
|
return PaginatedResponse(
|
||||||
data=users,
|
data=users,
|
||||||
pagination=Pagination(
|
pagination=OffsetPagination(
|
||||||
total_count=100,
|
total_count=100,
|
||||||
items_per_page=10,
|
items_per_page=10,
|
||||||
page=1,
|
page=1,
|
||||||
@@ -40,6 +44,26 @@ async def list_users() -> PaginatedResponse[UserSchema]:
|
|||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### [`CursorPagination`](../reference/schemas.md#fastapi_toolsets.schemas.CursorPagination)
|
||||||
|
|
||||||
|
Cursor based. Efficient for large or frequently updated datasets where offset pagination is impractical. Provides opaque `next_cursor` / `prev_cursor` tokens; no total count is exposed.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi_toolsets.schemas import PaginatedResponse, CursorPagination
|
||||||
|
|
||||||
|
@router.get("/events")
|
||||||
|
async def list_events() -> PaginatedResponse[EventSchema]:
|
||||||
|
return PaginatedResponse(
|
||||||
|
data=events,
|
||||||
|
pagination=CursorPagination(
|
||||||
|
next_cursor="eyJpZCI6IDQyfQ==",
|
||||||
|
prev_cursor=None,
|
||||||
|
items_per_page=20,
|
||||||
|
has_more=True,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
The optional `filter_attributes` field is populated when `facet_fields` are configured on the CRUD class (see [Filter attributes](crud.md#filter-attributes-facets)). It is `None` by default and can be hidden from API responses with `response_model_exclude_none=True`.
|
The optional `filter_attributes` field is populated when `facet_fields` are configured on the CRUD class (see [Filter attributes](crud.md#filter-attributes-facets)). It is `None` by default and can be hidden from API responses with `response_model_exclude_none=True`.
|
||||||
|
|
||||||
### [`ErrorResponse`](../reference/schemas.md#fastapi_toolsets.schemas.ErrorResponse)
|
### [`ErrorResponse`](../reference/schemas.md#fastapi_toolsets.schemas.ErrorResponse)
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ from fastapi_toolsets.exceptions import (
|
|||||||
ConflictError,
|
ConflictError,
|
||||||
NoSearchableFieldsError,
|
NoSearchableFieldsError,
|
||||||
InvalidFacetFilterError,
|
InvalidFacetFilterError,
|
||||||
|
InvalidOrderFieldError,
|
||||||
generate_error_responses,
|
generate_error_responses,
|
||||||
init_exceptions_handlers,
|
init_exceptions_handlers,
|
||||||
)
|
)
|
||||||
@@ -32,6 +33,8 @@ from fastapi_toolsets.exceptions import (
|
|||||||
|
|
||||||
## ::: fastapi_toolsets.exceptions.exceptions.InvalidFacetFilterError
|
## ::: fastapi_toolsets.exceptions.exceptions.InvalidFacetFilterError
|
||||||
|
|
||||||
|
## ::: fastapi_toolsets.exceptions.exceptions.InvalidOrderFieldError
|
||||||
|
|
||||||
## ::: fastapi_toolsets.exceptions.exceptions.generate_error_responses
|
## ::: fastapi_toolsets.exceptions.exceptions.generate_error_responses
|
||||||
|
|
||||||
## ::: fastapi_toolsets.exceptions.handler.init_exceptions_handlers
|
## ::: fastapi_toolsets.exceptions.handler.init_exceptions_handlers
|
||||||
|
|||||||
22
docs/reference/models.md
Normal file
22
docs/reference/models.md
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# `models`
|
||||||
|
|
||||||
|
Here's the reference for the SQLAlchemy model mixins provided by the `models` module.
|
||||||
|
|
||||||
|
You can import them directly from `fastapi_toolsets.models`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi_toolsets.models import (
|
||||||
|
UUIDMixin,
|
||||||
|
CreatedAtMixin,
|
||||||
|
UpdatedAtMixin,
|
||||||
|
TimestampMixin,
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
## ::: fastapi_toolsets.models.UUIDMixin
|
||||||
|
|
||||||
|
## ::: fastapi_toolsets.models.CreatedAtMixin
|
||||||
|
|
||||||
|
## ::: fastapi_toolsets.models.UpdatedAtMixin
|
||||||
|
|
||||||
|
## ::: fastapi_toolsets.models.TimestampMixin
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
# `schemas` module
|
# `schemas`
|
||||||
|
|
||||||
Here's the reference for all response models and types provided by the `schemas` module.
|
Here's the reference for all response models and types provided by the `schemas` module.
|
||||||
|
|
||||||
@@ -12,7 +12,8 @@ from fastapi_toolsets.schemas import (
|
|||||||
BaseResponse,
|
BaseResponse,
|
||||||
Response,
|
Response,
|
||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
Pagination,
|
OffsetPagination,
|
||||||
|
CursorPagination,
|
||||||
PaginatedResponse,
|
PaginatedResponse,
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
@@ -29,6 +30,8 @@ from fastapi_toolsets.schemas import (
|
|||||||
|
|
||||||
## ::: fastapi_toolsets.schemas.ErrorResponse
|
## ::: fastapi_toolsets.schemas.ErrorResponse
|
||||||
|
|
||||||
## ::: fastapi_toolsets.schemas.Pagination
|
## ::: fastapi_toolsets.schemas.OffsetPagination
|
||||||
|
|
||||||
|
## ::: fastapi_toolsets.schemas.CursorPagination
|
||||||
|
|
||||||
## ::: fastapi_toolsets.schemas.PaginatedResponse
|
## ::: fastapi_toolsets.schemas.PaginatedResponse
|
||||||
|
|||||||
0
docs_src/__init__.py
Normal file
0
docs_src/__init__.py
Normal file
0
docs_src/examples/__init__.py
Normal file
0
docs_src/examples/__init__.py
Normal file
0
docs_src/examples/pagination_search/__init__.py
Normal file
0
docs_src/examples/pagination_search/__init__.py
Normal file
9
docs_src/examples/pagination_search/app.py
Normal file
9
docs_src/examples/pagination_search/app.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
from fastapi_toolsets.exceptions import init_exceptions_handlers
|
||||||
|
|
||||||
|
from .routes import router
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app=app)
|
||||||
|
app.include_router(router=router)
|
||||||
21
docs_src/examples/pagination_search/crud.py
Normal file
21
docs_src/examples/pagination_search/crud.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
from fastapi_toolsets.crud import CrudFactory
|
||||||
|
|
||||||
|
from .models import Article, Category
|
||||||
|
|
||||||
|
ArticleCrud = CrudFactory(
|
||||||
|
model=Article,
|
||||||
|
cursor_column=Article.created_at,
|
||||||
|
searchable_fields=[ # default fields for full-text search
|
||||||
|
Article.title,
|
||||||
|
Article.body,
|
||||||
|
(Article.category, Category.name),
|
||||||
|
],
|
||||||
|
facet_fields=[ # fields exposed as filter dropdowns
|
||||||
|
Article.status,
|
||||||
|
(Article.category, Category.name),
|
||||||
|
],
|
||||||
|
order_fields=[ # fields exposed for client-driven ordering
|
||||||
|
Article.title,
|
||||||
|
Article.created_at,
|
||||||
|
],
|
||||||
|
)
|
||||||
17
docs_src/examples/pagination_search/db.py
Normal file
17
docs_src/examples/pagination_search/db.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
from fastapi import Depends
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||||
|
|
||||||
|
from fastapi_toolsets.db import create_db_context, create_db_dependency
|
||||||
|
|
||||||
|
DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost:5432/postgres"
|
||||||
|
|
||||||
|
engine = create_async_engine(url=DATABASE_URL, future=True)
|
||||||
|
async_session_maker = async_sessionmaker(bind=engine, expire_on_commit=False)
|
||||||
|
|
||||||
|
get_db = create_db_dependency(session_maker=async_session_maker)
|
||||||
|
get_db_context = create_db_context(session_maker=async_session_maker)
|
||||||
|
|
||||||
|
|
||||||
|
SessionDep = Annotated[AsyncSession, Depends(get_db)]
|
||||||
36
docs_src/examples/pagination_search/models.py
Normal file
36
docs_src/examples/pagination_search/models.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import datetime
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from sqlalchemy import Boolean, DateTime, ForeignKey, String, Text, func
|
||||||
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
|
||||||
|
class Base(DeclarativeBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Category(Base):
|
||||||
|
__tablename__ = "categories"
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||||
|
name: Mapped[str] = mapped_column(String(64), unique=True)
|
||||||
|
|
||||||
|
articles: Mapped[list["Article"]] = relationship(back_populates="category")
|
||||||
|
|
||||||
|
|
||||||
|
class Article(Base):
|
||||||
|
__tablename__ = "articles"
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||||
|
created_at: Mapped[datetime.datetime] = mapped_column(
|
||||||
|
DateTime(timezone=True), server_default=func.now()
|
||||||
|
)
|
||||||
|
title: Mapped[str] = mapped_column(String(256))
|
||||||
|
body: Mapped[str] = mapped_column(Text)
|
||||||
|
status: Mapped[str] = mapped_column(String(32))
|
||||||
|
published: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||||
|
category_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||||
|
ForeignKey("categories.id"), nullable=True
|
||||||
|
)
|
||||||
|
|
||||||
|
category: Mapped["Category | None"] = relationship(back_populates="articles")
|
||||||
59
docs_src/examples/pagination_search/routes.py
Normal file
59
docs_src/examples/pagination_search/routes.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, Query
|
||||||
|
|
||||||
|
from fastapi_toolsets.crud import OrderByClause
|
||||||
|
from fastapi_toolsets.schemas import PaginatedResponse
|
||||||
|
|
||||||
|
from .crud import ArticleCrud
|
||||||
|
from .db import SessionDep
|
||||||
|
from .models import Article
|
||||||
|
from .schemas import ArticleRead
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/articles")
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/offset")
|
||||||
|
async def list_articles_offset(
|
||||||
|
session: SessionDep,
|
||||||
|
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)),
|
||||||
|
],
|
||||||
|
page: int = Query(1, ge=1),
|
||||||
|
items_per_page: int = Query(20, ge=1, le=100),
|
||||||
|
search: str | None = None,
|
||||||
|
) -> PaginatedResponse[ArticleRead]:
|
||||||
|
return await ArticleCrud.offset_paginate(
|
||||||
|
session=session,
|
||||||
|
page=page,
|
||||||
|
items_per_page=items_per_page,
|
||||||
|
search=search,
|
||||||
|
filter_by=filter_by or None,
|
||||||
|
order_by=order_by,
|
||||||
|
schema=ArticleRead,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/cursor")
|
||||||
|
async def list_articles_cursor(
|
||||||
|
session: SessionDep,
|
||||||
|
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)),
|
||||||
|
],
|
||||||
|
cursor: str | None = None,
|
||||||
|
items_per_page: int = Query(20, ge=1, le=100),
|
||||||
|
search: str | None = None,
|
||||||
|
) -> PaginatedResponse[ArticleRead]:
|
||||||
|
return await ArticleCrud.cursor_paginate(
|
||||||
|
session=session,
|
||||||
|
cursor=cursor,
|
||||||
|
items_per_page=items_per_page,
|
||||||
|
search=search,
|
||||||
|
filter_by=filter_by or None,
|
||||||
|
order_by=order_by,
|
||||||
|
schema=ArticleRead,
|
||||||
|
)
|
||||||
13
docs_src/examples/pagination_search/schemas.py
Normal file
13
docs_src/examples/pagination_search/schemas.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import datetime
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from fastapi_toolsets.schemas import PydanticBase
|
||||||
|
|
||||||
|
|
||||||
|
class ArticleRead(PydanticBase):
|
||||||
|
id: uuid.UUID
|
||||||
|
created_at: datetime.datetime
|
||||||
|
title: str
|
||||||
|
status: str
|
||||||
|
published: bool
|
||||||
|
category_id: uuid.UUID | None
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "fastapi-toolsets"
|
name = "fastapi-toolsets"
|
||||||
version = "1.2.0"
|
version = "2.0.0"
|
||||||
description = "Reusable tools for FastAPI: async CRUD, fixtures, CLI, and standardized responses for SQLAlchemy + PostgreSQL"
|
description = "Production-ready utilities for FastAPI applications"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
license-files = ["LICENSE"]
|
license-files = ["LICENSE"]
|
||||||
@@ -11,7 +11,7 @@ authors = [
|
|||||||
]
|
]
|
||||||
keywords = ["fastapi", "sqlalchemy", "postgresql"]
|
keywords = ["fastapi", "sqlalchemy", "postgresql"]
|
||||||
classifiers = [
|
classifiers = [
|
||||||
"Development Status :: 4 - Beta",
|
"Development Status :: 5 - Production/Stable",
|
||||||
"Framework :: AsyncIO",
|
"Framework :: AsyncIO",
|
||||||
"Framework :: FastAPI",
|
"Framework :: FastAPI",
|
||||||
"Framework :: Pydantic",
|
"Framework :: Pydantic",
|
||||||
|
|||||||
@@ -21,4 +21,4 @@ Example usage:
|
|||||||
return Response(data={"user": user.username}, message="Success")
|
return Response(data={"user": user.username}, message="Success")
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version__ = "1.2.0"
|
__version__ = "2.0.0"
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ async def load(
|
|||||||
registry = get_fixtures_registry()
|
registry = get_fixtures_registry()
|
||||||
db_context = get_db_context()
|
db_context = get_db_context()
|
||||||
|
|
||||||
context_list = [c.value for c in contexts] if contexts else [Context.BASE]
|
context_list = list(contexts) if contexts else [Context.BASE]
|
||||||
|
|
||||||
ordered = registry.resolve_context_dependencies(*context_list)
|
ordered = registry.resolve_context_dependencies(*context_list)
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
"""Generic async CRUD operations for SQLAlchemy models."""
|
"""Generic async CRUD operations for SQLAlchemy models."""
|
||||||
|
|
||||||
from ..exceptions import InvalidFacetFilterError, NoSearchableFieldsError
|
from ..exceptions import InvalidFacetFilterError, NoSearchableFieldsError
|
||||||
from .factory import CrudFactory, JoinType, M2MFieldType
|
from ..types import (
|
||||||
from .search import (
|
|
||||||
FacetFieldType,
|
FacetFieldType,
|
||||||
SearchConfig,
|
JoinType,
|
||||||
get_searchable_fields,
|
M2MFieldType,
|
||||||
|
OrderByClause,
|
||||||
|
SearchFieldType,
|
||||||
)
|
)
|
||||||
|
from .factory import CrudFactory
|
||||||
|
from .search import SearchConfig, get_searchable_fields
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"CrudFactory",
|
"CrudFactory",
|
||||||
@@ -16,5 +19,7 @@ __all__ = [
|
|||||||
"JoinType",
|
"JoinType",
|
||||||
"M2MFieldType",
|
"M2MFieldType",
|
||||||
"NoSearchableFieldsError",
|
"NoSearchableFieldsError",
|
||||||
|
"OrderByClause",
|
||||||
"SearchConfig",
|
"SearchConfig",
|
||||||
|
"SearchFieldType",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -6,11 +6,10 @@ import base64
|
|||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
import uuid as uuid_module
|
import uuid as uuid_module
|
||||||
import warnings
|
from collections.abc import Awaitable, Callable, Sequence
|
||||||
from collections.abc import Awaitable, Callable, Mapping, Sequence
|
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from typing import Any, ClassVar, Generic, Literal, Self, TypeVar, cast, overload
|
from typing import Any, ClassVar, Generic, Literal, Self, cast, overload
|
||||||
|
|
||||||
from fastapi import Query
|
from fastapi import Query
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -24,23 +23,25 @@ from sqlalchemy.sql.base import ExecutableOption
|
|||||||
from sqlalchemy.sql.roles import WhereHavingRole
|
from sqlalchemy.sql.roles import WhereHavingRole
|
||||||
|
|
||||||
from ..db import get_transaction
|
from ..db import get_transaction
|
||||||
from ..exceptions import NotFoundError
|
from ..exceptions import InvalidOrderFieldError, NotFoundError
|
||||||
from ..schemas import CursorPagination, OffsetPagination, PaginatedResponse, Response
|
from ..schemas import CursorPagination, OffsetPagination, PaginatedResponse, Response
|
||||||
from .search import (
|
from ..types import (
|
||||||
FacetFieldType,
|
FacetFieldType,
|
||||||
SearchConfig,
|
JoinType,
|
||||||
|
M2MFieldType,
|
||||||
|
ModelType,
|
||||||
|
OrderByClause,
|
||||||
|
SchemaType,
|
||||||
SearchFieldType,
|
SearchFieldType,
|
||||||
|
)
|
||||||
|
from .search import (
|
||||||
|
SearchConfig,
|
||||||
build_facets,
|
build_facets,
|
||||||
build_filter_by,
|
build_filter_by,
|
||||||
build_search_filters,
|
build_search_filters,
|
||||||
facet_keys,
|
facet_keys,
|
||||||
)
|
)
|
||||||
|
|
||||||
ModelType = TypeVar("ModelType", bound=DeclarativeBase)
|
|
||||||
SchemaType = TypeVar("SchemaType", bound=BaseModel)
|
|
||||||
JoinType = list[tuple[type[DeclarativeBase], Any]]
|
|
||||||
M2MFieldType = Mapping[str, QueryableAttribute[Any]]
|
|
||||||
|
|
||||||
|
|
||||||
def _encode_cursor(value: Any) -> str:
|
def _encode_cursor(value: Any) -> str:
|
||||||
"""Encode cursor column value as an base64 string."""
|
"""Encode cursor column value as an base64 string."""
|
||||||
@@ -52,6 +53,22 @@ def _decode_cursor(cursor: str) -> str:
|
|||||||
return json.loads(base64.b64decode(cursor.encode()).decode())
|
return json.loads(base64.b64decode(cursor.encode()).decode())
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_joins(q: Any, joins: JoinType | None, outer_join: bool) -> Any:
|
||||||
|
"""Apply a list of (model, condition) joins to a SQLAlchemy select query."""
|
||||||
|
if not joins:
|
||||||
|
return q
|
||||||
|
for model, condition in joins:
|
||||||
|
q = q.outerjoin(model, condition) if outer_join else q.join(model, condition)
|
||||||
|
return q
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_search_joins(q: Any, search_joins: list[Any]) -> Any:
|
||||||
|
"""Apply relationship-based outer joins (from search/filter_by) to a query."""
|
||||||
|
for join_rel in search_joins:
|
||||||
|
q = q.outerjoin(join_rel)
|
||||||
|
return q
|
||||||
|
|
||||||
|
|
||||||
class AsyncCrud(Generic[ModelType]):
|
class AsyncCrud(Generic[ModelType]):
|
||||||
"""Generic async CRUD operations for SQLAlchemy models.
|
"""Generic async CRUD operations for SQLAlchemy models.
|
||||||
|
|
||||||
@@ -61,6 +78,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[QueryableAttribute[Any]] | None] = None
|
||||||
m2m_fields: ClassVar[M2MFieldType | None] = None
|
m2m_fields: ClassVar[M2MFieldType | None] = None
|
||||||
default_load_options: ClassVar[list[ExecutableOption] | None] = None
|
default_load_options: ClassVar[list[ExecutableOption] | None] = None
|
||||||
cursor_column: ClassVar[Any | None] = None
|
cursor_column: ClassVar[Any | None] = None
|
||||||
@@ -130,6 +148,48 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
return set()
|
return set()
|
||||||
return set(cls.m2m_fields.keys())
|
return set(cls.m2m_fields.keys())
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _resolve_facet_fields(
|
||||||
|
cls: type[Self],
|
||||||
|
facet_fields: Sequence[FacetFieldType] | None,
|
||||||
|
) -> Sequence[FacetFieldType] | None:
|
||||||
|
"""Return facet_fields if given, otherwise fall back to the class-level default."""
|
||||||
|
return facet_fields if facet_fields is not None else cls.facet_fields
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _prepare_filter_by(
|
||||||
|
cls: type[Self],
|
||||||
|
filter_by: dict[str, Any] | BaseModel | None,
|
||||||
|
facet_fields: Sequence[FacetFieldType] | None,
|
||||||
|
) -> tuple[list[Any], list[Any]]:
|
||||||
|
"""Normalize filter_by and return (filters, joins) to apply to the query."""
|
||||||
|
if isinstance(filter_by, BaseModel):
|
||||||
|
filter_by = filter_by.model_dump(exclude_none=True)
|
||||||
|
if not filter_by:
|
||||||
|
return [], []
|
||||||
|
resolved = cls._resolve_facet_fields(facet_fields)
|
||||||
|
return build_filter_by(filter_by, resolved or [])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def _build_filter_attributes(
|
||||||
|
cls: type[Self],
|
||||||
|
session: AsyncSession,
|
||||||
|
facet_fields: Sequence[FacetFieldType] | None,
|
||||||
|
filters: list[Any],
|
||||||
|
search_joins: list[Any],
|
||||||
|
) -> dict[str, list[Any]] | None:
|
||||||
|
"""Build facet filter_attributes, or return None if no facet fields configured."""
|
||||||
|
resolved = cls._resolve_facet_fields(facet_fields)
|
||||||
|
if not resolved:
|
||||||
|
return None
|
||||||
|
return await build_facets(
|
||||||
|
session,
|
||||||
|
cls.model,
|
||||||
|
resolved,
|
||||||
|
base_filters=filters,
|
||||||
|
base_joins=search_joins,
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def filter_params(
|
def filter_params(
|
||||||
cls: type[Self],
|
cls: type[Self],
|
||||||
@@ -150,7 +210,7 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
ValueError: If no facet fields are configured on this CRUD class and none are
|
ValueError: If no facet fields are configured on this CRUD class and none are
|
||||||
provided via ``facet_fields``.
|
provided via ``facet_fields``.
|
||||||
"""
|
"""
|
||||||
fields = facet_fields if facet_fields is not None else cls.facet_fields
|
fields = cls._resolve_facet_fields(facet_fields)
|
||||||
if not fields:
|
if not fields:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"{cls.__name__} has no facet_fields configured. "
|
f"{cls.__name__} has no facet_fields configured. "
|
||||||
@@ -176,6 +236,63 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
|
|
||||||
return dependency
|
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(
|
||||||
|
default_order, description="Sort direction"
|
||||||
|
),
|
||||||
|
) -> OrderByClause | None:
|
||||||
|
if order_by is None:
|
||||||
|
if default_field is None:
|
||||||
|
return None
|
||||||
|
field = default_field
|
||||||
|
elif order_by not in field_map:
|
||||||
|
raise InvalidOrderFieldError(order_by, valid_keys)
|
||||||
|
else:
|
||||||
|
field = field_map[order_by]
|
||||||
|
return field.asc() if order == "asc" else field.desc()
|
||||||
|
|
||||||
|
dependency.__name__ = f"{cls.model.__name__}OrderParams"
|
||||||
|
return dependency
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
@classmethod
|
@classmethod
|
||||||
async def create( # pragma: no cover
|
async def create( # pragma: no cover
|
||||||
@@ -184,10 +301,8 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
obj: BaseModel,
|
obj: BaseModel,
|
||||||
*,
|
*,
|
||||||
schema: type[SchemaType],
|
schema: type[SchemaType],
|
||||||
as_response: bool = ...,
|
|
||||||
) -> Response[SchemaType]: ...
|
) -> Response[SchemaType]: ...
|
||||||
|
|
||||||
# Backward-compatible - will be removed in v2.0
|
|
||||||
@overload
|
@overload
|
||||||
@classmethod
|
@classmethod
|
||||||
async def create( # pragma: no cover
|
async def create( # pragma: no cover
|
||||||
@@ -195,18 +310,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
obj: BaseModel,
|
obj: BaseModel,
|
||||||
*,
|
*,
|
||||||
as_response: Literal[True],
|
|
||||||
schema: None = ...,
|
|
||||||
) -> Response[ModelType]: ...
|
|
||||||
|
|
||||||
@overload
|
|
||||||
@classmethod
|
|
||||||
async def create( # pragma: no cover
|
|
||||||
cls: type[Self],
|
|
||||||
session: AsyncSession,
|
|
||||||
obj: BaseModel,
|
|
||||||
*,
|
|
||||||
as_response: Literal[False] = ...,
|
|
||||||
schema: None = ...,
|
schema: None = ...,
|
||||||
) -> ModelType: ...
|
) -> ModelType: ...
|
||||||
|
|
||||||
@@ -216,29 +319,19 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
obj: BaseModel,
|
obj: BaseModel,
|
||||||
*,
|
*,
|
||||||
as_response: bool = False,
|
|
||||||
schema: type[BaseModel] | None = None,
|
schema: type[BaseModel] | None = None,
|
||||||
) -> ModelType | Response[ModelType] | Response[Any]:
|
) -> ModelType | Response[Any]:
|
||||||
"""Create a new record in the database.
|
"""Create a new record in the database.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
session: DB async session
|
session: DB async session
|
||||||
obj: Pydantic model with data to create
|
obj: Pydantic model with data to create
|
||||||
as_response: Deprecated. Use ``schema`` instead. Will be removed in v2.0.
|
|
||||||
schema: Pydantic schema to serialize the result into. When provided,
|
schema: Pydantic schema to serialize the result into. When provided,
|
||||||
the result is automatically wrapped in a ``Response[schema]``.
|
the result is automatically wrapped in a ``Response[schema]``.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Created model instance, or ``Response[schema]`` when ``schema`` is given,
|
Created model instance, or ``Response[schema]`` when ``schema`` is given.
|
||||||
or ``Response[ModelType]`` when ``as_response=True`` (deprecated).
|
|
||||||
"""
|
"""
|
||||||
if as_response and schema is None:
|
|
||||||
warnings.warn(
|
|
||||||
"as_response is deprecated and will be removed in v2.0. "
|
|
||||||
"Use schema=YourSchema instead.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
async with get_transaction(session):
|
async with get_transaction(session):
|
||||||
m2m_exclude = cls._m2m_schema_fields()
|
m2m_exclude = cls._m2m_schema_fields()
|
||||||
data = (
|
data = (
|
||||||
@@ -254,9 +347,8 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
session.add(db_model)
|
session.add(db_model)
|
||||||
await session.refresh(db_model)
|
await session.refresh(db_model)
|
||||||
result = cast(ModelType, db_model)
|
result = cast(ModelType, db_model)
|
||||||
if as_response or schema:
|
if schema:
|
||||||
data_out = schema.model_validate(result) if schema else result
|
return Response(data=schema.model_validate(result))
|
||||||
return Response(data=data_out)
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
@@ -271,10 +363,8 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
with_for_update: bool = False,
|
with_for_update: bool = False,
|
||||||
load_options: list[ExecutableOption] | None = None,
|
load_options: list[ExecutableOption] | None = None,
|
||||||
schema: type[SchemaType],
|
schema: type[SchemaType],
|
||||||
as_response: bool = ...,
|
|
||||||
) -> Response[SchemaType]: ...
|
) -> Response[SchemaType]: ...
|
||||||
|
|
||||||
# Backward-compatible - will be removed in v2.0
|
|
||||||
@overload
|
@overload
|
||||||
@classmethod
|
@classmethod
|
||||||
async def get( # pragma: no cover
|
async def get( # pragma: no cover
|
||||||
@@ -286,22 +376,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
outer_join: bool = False,
|
outer_join: bool = False,
|
||||||
with_for_update: bool = False,
|
with_for_update: bool = False,
|
||||||
load_options: list[ExecutableOption] | None = None,
|
load_options: list[ExecutableOption] | None = None,
|
||||||
as_response: Literal[True],
|
|
||||||
schema: None = ...,
|
|
||||||
) -> Response[ModelType]: ...
|
|
||||||
|
|
||||||
@overload
|
|
||||||
@classmethod
|
|
||||||
async def get( # pragma: no cover
|
|
||||||
cls: type[Self],
|
|
||||||
session: AsyncSession,
|
|
||||||
filters: list[Any],
|
|
||||||
*,
|
|
||||||
joins: JoinType | None = None,
|
|
||||||
outer_join: bool = False,
|
|
||||||
with_for_update: bool = False,
|
|
||||||
load_options: list[ExecutableOption] | None = None,
|
|
||||||
as_response: Literal[False] = ...,
|
|
||||||
schema: None = ...,
|
schema: None = ...,
|
||||||
) -> ModelType: ...
|
) -> ModelType: ...
|
||||||
|
|
||||||
@@ -315,9 +389,8 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
outer_join: bool = False,
|
outer_join: bool = False,
|
||||||
with_for_update: bool = False,
|
with_for_update: bool = False,
|
||||||
load_options: list[ExecutableOption] | None = None,
|
load_options: list[ExecutableOption] | None = None,
|
||||||
as_response: bool = False,
|
|
||||||
schema: type[BaseModel] | None = None,
|
schema: type[BaseModel] | None = None,
|
||||||
) -> ModelType | Response[ModelType] | Response[Any]:
|
) -> ModelType | Response[Any]:
|
||||||
"""Get exactly one record. Raises NotFoundError if not found.
|
"""Get exactly one record. Raises NotFoundError if not found.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -327,33 +400,18 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
outer_join: Use LEFT OUTER JOIN instead of INNER JOIN
|
outer_join: Use LEFT OUTER JOIN instead of INNER JOIN
|
||||||
with_for_update: Lock the row for update
|
with_for_update: Lock the row for update
|
||||||
load_options: SQLAlchemy loader options (e.g., selectinload)
|
load_options: SQLAlchemy loader options (e.g., selectinload)
|
||||||
as_response: Deprecated. Use ``schema`` instead. Will be removed in v2.0.
|
|
||||||
schema: Pydantic schema to serialize the result into. When provided,
|
schema: Pydantic schema to serialize the result into. When provided,
|
||||||
the result is automatically wrapped in a ``Response[schema]``.
|
the result is automatically wrapped in a ``Response[schema]``.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Model instance, or ``Response[schema]`` when ``schema`` is given,
|
Model instance, or ``Response[schema]`` when ``schema`` is given.
|
||||||
or ``Response[ModelType]`` when ``as_response=True`` (deprecated).
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
NotFoundError: If no record found
|
NotFoundError: If no record found
|
||||||
MultipleResultsFound: If more than one record found
|
MultipleResultsFound: If more than one record found
|
||||||
"""
|
"""
|
||||||
if as_response and schema is None:
|
|
||||||
warnings.warn(
|
|
||||||
"as_response is deprecated and will be removed in v2.0. "
|
|
||||||
"Use schema=YourSchema instead.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
q = select(cls.model)
|
q = select(cls.model)
|
||||||
if joins:
|
q = _apply_joins(q, joins, outer_join)
|
||||||
for model, condition in joins:
|
|
||||||
q = (
|
|
||||||
q.outerjoin(model, condition)
|
|
||||||
if outer_join
|
|
||||||
else q.join(model, condition)
|
|
||||||
)
|
|
||||||
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):
|
||||||
q = q.options(*resolved)
|
q = q.options(*resolved)
|
||||||
@@ -364,9 +422,8 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
if not item:
|
if not item:
|
||||||
raise NotFoundError()
|
raise NotFoundError()
|
||||||
result = cast(ModelType, item)
|
result = cast(ModelType, item)
|
||||||
if as_response or schema:
|
if schema:
|
||||||
data_out = schema.model_validate(result) if schema else result
|
return Response(data=schema.model_validate(result))
|
||||||
return Response(data=data_out)
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -392,13 +449,7 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
Model instance or None
|
Model instance or None
|
||||||
"""
|
"""
|
||||||
q = select(cls.model)
|
q = select(cls.model)
|
||||||
if joins:
|
q = _apply_joins(q, joins, outer_join)
|
||||||
for model, condition in joins:
|
|
||||||
q = (
|
|
||||||
q.outerjoin(model, condition)
|
|
||||||
if outer_join
|
|
||||||
else q.join(model, condition)
|
|
||||||
)
|
|
||||||
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):
|
||||||
@@ -415,7 +466,7 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
joins: JoinType | None = None,
|
joins: JoinType | None = None,
|
||||||
outer_join: bool = False,
|
outer_join: bool = False,
|
||||||
load_options: list[ExecutableOption] | None = None,
|
load_options: list[ExecutableOption] | None = None,
|
||||||
order_by: Any | None = None,
|
order_by: OrderByClause | None = None,
|
||||||
limit: int | None = None,
|
limit: int | None = None,
|
||||||
offset: int | None = None,
|
offset: int | None = None,
|
||||||
) -> Sequence[ModelType]:
|
) -> Sequence[ModelType]:
|
||||||
@@ -435,13 +486,7 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
List of model instances
|
List of model instances
|
||||||
"""
|
"""
|
||||||
q = select(cls.model)
|
q = select(cls.model)
|
||||||
if joins:
|
q = _apply_joins(q, joins, outer_join)
|
||||||
for model, condition in joins:
|
|
||||||
q = (
|
|
||||||
q.outerjoin(model, condition)
|
|
||||||
if outer_join
|
|
||||||
else q.join(model, condition)
|
|
||||||
)
|
|
||||||
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):
|
||||||
@@ -466,10 +511,8 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
exclude_unset: bool = True,
|
exclude_unset: bool = True,
|
||||||
exclude_none: bool = False,
|
exclude_none: bool = False,
|
||||||
schema: type[SchemaType],
|
schema: type[SchemaType],
|
||||||
as_response: bool = ...,
|
|
||||||
) -> Response[SchemaType]: ...
|
) -> Response[SchemaType]: ...
|
||||||
|
|
||||||
# Backward-compatible - will be removed in v2.0
|
|
||||||
@overload
|
@overload
|
||||||
@classmethod
|
@classmethod
|
||||||
async def update( # pragma: no cover
|
async def update( # pragma: no cover
|
||||||
@@ -480,21 +523,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
*,
|
*,
|
||||||
exclude_unset: bool = True,
|
exclude_unset: bool = True,
|
||||||
exclude_none: bool = False,
|
exclude_none: bool = False,
|
||||||
as_response: Literal[True],
|
|
||||||
schema: None = ...,
|
|
||||||
) -> Response[ModelType]: ...
|
|
||||||
|
|
||||||
@overload
|
|
||||||
@classmethod
|
|
||||||
async def update( # pragma: no cover
|
|
||||||
cls: type[Self],
|
|
||||||
session: AsyncSession,
|
|
||||||
obj: BaseModel,
|
|
||||||
filters: list[Any],
|
|
||||||
*,
|
|
||||||
exclude_unset: bool = True,
|
|
||||||
exclude_none: bool = False,
|
|
||||||
as_response: Literal[False] = ...,
|
|
||||||
schema: None = ...,
|
schema: None = ...,
|
||||||
) -> ModelType: ...
|
) -> ModelType: ...
|
||||||
|
|
||||||
@@ -507,9 +535,8 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
*,
|
*,
|
||||||
exclude_unset: bool = True,
|
exclude_unset: bool = True,
|
||||||
exclude_none: bool = False,
|
exclude_none: bool = False,
|
||||||
as_response: bool = False,
|
|
||||||
schema: type[BaseModel] | None = None,
|
schema: type[BaseModel] | None = None,
|
||||||
) -> ModelType | Response[ModelType] | Response[Any]:
|
) -> ModelType | Response[Any]:
|
||||||
"""Update a record in the database.
|
"""Update a record in the database.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -518,24 +545,15 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
filters: List of SQLAlchemy filter conditions
|
filters: List of SQLAlchemy filter conditions
|
||||||
exclude_unset: Exclude fields not explicitly set in the schema
|
exclude_unset: Exclude fields not explicitly set in the schema
|
||||||
exclude_none: Exclude fields with None value
|
exclude_none: Exclude fields with None value
|
||||||
as_response: Deprecated. Use ``schema`` instead. Will be removed in v2.0.
|
|
||||||
schema: Pydantic schema to serialize the result into. When provided,
|
schema: Pydantic schema to serialize the result into. When provided,
|
||||||
the result is automatically wrapped in a ``Response[schema]``.
|
the result is automatically wrapped in a ``Response[schema]``.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Updated model instance, or ``Response[schema]`` when ``schema`` is given,
|
Updated model instance, or ``Response[schema]`` when ``schema`` is given.
|
||||||
or ``Response[ModelType]`` when ``as_response=True`` (deprecated).
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
NotFoundError: If no record found
|
NotFoundError: If no record found
|
||||||
"""
|
"""
|
||||||
if as_response and schema is None:
|
|
||||||
warnings.warn(
|
|
||||||
"as_response is deprecated and will be removed in v2.0. "
|
|
||||||
"Use schema=YourSchema instead.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
async with get_transaction(session):
|
async with get_transaction(session):
|
||||||
m2m_exclude = cls._m2m_schema_fields()
|
m2m_exclude = cls._m2m_schema_fields()
|
||||||
|
|
||||||
@@ -565,9 +583,8 @@ 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 as_response or schema:
|
if schema:
|
||||||
data_out = schema.model_validate(db_model) if schema else db_model
|
return Response(data=schema.model_validate(db_model))
|
||||||
return Response(data=data_out)
|
|
||||||
return db_model
|
return db_model
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -623,7 +640,7 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
filters: list[Any],
|
filters: list[Any],
|
||||||
*,
|
*,
|
||||||
as_response: Literal[True],
|
return_response: Literal[True],
|
||||||
) -> Response[None]: ...
|
) -> Response[None]: ...
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
@@ -633,8 +650,8 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
filters: list[Any],
|
filters: list[Any],
|
||||||
*,
|
*,
|
||||||
as_response: Literal[False] = ...,
|
return_response: Literal[False] = ...,
|
||||||
) -> bool: ...
|
) -> None: ...
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def delete(
|
async def delete(
|
||||||
@@ -642,33 +659,26 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
filters: list[Any],
|
filters: list[Any],
|
||||||
*,
|
*,
|
||||||
as_response: bool = False,
|
return_response: bool = False,
|
||||||
) -> bool | Response[None]:
|
) -> None | Response[None]:
|
||||||
"""Delete records from the database.
|
"""Delete records from the database.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
session: DB async session
|
session: DB async session
|
||||||
filters: List of SQLAlchemy filter conditions
|
filters: List of SQLAlchemy filter conditions
|
||||||
as_response: Deprecated. Will be removed in v2.0. When ``True``,
|
return_response: When ``True``, returns ``Response[None]`` instead
|
||||||
returns ``Response[None]`` instead of ``bool``.
|
of ``None``. Useful for API endpoints that expect a consistent
|
||||||
|
response envelope.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
``True`` if deletion was executed, or ``Response[None]`` when
|
``None``, or ``Response[None]`` when ``return_response=True``.
|
||||||
``as_response=True`` (deprecated).
|
|
||||||
"""
|
"""
|
||||||
if as_response:
|
|
||||||
warnings.warn(
|
|
||||||
"as_response is deprecated and will be removed in v2.0. "
|
|
||||||
"Use schema=YourSchema instead.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
async with get_transaction(session):
|
async with get_transaction(session):
|
||||||
q = sql_delete(cls.model).where(and_(*filters))
|
q = sql_delete(cls.model).where(and_(*filters))
|
||||||
await session.execute(q)
|
await session.execute(q)
|
||||||
if as_response:
|
if return_response:
|
||||||
return Response(data=None)
|
return Response(data=None)
|
||||||
return True
|
return None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def count(
|
async def count(
|
||||||
@@ -691,13 +701,7 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
Number of matching records
|
Number of matching records
|
||||||
"""
|
"""
|
||||||
q = select(func.count()).select_from(cls.model)
|
q = select(func.count()).select_from(cls.model)
|
||||||
if joins:
|
q = _apply_joins(q, joins, outer_join)
|
||||||
for model, condition in joins:
|
|
||||||
q = (
|
|
||||||
q.outerjoin(model, condition)
|
|
||||||
if outer_join
|
|
||||||
else q.join(model, condition)
|
|
||||||
)
|
|
||||||
if filters:
|
if filters:
|
||||||
q = q.where(and_(*filters))
|
q = q.where(and_(*filters))
|
||||||
result = await session.execute(q)
|
result = await session.execute(q)
|
||||||
@@ -724,58 +728,11 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
True if at least one record matches
|
True if at least one record matches
|
||||||
"""
|
"""
|
||||||
q = select(cls.model)
|
q = select(cls.model)
|
||||||
if joins:
|
q = _apply_joins(q, joins, outer_join)
|
||||||
for model, condition in joins:
|
|
||||||
q = (
|
|
||||||
q.outerjoin(model, condition)
|
|
||||||
if outer_join
|
|
||||||
else q.join(model, condition)
|
|
||||||
)
|
|
||||||
q = q.where(and_(*filters)).exists().select()
|
q = q.where(and_(*filters)).exists().select()
|
||||||
result = await session.execute(q)
|
result = await session.execute(q)
|
||||||
return bool(result.scalar())
|
return bool(result.scalar())
|
||||||
|
|
||||||
@overload
|
|
||||||
@classmethod
|
|
||||||
async def offset_paginate( # pragma: no cover
|
|
||||||
cls: type[Self],
|
|
||||||
session: AsyncSession,
|
|
||||||
*,
|
|
||||||
filters: list[Any] | None = None,
|
|
||||||
joins: JoinType | None = None,
|
|
||||||
outer_join: bool = False,
|
|
||||||
load_options: list[ExecutableOption] | None = None,
|
|
||||||
order_by: Any | None = None,
|
|
||||||
page: int = 1,
|
|
||||||
items_per_page: int = 20,
|
|
||||||
search: str | SearchConfig | None = None,
|
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
|
||||||
filter_by: dict[str, Any] | BaseModel | None = None,
|
|
||||||
schema: type[SchemaType],
|
|
||||||
) -> PaginatedResponse[SchemaType]: ...
|
|
||||||
|
|
||||||
# Backward-compatible - will be removed in v2.0
|
|
||||||
@overload
|
|
||||||
@classmethod
|
|
||||||
async def offset_paginate( # pragma: no cover
|
|
||||||
cls: type[Self],
|
|
||||||
session: AsyncSession,
|
|
||||||
*,
|
|
||||||
filters: list[Any] | None = None,
|
|
||||||
joins: JoinType | None = None,
|
|
||||||
outer_join: bool = False,
|
|
||||||
load_options: list[ExecutableOption] | None = None,
|
|
||||||
order_by: Any | None = None,
|
|
||||||
page: int = 1,
|
|
||||||
items_per_page: int = 20,
|
|
||||||
search: str | SearchConfig | None = None,
|
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
|
||||||
filter_by: dict[str, Any] | BaseModel | None = None,
|
|
||||||
schema: None = ...,
|
|
||||||
) -> PaginatedResponse[ModelType]: ...
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def offset_paginate(
|
async def offset_paginate(
|
||||||
cls: type[Self],
|
cls: type[Self],
|
||||||
@@ -785,15 +742,15 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
joins: JoinType | None = None,
|
joins: JoinType | None = None,
|
||||||
outer_join: bool = False,
|
outer_join: bool = False,
|
||||||
load_options: list[ExecutableOption] | None = None,
|
load_options: list[ExecutableOption] | None = None,
|
||||||
order_by: Any | None = None,
|
order_by: OrderByClause | None = None,
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
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,
|
||||||
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] | None = None,
|
schema: type[BaseModel],
|
||||||
) -> PaginatedResponse[ModelType] | PaginatedResponse[Any]:
|
) -> PaginatedResponse[Any]:
|
||||||
"""Get paginated results using offset-based pagination.
|
"""Get paginated results using offset-based pagination.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -811,54 +768,36 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
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,
|
||||||
list → IN clause. Raises InvalidFacetFilterError for unknown keys.
|
list → IN clause. Raises InvalidFacetFilterError for unknown keys.
|
||||||
schema: Optional Pydantic schema to serialize each item into.
|
schema: Pydantic schema to serialize each item into.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
PaginatedResponse with OffsetPagination metadata
|
PaginatedResponse with OffsetPagination metadata
|
||||||
"""
|
"""
|
||||||
filters = list(filters) if filters else []
|
filters = list(filters) if filters else []
|
||||||
offset = (page - 1) * items_per_page
|
offset = (page - 1) * items_per_page
|
||||||
search_joins: list[Any] = []
|
|
||||||
|
|
||||||
if isinstance(filter_by, BaseModel):
|
fb_filters, search_joins = cls._prepare_filter_by(filter_by, facet_fields)
|
||||||
filter_by = filter_by.model_dump(exclude_none=True) or None
|
filters.extend(fb_filters)
|
||||||
|
|
||||||
# Build filter_by conditions from declared facet fields
|
|
||||||
if filter_by:
|
|
||||||
resolved_facets_for_filter = (
|
|
||||||
facet_fields if facet_fields is not None else cls.facet_fields
|
|
||||||
)
|
|
||||||
fb_filters, fb_joins = build_filter_by(
|
|
||||||
filter_by, resolved_facets_for_filter or []
|
|
||||||
)
|
|
||||||
filters.extend(fb_filters)
|
|
||||||
search_joins.extend(fb_joins)
|
|
||||||
|
|
||||||
# Build search filters
|
# Build search filters
|
||||||
if search:
|
if search:
|
||||||
search_filters, search_joins = build_search_filters(
|
search_filters, new_search_joins = build_search_filters(
|
||||||
cls.model,
|
cls.model,
|
||||||
search,
|
search,
|
||||||
search_fields=search_fields,
|
search_fields=search_fields,
|
||||||
default_fields=cls.searchable_fields,
|
default_fields=cls.searchable_fields,
|
||||||
)
|
)
|
||||||
filters.extend(search_filters)
|
filters.extend(search_filters)
|
||||||
|
search_joins.extend(new_search_joins)
|
||||||
|
|
||||||
# Build query with joins
|
# Build query with joins
|
||||||
q = select(cls.model)
|
q = select(cls.model)
|
||||||
|
|
||||||
# Apply explicit joins
|
# Apply explicit joins
|
||||||
if joins:
|
q = _apply_joins(q, joins, outer_join)
|
||||||
for model, condition in joins:
|
|
||||||
q = (
|
|
||||||
q.outerjoin(model, condition)
|
|
||||||
if outer_join
|
|
||||||
else q.join(model, condition)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Apply search joins (always outer joins for search)
|
# Apply search joins (always outer joins for search)
|
||||||
for join_rel in search_joins:
|
q = _apply_search_joins(q, search_joins)
|
||||||
q = q.outerjoin(join_rel)
|
|
||||||
|
|
||||||
if filters:
|
if filters:
|
||||||
q = q.where(and_(*filters))
|
q = q.where(and_(*filters))
|
||||||
@@ -870,9 +809,7 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
q = q.offset(offset).limit(items_per_page)
|
q = q.offset(offset).limit(items_per_page)
|
||||||
result = await session.execute(q)
|
result = await session.execute(q)
|
||||||
raw_items = cast(list[ModelType], result.unique().scalars().all())
|
raw_items = cast(list[ModelType], result.unique().scalars().all())
|
||||||
items: list[Any] = (
|
items: list[Any] = [schema.model_validate(item) for item in raw_items]
|
||||||
[schema.model_validate(item) for item in raw_items] if schema else raw_items
|
|
||||||
)
|
|
||||||
|
|
||||||
# Count query (with same joins and filters)
|
# Count query (with same joins and filters)
|
||||||
pk_col = cls.model.__mapper__.primary_key[0]
|
pk_col = cls.model.__mapper__.primary_key[0]
|
||||||
@@ -880,17 +817,10 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
count_q = count_q.select_from(cls.model)
|
count_q = count_q.select_from(cls.model)
|
||||||
|
|
||||||
# Apply explicit joins to count query
|
# Apply explicit joins to count query
|
||||||
if joins:
|
count_q = _apply_joins(count_q, joins, outer_join)
|
||||||
for model, condition in joins:
|
|
||||||
count_q = (
|
|
||||||
count_q.outerjoin(model, condition)
|
|
||||||
if outer_join
|
|
||||||
else count_q.join(model, condition)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Apply search joins to count query
|
# Apply search joins to count query
|
||||||
for join_rel in search_joins:
|
count_q = _apply_search_joins(count_q, search_joins)
|
||||||
count_q = count_q.outerjoin(join_rel)
|
|
||||||
|
|
||||||
if filters:
|
if filters:
|
||||||
count_q = count_q.where(and_(*filters))
|
count_q = count_q.where(and_(*filters))
|
||||||
@@ -898,19 +828,9 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
count_result = await session.execute(count_q)
|
count_result = await session.execute(count_q)
|
||||||
total_count = count_result.scalar_one()
|
total_count = count_result.scalar_one()
|
||||||
|
|
||||||
# Build facets
|
filter_attributes = await cls._build_filter_attributes(
|
||||||
resolved_facet_fields = (
|
session, facet_fields, filters, search_joins
|
||||||
facet_fields if facet_fields is not None else cls.facet_fields
|
|
||||||
)
|
)
|
||||||
filter_attributes: dict[str, list[Any]] | None = None
|
|
||||||
if resolved_facet_fields:
|
|
||||||
filter_attributes = await build_facets(
|
|
||||||
session,
|
|
||||||
cls.model,
|
|
||||||
resolved_facet_fields,
|
|
||||||
base_filters=filters or None,
|
|
||||||
base_joins=search_joins or None,
|
|
||||||
)
|
|
||||||
|
|
||||||
return PaginatedResponse(
|
return PaginatedResponse(
|
||||||
data=items,
|
data=items,
|
||||||
@@ -923,50 +843,6 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
filter_attributes=filter_attributes,
|
filter_attributes=filter_attributes,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Backward-compatible - will be removed in v2.0
|
|
||||||
paginate = offset_paginate
|
|
||||||
|
|
||||||
@overload
|
|
||||||
@classmethod
|
|
||||||
async def cursor_paginate( # pragma: no cover
|
|
||||||
cls: type[Self],
|
|
||||||
session: AsyncSession,
|
|
||||||
*,
|
|
||||||
cursor: str | None = None,
|
|
||||||
filters: list[Any] | None = None,
|
|
||||||
joins: JoinType | None = None,
|
|
||||||
outer_join: bool = False,
|
|
||||||
load_options: list[ExecutableOption] | None = None,
|
|
||||||
order_by: Any | None = None,
|
|
||||||
items_per_page: int = 20,
|
|
||||||
search: str | SearchConfig | None = None,
|
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
|
||||||
filter_by: dict[str, Any] | BaseModel | None = None,
|
|
||||||
schema: type[SchemaType],
|
|
||||||
) -> PaginatedResponse[SchemaType]: ...
|
|
||||||
|
|
||||||
# Backward-compatible - will be removed in v2.0
|
|
||||||
@overload
|
|
||||||
@classmethod
|
|
||||||
async def cursor_paginate( # pragma: no cover
|
|
||||||
cls: type[Self],
|
|
||||||
session: AsyncSession,
|
|
||||||
*,
|
|
||||||
cursor: str | None = None,
|
|
||||||
filters: list[Any] | None = None,
|
|
||||||
joins: JoinType | None = None,
|
|
||||||
outer_join: bool = False,
|
|
||||||
load_options: list[ExecutableOption] | None = None,
|
|
||||||
order_by: Any | None = None,
|
|
||||||
items_per_page: int = 20,
|
|
||||||
search: str | SearchConfig | None = None,
|
|
||||||
search_fields: Sequence[SearchFieldType] | None = None,
|
|
||||||
facet_fields: Sequence[FacetFieldType] | None = None,
|
|
||||||
filter_by: dict[str, Any] | BaseModel | None = None,
|
|
||||||
schema: None = ...,
|
|
||||||
) -> PaginatedResponse[ModelType]: ...
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def cursor_paginate(
|
async def cursor_paginate(
|
||||||
cls: type[Self],
|
cls: type[Self],
|
||||||
@@ -977,14 +853,14 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
joins: JoinType | None = None,
|
joins: JoinType | None = None,
|
||||||
outer_join: bool = False,
|
outer_join: bool = False,
|
||||||
load_options: list[ExecutableOption] | None = None,
|
load_options: list[ExecutableOption] | None = None,
|
||||||
order_by: Any | None = None,
|
order_by: OrderByClause | 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,
|
||||||
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] | None = None,
|
schema: type[BaseModel],
|
||||||
) -> PaginatedResponse[ModelType] | PaginatedResponse[Any]:
|
) -> PaginatedResponse[Any]:
|
||||||
"""Get paginated results using cursor-based pagination.
|
"""Get paginated results using cursor-based pagination.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -1011,21 +887,9 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
PaginatedResponse with CursorPagination metadata
|
PaginatedResponse with CursorPagination metadata
|
||||||
"""
|
"""
|
||||||
filters = list(filters) if filters else []
|
filters = list(filters) if filters else []
|
||||||
search_joins: list[Any] = []
|
|
||||||
|
|
||||||
if isinstance(filter_by, BaseModel):
|
fb_filters, search_joins = cls._prepare_filter_by(filter_by, facet_fields)
|
||||||
filter_by = filter_by.model_dump(exclude_none=True) or None
|
filters.extend(fb_filters)
|
||||||
|
|
||||||
# Build filter_by conditions from declared facet fields
|
|
||||||
if filter_by:
|
|
||||||
resolved_facets_for_filter = (
|
|
||||||
facet_fields if facet_fields is not None else cls.facet_fields
|
|
||||||
)
|
|
||||||
fb_filters, fb_joins = build_filter_by(
|
|
||||||
filter_by, resolved_facets_for_filter or []
|
|
||||||
)
|
|
||||||
filters.extend(fb_filters)
|
|
||||||
search_joins.extend(fb_joins)
|
|
||||||
|
|
||||||
if cls.cursor_column is None:
|
if cls.cursor_column is None:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
@@ -1058,29 +922,23 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
|
|
||||||
# Build search filters
|
# Build search filters
|
||||||
if search:
|
if search:
|
||||||
search_filters, search_joins = build_search_filters(
|
search_filters, new_search_joins = build_search_filters(
|
||||||
cls.model,
|
cls.model,
|
||||||
search,
|
search,
|
||||||
search_fields=search_fields,
|
search_fields=search_fields,
|
||||||
default_fields=cls.searchable_fields,
|
default_fields=cls.searchable_fields,
|
||||||
)
|
)
|
||||||
filters.extend(search_filters)
|
filters.extend(search_filters)
|
||||||
|
search_joins.extend(new_search_joins)
|
||||||
|
|
||||||
# Build query
|
# Build query
|
||||||
q = select(cls.model)
|
q = select(cls.model)
|
||||||
|
|
||||||
# Apply explicit joins
|
# Apply explicit joins
|
||||||
if joins:
|
q = _apply_joins(q, joins, outer_join)
|
||||||
for model, condition in joins:
|
|
||||||
q = (
|
|
||||||
q.outerjoin(model, condition)
|
|
||||||
if outer_join
|
|
||||||
else q.join(model, condition)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Apply search joins (always outer joins)
|
# Apply search joins (always outer joins)
|
||||||
for join_rel in search_joins:
|
q = _apply_search_joins(q, search_joins)
|
||||||
q = q.outerjoin(join_rel)
|
|
||||||
|
|
||||||
if filters:
|
if filters:
|
||||||
q = q.where(and_(*filters))
|
q = q.where(and_(*filters))
|
||||||
@@ -1110,25 +968,11 @@ class AsyncCrud(Generic[ModelType]):
|
|||||||
if cursor is not None and items_page:
|
if cursor is not None and items_page:
|
||||||
prev_cursor = _encode_cursor(getattr(items_page[0], cursor_col_name))
|
prev_cursor = _encode_cursor(getattr(items_page[0], cursor_col_name))
|
||||||
|
|
||||||
items: list[Any] = (
|
items: list[Any] = [schema.model_validate(item) for item in items_page]
|
||||||
[schema.model_validate(item) for item in items_page]
|
|
||||||
if schema
|
|
||||||
else items_page
|
|
||||||
)
|
|
||||||
|
|
||||||
# Build facets
|
filter_attributes = await cls._build_filter_attributes(
|
||||||
resolved_facet_fields = (
|
session, facet_fields, filters, search_joins
|
||||||
facet_fields if facet_fields is not None else cls.facet_fields
|
|
||||||
)
|
)
|
||||||
filter_attributes: dict[str, list[Any]] | None = None
|
|
||||||
if resolved_facet_fields:
|
|
||||||
filter_attributes = await build_facets(
|
|
||||||
session,
|
|
||||||
cls.model,
|
|
||||||
resolved_facet_fields,
|
|
||||||
base_filters=filters or None,
|
|
||||||
base_joins=search_joins or None,
|
|
||||||
)
|
|
||||||
|
|
||||||
return PaginatedResponse(
|
return PaginatedResponse(
|
||||||
data=items,
|
data=items,
|
||||||
@@ -1147,6 +991,7 @@ def CrudFactory(
|
|||||||
*,
|
*,
|
||||||
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[QueryableAttribute[Any]] | None = None,
|
||||||
m2m_fields: M2MFieldType | None = None,
|
m2m_fields: M2MFieldType | None = None,
|
||||||
default_load_options: list[ExecutableOption] | None = None,
|
default_load_options: list[ExecutableOption] | None = None,
|
||||||
cursor_column: Any | None = None,
|
cursor_column: Any | None = None,
|
||||||
@@ -1159,6 +1004,8 @@ def CrudFactory(
|
|||||||
facet_fields: Optional list of columns to compute distinct values for in paginated
|
facet_fields: Optional list of columns to compute distinct values for in paginated
|
||||||
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
|
||||||
|
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.
|
||||||
@@ -1252,6 +1099,7 @@ def CrudFactory(
|
|||||||
"model": model,
|
"model": model,
|
||||||
"searchable_fields": searchable_fields,
|
"searchable_fields": searchable_fields,
|
||||||
"facet_fields": facet_fields,
|
"facet_fields": facet_fields,
|
||||||
|
"order_fields": order_fields,
|
||||||
"m2m_fields": m2m_fields,
|
"m2m_fields": m2m_fields,
|
||||||
"default_load_options": default_load_options,
|
"default_load_options": default_load_options,
|
||||||
"cursor_column": cursor_column,
|
"cursor_column": cursor_column,
|
||||||
|
|||||||
@@ -1,24 +1,23 @@
|
|||||||
"""Search utilities for AsyncCrud."""
|
"""Search utilities for AsyncCrud."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import functools
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass, replace
|
||||||
from typing import TYPE_CHECKING, Any, Literal
|
from typing import TYPE_CHECKING, Any, Literal
|
||||||
|
|
||||||
from sqlalchemy import String, 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 ..exceptions import InvalidFacetFilterError, NoSearchableFieldsError
|
from ..exceptions import InvalidFacetFilterError, NoSearchableFieldsError
|
||||||
|
from ..types import FacetFieldType, SearchFieldType
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from sqlalchemy.sql.elements import ColumnElement
|
from sqlalchemy.sql.elements import ColumnElement
|
||||||
|
|
||||||
SearchFieldType = InstrumentedAttribute[Any] | tuple[InstrumentedAttribute[Any], ...]
|
|
||||||
FacetFieldType = SearchFieldType
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class SearchConfig:
|
class SearchConfig:
|
||||||
@@ -37,6 +36,7 @@ class SearchConfig:
|
|||||||
match_mode: Literal["any", "all"] = "any"
|
match_mode: Literal["any", "all"] = "any"
|
||||||
|
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=128)
|
||||||
def get_searchable_fields(
|
def get_searchable_fields(
|
||||||
model: type[DeclarativeBase],
|
model: type[DeclarativeBase],
|
||||||
*,
|
*,
|
||||||
@@ -101,14 +101,11 @@ def build_search_filters(
|
|||||||
if isinstance(search, str):
|
if isinstance(search, str):
|
||||||
config = SearchConfig(query=search, fields=search_fields)
|
config = SearchConfig(query=search, fields=search_fields)
|
||||||
else:
|
else:
|
||||||
config = search
|
config = (
|
||||||
if search_fields is not None:
|
replace(search, fields=search_fields)
|
||||||
config = SearchConfig(
|
if search_fields is not None
|
||||||
query=config.query,
|
else search
|
||||||
fields=search_fields,
|
)
|
||||||
case_sensitive=config.case_sensitive,
|
|
||||||
match_mode=config.match_mode,
|
|
||||||
)
|
|
||||||
|
|
||||||
if not config.query or not config.query.strip():
|
if not config.query or not config.query.strip():
|
||||||
return [], []
|
return [], []
|
||||||
@@ -227,8 +224,6 @@ async def build_facets(
|
|||||||
q = q.outerjoin(rel)
|
q = q.outerjoin(rel)
|
||||||
|
|
||||||
if base_filters:
|
if base_filters:
|
||||||
from sqlalchemy import and_
|
|
||||||
|
|
||||||
q = q.where(and_(*base_filters))
|
q = q.where(and_(*base_filters))
|
||||||
|
|
||||||
q = q.order_by(column)
|
q = q.order_by(column)
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ from sqlalchemy import text
|
|||||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||||
from sqlalchemy.orm import DeclarativeBase
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
|
|
||||||
|
from .exceptions import NotFoundError
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"LockMode",
|
"LockMode",
|
||||||
"create_db_context",
|
"create_db_context",
|
||||||
@@ -216,7 +218,7 @@ async def wait_for_row_change(
|
|||||||
The refreshed model instance with updated values
|
The refreshed model instance with updated values
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
LookupError: If the row does not exist or is deleted during polling
|
NotFoundError: If the row does not exist or is deleted during polling
|
||||||
TimeoutError: If timeout expires before a change is detected
|
TimeoutError: If timeout expires before a change is detected
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
@@ -237,7 +239,7 @@ async def wait_for_row_change(
|
|||||||
"""
|
"""
|
||||||
instance = await session.get(model, pk_value)
|
instance = await session.get(model, pk_value)
|
||||||
if instance is None:
|
if instance is None:
|
||||||
raise LookupError(f"{model.__name__} with pk={pk_value!r} not found")
|
raise NotFoundError(f"{model.__name__} with pk={pk_value!r} not found")
|
||||||
|
|
||||||
if columns is not None:
|
if columns is not None:
|
||||||
watch_cols = columns
|
watch_cols = columns
|
||||||
@@ -261,7 +263,7 @@ async def wait_for_row_change(
|
|||||||
instance = await session.get(model, pk_value)
|
instance = await session.get(model, pk_value)
|
||||||
|
|
||||||
if instance is None:
|
if instance is None:
|
||||||
raise LookupError(f"{model.__name__} with pk={pk_value!r} was deleted")
|
raise NotFoundError(f"{model.__name__} with pk={pk_value!r} was deleted")
|
||||||
|
|
||||||
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:
|
||||||
|
|||||||
@@ -1,20 +1,17 @@
|
|||||||
"""Dependency factories for FastAPI routes."""
|
"""Dependency factories for FastAPI routes."""
|
||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
from collections.abc import AsyncGenerator, Callable
|
from collections.abc import Callable
|
||||||
from typing import Any, TypeVar, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.orm import DeclarativeBase
|
|
||||||
|
|
||||||
from .crud import CrudFactory
|
from .crud import CrudFactory
|
||||||
|
from .types import ModelType, SessionDependency
|
||||||
|
|
||||||
__all__ = ["BodyDependency", "PathDependency"]
|
__all__ = ["BodyDependency", "PathDependency"]
|
||||||
|
|
||||||
ModelType = TypeVar("ModelType", bound=DeclarativeBase)
|
|
||||||
SessionDependency = Callable[[], AsyncGenerator[AsyncSession, None]]
|
|
||||||
|
|
||||||
|
|
||||||
def PathDependency(
|
def PathDependency(
|
||||||
model: type[ModelType],
|
model: type[ModelType],
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from .exceptions import (
|
|||||||
ConflictError,
|
ConflictError,
|
||||||
ForbiddenError,
|
ForbiddenError,
|
||||||
InvalidFacetFilterError,
|
InvalidFacetFilterError,
|
||||||
|
InvalidOrderFieldError,
|
||||||
NoSearchableFieldsError,
|
NoSearchableFieldsError,
|
||||||
NotFoundError,
|
NotFoundError,
|
||||||
UnauthorizedError,
|
UnauthorizedError,
|
||||||
@@ -21,6 +22,7 @@ __all__ = [
|
|||||||
"generate_error_responses",
|
"generate_error_responses",
|
||||||
"init_exceptions_handlers",
|
"init_exceptions_handlers",
|
||||||
"InvalidFacetFilterError",
|
"InvalidFacetFilterError",
|
||||||
|
"InvalidOrderFieldError",
|
||||||
"NoSearchableFieldsError",
|
"NoSearchableFieldsError",
|
||||||
"NotFoundError",
|
"NotFoundError",
|
||||||
"UnauthorizedError",
|
"UnauthorizedError",
|
||||||
|
|||||||
@@ -6,32 +6,46 @@ from ..schemas import ApiError, ErrorResponse, ResponseStatus
|
|||||||
|
|
||||||
|
|
||||||
class ApiException(Exception):
|
class ApiException(Exception):
|
||||||
"""Base exception for API errors with structured response.
|
"""Base exception for API errors with structured response."""
|
||||||
|
|
||||||
Subclass this to create custom API exceptions with consistent error format.
|
|
||||||
The exception handler will use api_error to generate the response.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
```python
|
|
||||||
class CustomError(ApiException):
|
|
||||||
api_error = ApiError(
|
|
||||||
code=400,
|
|
||||||
msg="Bad Request",
|
|
||||||
desc="The request was invalid.",
|
|
||||||
err_code="CUSTOM-400",
|
|
||||||
)
|
|
||||||
```
|
|
||||||
"""
|
|
||||||
|
|
||||||
api_error: ClassVar[ApiError]
|
api_error: ClassVar[ApiError]
|
||||||
|
|
||||||
def __init__(self, detail: str | None = None):
|
def __init_subclass__(cls, abstract: bool = False, **kwargs: Any) -> None:
|
||||||
|
super().__init_subclass__(**kwargs)
|
||||||
|
if not abstract and not hasattr(cls, "api_error"):
|
||||||
|
raise TypeError(
|
||||||
|
f"{cls.__name__} must define an 'api_error' class attribute. "
|
||||||
|
"Pass abstract=True when creating intermediate base classes."
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
detail: str | None = None,
|
||||||
|
*,
|
||||||
|
desc: str | None = None,
|
||||||
|
data: Any = None,
|
||||||
|
) -> None:
|
||||||
"""Initialize the exception.
|
"""Initialize the exception.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
detail: Optional override for the error message
|
detail: Optional human-readable message
|
||||||
|
desc: Optional per-instance override for the ``description`` field
|
||||||
|
in the HTTP response body.
|
||||||
|
data: Optional per-instance override for the ``data`` field in the
|
||||||
|
HTTP response body.
|
||||||
"""
|
"""
|
||||||
super().__init__(detail or self.api_error.msg)
|
updates: dict[str, Any] = {}
|
||||||
|
if detail is not None:
|
||||||
|
updates["msg"] = detail
|
||||||
|
if desc is not None:
|
||||||
|
updates["desc"] = desc
|
||||||
|
if data is not None:
|
||||||
|
updates["data"] = data
|
||||||
|
if updates:
|
||||||
|
object.__setattr__(
|
||||||
|
self, "api_error", self.__class__.api_error.model_copy(update=updates)
|
||||||
|
)
|
||||||
|
super().__init__(self.api_error.msg)
|
||||||
|
|
||||||
|
|
||||||
class UnauthorizedError(ApiException):
|
class UnauthorizedError(ApiException):
|
||||||
@@ -92,14 +106,15 @@ class NoSearchableFieldsError(ApiException):
|
|||||||
"""Initialize the exception.
|
"""Initialize the exception.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
model: The SQLAlchemy model class that has no searchable fields
|
model: The model class that has no searchable fields configured.
|
||||||
"""
|
"""
|
||||||
self.model = model
|
self.model = model
|
||||||
detail = (
|
super().__init__(
|
||||||
f"No searchable fields found for model '{model.__name__}'. "
|
desc=(
|
||||||
"Provide 'search_fields' parameter or set 'searchable_fields' on the CRUD class."
|
f"No searchable fields found for model '{model.__name__}'. "
|
||||||
|
"Provide 'search_fields' parameter or set 'searchable_fields' on the CRUD class."
|
||||||
|
)
|
||||||
)
|
)
|
||||||
super().__init__(detail)
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidFacetFilterError(ApiException):
|
class InvalidFacetFilterError(ApiException):
|
||||||
@@ -116,16 +131,41 @@ class InvalidFacetFilterError(ApiException):
|
|||||||
"""Initialize the exception.
|
"""Initialize the exception.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
key: The unknown filter key provided by the caller
|
key: The unknown filter key provided by the caller.
|
||||||
valid_keys: Set of valid keys derived from the declared facet_fields
|
valid_keys: Set of valid keys derived from the declared facet_fields.
|
||||||
"""
|
"""
|
||||||
self.key = key
|
self.key = key
|
||||||
self.valid_keys = valid_keys
|
self.valid_keys = valid_keys
|
||||||
detail = (
|
super().__init__(
|
||||||
f"'{key}' is not a declared facet field. "
|
desc=(
|
||||||
f"Valid keys: {sorted(valid_keys) or 'none — set facet_fields on the CRUD class'}."
|
f"'{key}' is not a declared facet field. "
|
||||||
|
f"Valid keys: {sorted(valid_keys) or 'none — set facet_fields on the CRUD class'}."
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidOrderFieldError(ApiException):
|
||||||
|
"""Raised when order_by contains a field not in the allowed order fields."""
|
||||||
|
|
||||||
|
api_error = ApiError(
|
||||||
|
code=422,
|
||||||
|
msg="Invalid Order Field",
|
||||||
|
desc="The requested order field is not allowed for this resource.",
|
||||||
|
err_code="SORT-422",
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, field: str, valid_fields: list[str]) -> None:
|
||||||
|
"""Initialize the exception.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
field: The unknown order field provided by the caller.
|
||||||
|
valid_fields: List of valid field names.
|
||||||
|
"""
|
||||||
|
self.field = field
|
||||||
|
self.valid_fields = valid_fields
|
||||||
|
super().__init__(
|
||||||
|
desc=f"'{field}' is not an allowed order field. Valid fields: {valid_fields}."
|
||||||
)
|
)
|
||||||
super().__init__(detail)
|
|
||||||
|
|
||||||
|
|
||||||
def generate_error_responses(
|
def generate_error_responses(
|
||||||
@@ -133,44 +173,39 @@ def generate_error_responses(
|
|||||||
) -> dict[int | str, dict[str, Any]]:
|
) -> dict[int | str, dict[str, Any]]:
|
||||||
"""Generate OpenAPI response documentation for exceptions.
|
"""Generate OpenAPI response documentation for exceptions.
|
||||||
|
|
||||||
Use this to document possible error responses for an endpoint.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
*errors: Exception classes that inherit from ApiException
|
*errors: Exception classes that inherit from ApiException.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict suitable for FastAPI's responses parameter
|
Dict suitable for FastAPI's ``responses`` parameter.
|
||||||
|
|
||||||
Example:
|
|
||||||
```python
|
|
||||||
from fastapi_toolsets.exceptions import generate_error_responses, UnauthorizedError, ForbiddenError
|
|
||||||
|
|
||||||
@app.get(
|
|
||||||
"/admin",
|
|
||||||
responses=generate_error_responses(UnauthorizedError, ForbiddenError)
|
|
||||||
)
|
|
||||||
async def admin_endpoint():
|
|
||||||
...
|
|
||||||
```
|
|
||||||
"""
|
"""
|
||||||
responses: dict[int | str, dict[str, Any]] = {}
|
responses: dict[int | str, dict[str, Any]] = {}
|
||||||
|
|
||||||
for error in errors:
|
for error in errors:
|
||||||
api_error = error.api_error
|
api_error = error.api_error
|
||||||
|
code = api_error.code
|
||||||
|
|
||||||
responses[api_error.code] = {
|
if code not in responses:
|
||||||
"model": ErrorResponse,
|
responses[code] = {
|
||||||
"description": api_error.msg,
|
"model": ErrorResponse,
|
||||||
"content": {
|
"description": api_error.msg,
|
||||||
"application/json": {
|
"content": {
|
||||||
"example": {
|
"application/json": {
|
||||||
"data": api_error.data,
|
"examples": {},
|
||||||
"status": ResponseStatus.FAIL.value,
|
|
||||||
"message": api_error.msg,
|
|
||||||
"description": api_error.desc,
|
|
||||||
"error_code": api_error.err_code,
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
responses[code]["content"]["application/json"]["examples"][
|
||||||
|
api_error.err_code
|
||||||
|
] = {
|
||||||
|
"summary": api_error.msg,
|
||||||
|
"value": {
|
||||||
|
"data": api_error.data,
|
||||||
|
"status": ResponseStatus.FAIL.value,
|
||||||
|
"message": api_error.msg,
|
||||||
|
"description": api_error.desc,
|
||||||
|
"error_code": api_error.err_code,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,56 +1,41 @@
|
|||||||
"""Exception handlers for FastAPI applications."""
|
"""Exception handlers for FastAPI applications."""
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from fastapi import FastAPI, Request, Response, status
|
from fastapi import FastAPI, Request, Response, status
|
||||||
from fastapi.exceptions import RequestValidationError, ResponseValidationError
|
from fastapi.exceptions import (
|
||||||
from fastapi.openapi.utils import get_openapi
|
HTTPException,
|
||||||
|
RequestValidationError,
|
||||||
|
ResponseValidationError,
|
||||||
|
)
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
from ..schemas import ErrorResponse, ResponseStatus
|
from ..schemas import ErrorResponse, ResponseStatus
|
||||||
from .exceptions import ApiException
|
from .exceptions import ApiException
|
||||||
|
|
||||||
|
_VALIDATION_LOCATION_PARAMS: frozenset[str] = frozenset(
|
||||||
|
{"body", "query", "path", "header", "cookie"}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def init_exceptions_handlers(app: FastAPI) -> FastAPI:
|
def init_exceptions_handlers(app: FastAPI) -> FastAPI:
|
||||||
"""Register exception handlers and custom OpenAPI schema on a FastAPI app.
|
"""Register exception handlers and custom OpenAPI schema on a FastAPI app.
|
||||||
|
|
||||||
Installs handlers for :class:`ApiException`, validation errors, and
|
|
||||||
unhandled exceptions, and replaces the default 422 schema with a
|
|
||||||
consistent error format.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
app: FastAPI application instance
|
app: FastAPI application instance.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The same FastAPI instance (for chaining)
|
The same FastAPI instance (for chaining).
|
||||||
|
|
||||||
Example:
|
|
||||||
```python
|
|
||||||
from fastapi import FastAPI
|
|
||||||
from fastapi_toolsets.exceptions import init_exceptions_handlers
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
init_exceptions_handlers(app)
|
|
||||||
```
|
|
||||||
"""
|
"""
|
||||||
_register_exception_handlers(app)
|
_register_exception_handlers(app)
|
||||||
app.openapi = lambda: _custom_openapi(app) # type: ignore[method-assign]
|
_original_openapi = app.openapi
|
||||||
|
app.openapi = lambda: _patched_openapi(app, _original_openapi) # type: ignore[method-assign]
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
def _register_exception_handlers(app: FastAPI) -> None:
|
def _register_exception_handlers(app: FastAPI) -> None:
|
||||||
"""Register all exception handlers on a FastAPI application.
|
"""Register all exception handlers on a FastAPI application."""
|
||||||
|
|
||||||
Args:
|
|
||||||
app: FastAPI application instance
|
|
||||||
|
|
||||||
Example:
|
|
||||||
from fastapi import FastAPI
|
|
||||||
from fastapi_toolsets.exceptions import init_exceptions_handlers
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
init_exceptions_handlers(app)
|
|
||||||
"""
|
|
||||||
|
|
||||||
@app.exception_handler(ApiException)
|
@app.exception_handler(ApiException)
|
||||||
async def api_exception_handler(request: Request, exc: ApiException) -> Response:
|
async def api_exception_handler(request: Request, exc: ApiException) -> Response:
|
||||||
@@ -62,12 +47,25 @@ def _register_exception_handlers(app: FastAPI) -> None:
|
|||||||
description=api_error.desc,
|
description=api_error.desc,
|
||||||
error_code=api_error.err_code,
|
error_code=api_error.err_code,
|
||||||
)
|
)
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=api_error.code,
|
status_code=api_error.code,
|
||||||
content=error_response.model_dump(),
|
content=error_response.model_dump(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@app.exception_handler(HTTPException)
|
||||||
|
async def http_exception_handler(request: Request, exc: HTTPException) -> Response:
|
||||||
|
"""Handle Starlette/FastAPI HTTPException with a consistent error format."""
|
||||||
|
detail = exc.detail if isinstance(exc.detail, str) else "HTTP Error"
|
||||||
|
error_response = ErrorResponse(
|
||||||
|
message=detail,
|
||||||
|
error_code=f"HTTP-{exc.status_code}",
|
||||||
|
)
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=exc.status_code,
|
||||||
|
content=error_response.model_dump(),
|
||||||
|
headers=getattr(exc, "headers", None),
|
||||||
|
)
|
||||||
|
|
||||||
@app.exception_handler(RequestValidationError)
|
@app.exception_handler(RequestValidationError)
|
||||||
async def request_validation_handler(
|
async def request_validation_handler(
|
||||||
request: Request, exc: RequestValidationError
|
request: Request, exc: RequestValidationError
|
||||||
@@ -90,7 +88,6 @@ def _register_exception_handlers(app: FastAPI) -> None:
|
|||||||
description="An unexpected error occurred. Please try again later.",
|
description="An unexpected error occurred. Please try again later.",
|
||||||
error_code="SERVER-500",
|
error_code="SERVER-500",
|
||||||
)
|
)
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
content=error_response.model_dump(),
|
content=error_response.model_dump(),
|
||||||
@@ -105,11 +102,10 @@ def _format_validation_error(
|
|||||||
formatted_errors = []
|
formatted_errors = []
|
||||||
|
|
||||||
for error in errors:
|
for error in errors:
|
||||||
field_path = ".".join(
|
locs = error["loc"]
|
||||||
str(loc)
|
if locs and locs[0] in _VALIDATION_LOCATION_PARAMS:
|
||||||
for loc in error["loc"]
|
locs = locs[1:]
|
||||||
if loc not in ("body", "query", "path", "header", "cookie")
|
field_path = ".".join(str(loc) for loc in locs)
|
||||||
)
|
|
||||||
formatted_errors.append(
|
formatted_errors.append(
|
||||||
{
|
{
|
||||||
"field": field_path or "root",
|
"field": field_path or "root",
|
||||||
@@ -131,34 +127,22 @@ def _format_validation_error(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _custom_openapi(app: FastAPI) -> dict[str, Any]:
|
def _patched_openapi(
|
||||||
"""Generate custom OpenAPI schema with standardized error format.
|
app: FastAPI, original_openapi: Callable[[], dict[str, Any]]
|
||||||
|
) -> dict[str, Any]:
|
||||||
Replaces default 422 validation error responses with the custom format.
|
"""Generate the OpenAPI schema and replace default 422 responses.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
app: FastAPI application instance
|
app: FastAPI application instance.
|
||||||
|
original_openapi: The previous ``app.openapi`` callable to delegate to.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
OpenAPI schema dict
|
Patched OpenAPI schema dict.
|
||||||
|
|
||||||
Example:
|
|
||||||
from fastapi import FastAPI
|
|
||||||
from fastapi_toolsets.exceptions import init_exceptions_handlers
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
init_exceptions_handlers(app) # Automatically sets custom OpenAPI
|
|
||||||
"""
|
"""
|
||||||
if app.openapi_schema:
|
if app.openapi_schema:
|
||||||
return app.openapi_schema
|
return app.openapi_schema
|
||||||
|
|
||||||
openapi_schema = get_openapi(
|
openapi_schema = original_openapi()
|
||||||
title=app.title,
|
|
||||||
version=app.version,
|
|
||||||
openapi_version=app.openapi_version,
|
|
||||||
description=app.description,
|
|
||||||
routes=app.routes,
|
|
||||||
)
|
|
||||||
|
|
||||||
for path_data in openapi_schema.get("paths", {}).values():
|
for path_data in openapi_schema.get("paths", {}).values():
|
||||||
for operation in path_data.values():
|
for operation in path_data.values():
|
||||||
@@ -168,20 +152,25 @@ def _custom_openapi(app: FastAPI) -> dict[str, Any]:
|
|||||||
"description": "Validation Error",
|
"description": "Validation Error",
|
||||||
"content": {
|
"content": {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
"example": {
|
"examples": {
|
||||||
"data": {
|
"VAL-422": {
|
||||||
"errors": [
|
"summary": "Validation Error",
|
||||||
{
|
"value": {
|
||||||
"field": "field_name",
|
"data": {
|
||||||
"message": "value is not valid",
|
"errors": [
|
||||||
"type": "value_error",
|
{
|
||||||
}
|
"field": "field_name",
|
||||||
]
|
"message": "value is not valid",
|
||||||
},
|
"type": "value_error",
|
||||||
"status": ResponseStatus.FAIL.value,
|
}
|
||||||
"message": "Validation Error",
|
]
|
||||||
"description": "1 validation error(s) detected",
|
},
|
||||||
"error_code": "VAL-422",
|
"status": ResponseStatus.FAIL.value,
|
||||||
|
"message": "Validation Error",
|
||||||
|
"description": "1 validation error(s) detected",
|
||||||
|
"error_code": "VAL-422",
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,24 +1,84 @@
|
|||||||
"""Fixture loading utilities for database seeding."""
|
"""Fixture loading utilities for database seeding."""
|
||||||
|
|
||||||
from collections.abc import Callable, Sequence
|
from collections.abc import Callable, Sequence
|
||||||
from typing import Any, TypeVar
|
from typing import Any
|
||||||
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.orm import DeclarativeBase
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
|
|
||||||
from ..db import get_transaction
|
from ..db import get_transaction
|
||||||
from ..logger import get_logger
|
from ..logger import get_logger
|
||||||
|
from ..types import ModelType
|
||||||
from .enum import LoadStrategy
|
from .enum import LoadStrategy
|
||||||
from .registry import Context, FixtureRegistry
|
from .registry import Context, FixtureRegistry
|
||||||
|
|
||||||
logger = get_logger()
|
logger = get_logger()
|
||||||
|
|
||||||
T = TypeVar("T", bound=DeclarativeBase)
|
|
||||||
|
async def _load_ordered(
|
||||||
|
session: AsyncSession,
|
||||||
|
registry: FixtureRegistry,
|
||||||
|
ordered_names: list[str],
|
||||||
|
strategy: LoadStrategy,
|
||||||
|
) -> dict[str, list[DeclarativeBase]]:
|
||||||
|
"""Load fixtures in order."""
|
||||||
|
results: dict[str, list[DeclarativeBase]] = {}
|
||||||
|
|
||||||
|
for name in ordered_names:
|
||||||
|
fixture = registry.get(name)
|
||||||
|
instances = list(fixture.func())
|
||||||
|
|
||||||
|
if not instances:
|
||||||
|
results[name] = []
|
||||||
|
continue
|
||||||
|
|
||||||
|
model_name = type(instances[0]).__name__
|
||||||
|
loaded: list[DeclarativeBase] = []
|
||||||
|
|
||||||
|
async with get_transaction(session):
|
||||||
|
for instance in instances:
|
||||||
|
if strategy == LoadStrategy.INSERT:
|
||||||
|
session.add(instance)
|
||||||
|
loaded.append(instance)
|
||||||
|
|
||||||
|
elif strategy == LoadStrategy.MERGE:
|
||||||
|
merged = await session.merge(instance)
|
||||||
|
loaded.append(merged)
|
||||||
|
|
||||||
|
else: # LoadStrategy.SKIP_EXISTING
|
||||||
|
pk = _get_primary_key(instance)
|
||||||
|
if pk is not None:
|
||||||
|
existing = await session.get(type(instance), pk)
|
||||||
|
if existing is None:
|
||||||
|
session.add(instance)
|
||||||
|
loaded.append(instance)
|
||||||
|
else:
|
||||||
|
session.add(instance)
|
||||||
|
loaded.append(instance)
|
||||||
|
|
||||||
|
results[name] = loaded
|
||||||
|
logger.info(f"Loaded fixture '{name}': {len(loaded)} {model_name}(s)")
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
def _get_primary_key(instance: DeclarativeBase) -> Any | None:
|
||||||
|
"""Get the primary key value of a model instance."""
|
||||||
|
mapper = instance.__class__.__mapper__
|
||||||
|
pk_cols = mapper.primary_key
|
||||||
|
|
||||||
|
if len(pk_cols) == 1:
|
||||||
|
return getattr(instance, pk_cols[0].name, None)
|
||||||
|
|
||||||
|
pk_values = tuple(getattr(instance, col.name, None) for col in pk_cols)
|
||||||
|
if all(v is not None for v in pk_values):
|
||||||
|
return pk_values
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_obj_by_attr(
|
def get_obj_by_attr(
|
||||||
fixtures: Callable[[], Sequence[T]], attr_name: str, value: Any
|
fixtures: Callable[[], Sequence[ModelType]], attr_name: str, value: Any
|
||||||
) -> T:
|
) -> ModelType:
|
||||||
"""Get a SQLAlchemy model instance by matching an attribute value.
|
"""Get a SQLAlchemy model instance by matching an attribute value.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -57,13 +117,6 @@ async def load_fixtures(
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict mapping fixture names to loaded instances
|
Dict mapping fixture names to loaded instances
|
||||||
|
|
||||||
Example:
|
|
||||||
```python
|
|
||||||
# Loads 'roles' first (dependency), then 'users'
|
|
||||||
result = await load_fixtures(session, fixtures, "users")
|
|
||||||
print(result["users"]) # [User(...), ...]
|
|
||||||
```
|
|
||||||
"""
|
"""
|
||||||
ordered = registry.resolve_dependencies(*names)
|
ordered = registry.resolve_dependencies(*names)
|
||||||
return await _load_ordered(session, registry, ordered, strategy)
|
return await _load_ordered(session, registry, ordered, strategy)
|
||||||
@@ -85,76 +138,6 @@ async def load_fixtures_by_context(
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict mapping fixture names to loaded instances
|
Dict mapping fixture names to loaded instances
|
||||||
|
|
||||||
Example:
|
|
||||||
```python
|
|
||||||
# Load base + testing fixtures
|
|
||||||
await load_fixtures_by_context(
|
|
||||||
session, fixtures,
|
|
||||||
Context.BASE, Context.TESTING
|
|
||||||
)
|
|
||||||
```
|
|
||||||
"""
|
"""
|
||||||
ordered = registry.resolve_context_dependencies(*contexts)
|
ordered = registry.resolve_context_dependencies(*contexts)
|
||||||
return await _load_ordered(session, registry, ordered, strategy)
|
return await _load_ordered(session, registry, ordered, strategy)
|
||||||
|
|
||||||
|
|
||||||
async def _load_ordered(
|
|
||||||
session: AsyncSession,
|
|
||||||
registry: FixtureRegistry,
|
|
||||||
ordered_names: list[str],
|
|
||||||
strategy: LoadStrategy,
|
|
||||||
) -> dict[str, list[DeclarativeBase]]:
|
|
||||||
"""Load fixtures in order."""
|
|
||||||
results: dict[str, list[DeclarativeBase]] = {}
|
|
||||||
|
|
||||||
for name in ordered_names:
|
|
||||||
fixture = registry.get(name)
|
|
||||||
instances = list(fixture.func())
|
|
||||||
|
|
||||||
if not instances:
|
|
||||||
results[name] = []
|
|
||||||
continue
|
|
||||||
|
|
||||||
model_name = type(instances[0]).__name__
|
|
||||||
loaded: list[DeclarativeBase] = []
|
|
||||||
|
|
||||||
async with get_transaction(session):
|
|
||||||
for instance in instances:
|
|
||||||
if strategy == LoadStrategy.INSERT:
|
|
||||||
session.add(instance)
|
|
||||||
loaded.append(instance)
|
|
||||||
|
|
||||||
elif strategy == LoadStrategy.MERGE:
|
|
||||||
merged = await session.merge(instance)
|
|
||||||
loaded.append(merged)
|
|
||||||
|
|
||||||
elif strategy == LoadStrategy.SKIP_EXISTING:
|
|
||||||
pk = _get_primary_key(instance)
|
|
||||||
if pk is not None:
|
|
||||||
existing = await session.get(type(instance), pk)
|
|
||||||
if existing is None:
|
|
||||||
session.add(instance)
|
|
||||||
loaded.append(instance)
|
|
||||||
else:
|
|
||||||
session.add(instance)
|
|
||||||
loaded.append(instance)
|
|
||||||
|
|
||||||
results[name] = loaded
|
|
||||||
logger.info(f"Loaded fixture '{name}': {len(loaded)} {model_name}(s)")
|
|
||||||
|
|
||||||
return results
|
|
||||||
|
|
||||||
|
|
||||||
def _get_primary_key(instance: DeclarativeBase) -> Any | None:
|
|
||||||
"""Get the primary key value of a model instance."""
|
|
||||||
mapper = instance.__class__.__mapper__
|
|
||||||
pk_cols = mapper.primary_key
|
|
||||||
|
|
||||||
if len(pk_cols) == 1:
|
|
||||||
return getattr(instance, pk_cols[0].name, None)
|
|
||||||
|
|
||||||
pk_values = tuple(getattr(instance, col.name, None) for col in pk_cols)
|
|
||||||
if all(v is not None for v in pk_values):
|
|
||||||
return pk_values
|
|
||||||
return None
|
|
||||||
|
|||||||
@@ -53,17 +53,23 @@ def init_metrics(
|
|||||||
logger.debug("Initialising metric provider '%s'", provider.name)
|
logger.debug("Initialising metric provider '%s'", provider.name)
|
||||||
provider.func()
|
provider.func()
|
||||||
|
|
||||||
collectors = registry.get_collectors()
|
# Partition collectors and cache env check at startup — both are stable for the app lifetime.
|
||||||
|
async_collectors = [
|
||||||
|
c for c in registry.get_collectors() if asyncio.iscoroutinefunction(c.func)
|
||||||
|
]
|
||||||
|
sync_collectors = [
|
||||||
|
c for c in registry.get_collectors() if not asyncio.iscoroutinefunction(c.func)
|
||||||
|
]
|
||||||
|
multiprocess_mode = _is_multiprocess()
|
||||||
|
|
||||||
@app.get(path, include_in_schema=False)
|
@app.get(path, include_in_schema=False)
|
||||||
async def metrics_endpoint() -> Response:
|
async def metrics_endpoint() -> Response:
|
||||||
for collector in collectors:
|
for collector in sync_collectors:
|
||||||
if asyncio.iscoroutinefunction(collector.func):
|
collector.func()
|
||||||
await collector.func()
|
for collector in async_collectors:
|
||||||
else:
|
await collector.func()
|
||||||
collector.func()
|
|
||||||
|
|
||||||
if _is_multiprocess():
|
if multiprocess_mode:
|
||||||
prom_registry = CollectorRegistry()
|
prom_registry = CollectorRegistry()
|
||||||
multiprocess.MultiProcessCollector(prom_registry)
|
multiprocess.MultiProcessCollector(prom_registry)
|
||||||
output = generate_latest(prom_registry)
|
output = generate_latest(prom_registry)
|
||||||
|
|||||||
47
src/fastapi_toolsets/models.py
Normal file
47
src/fastapi_toolsets/models.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
"""SQLAlchemy model mixins for common column patterns."""
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from sqlalchemy import DateTime, Uuid, func, text
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"UUIDMixin",
|
||||||
|
"CreatedAtMixin",
|
||||||
|
"UpdatedAtMixin",
|
||||||
|
"TimestampMixin",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class UUIDMixin:
|
||||||
|
"""Mixin that adds a UUID primary key auto-generated by the database."""
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(
|
||||||
|
Uuid,
|
||||||
|
primary_key=True,
|
||||||
|
server_default=text("gen_random_uuid()"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CreatedAtMixin:
|
||||||
|
"""Mixin that adds a ``created_at`` timestamp column."""
|
||||||
|
|
||||||
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
|
DateTime(timezone=True),
|
||||||
|
server_default=func.now(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class UpdatedAtMixin:
|
||||||
|
"""Mixin that adds an ``updated_at`` timestamp column."""
|
||||||
|
|
||||||
|
updated_at: Mapped[datetime] = mapped_column(
|
||||||
|
DateTime(timezone=True),
|
||||||
|
server_default=func.now(),
|
||||||
|
onupdate=func.now(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TimestampMixin(CreatedAtMixin, UpdatedAtMixin):
|
||||||
|
"""Mixin that combines ``created_at`` and ``updated_at`` timestamp columns."""
|
||||||
@@ -1,24 +1,23 @@
|
|||||||
"""Base Pydantic schemas for API responses."""
|
"""Base Pydantic schemas for API responses."""
|
||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, ClassVar, Generic, TypeVar
|
from typing import Any, ClassVar, Generic
|
||||||
|
|
||||||
from pydantic import BaseModel, ConfigDict
|
from pydantic import BaseModel, ConfigDict
|
||||||
|
|
||||||
|
from .types import DataT
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"ApiError",
|
"ApiError",
|
||||||
"CursorPagination",
|
"CursorPagination",
|
||||||
"ErrorResponse",
|
"ErrorResponse",
|
||||||
"OffsetPagination",
|
"OffsetPagination",
|
||||||
"Pagination",
|
|
||||||
"PaginatedResponse",
|
"PaginatedResponse",
|
||||||
"PydanticBase",
|
"PydanticBase",
|
||||||
"Response",
|
"Response",
|
||||||
"ResponseStatus",
|
"ResponseStatus",
|
||||||
]
|
]
|
||||||
|
|
||||||
DataT = TypeVar("DataT")
|
|
||||||
|
|
||||||
|
|
||||||
class PydanticBase(BaseModel):
|
class PydanticBase(BaseModel):
|
||||||
"""Base class for all Pydantic models with common configuration."""
|
"""Base class for all Pydantic models with common configuration."""
|
||||||
@@ -108,10 +107,6 @@ class OffsetPagination(PydanticBase):
|
|||||||
has_more: bool
|
has_more: bool
|
||||||
|
|
||||||
|
|
||||||
# Backward-compatible - will be removed in v2.0
|
|
||||||
Pagination = OffsetPagination
|
|
||||||
|
|
||||||
|
|
||||||
class CursorPagination(PydanticBase):
|
class CursorPagination(PydanticBase):
|
||||||
"""Pagination metadata for cursor-based list responses.
|
"""Pagination metadata for cursor-based list responses.
|
||||||
|
|
||||||
|
|||||||
27
src/fastapi_toolsets/types.py
Normal file
27
src/fastapi_toolsets/types.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
"""Shared type aliases for the fastapi-toolsets package."""
|
||||||
|
|
||||||
|
from collections.abc import AsyncGenerator, Callable, Mapping
|
||||||
|
from typing import Any, TypeVar
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from sqlalchemy.orm import DeclarativeBase, QueryableAttribute
|
||||||
|
from sqlalchemy.orm.attributes import InstrumentedAttribute
|
||||||
|
from sqlalchemy.sql.elements import ColumnElement
|
||||||
|
|
||||||
|
# Generic TypeVars
|
||||||
|
DataT = TypeVar("DataT")
|
||||||
|
ModelType = TypeVar("ModelType", bound=DeclarativeBase)
|
||||||
|
SchemaType = TypeVar("SchemaType", bound=BaseModel)
|
||||||
|
|
||||||
|
# CRUD type aliases
|
||||||
|
JoinType = list[tuple[type[DeclarativeBase], Any]]
|
||||||
|
M2MFieldType = Mapping[str, QueryableAttribute[Any]]
|
||||||
|
OrderByClause = ColumnElement[Any] | QueryableAttribute[Any]
|
||||||
|
|
||||||
|
# Search / facet type aliases
|
||||||
|
SearchFieldType = InstrumentedAttribute[Any] | tuple[InstrumentedAttribute[Any], ...]
|
||||||
|
FacetFieldType = SearchFieldType
|
||||||
|
|
||||||
|
# Dependency type aliases
|
||||||
|
SessionDependency = Callable[[], AsyncGenerator[AsyncSession, None]]
|
||||||
@@ -92,6 +92,15 @@ class IntRole(Base):
|
|||||||
name: Mapped[str] = mapped_column(String(50), unique=True)
|
name: Mapped[str] = mapped_column(String(50), unique=True)
|
||||||
|
|
||||||
|
|
||||||
|
class Permission(Base):
|
||||||
|
"""Test model with composite primary key."""
|
||||||
|
|
||||||
|
__tablename__ = "permissions"
|
||||||
|
|
||||||
|
subject: Mapped[str] = mapped_column(String(50), primary_key=True)
|
||||||
|
action: Mapped[str] = mapped_column(String(50), primary_key=True)
|
||||||
|
|
||||||
|
|
||||||
class Event(Base):
|
class Event(Base):
|
||||||
"""Test model with DateTime and Date cursor columns."""
|
"""Test model with DateTime and Date cursor columns."""
|
||||||
|
|
||||||
@@ -162,6 +171,7 @@ class UserRead(PydanticBase):
|
|||||||
|
|
||||||
id: uuid.UUID
|
id: uuid.UUID
|
||||||
username: str
|
username: str
|
||||||
|
is_active: bool = True
|
||||||
|
|
||||||
|
|
||||||
class UserUpdate(BaseModel):
|
class UserUpdate(BaseModel):
|
||||||
@@ -218,12 +228,26 @@ class PostM2MUpdate(BaseModel):
|
|||||||
tag_ids: list[uuid.UUID] | None = None
|
tag_ids: list[uuid.UUID] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class IntRoleRead(PydanticBase):
|
||||||
|
"""Schema for reading an IntRole."""
|
||||||
|
|
||||||
|
id: int
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
class IntRoleCreate(BaseModel):
|
class IntRoleCreate(BaseModel):
|
||||||
"""Schema for creating an IntRole."""
|
"""Schema for creating an IntRole."""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
|
||||||
|
class EventRead(PydanticBase):
|
||||||
|
"""Schema for reading an Event."""
|
||||||
|
|
||||||
|
id: uuid.UUID
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
class EventCreate(BaseModel):
|
class EventCreate(BaseModel):
|
||||||
"""Schema for creating an Event."""
|
"""Schema for creating an Event."""
|
||||||
|
|
||||||
@@ -232,6 +256,13 @@ class EventCreate(BaseModel):
|
|||||||
scheduled_date: datetime.date
|
scheduled_date: datetime.date
|
||||||
|
|
||||||
|
|
||||||
|
class ProductRead(PydanticBase):
|
||||||
|
"""Schema for reading a Product."""
|
||||||
|
|
||||||
|
id: uuid.UUID
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
class ProductCreate(BaseModel):
|
class ProductCreate(BaseModel):
|
||||||
"""Schema for creating a Product."""
|
"""Schema for creating a Product."""
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,10 @@ from .conftest import (
|
|||||||
EventCrud,
|
EventCrud,
|
||||||
EventDateCursorCrud,
|
EventDateCursorCrud,
|
||||||
EventDateTimeCursorCrud,
|
EventDateTimeCursorCrud,
|
||||||
|
EventRead,
|
||||||
IntRoleCreate,
|
IntRoleCreate,
|
||||||
IntRoleCursorCrud,
|
IntRoleCursorCrud,
|
||||||
|
IntRoleRead,
|
||||||
Post,
|
Post,
|
||||||
PostCreate,
|
PostCreate,
|
||||||
PostCrud,
|
PostCrud,
|
||||||
@@ -26,6 +28,7 @@ from .conftest import (
|
|||||||
ProductCreate,
|
ProductCreate,
|
||||||
ProductCrud,
|
ProductCrud,
|
||||||
ProductNumericCursorCrud,
|
ProductNumericCursorCrud,
|
||||||
|
ProductRead,
|
||||||
Role,
|
Role,
|
||||||
RoleCreate,
|
RoleCreate,
|
||||||
RoleCrud,
|
RoleCrud,
|
||||||
@@ -169,7 +172,14 @@ class TestDefaultLoadOptionsIntegration:
|
|||||||
async def test_default_load_options_applied_to_paginate(
|
async def test_default_load_options_applied_to_paginate(
|
||||||
self, db_session: AsyncSession
|
self, db_session: AsyncSession
|
||||||
):
|
):
|
||||||
"""default_load_options loads relationships automatically on paginate()."""
|
"""default_load_options loads relationships automatically on offset_paginate()."""
|
||||||
|
from fastapi_toolsets.schemas import PydanticBase
|
||||||
|
|
||||||
|
class UserWithRoleRead(PydanticBase):
|
||||||
|
id: uuid.UUID
|
||||||
|
username: str
|
||||||
|
role: RoleRead | None = None
|
||||||
|
|
||||||
UserWithDefaultLoad = CrudFactory(
|
UserWithDefaultLoad = CrudFactory(
|
||||||
User, default_load_options=[selectinload(User.role)]
|
User, default_load_options=[selectinload(User.role)]
|
||||||
)
|
)
|
||||||
@@ -178,7 +188,9 @@ class TestDefaultLoadOptionsIntegration:
|
|||||||
db_session,
|
db_session,
|
||||||
UserCreate(username="alice", email="alice@test.com", role_id=role.id),
|
UserCreate(username="alice", email="alice@test.com", role_id=role.id),
|
||||||
)
|
)
|
||||||
result = await UserWithDefaultLoad.paginate(db_session)
|
result = await UserWithDefaultLoad.offset_paginate(
|
||||||
|
db_session, schema=UserWithRoleRead
|
||||||
|
)
|
||||||
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"
|
||||||
|
|
||||||
@@ -430,7 +442,7 @@ class TestCrudDelete:
|
|||||||
role = await RoleCrud.create(db_session, RoleCreate(name="to_delete"))
|
role = await RoleCrud.create(db_session, RoleCreate(name="to_delete"))
|
||||||
result = await RoleCrud.delete(db_session, [Role.id == role.id])
|
result = await RoleCrud.delete(db_session, [Role.id == role.id])
|
||||||
|
|
||||||
assert result is True
|
assert result is None
|
||||||
assert await RoleCrud.first(db_session, [Role.id == role.id]) is None
|
assert await RoleCrud.first(db_session, [Role.id == role.id]) is None
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
@@ -454,6 +466,20 @@ class TestCrudDelete:
|
|||||||
assert len(remaining) == 1
|
assert len(remaining) == 1
|
||||||
assert remaining[0].username == "u3"
|
assert remaining[0].username == "u3"
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_delete_return_response(self, db_session: AsyncSession):
|
||||||
|
"""Delete with return_response=True returns Response[None]."""
|
||||||
|
from fastapi_toolsets.schemas import Response
|
||||||
|
|
||||||
|
role = await RoleCrud.create(db_session, RoleCreate(name="to_delete_resp"))
|
||||||
|
result = await RoleCrud.delete(
|
||||||
|
db_session, [Role.id == role.id], return_response=True
|
||||||
|
)
|
||||||
|
|
||||||
|
assert isinstance(result, Response)
|
||||||
|
assert result.data is None
|
||||||
|
assert await RoleCrud.first(db_session, [Role.id == role.id]) is None
|
||||||
|
|
||||||
|
|
||||||
class TestCrudExists:
|
class TestCrudExists:
|
||||||
"""Tests for CRUD exists operations."""
|
"""Tests for CRUD exists operations."""
|
||||||
@@ -594,7 +620,9 @@ class TestCrudPaginate:
|
|||||||
|
|
||||||
from fastapi_toolsets.schemas import OffsetPagination
|
from fastapi_toolsets.schemas import OffsetPagination
|
||||||
|
|
||||||
result = await RoleCrud.paginate(db_session, page=1, items_per_page=10)
|
result = await RoleCrud.offset_paginate(
|
||||||
|
db_session, page=1, items_per_page=10, schema=RoleRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
assert len(result.data) == 10
|
assert len(result.data) == 10
|
||||||
@@ -609,7 +637,9 @@ class TestCrudPaginate:
|
|||||||
for i in range(25):
|
for i in range(25):
|
||||||
await RoleCrud.create(db_session, RoleCreate(name=f"role{i:02d}"))
|
await RoleCrud.create(db_session, RoleCreate(name=f"role{i:02d}"))
|
||||||
|
|
||||||
result = await RoleCrud.paginate(db_session, page=3, items_per_page=10)
|
result = await RoleCrud.offset_paginate(
|
||||||
|
db_session, page=3, items_per_page=10, schema=RoleRead
|
||||||
|
)
|
||||||
|
|
||||||
assert len(result.data) == 5
|
assert len(result.data) == 5
|
||||||
assert result.pagination.has_more is False
|
assert result.pagination.has_more is False
|
||||||
@@ -629,11 +659,12 @@ class TestCrudPaginate:
|
|||||||
|
|
||||||
from fastapi_toolsets.schemas import OffsetPagination
|
from fastapi_toolsets.schemas import OffsetPagination
|
||||||
|
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
filters=[User.is_active == True], # noqa: E712
|
filters=[User.is_active == True], # noqa: E712
|
||||||
page=1,
|
page=1,
|
||||||
items_per_page=10,
|
items_per_page=10,
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -646,11 +677,12 @@ class TestCrudPaginate:
|
|||||||
await RoleCrud.create(db_session, RoleCreate(name="alpha"))
|
await RoleCrud.create(db_session, RoleCreate(name="alpha"))
|
||||||
await RoleCrud.create(db_session, RoleCreate(name="bravo"))
|
await RoleCrud.create(db_session, RoleCreate(name="bravo"))
|
||||||
|
|
||||||
result = await RoleCrud.paginate(
|
result = await RoleCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
order_by=Role.name,
|
order_by=Role.name,
|
||||||
page=1,
|
page=1,
|
||||||
items_per_page=10,
|
items_per_page=10,
|
||||||
|
schema=RoleRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
names = [r.name for r in result.data]
|
names = [r.name for r in result.data]
|
||||||
@@ -855,12 +887,13 @@ class TestCrudJoins:
|
|||||||
from fastapi_toolsets.schemas import OffsetPagination
|
from fastapi_toolsets.schemas import OffsetPagination
|
||||||
|
|
||||||
# Paginate users with published posts
|
# Paginate users with published posts
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
joins=[(Post, Post.author_id == User.id)],
|
joins=[(Post, Post.author_id == User.id)],
|
||||||
filters=[Post.is_published == True], # noqa: E712
|
filters=[Post.is_published == True], # noqa: E712
|
||||||
page=1,
|
page=1,
|
||||||
items_per_page=10,
|
items_per_page=10,
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -889,12 +922,13 @@ class TestCrudJoins:
|
|||||||
from fastapi_toolsets.schemas import OffsetPagination
|
from fastapi_toolsets.schemas import OffsetPagination
|
||||||
|
|
||||||
# Paginate with outer join
|
# Paginate with outer join
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
joins=[(Post, Post.author_id == User.id)],
|
joins=[(Post, Post.author_id == User.id)],
|
||||||
outer_join=True,
|
outer_join=True,
|
||||||
page=1,
|
page=1,
|
||||||
items_per_page=10,
|
items_per_page=10,
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -931,70 +965,6 @@ class TestCrudJoins:
|
|||||||
assert users[0].username == "multi_join"
|
assert users[0].username == "multi_join"
|
||||||
|
|
||||||
|
|
||||||
class TestAsResponse:
|
|
||||||
"""Tests for as_response parameter (deprecated, kept for backward compat)."""
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_create_as_response(self, db_session: AsyncSession):
|
|
||||||
"""Create with as_response=True returns Response and emits DeprecationWarning."""
|
|
||||||
from fastapi_toolsets.schemas import Response
|
|
||||||
|
|
||||||
data = RoleCreate(name="response_role")
|
|
||||||
with pytest.warns(DeprecationWarning, match="as_response is deprecated"):
|
|
||||||
result = await RoleCrud.create(db_session, data, as_response=True)
|
|
||||||
|
|
||||||
assert isinstance(result, Response)
|
|
||||||
assert result.data is not None
|
|
||||||
assert result.data.name == "response_role"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_get_as_response(self, db_session: AsyncSession):
|
|
||||||
"""Get with as_response=True returns Response and emits DeprecationWarning."""
|
|
||||||
from fastapi_toolsets.schemas import Response
|
|
||||||
|
|
||||||
created = await RoleCrud.create(db_session, RoleCreate(name="get_response"))
|
|
||||||
with pytest.warns(DeprecationWarning, match="as_response is deprecated"):
|
|
||||||
result = await RoleCrud.get(
|
|
||||||
db_session, [Role.id == created.id], as_response=True
|
|
||||||
)
|
|
||||||
|
|
||||||
assert isinstance(result, Response)
|
|
||||||
assert result.data is not None
|
|
||||||
assert result.data.id == created.id
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_update_as_response(self, db_session: AsyncSession):
|
|
||||||
"""Update with as_response=True returns Response and emits DeprecationWarning."""
|
|
||||||
from fastapi_toolsets.schemas import Response
|
|
||||||
|
|
||||||
created = await RoleCrud.create(db_session, RoleCreate(name="old_name"))
|
|
||||||
with pytest.warns(DeprecationWarning, match="as_response is deprecated"):
|
|
||||||
result = await RoleCrud.update(
|
|
||||||
db_session,
|
|
||||||
RoleUpdate(name="new_name"),
|
|
||||||
[Role.id == created.id],
|
|
||||||
as_response=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert isinstance(result, Response)
|
|
||||||
assert result.data is not None
|
|
||||||
assert result.data.name == "new_name"
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_delete_as_response(self, db_session: AsyncSession):
|
|
||||||
"""Delete with as_response=True returns Response and emits DeprecationWarning."""
|
|
||||||
from fastapi_toolsets.schemas import Response
|
|
||||||
|
|
||||||
created = await RoleCrud.create(db_session, RoleCreate(name="to_delete"))
|
|
||||||
with pytest.warns(DeprecationWarning, match="as_response is deprecated"):
|
|
||||||
result = await RoleCrud.delete(
|
|
||||||
db_session, [Role.id == created.id], as_response=True
|
|
||||||
)
|
|
||||||
|
|
||||||
assert isinstance(result, Response)
|
|
||||||
assert result.data is None
|
|
||||||
|
|
||||||
|
|
||||||
class TestCrudFactoryM2M:
|
class TestCrudFactoryM2M:
|
||||||
"""Tests for CrudFactory with m2m_fields parameter."""
|
"""Tests for CrudFactory with m2m_fields parameter."""
|
||||||
|
|
||||||
@@ -1475,92 +1445,35 @@ class TestSchemaResponse:
|
|||||||
assert isinstance(result, Response)
|
assert isinstance(result, Response)
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_paginate_with_schema(self, db_session: AsyncSession):
|
async def test_offset_paginate_with_schema(self, db_session: AsyncSession):
|
||||||
"""paginate with schema returns PaginatedResponse[SchemaType]."""
|
"""offset_paginate with schema returns PaginatedResponse[SchemaType]."""
|
||||||
from fastapi_toolsets.schemas import PaginatedResponse
|
from fastapi_toolsets.schemas import PaginatedResponse
|
||||||
|
|
||||||
await RoleCrud.create(db_session, RoleCreate(name="p_role1"))
|
await RoleCrud.create(db_session, RoleCreate(name="p_role1"))
|
||||||
await RoleCrud.create(db_session, RoleCreate(name="p_role2"))
|
await RoleCrud.create(db_session, RoleCreate(name="p_role2"))
|
||||||
|
|
||||||
result = await RoleCrud.paginate(db_session, schema=RoleRead)
|
result = await RoleCrud.offset_paginate(db_session, schema=RoleRead)
|
||||||
|
|
||||||
assert isinstance(result, PaginatedResponse)
|
assert isinstance(result, PaginatedResponse)
|
||||||
assert len(result.data) == 2
|
assert len(result.data) == 2
|
||||||
assert all(isinstance(item, RoleRead) for item in result.data)
|
assert all(isinstance(item, RoleRead) for item in result.data)
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_paginate_schema_filters_fields(self, db_session: AsyncSession):
|
async def test_offset_paginate_schema_filters_fields(
|
||||||
"""paginate with schema only exposes schema fields per item."""
|
self, db_session: AsyncSession
|
||||||
|
):
|
||||||
|
"""offset_paginate with schema only exposes schema fields per item."""
|
||||||
await UserCrud.create(
|
await UserCrud.create(
|
||||||
db_session,
|
db_session,
|
||||||
UserCreate(username="pg_user", email="pg@test.com"),
|
UserCreate(username="pg_user", email="pg@test.com"),
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(db_session, schema=UserRead)
|
result = await UserCrud.offset_paginate(db_session, schema=UserRead)
|
||||||
|
|
||||||
assert isinstance(result.data[0], UserRead)
|
assert isinstance(result.data[0], UserRead)
|
||||||
assert result.data[0].username == "pg_user"
|
assert result.data[0].username == "pg_user"
|
||||||
assert not hasattr(result.data[0], "email")
|
assert not hasattr(result.data[0], "email")
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_as_response_true_without_schema_unchanged(
|
|
||||||
self, db_session: AsyncSession
|
|
||||||
):
|
|
||||||
"""as_response=True without schema still returns Response[ModelType] with a warning."""
|
|
||||||
from fastapi_toolsets.schemas import Response
|
|
||||||
|
|
||||||
created = await RoleCrud.create(db_session, RoleCreate(name="compat"))
|
|
||||||
with pytest.warns(DeprecationWarning, match="as_response is deprecated"):
|
|
||||||
result = await RoleCrud.get(
|
|
||||||
db_session, [Role.id == created.id], as_response=True
|
|
||||||
)
|
|
||||||
|
|
||||||
assert isinstance(result, Response)
|
|
||||||
assert isinstance(result.data, Role)
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_schema_with_explicit_as_response_true(
|
|
||||||
self, db_session: AsyncSession
|
|
||||||
):
|
|
||||||
"""schema combined with explicit as_response=True works correctly."""
|
|
||||||
from fastapi_toolsets.schemas import Response
|
|
||||||
|
|
||||||
created = await RoleCrud.create(db_session, RoleCreate(name="combined"))
|
|
||||||
result = await RoleCrud.get(
|
|
||||||
db_session,
|
|
||||||
[Role.id == created.id],
|
|
||||||
as_response=True,
|
|
||||||
schema=RoleRead,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert isinstance(result, Response)
|
|
||||||
assert isinstance(result.data, RoleRead)
|
|
||||||
|
|
||||||
|
|
||||||
class TestPaginateAlias:
|
|
||||||
"""Tests that paginate is a backward-compatible alias for offset_paginate."""
|
|
||||||
|
|
||||||
def test_paginate_is_alias_of_offset_paginate(self):
|
|
||||||
"""paginate and offset_paginate are the same underlying function."""
|
|
||||||
assert RoleCrud.paginate.__func__ is RoleCrud.offset_paginate.__func__
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
|
||||||
async def test_paginate_alias_returns_offset_pagination(
|
|
||||||
self, db_session: AsyncSession
|
|
||||||
):
|
|
||||||
"""paginate() still works and returns PaginatedResponse with OffsetPagination."""
|
|
||||||
from fastapi_toolsets.schemas import OffsetPagination, PaginatedResponse
|
|
||||||
|
|
||||||
for i in range(3):
|
|
||||||
await RoleCrud.create(db_session, RoleCreate(name=f"role{i:02d}"))
|
|
||||||
|
|
||||||
result = await RoleCrud.paginate(db_session, page=1, items_per_page=10)
|
|
||||||
|
|
||||||
assert isinstance(result, PaginatedResponse)
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
|
||||||
assert result.pagination.total_count == 3
|
|
||||||
assert result.pagination.page == 1
|
|
||||||
|
|
||||||
|
|
||||||
class TestCursorPaginate:
|
class TestCursorPaginate:
|
||||||
"""Tests for cursor-based pagination via cursor_paginate()."""
|
"""Tests for cursor-based pagination via cursor_paginate()."""
|
||||||
@@ -1573,7 +1486,9 @@ class TestCursorPaginate:
|
|||||||
for i in range(25):
|
for i in range(25):
|
||||||
await RoleCrud.create(db_session, RoleCreate(name=f"role{i:02d}"))
|
await RoleCrud.create(db_session, RoleCreate(name=f"role{i:02d}"))
|
||||||
|
|
||||||
result = await RoleCursorCrud.cursor_paginate(db_session, items_per_page=10)
|
result = await RoleCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=10, schema=RoleRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(result, PaginatedResponse)
|
assert isinstance(result, PaginatedResponse)
|
||||||
assert isinstance(result.pagination, CursorPagination)
|
assert isinstance(result.pagination, CursorPagination)
|
||||||
@@ -1591,7 +1506,9 @@ class TestCursorPaginate:
|
|||||||
for i in range(5):
|
for i in range(5):
|
||||||
await RoleCrud.create(db_session, RoleCreate(name=f"role{i:02d}"))
|
await RoleCrud.create(db_session, RoleCreate(name=f"role{i:02d}"))
|
||||||
|
|
||||||
result = await RoleCursorCrud.cursor_paginate(db_session, items_per_page=10)
|
result = await RoleCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=10, schema=RoleRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, CursorPagination)
|
assert isinstance(result.pagination, CursorPagination)
|
||||||
assert len(result.data) == 5
|
assert len(result.data) == 5
|
||||||
@@ -1606,14 +1523,16 @@ class TestCursorPaginate:
|
|||||||
|
|
||||||
from fastapi_toolsets.schemas import CursorPagination
|
from fastapi_toolsets.schemas import CursorPagination
|
||||||
|
|
||||||
page1 = await RoleCursorCrud.cursor_paginate(db_session, items_per_page=10)
|
page1 = await RoleCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=10, schema=RoleRead
|
||||||
|
)
|
||||||
assert isinstance(page1.pagination, CursorPagination)
|
assert isinstance(page1.pagination, CursorPagination)
|
||||||
assert len(page1.data) == 10
|
assert len(page1.data) == 10
|
||||||
assert page1.pagination.has_more is True
|
assert page1.pagination.has_more is True
|
||||||
|
|
||||||
cursor = page1.pagination.next_cursor
|
cursor = page1.pagination.next_cursor
|
||||||
page2 = await RoleCursorCrud.cursor_paginate(
|
page2 = await RoleCursorCrud.cursor_paginate(
|
||||||
db_session, cursor=cursor, items_per_page=10
|
db_session, cursor=cursor, items_per_page=10, schema=RoleRead
|
||||||
)
|
)
|
||||||
assert isinstance(page2.pagination, CursorPagination)
|
assert isinstance(page2.pagination, CursorPagination)
|
||||||
assert len(page2.data) == 5
|
assert len(page2.data) == 5
|
||||||
@@ -1628,12 +1547,15 @@ class TestCursorPaginate:
|
|||||||
|
|
||||||
from fastapi_toolsets.schemas import CursorPagination
|
from fastapi_toolsets.schemas import CursorPagination
|
||||||
|
|
||||||
page1 = await RoleCursorCrud.cursor_paginate(db_session, items_per_page=4)
|
page1 = await RoleCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=4, schema=RoleRead
|
||||||
|
)
|
||||||
assert isinstance(page1.pagination, CursorPagination)
|
assert isinstance(page1.pagination, CursorPagination)
|
||||||
page2 = await RoleCursorCrud.cursor_paginate(
|
page2 = await RoleCursorCrud.cursor_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
cursor=page1.pagination.next_cursor,
|
cursor=page1.pagination.next_cursor,
|
||||||
items_per_page=4,
|
items_per_page=4,
|
||||||
|
schema=RoleRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
ids_page1 = {r.id for r in page1.data}
|
ids_page1 = {r.id for r in page1.data}
|
||||||
@@ -1646,7 +1568,9 @@ class TestCursorPaginate:
|
|||||||
"""cursor_paginate on an empty table returns empty data with no cursor."""
|
"""cursor_paginate on an empty table returns empty data with no cursor."""
|
||||||
from fastapi_toolsets.schemas import CursorPagination
|
from fastapi_toolsets.schemas import CursorPagination
|
||||||
|
|
||||||
result = await RoleCursorCrud.cursor_paginate(db_session, items_per_page=10)
|
result = await RoleCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=10, schema=RoleRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, CursorPagination)
|
assert isinstance(result.pagination, CursorPagination)
|
||||||
assert result.data == []
|
assert result.data == []
|
||||||
@@ -1671,6 +1595,7 @@ class TestCursorPaginate:
|
|||||||
db_session,
|
db_session,
|
||||||
filters=[User.is_active == True], # noqa: E712
|
filters=[User.is_active == True], # noqa: E712
|
||||||
items_per_page=20,
|
items_per_page=20,
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(result.data) == 5
|
assert len(result.data) == 5
|
||||||
@@ -1703,7 +1628,9 @@ class TestCursorPaginate:
|
|||||||
for i in range(5):
|
for i in range(5):
|
||||||
await RoleNameCrud.create(db_session, RoleCreate(name=f"role{i:02d}"))
|
await RoleNameCrud.create(db_session, RoleCreate(name=f"role{i:02d}"))
|
||||||
|
|
||||||
result = await RoleNameCrud.cursor_paginate(db_session, items_per_page=3)
|
result = await RoleNameCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=3, schema=RoleRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, CursorPagination)
|
assert isinstance(result.pagination, CursorPagination)
|
||||||
assert len(result.data) == 3
|
assert len(result.data) == 3
|
||||||
@@ -1714,7 +1641,7 @@ class TestCursorPaginate:
|
|||||||
async def test_raises_without_cursor_column(self, db_session: AsyncSession):
|
async def test_raises_without_cursor_column(self, db_session: AsyncSession):
|
||||||
"""cursor_paginate raises ValueError when cursor_column is not configured."""
|
"""cursor_paginate raises ValueError when cursor_column is not configured."""
|
||||||
with pytest.raises(ValueError, match="cursor_column is not set"):
|
with pytest.raises(ValueError, match="cursor_column is not set"):
|
||||||
await RoleCrud.cursor_paginate(db_session)
|
await RoleCrud.cursor_paginate(db_session, schema=RoleRead)
|
||||||
|
|
||||||
|
|
||||||
class TestCursorPaginatePrevCursor:
|
class TestCursorPaginatePrevCursor:
|
||||||
@@ -1728,7 +1655,9 @@ class TestCursorPaginatePrevCursor:
|
|||||||
|
|
||||||
from fastapi_toolsets.schemas import CursorPagination
|
from fastapi_toolsets.schemas import CursorPagination
|
||||||
|
|
||||||
result = await RoleCursorCrud.cursor_paginate(db_session, items_per_page=3)
|
result = await RoleCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=3, schema=RoleRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, CursorPagination)
|
assert isinstance(result.pagination, CursorPagination)
|
||||||
assert result.pagination.prev_cursor is None
|
assert result.pagination.prev_cursor is None
|
||||||
@@ -1741,12 +1670,15 @@ class TestCursorPaginatePrevCursor:
|
|||||||
|
|
||||||
from fastapi_toolsets.schemas import CursorPagination
|
from fastapi_toolsets.schemas import CursorPagination
|
||||||
|
|
||||||
page1 = await RoleCursorCrud.cursor_paginate(db_session, items_per_page=5)
|
page1 = await RoleCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=5, schema=RoleRead
|
||||||
|
)
|
||||||
assert isinstance(page1.pagination, CursorPagination)
|
assert isinstance(page1.pagination, CursorPagination)
|
||||||
page2 = await RoleCursorCrud.cursor_paginate(
|
page2 = await RoleCursorCrud.cursor_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
cursor=page1.pagination.next_cursor,
|
cursor=page1.pagination.next_cursor,
|
||||||
items_per_page=5,
|
items_per_page=5,
|
||||||
|
schema=RoleRead,
|
||||||
)
|
)
|
||||||
assert isinstance(page2.pagination, CursorPagination)
|
assert isinstance(page2.pagination, CursorPagination)
|
||||||
assert page2.pagination.prev_cursor is not None
|
assert page2.pagination.prev_cursor is not None
|
||||||
@@ -1762,12 +1694,15 @@ class TestCursorPaginatePrevCursor:
|
|||||||
|
|
||||||
from fastapi_toolsets.schemas import CursorPagination
|
from fastapi_toolsets.schemas import CursorPagination
|
||||||
|
|
||||||
page1 = await RoleCursorCrud.cursor_paginate(db_session, items_per_page=5)
|
page1 = await RoleCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=5, schema=RoleRead
|
||||||
|
)
|
||||||
assert isinstance(page1.pagination, CursorPagination)
|
assert isinstance(page1.pagination, CursorPagination)
|
||||||
page2 = await RoleCursorCrud.cursor_paginate(
|
page2 = await RoleCursorCrud.cursor_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
cursor=page1.pagination.next_cursor,
|
cursor=page1.pagination.next_cursor,
|
||||||
items_per_page=5,
|
items_per_page=5,
|
||||||
|
schema=RoleRead,
|
||||||
)
|
)
|
||||||
assert isinstance(page2.pagination, CursorPagination)
|
assert isinstance(page2.pagination, CursorPagination)
|
||||||
assert page2.pagination.prev_cursor is not None
|
assert page2.pagination.prev_cursor is not None
|
||||||
@@ -1802,6 +1737,7 @@ class TestCursorPaginateWithSearch:
|
|||||||
db_session,
|
db_session,
|
||||||
search="admin",
|
search="admin",
|
||||||
items_per_page=20,
|
items_per_page=20,
|
||||||
|
schema=RoleRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(result.data) == 5
|
assert len(result.data) == 5
|
||||||
@@ -1836,6 +1772,7 @@ class TestCursorPaginateExtraOptions:
|
|||||||
db_session,
|
db_session,
|
||||||
joins=[(Role, User.role_id == Role.id)],
|
joins=[(Role, User.role_id == Role.id)],
|
||||||
items_per_page=20,
|
items_per_page=20,
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, CursorPagination)
|
assert isinstance(result.pagination, CursorPagination)
|
||||||
@@ -1867,6 +1804,7 @@ class TestCursorPaginateExtraOptions:
|
|||||||
joins=[(Role, User.role_id == Role.id)],
|
joins=[(Role, User.role_id == Role.id)],
|
||||||
outer_join=True,
|
outer_join=True,
|
||||||
items_per_page=20,
|
items_per_page=20,
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, CursorPagination)
|
assert isinstance(result.pagination, CursorPagination)
|
||||||
@@ -1876,7 +1814,12 @@ class TestCursorPaginateExtraOptions:
|
|||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_with_load_options(self, db_session: AsyncSession):
|
async def test_with_load_options(self, db_session: AsyncSession):
|
||||||
"""cursor_paginate passes load_options to the query."""
|
"""cursor_paginate passes load_options to the query."""
|
||||||
from fastapi_toolsets.schemas import CursorPagination
|
from fastapi_toolsets.schemas import CursorPagination, PydanticBase
|
||||||
|
|
||||||
|
class UserWithRoleRead(PydanticBase):
|
||||||
|
id: uuid.UUID
|
||||||
|
username: str
|
||||||
|
role: RoleRead | None = None
|
||||||
|
|
||||||
role = await RoleCrud.create(db_session, RoleCreate(name="manager"))
|
role = await RoleCrud.create(db_session, RoleCreate(name="manager"))
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
@@ -1893,6 +1836,7 @@ class TestCursorPaginateExtraOptions:
|
|||||||
db_session,
|
db_session,
|
||||||
load_options=[selectinload(User.role)],
|
load_options=[selectinload(User.role)],
|
||||||
items_per_page=20,
|
items_per_page=20,
|
||||||
|
schema=UserWithRoleRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, CursorPagination)
|
assert isinstance(result.pagination, CursorPagination)
|
||||||
@@ -1912,6 +1856,7 @@ class TestCursorPaginateExtraOptions:
|
|||||||
db_session,
|
db_session,
|
||||||
order_by=Role.name.desc(),
|
order_by=Role.name.desc(),
|
||||||
items_per_page=3,
|
items_per_page=3,
|
||||||
|
schema=RoleRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, CursorPagination)
|
assert isinstance(result.pagination, CursorPagination)
|
||||||
@@ -1925,7 +1870,9 @@ class TestCursorPaginateExtraOptions:
|
|||||||
for i in range(5):
|
for i in range(5):
|
||||||
await IntRoleCursorCrud.create(db_session, IntRoleCreate(name=f"role{i}"))
|
await IntRoleCursorCrud.create(db_session, IntRoleCreate(name=f"role{i}"))
|
||||||
|
|
||||||
page1 = await IntRoleCursorCrud.cursor_paginate(db_session, items_per_page=3)
|
page1 = await IntRoleCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=3, schema=IntRoleRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(page1.pagination, CursorPagination)
|
assert isinstance(page1.pagination, CursorPagination)
|
||||||
assert len(page1.data) == 3
|
assert len(page1.data) == 3
|
||||||
@@ -1935,6 +1882,7 @@ class TestCursorPaginateExtraOptions:
|
|||||||
db_session,
|
db_session,
|
||||||
cursor=page1.pagination.next_cursor,
|
cursor=page1.pagination.next_cursor,
|
||||||
items_per_page=3,
|
items_per_page=3,
|
||||||
|
schema=IntRoleRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(page2.pagination, CursorPagination)
|
assert isinstance(page2.pagination, CursorPagination)
|
||||||
@@ -1955,7 +1903,9 @@ class TestCursorPaginateExtraOptions:
|
|||||||
await RoleCrud.create(db_session, RoleCreate(name="role01"))
|
await RoleCrud.create(db_session, RoleCreate(name="role01"))
|
||||||
|
|
||||||
# First page succeeds (no cursor to decode)
|
# First page succeeds (no cursor to decode)
|
||||||
page1 = await RoleNameCursorCrud.cursor_paginate(db_session, items_per_page=1)
|
page1 = await RoleNameCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=1, schema=RoleRead
|
||||||
|
)
|
||||||
assert page1.pagination.has_more is True
|
assert page1.pagination.has_more is True
|
||||||
assert isinstance(page1.pagination, CursorPagination)
|
assert isinstance(page1.pagination, CursorPagination)
|
||||||
|
|
||||||
@@ -1965,6 +1915,7 @@ class TestCursorPaginateExtraOptions:
|
|||||||
db_session,
|
db_session,
|
||||||
cursor=page1.pagination.next_cursor,
|
cursor=page1.pagination.next_cursor,
|
||||||
items_per_page=1,
|
items_per_page=1,
|
||||||
|
schema=RoleRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -2003,6 +1954,7 @@ class TestCursorPaginateSearchJoins:
|
|||||||
search="administrator",
|
search="administrator",
|
||||||
search_fields=[(User.role, Role.name)],
|
search_fields=[(User.role, Role.name)],
|
||||||
items_per_page=20,
|
items_per_page=20,
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, CursorPagination)
|
assert isinstance(result.pagination, CursorPagination)
|
||||||
@@ -2049,7 +2001,7 @@ class TestCursorPaginateColumnTypes:
|
|||||||
)
|
)
|
||||||
|
|
||||||
page1 = await EventDateTimeCursorCrud.cursor_paginate(
|
page1 = await EventDateTimeCursorCrud.cursor_paginate(
|
||||||
db_session, items_per_page=3
|
db_session, items_per_page=3, schema=EventRead
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(page1.pagination, CursorPagination)
|
assert isinstance(page1.pagination, CursorPagination)
|
||||||
@@ -2060,6 +2012,7 @@ class TestCursorPaginateColumnTypes:
|
|||||||
db_session,
|
db_session,
|
||||||
cursor=page1.pagination.next_cursor,
|
cursor=page1.pagination.next_cursor,
|
||||||
items_per_page=3,
|
items_per_page=3,
|
||||||
|
schema=EventRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(page2.pagination, CursorPagination)
|
assert isinstance(page2.pagination, CursorPagination)
|
||||||
@@ -2087,7 +2040,9 @@ class TestCursorPaginateColumnTypes:
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
page1 = await EventDateCursorCrud.cursor_paginate(db_session, items_per_page=3)
|
page1 = await EventDateCursorCrud.cursor_paginate(
|
||||||
|
db_session, items_per_page=3, schema=EventRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(page1.pagination, CursorPagination)
|
assert isinstance(page1.pagination, CursorPagination)
|
||||||
assert len(page1.data) == 3
|
assert len(page1.data) == 3
|
||||||
@@ -2097,6 +2052,7 @@ class TestCursorPaginateColumnTypes:
|
|||||||
db_session,
|
db_session,
|
||||||
cursor=page1.pagination.next_cursor,
|
cursor=page1.pagination.next_cursor,
|
||||||
items_per_page=3,
|
items_per_page=3,
|
||||||
|
schema=EventRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(page2.pagination, CursorPagination)
|
assert isinstance(page2.pagination, CursorPagination)
|
||||||
@@ -2123,7 +2079,7 @@ class TestCursorPaginateColumnTypes:
|
|||||||
)
|
)
|
||||||
|
|
||||||
page1 = await ProductNumericCursorCrud.cursor_paginate(
|
page1 = await ProductNumericCursorCrud.cursor_paginate(
|
||||||
db_session, items_per_page=3
|
db_session, items_per_page=3, schema=ProductRead
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(page1.pagination, CursorPagination)
|
assert isinstance(page1.pagination, CursorPagination)
|
||||||
@@ -2134,6 +2090,7 @@ class TestCursorPaginateColumnTypes:
|
|||||||
db_session,
|
db_session,
|
||||||
cursor=page1.pagination.next_cursor,
|
cursor=page1.pagination.next_cursor,
|
||||||
items_per_page=3,
|
items_per_page=3,
|
||||||
|
schema=ProductRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(page2.pagination, CursorPagination)
|
assert isinstance(page2.pagination, CursorPagination)
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
"""Tests for CRUD search functionality."""
|
"""Tests for CRUD search functionality."""
|
||||||
|
|
||||||
|
import inspect
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from sqlalchemy.sql.elements import ColumnElement, UnaryExpression
|
||||||
|
|
||||||
from fastapi_toolsets.crud import (
|
from fastapi_toolsets.crud import (
|
||||||
CrudFactory,
|
CrudFactory,
|
||||||
@@ -11,6 +13,7 @@ from fastapi_toolsets.crud import (
|
|||||||
SearchConfig,
|
SearchConfig,
|
||||||
get_searchable_fields,
|
get_searchable_fields,
|
||||||
)
|
)
|
||||||
|
from fastapi_toolsets.exceptions import InvalidOrderFieldError
|
||||||
from fastapi_toolsets.schemas import OffsetPagination
|
from fastapi_toolsets.schemas import OffsetPagination
|
||||||
|
|
||||||
from .conftest import (
|
from .conftest import (
|
||||||
@@ -20,6 +23,7 @@ from .conftest import (
|
|||||||
User,
|
User,
|
||||||
UserCreate,
|
UserCreate,
|
||||||
UserCrud,
|
UserCrud,
|
||||||
|
UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -39,10 +43,11 @@ class TestPaginateSearch:
|
|||||||
db_session, UserCreate(username="bob_smith", email="bob@test.com")
|
db_session, UserCreate(username="bob_smith", email="bob@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search="doe",
|
search="doe",
|
||||||
search_fields=[User.username],
|
search_fields=[User.username],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -58,10 +63,11 @@ class TestPaginateSearch:
|
|||||||
db_session, UserCreate(username="company_bob", email="bob@other.com")
|
db_session, UserCreate(username="company_bob", email="bob@other.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search="company",
|
search="company",
|
||||||
search_fields=[User.username, User.email],
|
search_fields=[User.username, User.email],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -86,10 +92,11 @@ class TestPaginateSearch:
|
|||||||
UserCreate(username="user1", email="u1@test.com", role_id=user_role.id),
|
UserCreate(username="user1", email="u1@test.com", role_id=user_role.id),
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search="admin",
|
search="admin",
|
||||||
search_fields=[(User.role, Role.name)],
|
search_fields=[(User.role, Role.name)],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -105,10 +112,11 @@ class TestPaginateSearch:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Search "admin" in username OR role.name
|
# Search "admin" in username OR role.name
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search="admin",
|
search="admin",
|
||||||
search_fields=[User.username, (User.role, Role.name)],
|
search_fields=[User.username, (User.role, Role.name)],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -121,10 +129,11 @@ class TestPaginateSearch:
|
|||||||
db_session, UserCreate(username="JohnDoe", email="j@test.com")
|
db_session, UserCreate(username="JohnDoe", email="j@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search="johndoe",
|
search="johndoe",
|
||||||
search_fields=[User.username],
|
search_fields=[User.username],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -138,19 +147,21 @@ class TestPaginateSearch:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Should not find (case mismatch)
|
# Should not find (case mismatch)
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search=SearchConfig(query="johndoe", case_sensitive=True),
|
search=SearchConfig(query="johndoe", case_sensitive=True),
|
||||||
search_fields=[User.username],
|
search_fields=[User.username],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
assert result.pagination.total_count == 0
|
assert result.pagination.total_count == 0
|
||||||
|
|
||||||
# Should find (case match)
|
# Should find (case match)
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search=SearchConfig(query="JohnDoe", case_sensitive=True),
|
search=SearchConfig(query="JohnDoe", case_sensitive=True),
|
||||||
search_fields=[User.username],
|
search_fields=[User.username],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
assert result.pagination.total_count == 1
|
assert result.pagination.total_count == 1
|
||||||
@@ -165,11 +176,13 @@ class TestPaginateSearch:
|
|||||||
db_session, UserCreate(username="user2", email="u2@test.com")
|
db_session, UserCreate(username="user2", email="u2@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(db_session, search="")
|
result = await UserCrud.offset_paginate(db_session, search="", schema=UserRead)
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
assert result.pagination.total_count == 2
|
assert result.pagination.total_count == 2
|
||||||
|
|
||||||
result = await UserCrud.paginate(db_session, search=None)
|
result = await UserCrud.offset_paginate(
|
||||||
|
db_session, search=None, schema=UserRead
|
||||||
|
)
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
assert result.pagination.total_count == 2
|
assert result.pagination.total_count == 2
|
||||||
|
|
||||||
@@ -185,11 +198,12 @@ class TestPaginateSearch:
|
|||||||
UserCreate(username="inactive_john", email="ij@test.com", is_active=False),
|
UserCreate(username="inactive_john", email="ij@test.com", is_active=False),
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
filters=[User.is_active == True], # noqa: E712
|
filters=[User.is_active == True], # noqa: E712
|
||||||
search="john",
|
search="john",
|
||||||
search_fields=[User.username],
|
search_fields=[User.username],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -203,7 +217,9 @@ class TestPaginateSearch:
|
|||||||
db_session, UserCreate(username="findme", email="other@test.com")
|
db_session, UserCreate(username="findme", email="other@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(db_session, search="findme")
|
result = await UserCrud.offset_paginate(
|
||||||
|
db_session, search="findme", schema=UserRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
assert result.pagination.total_count == 1
|
assert result.pagination.total_count == 1
|
||||||
@@ -215,10 +231,11 @@ class TestPaginateSearch:
|
|||||||
db_session, UserCreate(username="john", email="j@test.com")
|
db_session, UserCreate(username="john", email="j@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search="nonexistent",
|
search="nonexistent",
|
||||||
search_fields=[User.username],
|
search_fields=[User.username],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -234,12 +251,13 @@ class TestPaginateSearch:
|
|||||||
UserCreate(username=f"user_{i}", email=f"user{i}@test.com"),
|
UserCreate(username=f"user_{i}", email=f"user{i}@test.com"),
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search="user_",
|
search="user_",
|
||||||
search_fields=[User.username],
|
search_fields=[User.username],
|
||||||
page=1,
|
page=1,
|
||||||
items_per_page=5,
|
items_per_page=5,
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -261,10 +279,11 @@ class TestPaginateSearch:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Search in username, not in role
|
# Search in username, not in role
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search="role",
|
search="role",
|
||||||
search_fields=[User.username],
|
search_fields=[User.username],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -283,11 +302,12 @@ class TestPaginateSearch:
|
|||||||
db_session, UserCreate(username="bob", email="b@test.com")
|
db_session, UserCreate(username="bob", email="b@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search="@test.com",
|
search="@test.com",
|
||||||
search_fields=[User.email],
|
search_fields=[User.email],
|
||||||
order_by=User.username,
|
order_by=User.username,
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -307,10 +327,11 @@ class TestPaginateSearch:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Search by UUID (partial match)
|
# Search by UUID (partial match)
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search="12345678",
|
search="12345678",
|
||||||
search_fields=[User.id, User.username],
|
search_fields=[User.id, User.username],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -360,10 +381,11 @@ class TestSearchConfig:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 'john' must be in username AND email
|
# 'john' must be in username AND email
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search=SearchConfig(query="john", match_mode="all"),
|
search=SearchConfig(query="john", match_mode="all"),
|
||||||
search_fields=[User.username, User.email],
|
search_fields=[User.username, User.email],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -377,9 +399,10 @@ class TestSearchConfig:
|
|||||||
db_session, UserCreate(username="test", email="findme@test.com")
|
db_session, UserCreate(username="test", email="findme@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.paginate(
|
result = await UserCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
search=SearchConfig(query="findme", fields=[User.email]),
|
search=SearchConfig(query="findme", fields=[User.email]),
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -407,7 +430,7 @@ class TestNoSearchableFieldsError:
|
|||||||
from fastapi_toolsets.exceptions import NoSearchableFieldsError
|
from fastapi_toolsets.exceptions import NoSearchableFieldsError
|
||||||
|
|
||||||
error = NoSearchableFieldsError(User)
|
error = NoSearchableFieldsError(User)
|
||||||
assert "User" in str(error)
|
assert "User" in error.api_error.desc
|
||||||
assert error.model is User
|
assert error.model is User
|
||||||
|
|
||||||
def test_error_raised_when_no_fields(self):
|
def test_error_raised_when_no_fields(self):
|
||||||
@@ -431,7 +454,7 @@ class TestNoSearchableFieldsError:
|
|||||||
build_search_filters(NoStringModel, "test")
|
build_search_filters(NoStringModel, "test")
|
||||||
|
|
||||||
assert exc_info.value.model is NoStringModel
|
assert exc_info.value.model is NoStringModel
|
||||||
assert "NoStringModel" in str(exc_info.value)
|
assert "NoStringModel" in exc_info.value.api_error.desc
|
||||||
|
|
||||||
|
|
||||||
class TestGetSearchableFields:
|
class TestGetSearchableFields:
|
||||||
@@ -475,7 +498,7 @@ class TestFacetsNotSet:
|
|||||||
db_session, UserCreate(username="alice", email="a@test.com")
|
db_session, UserCreate(username="alice", email="a@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCrud.offset_paginate(db_session)
|
result = await UserCrud.offset_paginate(db_session, schema=UserRead)
|
||||||
|
|
||||||
assert result.filter_attributes is None
|
assert result.filter_attributes is None
|
||||||
|
|
||||||
@@ -487,7 +510,7 @@ class TestFacetsNotSet:
|
|||||||
db_session, UserCreate(username="alice", email="a@test.com")
|
db_session, UserCreate(username="alice", email="a@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserCursorCrud.cursor_paginate(db_session)
|
result = await UserCursorCrud.cursor_paginate(db_session, schema=UserRead)
|
||||||
|
|
||||||
assert result.filter_attributes is None
|
assert result.filter_attributes is None
|
||||||
|
|
||||||
@@ -506,7 +529,7 @@ class TestFacetsDirectColumn:
|
|||||||
db_session, UserCreate(username="bob", email="b@test.com")
|
db_session, UserCreate(username="bob", email="b@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserFacetCrud.offset_paginate(db_session)
|
result = await UserFacetCrud.offset_paginate(db_session, schema=UserRead)
|
||||||
|
|
||||||
assert result.filter_attributes is not None
|
assert result.filter_attributes is not None
|
||||||
# Distinct usernames, sorted
|
# Distinct usernames, sorted
|
||||||
@@ -525,7 +548,7 @@ class TestFacetsDirectColumn:
|
|||||||
db_session, UserCreate(username="bob", email="b@test.com")
|
db_session, UserCreate(username="bob", email="b@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserFacetCursorCrud.cursor_paginate(db_session)
|
result = await UserFacetCursorCrud.cursor_paginate(db_session, schema=UserRead)
|
||||||
|
|
||||||
assert result.filter_attributes is not None
|
assert result.filter_attributes is not None
|
||||||
assert set(result.filter_attributes["email"]) == {"a@test.com", "b@test.com"}
|
assert set(result.filter_attributes["email"]) == {"a@test.com", "b@test.com"}
|
||||||
@@ -541,7 +564,7 @@ class TestFacetsDirectColumn:
|
|||||||
db_session, UserCreate(username="bob", email="b@test.com")
|
db_session, UserCreate(username="bob", email="b@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserFacetCrud.offset_paginate(db_session)
|
result = await UserFacetCrud.offset_paginate(db_session, schema=UserRead)
|
||||||
|
|
||||||
assert result.filter_attributes is not None
|
assert result.filter_attributes is not None
|
||||||
assert "username" in result.filter_attributes
|
assert "username" in result.filter_attributes
|
||||||
@@ -558,7 +581,7 @@ class TestFacetsDirectColumn:
|
|||||||
|
|
||||||
# Override: ask for email instead of username
|
# Override: ask for email instead of username
|
||||||
result = await UserFacetCrud.offset_paginate(
|
result = await UserFacetCrud.offset_paginate(
|
||||||
db_session, facet_fields=[User.email]
|
db_session, facet_fields=[User.email], schema=UserRead
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result.filter_attributes is not None
|
assert result.filter_attributes is not None
|
||||||
@@ -584,6 +607,7 @@ class TestFacetsRespectFilters:
|
|||||||
result = await UserFacetCrud.offset_paginate(
|
result = await UserFacetCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
filters=[User.is_active == True], # noqa: E712
|
filters=[User.is_active == True], # noqa: E712
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result.filter_attributes is not None
|
assert result.filter_attributes is not None
|
||||||
@@ -614,7 +638,7 @@ class TestFacetsRelationship:
|
|||||||
db_session, UserCreate(username="charlie", email="c@test.com")
|
db_session, UserCreate(username="charlie", email="c@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserRelFacetCrud.offset_paginate(db_session)
|
result = await UserRelFacetCrud.offset_paginate(db_session, schema=UserRead)
|
||||||
|
|
||||||
assert result.filter_attributes is not None
|
assert result.filter_attributes is not None
|
||||||
assert set(result.filter_attributes["name"]) == {"admin", "editor"}
|
assert set(result.filter_attributes["name"]) == {"admin", "editor"}
|
||||||
@@ -629,7 +653,7 @@ class TestFacetsRelationship:
|
|||||||
db_session, UserCreate(username="norole", email="n@test.com")
|
db_session, UserCreate(username="norole", email="n@test.com")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await UserRelFacetCrud.offset_paginate(db_session)
|
result = await UserRelFacetCrud.offset_paginate(db_session, schema=UserRead)
|
||||||
|
|
||||||
assert result.filter_attributes is not None
|
assert result.filter_attributes is not None
|
||||||
assert result.filter_attributes["name"] == []
|
assert result.filter_attributes["name"] == []
|
||||||
@@ -653,7 +677,10 @@ class TestFacetsRelationship:
|
|||||||
)
|
)
|
||||||
|
|
||||||
result = await UserSearchFacetCrud.offset_paginate(
|
result = await UserSearchFacetCrud.offset_paginate(
|
||||||
db_session, search="admin", search_fields=[(User.role, Role.name)]
|
db_session,
|
||||||
|
search="admin",
|
||||||
|
search_fields=[(User.role, Role.name)],
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result.filter_attributes is not None
|
assert result.filter_attributes is not None
|
||||||
@@ -675,7 +702,7 @@ class TestFilterBy:
|
|||||||
)
|
)
|
||||||
|
|
||||||
result = await UserFacetCrud.offset_paginate(
|
result = await UserFacetCrud.offset_paginate(
|
||||||
db_session, filter_by={"username": "alice"}
|
db_session, filter_by={"username": "alice"}, schema=UserRead
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(result.data) == 1
|
assert len(result.data) == 1
|
||||||
@@ -698,7 +725,7 @@ class TestFilterBy:
|
|||||||
)
|
)
|
||||||
|
|
||||||
result = await UserFacetCrud.offset_paginate(
|
result = await UserFacetCrud.offset_paginate(
|
||||||
db_session, filter_by={"username": ["alice", "bob"]}
|
db_session, filter_by={"username": ["alice", "bob"]}, schema=UserRead
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -723,7 +750,7 @@ class TestFilterBy:
|
|||||||
)
|
)
|
||||||
|
|
||||||
result = await UserRelFacetCrud.offset_paginate(
|
result = await UserRelFacetCrud.offset_paginate(
|
||||||
db_session, filter_by={"name": "admin"}
|
db_session, filter_by={"name": "admin"}, schema=UserRead
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -746,6 +773,7 @@ class TestFilterBy:
|
|||||||
db_session,
|
db_session,
|
||||||
filters=[User.is_active == True], # noqa: E712
|
filters=[User.is_active == True], # noqa: E712
|
||||||
filter_by={"username": ["alice", "alice2"]},
|
filter_by={"username": ["alice", "alice2"]},
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Only alice passes both: is_active=True AND username IN [alice, alice2]
|
# Only alice passes both: is_active=True AND username IN [alice, alice2]
|
||||||
@@ -760,7 +788,7 @@ class TestFilterBy:
|
|||||||
|
|
||||||
with pytest.raises(InvalidFacetFilterError) as exc_info:
|
with pytest.raises(InvalidFacetFilterError) as exc_info:
|
||||||
await UserFacetCrud.offset_paginate(
|
await UserFacetCrud.offset_paginate(
|
||||||
db_session, filter_by={"nonexistent": "value"}
|
db_session, filter_by={"nonexistent": "value"}, schema=UserRead
|
||||||
)
|
)
|
||||||
|
|
||||||
assert exc_info.value.key == "nonexistent"
|
assert exc_info.value.key == "nonexistent"
|
||||||
@@ -792,6 +820,7 @@ class TestFilterBy:
|
|||||||
result = await UserRoleFacetCrud.offset_paginate(
|
result = await UserRoleFacetCrud.offset_paginate(
|
||||||
db_session,
|
db_session,
|
||||||
filter_by={"name": "admin", "id": str(admin.id)},
|
filter_by={"name": "admin", "id": str(admin.id)},
|
||||||
|
schema=UserRead,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -812,7 +841,7 @@ class TestFilterBy:
|
|||||||
)
|
)
|
||||||
|
|
||||||
result = await UserFacetCursorCrud.cursor_paginate(
|
result = await UserFacetCursorCrud.cursor_paginate(
|
||||||
db_session, filter_by={"username": "alice"}
|
db_session, filter_by={"username": "alice"}, schema=UserRead
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(result.data) == 1
|
assert len(result.data) == 1
|
||||||
@@ -836,7 +865,7 @@ class TestFilterBy:
|
|||||||
)
|
)
|
||||||
|
|
||||||
result = await UserFacetCrud.offset_paginate(
|
result = await UserFacetCrud.offset_paginate(
|
||||||
db_session, filter_by=UserFilter(username="alice")
|
db_session, filter_by=UserFilter(username="alice"), schema=UserRead
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
@@ -862,7 +891,7 @@ class TestFilterBy:
|
|||||||
)
|
)
|
||||||
|
|
||||||
result = await UserFacetCursorCrud.cursor_paginate(
|
result = await UserFacetCursorCrud.cursor_paginate(
|
||||||
db_session, filter_by=UserFilter(username="alice")
|
db_session, filter_by=UserFilter(username="alice"), schema=UserRead
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(result.data) == 1
|
assert len(result.data) == 1
|
||||||
@@ -971,7 +1000,9 @@ class TestFilterParamsSchema:
|
|||||||
|
|
||||||
dep = UserFacetCrud.filter_params()
|
dep = UserFacetCrud.filter_params()
|
||||||
f = await dep(username=["alice"])
|
f = await dep(username=["alice"])
|
||||||
result = await UserFacetCrud.offset_paginate(db_session, filter_by=f)
|
result = await UserFacetCrud.offset_paginate(
|
||||||
|
db_session, filter_by=f, schema=UserRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
assert result.pagination.total_count == 1
|
assert result.pagination.total_count == 1
|
||||||
@@ -992,7 +1023,9 @@ class TestFilterParamsSchema:
|
|||||||
|
|
||||||
dep = UserFacetCursorCrud.filter_params()
|
dep = UserFacetCursorCrud.filter_params()
|
||||||
f = await dep(username=["alice"])
|
f = await dep(username=["alice"])
|
||||||
result = await UserFacetCursorCrud.cursor_paginate(db_session, filter_by=f)
|
result = await UserFacetCursorCrud.cursor_paginate(
|
||||||
|
db_session, filter_by=f, schema=UserRead
|
||||||
|
)
|
||||||
|
|
||||||
assert len(result.data) == 1
|
assert len(result.data) == 1
|
||||||
assert result.data[0].username == "alice"
|
assert result.data[0].username == "alice"
|
||||||
@@ -1010,7 +1043,150 @@ class TestFilterParamsSchema:
|
|||||||
|
|
||||||
dep = UserFacetCrud.filter_params()
|
dep = UserFacetCrud.filter_params()
|
||||||
f = await dep() # all fields None
|
f = await dep() # all fields None
|
||||||
result = await UserFacetCrud.offset_paginate(db_session, filter_by=f)
|
result = await UserFacetCrud.offset_paginate(
|
||||||
|
db_session, filter_by=f, schema=UserRead
|
||||||
|
)
|
||||||
|
|
||||||
assert isinstance(result.pagination, OffsetPagination)
|
assert isinstance(result.pagination, OffsetPagination)
|
||||||
assert result.pagination.total_count == 2
|
assert result.pagination.total_count == 2
|
||||||
|
|
||||||
|
|
||||||
|
class TestOrderParamsSchema:
|
||||||
|
"""Tests for AsyncCrud.order_params()."""
|
||||||
|
|
||||||
|
def test_generates_order_by_and_order_params(self):
|
||||||
|
"""Returned dependency has order_by and order query params."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username, User.email])
|
||||||
|
dep = UserOrderCrud.order_params()
|
||||||
|
|
||||||
|
param_names = set(inspect.signature(dep).parameters)
|
||||||
|
assert param_names == {"order_by", "order"}
|
||||||
|
|
||||||
|
def test_dependency_name_includes_model_name(self):
|
||||||
|
"""Dependency function is named after the model."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username])
|
||||||
|
dep = UserOrderCrud.order_params()
|
||||||
|
assert getattr(dep, "__name__") == "UserOrderParams"
|
||||||
|
|
||||||
|
def test_raises_when_no_order_fields(self):
|
||||||
|
"""ValueError raised when no order_fields are configured or provided."""
|
||||||
|
with pytest.raises(ValueError, match="no order_fields"):
|
||||||
|
UserCrud.order_params()
|
||||||
|
|
||||||
|
def test_order_fields_override(self):
|
||||||
|
"""order_fields= parameter overrides the class-level default."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username, User.email])
|
||||||
|
dep = UserOrderCrud.order_params(order_fields=[User.email])
|
||||||
|
|
||||||
|
param_names = set(inspect.signature(dep).parameters)
|
||||||
|
assert "order_by" in param_names
|
||||||
|
# description should only mention email, not username
|
||||||
|
sig = inspect.signature(dep)
|
||||||
|
description = sig.parameters["order_by"].default.description
|
||||||
|
assert "email" in description
|
||||||
|
assert "username" not in description
|
||||||
|
|
||||||
|
def test_order_by_description_lists_valid_fields(self):
|
||||||
|
"""order_by query param description mentions each allowed field."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username, User.email])
|
||||||
|
dep = UserOrderCrud.order_params()
|
||||||
|
|
||||||
|
sig = inspect.signature(dep)
|
||||||
|
description = sig.parameters["order_by"].default.description
|
||||||
|
assert "username" in description
|
||||||
|
assert "email" in description
|
||||||
|
|
||||||
|
def test_default_order_reflected_in_order_default(self):
|
||||||
|
"""default_order is used as the default value for order."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username])
|
||||||
|
dep_asc = UserOrderCrud.order_params(default_order="asc")
|
||||||
|
dep_desc = UserOrderCrud.order_params(default_order="desc")
|
||||||
|
|
||||||
|
sig_asc = inspect.signature(dep_asc)
|
||||||
|
sig_desc = inspect.signature(dep_desc)
|
||||||
|
assert sig_asc.parameters["order"].default.default == "asc"
|
||||||
|
assert sig_desc.parameters["order"].default.default == "desc"
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_no_order_by_no_default_returns_none(self):
|
||||||
|
"""Returns None when order_by is absent and no default_field is set."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username])
|
||||||
|
dep = UserOrderCrud.order_params()
|
||||||
|
result = await dep(order_by=None, order="asc")
|
||||||
|
assert result is None
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_no_order_by_with_default_field_returns_asc_expression(self):
|
||||||
|
"""Returns default_field.asc() when order_by absent and order=asc."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username])
|
||||||
|
dep = UserOrderCrud.order_params(default_field=User.username)
|
||||||
|
result = await dep(order_by=None, order="asc")
|
||||||
|
assert isinstance(result, UnaryExpression)
|
||||||
|
assert "ASC" in str(result)
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_no_order_by_with_default_field_returns_desc_expression(self):
|
||||||
|
"""Returns default_field.desc() when order_by absent and order=desc."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username])
|
||||||
|
dep = UserOrderCrud.order_params(default_field=User.username)
|
||||||
|
result = await dep(order_by=None, order="desc")
|
||||||
|
assert isinstance(result, UnaryExpression)
|
||||||
|
assert "DESC" in str(result)
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_valid_order_by_asc(self):
|
||||||
|
"""Returns field.asc() for a valid order_by with order=asc."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username])
|
||||||
|
dep = UserOrderCrud.order_params()
|
||||||
|
result = await dep(order_by="username", order="asc")
|
||||||
|
assert isinstance(result, UnaryExpression)
|
||||||
|
assert "ASC" in str(result)
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_valid_order_by_desc(self):
|
||||||
|
"""Returns field.desc() for a valid order_by with order=desc."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username])
|
||||||
|
dep = UserOrderCrud.order_params()
|
||||||
|
result = await dep(order_by="username", order="desc")
|
||||||
|
assert isinstance(result, UnaryExpression)
|
||||||
|
assert "DESC" in str(result)
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_invalid_order_by_raises_invalid_order_field_error(self):
|
||||||
|
"""Raises InvalidOrderFieldError for an unknown order_by value."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username])
|
||||||
|
dep = UserOrderCrud.order_params()
|
||||||
|
with pytest.raises(InvalidOrderFieldError) as exc_info:
|
||||||
|
await dep(order_by="nonexistent", order="asc")
|
||||||
|
assert exc_info.value.field == "nonexistent"
|
||||||
|
assert "username" in exc_info.value.valid_fields
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_multiple_fields_all_resolve(self):
|
||||||
|
"""All configured fields resolve correctly via order_by."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username, User.email])
|
||||||
|
dep = UserOrderCrud.order_params()
|
||||||
|
result_username = await dep(order_by="username", order="asc")
|
||||||
|
result_email = await dep(order_by="email", order="desc")
|
||||||
|
assert isinstance(result_username, ColumnElement)
|
||||||
|
assert isinstance(result_email, ColumnElement)
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_order_params_integrates_with_get_multi(
|
||||||
|
self, db_session: AsyncSession
|
||||||
|
):
|
||||||
|
"""order_params output is accepted by get_multi(order_by=...)."""
|
||||||
|
UserOrderCrud = CrudFactory(User, order_fields=[User.username])
|
||||||
|
await UserCrud.create(
|
||||||
|
db_session, UserCreate(username="charlie", email="c@test.com")
|
||||||
|
)
|
||||||
|
await UserCrud.create(
|
||||||
|
db_session, UserCreate(username="alice", email="a@test.com")
|
||||||
|
)
|
||||||
|
|
||||||
|
dep = UserOrderCrud.order_params()
|
||||||
|
order_by = await dep(order_by="username", order="asc")
|
||||||
|
results = await UserOrderCrud.get_multi(db_session, order_by=order_by)
|
||||||
|
|
||||||
|
assert results[0].username == "alice"
|
||||||
|
assert results[1].username == "charlie"
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ from fastapi_toolsets.db import (
|
|||||||
lock_tables,
|
lock_tables,
|
||||||
wait_for_row_change,
|
wait_for_row_change,
|
||||||
)
|
)
|
||||||
|
from fastapi_toolsets.exceptions import NotFoundError
|
||||||
|
|
||||||
from .conftest import DATABASE_URL, Base, Role, RoleCrud, User
|
from .conftest import DATABASE_URL, Base, Role, RoleCrud, User
|
||||||
|
|
||||||
@@ -307,9 +308,9 @@ class TestWaitForRowChange:
|
|||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_nonexistent_row_raises(self, db_session: AsyncSession):
|
async def test_nonexistent_row_raises(self, db_session: AsyncSession):
|
||||||
"""Raises LookupError when the row does not exist."""
|
"""Raises NotFoundError when the row does not exist."""
|
||||||
fake_id = uuid.uuid4()
|
fake_id = uuid.uuid4()
|
||||||
with pytest.raises(LookupError, match="not found"):
|
with pytest.raises(NotFoundError, match="not found"):
|
||||||
await wait_for_row_change(db_session, Role, fake_id, interval=0.05)
|
await wait_for_row_change(db_session, Role, fake_id, interval=0.05)
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
@@ -326,7 +327,7 @@ class TestWaitForRowChange:
|
|||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_deleted_row_raises(self, db_session: AsyncSession, engine):
|
async def test_deleted_row_raises(self, db_session: AsyncSession, engine):
|
||||||
"""Raises LookupError when the row is deleted during polling."""
|
"""Raises NotFoundError when the row is deleted during polling."""
|
||||||
role = Role(name="delete_role")
|
role = Role(name="delete_role")
|
||||||
db_session.add(role)
|
db_session.add(role)
|
||||||
await db_session.commit()
|
await db_session.commit()
|
||||||
@@ -340,6 +341,6 @@ class TestWaitForRowChange:
|
|||||||
await other.commit()
|
await other.commit()
|
||||||
|
|
||||||
delete_task = asyncio.create_task(delete_later())
|
delete_task = asyncio.create_task(delete_later())
|
||||||
with pytest.raises(LookupError):
|
with pytest.raises(NotFoundError):
|
||||||
await wait_for_row_change(db_session, Role, role.id, interval=0.05)
|
await wait_for_row_change(db_session, Role, role.id, interval=0.05)
|
||||||
await delete_task
|
await delete_task
|
||||||
|
|||||||
395
tests/test_example_pagination_search.py
Normal file
395
tests/test_example_pagination_search.py
Normal file
@@ -0,0 +1,395 @@
|
|||||||
|
"""Live test for the docs/examples/pagination-search.md example.
|
||||||
|
|
||||||
|
Spins up the exact FastAPI app described in the example (sourced from
|
||||||
|
docs_src/examples/pagination_search/) and exercises it through a real HTTP
|
||||||
|
client against a real PostgreSQL database.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from httpx import ASGITransport, AsyncClient
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||||
|
|
||||||
|
from docs_src.examples.pagination_search.db import get_db
|
||||||
|
from docs_src.examples.pagination_search.models import Article, Base, Category
|
||||||
|
from docs_src.examples.pagination_search.routes import router
|
||||||
|
from fastapi_toolsets.exceptions import init_exceptions_handlers
|
||||||
|
|
||||||
|
from .conftest import DATABASE_URL
|
||||||
|
|
||||||
|
|
||||||
|
def build_app(session: AsyncSession) -> FastAPI:
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
async def override_get_db():
|
||||||
|
yield session
|
||||||
|
|
||||||
|
app.dependency_overrides[get_db] = override_get_db
|
||||||
|
app.include_router(router)
|
||||||
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="function")
|
||||||
|
async def ex_db_session():
|
||||||
|
"""Isolated session for the example models (separate tables from conftest)."""
|
||||||
|
engine = create_async_engine(DATABASE_URL, echo=False)
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
|
||||||
|
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
||||||
|
session = session_factory()
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield session
|
||||||
|
finally:
|
||||||
|
await session.close()
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
await conn.run_sync(Base.metadata.drop_all)
|
||||||
|
await engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def client(ex_db_session: AsyncSession):
|
||||||
|
app = build_app(ex_db_session)
|
||||||
|
async with AsyncClient(
|
||||||
|
transport=ASGITransport(app=app), base_url="http://test"
|
||||||
|
) as ac:
|
||||||
|
yield ac
|
||||||
|
|
||||||
|
|
||||||
|
async def seed(session: AsyncSession):
|
||||||
|
"""Insert representative fixture data."""
|
||||||
|
python = Category(name="python")
|
||||||
|
backend = Category(name="backend")
|
||||||
|
session.add_all([python, backend])
|
||||||
|
await session.flush()
|
||||||
|
|
||||||
|
now = datetime.datetime(2024, 1, 1, tzinfo=datetime.timezone.utc)
|
||||||
|
session.add_all(
|
||||||
|
[
|
||||||
|
Article(
|
||||||
|
title="FastAPI tips",
|
||||||
|
body="Ten useful tips for FastAPI.",
|
||||||
|
status="published",
|
||||||
|
published=True,
|
||||||
|
category_id=python.id,
|
||||||
|
created_at=now,
|
||||||
|
),
|
||||||
|
Article(
|
||||||
|
title="SQLAlchemy async",
|
||||||
|
body="How to use async SQLAlchemy.",
|
||||||
|
status="published",
|
||||||
|
published=True,
|
||||||
|
category_id=backend.id,
|
||||||
|
created_at=now + datetime.timedelta(seconds=1),
|
||||||
|
),
|
||||||
|
Article(
|
||||||
|
title="Draft notes",
|
||||||
|
body="Work in progress.",
|
||||||
|
status="draft",
|
||||||
|
published=False,
|
||||||
|
category_id=None,
|
||||||
|
created_at=now + datetime.timedelta(seconds=2),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
class TestAppSessionDep:
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_get_db_yields_async_session(self):
|
||||||
|
"""get_db yields a real AsyncSession when called directly."""
|
||||||
|
from docs_src.examples.pagination_search.db import get_db
|
||||||
|
|
||||||
|
gen = get_db()
|
||||||
|
session = await gen.__anext__()
|
||||||
|
assert isinstance(session, AsyncSession)
|
||||||
|
await session.close()
|
||||||
|
|
||||||
|
|
||||||
|
class TestOffsetPagination:
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_returns_all_articles(self, client: AsyncClient, ex_db_session):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["pagination"]["total_count"] == 3
|
||||||
|
assert len(body["data"]) == 3
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_pagination_page_size(self, client: AsyncClient, ex_db_session):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset?items_per_page=2&page=1")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert len(body["data"]) == 2
|
||||||
|
assert body["pagination"]["total_count"] == 3
|
||||||
|
assert body["pagination"]["has_more"] is True
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_full_text_search(self, client: AsyncClient, ex_db_session):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset?search=fastapi")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["pagination"]["total_count"] == 1
|
||||||
|
assert body["data"][0]["title"] == "FastAPI tips"
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_search_traverses_relationship(
|
||||||
|
self, client: AsyncClient, ex_db_session
|
||||||
|
):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
# "python" matches Category.name, not Article.title or body
|
||||||
|
resp = await client.get("/articles/offset?search=python")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["pagination"]["total_count"] == 1
|
||||||
|
assert body["data"][0]["title"] == "FastAPI tips"
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_facet_filter_scalar(self, client: AsyncClient, ex_db_session):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset?status=published")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["pagination"]["total_count"] == 2
|
||||||
|
assert all(a["status"] == "published" for a in body["data"])
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_facet_filter_multi_value(self, client: AsyncClient, ex_db_session):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset?status=published&status=draft")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["pagination"]["total_count"] == 3
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_filter_attributes_in_response(
|
||||||
|
self, client: AsyncClient, ex_db_session
|
||||||
|
):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
fa = body["filter_attributes"]
|
||||||
|
assert set(fa["status"]) == {"draft", "published"}
|
||||||
|
# "name" is unique across all facet fields — no prefix needed
|
||||||
|
assert set(fa["name"]) == {"backend", "python"}
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_filter_attributes_scoped_to_filter(
|
||||||
|
self, client: AsyncClient, ex_db_session
|
||||||
|
):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset?status=published")
|
||||||
|
|
||||||
|
body = resp.json()
|
||||||
|
# draft is filtered out → should not appear in filter_attributes
|
||||||
|
assert "draft" not in body["filter_attributes"]["status"]
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_search_and_filter_combined(self, client: AsyncClient, ex_db_session):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset?search=async&status=published")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["pagination"]["total_count"] == 1
|
||||||
|
assert body["data"][0]["title"] == "SQLAlchemy async"
|
||||||
|
|
||||||
|
|
||||||
|
class TestCursorPagination:
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_first_page(self, client: AsyncClient, ex_db_session):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/cursor?items_per_page=2")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert len(body["data"]) == 2
|
||||||
|
assert body["pagination"]["has_more"] is True
|
||||||
|
assert body["pagination"]["next_cursor"] is not None
|
||||||
|
assert body["pagination"]["prev_cursor"] is None
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_second_page(self, client: AsyncClient, ex_db_session):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
first = await client.get("/articles/cursor?items_per_page=2")
|
||||||
|
next_cursor = first.json()["pagination"]["next_cursor"]
|
||||||
|
|
||||||
|
resp = await client.get(
|
||||||
|
f"/articles/cursor?items_per_page=2&cursor={next_cursor}"
|
||||||
|
)
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert len(body["data"]) == 1
|
||||||
|
assert body["pagination"]["has_more"] is False
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_facet_filter(self, client: AsyncClient, ex_db_session):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/cursor?status=draft")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert len(body["data"]) == 1
|
||||||
|
assert body["data"][0]["status"] == "draft"
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_full_text_search(self, client: AsyncClient, ex_db_session):
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/cursor?search=sqlalchemy")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert len(body["data"]) == 1
|
||||||
|
assert body["data"][0]["title"] == "SQLAlchemy async"
|
||||||
|
|
||||||
|
|
||||||
|
class TestOffsetSorting:
|
||||||
|
"""Tests for order_by / order query parameters on the offset endpoint."""
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_default_order_uses_created_at_asc(
|
||||||
|
self, client: AsyncClient, ex_db_session
|
||||||
|
):
|
||||||
|
"""No order_by → default field (created_at) ASC."""
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
titles = [a["title"] for a in resp.json()["data"]]
|
||||||
|
assert titles == ["FastAPI tips", "SQLAlchemy async", "Draft notes"]
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_order_by_title_asc(self, client: AsyncClient, ex_db_session):
|
||||||
|
"""order_by=title&order=asc returns alphabetical order."""
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset?order_by=title&order=asc")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
titles = [a["title"] for a in resp.json()["data"]]
|
||||||
|
assert titles == ["Draft notes", "FastAPI tips", "SQLAlchemy async"]
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_order_by_title_desc(self, client: AsyncClient, ex_db_session):
|
||||||
|
"""order_by=title&order=desc returns reverse alphabetical order."""
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset?order_by=title&order=desc")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
titles = [a["title"] for a in resp.json()["data"]]
|
||||||
|
assert titles == ["SQLAlchemy async", "FastAPI tips", "Draft notes"]
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_order_by_created_at_desc(self, client: AsyncClient, ex_db_session):
|
||||||
|
"""order_by=created_at&order=desc returns newest-first."""
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/offset?order_by=created_at&order=desc")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
titles = [a["title"] for a in resp.json()["data"]]
|
||||||
|
assert titles == ["Draft notes", "SQLAlchemy async", "FastAPI tips"]
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_invalid_order_by_returns_422(
|
||||||
|
self, client: AsyncClient, ex_db_session
|
||||||
|
):
|
||||||
|
"""Unknown order_by field returns 422 with SORT-422 error code."""
|
||||||
|
resp = await client.get("/articles/offset?order_by=nonexistent_field")
|
||||||
|
|
||||||
|
assert resp.status_code == 422
|
||||||
|
body = resp.json()
|
||||||
|
assert body["error_code"] == "SORT-422"
|
||||||
|
assert body["status"] == "FAIL"
|
||||||
|
|
||||||
|
|
||||||
|
class TestCursorSorting:
|
||||||
|
"""Tests for order_by / order query parameters on the cursor endpoint.
|
||||||
|
|
||||||
|
In cursor_paginate the cursor_column is always the primary sort; order_by
|
||||||
|
acts as a secondary tiebreaker. With the seeded articles (all having unique
|
||||||
|
created_at values) the overall ordering is always created_at ASC regardless
|
||||||
|
of the order_by value — only the valid/invalid field check and the response
|
||||||
|
shape are meaningful here.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_default_order_uses_created_at_asc(
|
||||||
|
self, client: AsyncClient, ex_db_session
|
||||||
|
):
|
||||||
|
"""No order_by → default field (created_at) ASC."""
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/cursor")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
titles = [a["title"] for a in resp.json()["data"]]
|
||||||
|
assert titles == ["FastAPI tips", "SQLAlchemy async", "Draft notes"]
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_order_by_title_asc_accepted(
|
||||||
|
self, client: AsyncClient, ex_db_session
|
||||||
|
):
|
||||||
|
"""order_by=title is a valid field — request succeeds and returns all articles."""
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/cursor?order_by=title&order=asc")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert len(resp.json()["data"]) == 3
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_order_by_title_desc_accepted(
|
||||||
|
self, client: AsyncClient, ex_db_session
|
||||||
|
):
|
||||||
|
"""order_by=title&order=desc is valid — request succeeds and returns all articles."""
|
||||||
|
await seed(ex_db_session)
|
||||||
|
|
||||||
|
resp = await client.get("/articles/cursor?order_by=title&order=desc")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert len(resp.json()["data"]) == 3
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_invalid_order_by_returns_422(
|
||||||
|
self, client: AsyncClient, ex_db_session
|
||||||
|
):
|
||||||
|
"""Unknown order_by field returns 422 with SORT-422 error code."""
|
||||||
|
resp = await client.get("/articles/cursor?order_by=nonexistent_field")
|
||||||
|
|
||||||
|
assert resp.status_code == 422
|
||||||
|
body = resp.json()
|
||||||
|
assert body["error_code"] == "SORT-422"
|
||||||
|
assert body["status"] == "FAIL"
|
||||||
@@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
from fastapi.exceptions import HTTPException
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from fastapi_toolsets.exceptions import (
|
from fastapi_toolsets.exceptions import (
|
||||||
ApiException,
|
ApiException,
|
||||||
ConflictError,
|
ConflictError,
|
||||||
ForbiddenError,
|
ForbiddenError,
|
||||||
|
InvalidOrderFieldError,
|
||||||
NotFoundError,
|
NotFoundError,
|
||||||
UnauthorizedError,
|
UnauthorizedError,
|
||||||
generate_error_responses,
|
generate_error_responses,
|
||||||
@@ -35,8 +37,8 @@ class TestApiException:
|
|||||||
assert error.api_error.msg == "I'm a teapot"
|
assert error.api_error.msg == "I'm a teapot"
|
||||||
assert str(error) == "I'm a teapot"
|
assert str(error) == "I'm a teapot"
|
||||||
|
|
||||||
def test_custom_detail_message(self):
|
def test_detail_overrides_msg_and_str(self):
|
||||||
"""Custom detail overrides default message."""
|
"""detail sets both str(exc) and api_error.msg; class-level msg is unchanged."""
|
||||||
|
|
||||||
class CustomError(ApiException):
|
class CustomError(ApiException):
|
||||||
api_error = ApiError(
|
api_error = ApiError(
|
||||||
@@ -46,8 +48,172 @@ class TestApiException:
|
|||||||
err_code="BAD-400",
|
err_code="BAD-400",
|
||||||
)
|
)
|
||||||
|
|
||||||
error = CustomError("Custom message")
|
error = CustomError("Widget not found")
|
||||||
assert str(error) == "Custom message"
|
assert str(error) == "Widget not found"
|
||||||
|
assert error.api_error.msg == "Widget not found"
|
||||||
|
assert CustomError.api_error.msg == "Bad Request" # class unchanged
|
||||||
|
|
||||||
|
def test_desc_override(self):
|
||||||
|
"""desc kwarg overrides api_error.desc on the instance only."""
|
||||||
|
|
||||||
|
class MyError(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=400, msg="Error", desc="Default.", err_code="ERR-400"
|
||||||
|
)
|
||||||
|
|
||||||
|
err = MyError(desc="Custom desc.")
|
||||||
|
assert err.api_error.desc == "Custom desc."
|
||||||
|
assert MyError.api_error.desc == "Default." # class unchanged
|
||||||
|
|
||||||
|
def test_data_override(self):
|
||||||
|
"""data kwarg sets api_error.data on the instance only."""
|
||||||
|
|
||||||
|
class MyError(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=400, msg="Error", desc="Default.", err_code="ERR-400"
|
||||||
|
)
|
||||||
|
|
||||||
|
err = MyError(data={"key": "value"})
|
||||||
|
assert err.api_error.data == {"key": "value"}
|
||||||
|
assert MyError.api_error.data is None # class unchanged
|
||||||
|
|
||||||
|
def test_desc_and_data_override(self):
|
||||||
|
"""detail, desc and data can all be overridden together."""
|
||||||
|
|
||||||
|
class MyError(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=400, msg="Error", desc="Default.", err_code="ERR-400"
|
||||||
|
)
|
||||||
|
|
||||||
|
err = MyError("custom msg", desc="New desc.", data={"x": 1})
|
||||||
|
assert str(err) == "custom msg"
|
||||||
|
assert err.api_error.msg == "custom msg" # detail also updates msg
|
||||||
|
assert err.api_error.desc == "New desc."
|
||||||
|
assert err.api_error.data == {"x": 1}
|
||||||
|
assert err.api_error.code == 400 # other fields unchanged
|
||||||
|
|
||||||
|
def test_class_api_error_not_mutated_after_instance_override(self):
|
||||||
|
"""Raising with desc/data does not mutate the class-level api_error."""
|
||||||
|
|
||||||
|
class MyError(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=400, msg="Error", desc="Default.", err_code="ERR-400"
|
||||||
|
)
|
||||||
|
|
||||||
|
MyError(desc="Changed", data={"x": 1})
|
||||||
|
assert MyError.api_error.desc == "Default."
|
||||||
|
assert MyError.api_error.data is None
|
||||||
|
|
||||||
|
def test_subclass_uses_super_with_desc_and_data(self):
|
||||||
|
"""Subclasses can delegate detail/desc/data to super().__init__()."""
|
||||||
|
|
||||||
|
class BuildValidationError(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=422,
|
||||||
|
msg="Build Validation Error",
|
||||||
|
desc="The build configuration is invalid.",
|
||||||
|
err_code="BUILD-422",
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, *errors: str) -> None:
|
||||||
|
super().__init__(
|
||||||
|
f"{len(errors)} validation error(s)",
|
||||||
|
desc=", ".join(errors),
|
||||||
|
data={"errors": [{"message": e} for e in errors]},
|
||||||
|
)
|
||||||
|
|
||||||
|
err = BuildValidationError("Field A is required", "Field B is invalid")
|
||||||
|
assert str(err) == "2 validation error(s)"
|
||||||
|
assert err.api_error.msg == "2 validation error(s)" # detail set msg
|
||||||
|
assert err.api_error.desc == "Field A is required, Field B is invalid"
|
||||||
|
assert err.api_error.data == {
|
||||||
|
"errors": [
|
||||||
|
{"message": "Field A is required"},
|
||||||
|
{"message": "Field B is invalid"},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
assert err.api_error.code == 422 # other fields unchanged
|
||||||
|
|
||||||
|
def test_detail_desc_data_in_http_response(self):
|
||||||
|
"""detail/desc/data overrides all appear correctly in the FastAPI HTTP response."""
|
||||||
|
|
||||||
|
class DynamicError(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=400, msg="Error", desc="Default.", err_code="ERR-400"
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, message: str) -> None:
|
||||||
|
super().__init__(
|
||||||
|
message,
|
||||||
|
desc=f"Detail: {message}",
|
||||||
|
data={"reason": message},
|
||||||
|
)
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
@app.get("/error")
|
||||||
|
async def raise_error():
|
||||||
|
raise DynamicError("something went wrong")
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.get("/error")
|
||||||
|
|
||||||
|
assert response.status_code == 400
|
||||||
|
body = response.json()
|
||||||
|
assert body["message"] == "something went wrong"
|
||||||
|
assert body["description"] == "Detail: something went wrong"
|
||||||
|
assert body["data"] == {"reason": "something went wrong"}
|
||||||
|
|
||||||
|
|
||||||
|
class TestApiExceptionGuard:
|
||||||
|
"""Tests for the __init_subclass__ api_error guard."""
|
||||||
|
|
||||||
|
def test_missing_api_error_raises_type_error(self):
|
||||||
|
"""Defining a subclass without api_error raises TypeError at class creation time."""
|
||||||
|
with pytest.raises(
|
||||||
|
TypeError, match="must define an 'api_error' class attribute"
|
||||||
|
):
|
||||||
|
|
||||||
|
class BrokenError(ApiException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_abstract_subclass_skips_guard(self):
|
||||||
|
"""abstract=True allows intermediate base classes without api_error."""
|
||||||
|
|
||||||
|
class BaseGroupError(ApiException, abstract=True):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Concrete child must still define it
|
||||||
|
class ConcreteError(BaseGroupError):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=400, msg="Error", desc="Desc.", err_code="ERR-400"
|
||||||
|
)
|
||||||
|
|
||||||
|
err = ConcreteError()
|
||||||
|
assert err.api_error.code == 400
|
||||||
|
|
||||||
|
def test_abstract_child_still_requires_api_error_on_concrete(self):
|
||||||
|
"""Concrete subclass of an abstract class must define api_error."""
|
||||||
|
|
||||||
|
class Base(ApiException, abstract=True):
|
||||||
|
pass
|
||||||
|
|
||||||
|
with pytest.raises(
|
||||||
|
TypeError, match="must define an 'api_error' class attribute"
|
||||||
|
):
|
||||||
|
|
||||||
|
class Concrete(Base):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_inherited_api_error_satisfies_guard(self):
|
||||||
|
"""Subclass that inherits api_error from a parent does not need its own."""
|
||||||
|
|
||||||
|
class ConcreteError(NotFoundError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
err = ConcreteError()
|
||||||
|
assert err.api_error.code == 404
|
||||||
|
|
||||||
|
|
||||||
class TestBuiltInExceptions:
|
class TestBuiltInExceptions:
|
||||||
@@ -89,7 +255,7 @@ class TestGenerateErrorResponses:
|
|||||||
assert responses[404]["description"] == "Not Found"
|
assert responses[404]["description"] == "Not Found"
|
||||||
|
|
||||||
def test_generates_multiple_responses(self):
|
def test_generates_multiple_responses(self):
|
||||||
"""Generates responses for multiple exceptions."""
|
"""Generates responses for multiple exceptions with distinct status codes."""
|
||||||
responses = generate_error_responses(
|
responses = generate_error_responses(
|
||||||
UnauthorizedError,
|
UnauthorizedError,
|
||||||
ForbiddenError,
|
ForbiddenError,
|
||||||
@@ -100,15 +266,24 @@ class TestGenerateErrorResponses:
|
|||||||
assert 403 in responses
|
assert 403 in responses
|
||||||
assert 404 in responses
|
assert 404 in responses
|
||||||
|
|
||||||
def test_response_has_example(self):
|
def test_response_has_named_example(self):
|
||||||
"""Generated response includes example."""
|
"""Generated response uses named examples keyed by err_code."""
|
||||||
responses = generate_error_responses(NotFoundError)
|
responses = generate_error_responses(NotFoundError)
|
||||||
example = responses[404]["content"]["application/json"]["example"]
|
examples = responses[404]["content"]["application/json"]["examples"]
|
||||||
|
|
||||||
assert example["status"] == "FAIL"
|
assert "RES-404" in examples
|
||||||
assert example["error_code"] == "RES-404"
|
value = examples["RES-404"]["value"]
|
||||||
assert example["message"] == "Not Found"
|
assert value["status"] == "FAIL"
|
||||||
assert example["data"] is None
|
assert value["error_code"] == "RES-404"
|
||||||
|
assert value["message"] == "Not Found"
|
||||||
|
assert value["data"] is None
|
||||||
|
|
||||||
|
def test_response_example_has_summary(self):
|
||||||
|
"""Each named example carries a summary equal to api_error.msg."""
|
||||||
|
responses = generate_error_responses(NotFoundError)
|
||||||
|
example = responses[404]["content"]["application/json"]["examples"]["RES-404"]
|
||||||
|
|
||||||
|
assert example["summary"] == "Not Found"
|
||||||
|
|
||||||
def test_response_example_with_data(self):
|
def test_response_example_with_data(self):
|
||||||
"""Generated response includes data when set on ApiError."""
|
"""Generated response includes data when set on ApiError."""
|
||||||
@@ -123,9 +298,49 @@ class TestGenerateErrorResponses:
|
|||||||
)
|
)
|
||||||
|
|
||||||
responses = generate_error_responses(ErrorWithData)
|
responses = generate_error_responses(ErrorWithData)
|
||||||
example = responses[400]["content"]["application/json"]["example"]
|
value = responses[400]["content"]["application/json"]["examples"]["BAD-400"][
|
||||||
|
"value"
|
||||||
|
]
|
||||||
|
|
||||||
assert example["data"] == {"details": "some context"}
|
assert value["data"] == {"details": "some context"}
|
||||||
|
|
||||||
|
def test_two_errors_same_code_both_present(self):
|
||||||
|
"""Two exceptions with the same HTTP code produce two named examples."""
|
||||||
|
|
||||||
|
class BadRequestA(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=400, msg="Bad A", desc="Reason A.", err_code="ERR-A"
|
||||||
|
)
|
||||||
|
|
||||||
|
class BadRequestB(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=400, msg="Bad B", desc="Reason B.", err_code="ERR-B"
|
||||||
|
)
|
||||||
|
|
||||||
|
responses = generate_error_responses(BadRequestA, BadRequestB)
|
||||||
|
|
||||||
|
assert 400 in responses
|
||||||
|
examples = responses[400]["content"]["application/json"]["examples"]
|
||||||
|
assert "ERR-A" in examples
|
||||||
|
assert "ERR-B" in examples
|
||||||
|
assert examples["ERR-A"]["value"]["message"] == "Bad A"
|
||||||
|
assert examples["ERR-B"]["value"]["message"] == "Bad B"
|
||||||
|
|
||||||
|
def test_two_errors_same_code_single_top_level_entry(self):
|
||||||
|
"""Two exceptions with the same HTTP code produce exactly one top-level entry."""
|
||||||
|
|
||||||
|
class BadRequestA(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=400, msg="Bad A", desc="Reason A.", err_code="ERR-A"
|
||||||
|
)
|
||||||
|
|
||||||
|
class BadRequestB(ApiException):
|
||||||
|
api_error = ApiError(
|
||||||
|
code=400, msg="Bad B", desc="Reason B.", err_code="ERR-B"
|
||||||
|
)
|
||||||
|
|
||||||
|
responses = generate_error_responses(BadRequestA, BadRequestB)
|
||||||
|
assert len([k for k in responses if k == 400]) == 1
|
||||||
|
|
||||||
|
|
||||||
class TestInitExceptionsHandlers:
|
class TestInitExceptionsHandlers:
|
||||||
@@ -249,13 +464,68 @@ class TestInitExceptionsHandlers:
|
|||||||
assert data["status"] == "FAIL"
|
assert data["status"] == "FAIL"
|
||||||
assert data["error_code"] == "SERVER-500"
|
assert data["error_code"] == "SERVER-500"
|
||||||
|
|
||||||
def test_custom_openapi_schema(self):
|
def test_handles_http_exception(self):
|
||||||
"""Customizes OpenAPI schema for 422 responses."""
|
"""Handles starlette HTTPException with consistent ErrorResponse envelope."""
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
init_exceptions_handlers(app)
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
@app.get("/protected")
|
||||||
|
async def protected():
|
||||||
|
raise HTTPException(status_code=403, detail="Forbidden")
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.get("/protected")
|
||||||
|
|
||||||
|
assert response.status_code == 403
|
||||||
|
data = response.json()
|
||||||
|
assert data["status"] == "FAIL"
|
||||||
|
assert data["error_code"] == "HTTP-403"
|
||||||
|
assert data["message"] == "Forbidden"
|
||||||
|
|
||||||
|
def test_handles_http_exception_404_from_route(self):
|
||||||
|
"""HTTPException(404) raised inside a route uses the consistent ErrorResponse envelope."""
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
@app.get("/items/{item_id}")
|
||||||
|
async def get_item(item_id: int):
|
||||||
|
raise HTTPException(status_code=404, detail="Item not found")
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.get("/items/99")
|
||||||
|
|
||||||
|
assert response.status_code == 404
|
||||||
|
data = response.json()
|
||||||
|
assert data["status"] == "FAIL"
|
||||||
|
assert data["error_code"] == "HTTP-404"
|
||||||
|
assert data["message"] == "Item not found"
|
||||||
|
|
||||||
|
def test_handles_http_exception_forwards_headers(self):
|
||||||
|
"""HTTPException with WWW-Authenticate header forwards it in the response."""
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
@app.get("/secure")
|
||||||
|
async def secure():
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail="Not authenticated",
|
||||||
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
|
)
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.get("/secure")
|
||||||
|
|
||||||
|
assert response.status_code == 401
|
||||||
|
assert response.headers.get("www-authenticate") == "Bearer"
|
||||||
|
|
||||||
|
def test_custom_openapi_schema(self):
|
||||||
|
"""Customises OpenAPI schema for 422 responses using named examples."""
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
class Item(BaseModel):
|
class Item(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
@@ -268,8 +538,128 @@ class TestInitExceptionsHandlers:
|
|||||||
post_op = openapi["paths"]["/items"]["post"]
|
post_op = openapi["paths"]["/items"]["post"]
|
||||||
assert "422" in post_op["responses"]
|
assert "422" in post_op["responses"]
|
||||||
resp_422 = post_op["responses"]["422"]
|
resp_422 = post_op["responses"]["422"]
|
||||||
example = resp_422["content"]["application/json"]["example"]
|
examples = resp_422["content"]["application/json"]["examples"]
|
||||||
assert example["error_code"] == "VAL-422"
|
assert "VAL-422" in examples
|
||||||
|
assert examples["VAL-422"]["value"]["error_code"] == "VAL-422"
|
||||||
|
|
||||||
|
def test_custom_openapi_preserves_app_metadata(self):
|
||||||
|
"""_patched_openapi preserves custom FastAPI app-level metadata."""
|
||||||
|
app = FastAPI(
|
||||||
|
title="My API",
|
||||||
|
version="2.0.0",
|
||||||
|
description="Custom description",
|
||||||
|
)
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
schema = app.openapi()
|
||||||
|
assert schema["info"]["title"] == "My API"
|
||||||
|
assert schema["info"]["version"] == "2.0.0"
|
||||||
|
|
||||||
|
def test_handles_response_validation_error(self):
|
||||||
|
"""Handles ResponseValidationError with a structured 422 response."""
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class CountResponse(BaseModel):
|
||||||
|
count: int
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
@app.get("/broken", response_model=CountResponse)
|
||||||
|
async def broken():
|
||||||
|
return {"count": "not-a-number"} # triggers ResponseValidationError
|
||||||
|
|
||||||
|
client = TestClient(app, raise_server_exceptions=False)
|
||||||
|
response = client.get("/broken")
|
||||||
|
|
||||||
|
assert response.status_code == 422
|
||||||
|
data = response.json()
|
||||||
|
assert data["status"] == "FAIL"
|
||||||
|
assert data["error_code"] == "VAL-422"
|
||||||
|
assert "errors" in data["data"]
|
||||||
|
|
||||||
|
def test_handles_validation_error_with_non_standard_loc(self):
|
||||||
|
"""Validation error with empty loc tuple maps the field to 'root'."""
|
||||||
|
from fastapi.exceptions import RequestValidationError
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
@app.get("/root-error")
|
||||||
|
async def root_error():
|
||||||
|
raise RequestValidationError(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"type": "custom",
|
||||||
|
"loc": (),
|
||||||
|
"msg": "root level error",
|
||||||
|
"input": None,
|
||||||
|
"url": "",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.get("/root-error")
|
||||||
|
|
||||||
|
assert response.status_code == 422
|
||||||
|
data = response.json()
|
||||||
|
assert data["data"]["errors"][0]["field"] == "root"
|
||||||
|
|
||||||
|
def test_openapi_schema_cached_after_first_call(self):
|
||||||
|
"""app.openapi() returns the cached schema on subsequent calls."""
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
class Item(BaseModel):
|
||||||
|
name: str
|
||||||
|
|
||||||
|
@app.post("/items")
|
||||||
|
async def create_item(item: Item):
|
||||||
|
return item
|
||||||
|
|
||||||
|
schema_first = app.openapi()
|
||||||
|
schema_second = app.openapi()
|
||||||
|
assert schema_first is schema_second
|
||||||
|
|
||||||
|
def test_openapi_skips_operations_without_422(self):
|
||||||
|
"""_patched_openapi leaves operations that have no 422 response unchanged."""
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
@app.get("/ping")
|
||||||
|
async def ping():
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
schema = app.openapi()
|
||||||
|
get_op = schema["paths"]["/ping"]["get"]
|
||||||
|
assert "422" not in get_op["responses"]
|
||||||
|
assert "200" in get_op["responses"]
|
||||||
|
|
||||||
|
def test_openapi_skips_non_dict_path_item_values(self):
|
||||||
|
"""_patched_openapi ignores non-dict values in path items (e.g. path-level parameters)."""
|
||||||
|
from fastapi_toolsets.exceptions.handler import _patched_openapi
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
def fake_openapi() -> dict:
|
||||||
|
return {
|
||||||
|
"paths": {
|
||||||
|
"/items": {
|
||||||
|
"parameters": [
|
||||||
|
{"name": "q", "in": "query"}
|
||||||
|
], # list, not a dict
|
||||||
|
"get": {"responses": {"200": {"description": "OK"}}},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
schema = _patched_openapi(app, fake_openapi)
|
||||||
|
# The list value was skipped without error; the GET operation is intact
|
||||||
|
assert schema["paths"]["/items"]["parameters"] == [{"name": "q", "in": "query"}]
|
||||||
|
assert "422" not in schema["paths"]["/items"]["get"]["responses"]
|
||||||
|
|
||||||
|
|
||||||
class TestExceptionIntegration:
|
class TestExceptionIntegration:
|
||||||
@@ -334,3 +724,43 @@ class TestExceptionIntegration:
|
|||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json() == {"id": 1}
|
assert response.json() == {"id": 1}
|
||||||
|
|
||||||
|
|
||||||
|
class TestInvalidOrderFieldError:
|
||||||
|
"""Tests for InvalidOrderFieldError exception."""
|
||||||
|
|
||||||
|
def test_api_error_attributes(self):
|
||||||
|
"""InvalidOrderFieldError has correct api_error metadata."""
|
||||||
|
assert InvalidOrderFieldError.api_error.code == 422
|
||||||
|
assert InvalidOrderFieldError.api_error.err_code == "SORT-422"
|
||||||
|
assert InvalidOrderFieldError.api_error.msg == "Invalid Order Field"
|
||||||
|
|
||||||
|
def test_stores_field_and_valid_fields(self):
|
||||||
|
"""InvalidOrderFieldError stores field and valid_fields on the instance."""
|
||||||
|
error = InvalidOrderFieldError("unknown", ["name", "created_at"])
|
||||||
|
assert error.field == "unknown"
|
||||||
|
assert error.valid_fields == ["name", "created_at"]
|
||||||
|
|
||||||
|
def test_description_contains_field_and_valid_fields(self):
|
||||||
|
"""api_error.desc mentions the bad field and valid options."""
|
||||||
|
error = InvalidOrderFieldError("bad_field", ["name", "email"])
|
||||||
|
assert "bad_field" in error.api_error.desc
|
||||||
|
assert "name" in error.api_error.desc
|
||||||
|
assert "email" in error.api_error.desc
|
||||||
|
|
||||||
|
def test_handled_as_422_by_exception_handler(self):
|
||||||
|
"""init_exceptions_handlers turns InvalidOrderFieldError into a 422 response."""
|
||||||
|
app = FastAPI()
|
||||||
|
init_exceptions_handlers(app)
|
||||||
|
|
||||||
|
@app.get("/items")
|
||||||
|
async def list_items():
|
||||||
|
raise InvalidOrderFieldError("bad", ["name"])
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.get("/items")
|
||||||
|
|
||||||
|
assert response.status_code == 422
|
||||||
|
data = response.json()
|
||||||
|
assert data["error_code"] == "SORT-422"
|
||||||
|
assert data["status"] == "FAIL"
|
||||||
|
|||||||
@@ -14,7 +14,9 @@ from fastapi_toolsets.fixtures import (
|
|||||||
load_fixtures_by_context,
|
load_fixtures_by_context,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .conftest import Role, User
|
from fastapi_toolsets.fixtures.utils import _get_primary_key
|
||||||
|
|
||||||
|
from .conftest import IntRole, Permission, Role, User
|
||||||
|
|
||||||
|
|
||||||
class TestContext:
|
class TestContext:
|
||||||
@@ -597,6 +599,46 @@ class TestLoadFixtures:
|
|||||||
count = await RoleCrud.count(db_session)
|
count = await RoleCrud.count(db_session)
|
||||||
assert count == 2
|
assert count == 2
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_skip_existing_skips_if_record_exists(self, db_session: AsyncSession):
|
||||||
|
"""SKIP_EXISTING returns empty loaded list when the record already exists."""
|
||||||
|
registry = FixtureRegistry()
|
||||||
|
role_id = uuid.uuid4()
|
||||||
|
|
||||||
|
@registry.register
|
||||||
|
def roles():
|
||||||
|
return [Role(id=role_id, name="admin")]
|
||||||
|
|
||||||
|
# First load — inserts the record.
|
||||||
|
result1 = await load_fixtures(
|
||||||
|
db_session, registry, "roles", strategy=LoadStrategy.SKIP_EXISTING
|
||||||
|
)
|
||||||
|
assert len(result1["roles"]) == 1
|
||||||
|
|
||||||
|
# Remove from identity map so session.get() queries the DB in the second load.
|
||||||
|
db_session.expunge_all()
|
||||||
|
|
||||||
|
# Second load — record exists in DB, nothing should be added.
|
||||||
|
result2 = await load_fixtures(
|
||||||
|
db_session, registry, "roles", strategy=LoadStrategy.SKIP_EXISTING
|
||||||
|
)
|
||||||
|
assert result2["roles"] == []
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_skip_existing_null_pk_inserts(self, db_session: AsyncSession):
|
||||||
|
"""SKIP_EXISTING inserts when the instance has no PK set (auto-increment)."""
|
||||||
|
registry = FixtureRegistry()
|
||||||
|
|
||||||
|
@registry.register
|
||||||
|
def int_roles():
|
||||||
|
# No id provided — PK is None before INSERT (autoincrement).
|
||||||
|
return [IntRole(name="member")]
|
||||||
|
|
||||||
|
result = await load_fixtures(
|
||||||
|
db_session, registry, "int_roles", strategy=LoadStrategy.SKIP_EXISTING
|
||||||
|
)
|
||||||
|
assert len(result["int_roles"]) == 1
|
||||||
|
|
||||||
|
|
||||||
class TestLoadFixturesByContext:
|
class TestLoadFixturesByContext:
|
||||||
"""Tests for load_fixtures_by_context function."""
|
"""Tests for load_fixtures_by_context function."""
|
||||||
@@ -755,3 +797,19 @@ class TestGetObjByAttr:
|
|||||||
"""Raises StopIteration when value type doesn't match."""
|
"""Raises StopIteration when value type doesn't match."""
|
||||||
with pytest.raises(StopIteration):
|
with pytest.raises(StopIteration):
|
||||||
get_obj_by_attr(self.roles, "id", "not-a-uuid")
|
get_obj_by_attr(self.roles, "id", "not-a-uuid")
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetPrimaryKey:
|
||||||
|
"""Unit tests for the _get_primary_key helper (composite PK paths)."""
|
||||||
|
|
||||||
|
def test_composite_pk_all_set(self):
|
||||||
|
"""Returns a tuple when all composite PK values are set."""
|
||||||
|
instance = Permission(subject="post", action="read")
|
||||||
|
pk = _get_primary_key(instance)
|
||||||
|
assert pk == ("post", "read")
|
||||||
|
|
||||||
|
def test_composite_pk_partial_none(self):
|
||||||
|
"""Returns None when any composite PK value is None."""
|
||||||
|
instance = Permission(subject="post") # action is None
|
||||||
|
pk = _get_primary_key(instance)
|
||||||
|
assert pk is None
|
||||||
|
|||||||
247
tests/test_models.py
Normal file
247
tests/test_models.py
Normal file
@@ -0,0 +1,247 @@
|
|||||||
|
"""Tests for fastapi_toolsets.models mixins."""
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from sqlalchemy import String
|
||||||
|
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||||
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||||
|
|
||||||
|
from fastapi_toolsets.models import (
|
||||||
|
CreatedAtMixin,
|
||||||
|
TimestampMixin,
|
||||||
|
UUIDMixin,
|
||||||
|
UpdatedAtMixin,
|
||||||
|
)
|
||||||
|
|
||||||
|
from .conftest import DATABASE_URL
|
||||||
|
|
||||||
|
|
||||||
|
class MixinBase(DeclarativeBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class UUIDModel(MixinBase, UUIDMixin):
|
||||||
|
__tablename__ = "mixin_uuid_models"
|
||||||
|
|
||||||
|
name: Mapped[str] = mapped_column(String(50))
|
||||||
|
|
||||||
|
|
||||||
|
class UpdatedAtModel(MixinBase, UpdatedAtMixin):
|
||||||
|
__tablename__ = "mixin_updated_at_models"
|
||||||
|
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
|
name: Mapped[str] = mapped_column(String(50))
|
||||||
|
|
||||||
|
|
||||||
|
class CreatedAtModel(MixinBase, CreatedAtMixin):
|
||||||
|
__tablename__ = "mixin_created_at_models"
|
||||||
|
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
|
name: Mapped[str] = mapped_column(String(50))
|
||||||
|
|
||||||
|
|
||||||
|
class TimestampModel(MixinBase, TimestampMixin):
|
||||||
|
__tablename__ = "mixin_timestamp_models"
|
||||||
|
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
|
name: Mapped[str] = mapped_column(String(50))
|
||||||
|
|
||||||
|
|
||||||
|
class FullMixinModel(MixinBase, UUIDMixin, UpdatedAtMixin):
|
||||||
|
__tablename__ = "mixin_full_models"
|
||||||
|
|
||||||
|
name: Mapped[str] = mapped_column(String(50))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="function")
|
||||||
|
async def mixin_session():
|
||||||
|
engine = create_async_engine(DATABASE_URL, echo=False)
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
await conn.run_sync(MixinBase.metadata.create_all)
|
||||||
|
|
||||||
|
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
||||||
|
session = session_factory()
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield session
|
||||||
|
finally:
|
||||||
|
await session.close()
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
await conn.run_sync(MixinBase.metadata.drop_all)
|
||||||
|
await engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
class TestUUIDMixin:
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_uuid_generated_by_db(self, mixin_session):
|
||||||
|
"""UUID is generated server-side and populated after flush."""
|
||||||
|
obj = UUIDModel(name="test")
|
||||||
|
mixin_session.add(obj)
|
||||||
|
await mixin_session.flush()
|
||||||
|
|
||||||
|
assert obj.id is not None
|
||||||
|
assert isinstance(obj.id, uuid.UUID)
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_uuid_is_primary_key(self):
|
||||||
|
"""UUIDMixin adds id as primary key column."""
|
||||||
|
pk_cols = [c.name for c in UUIDModel.__table__.primary_key]
|
||||||
|
assert pk_cols == ["id"]
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_each_row_gets_unique_uuid(self, mixin_session):
|
||||||
|
"""Each inserted row gets a distinct UUID."""
|
||||||
|
a = UUIDModel(name="a")
|
||||||
|
b = UUIDModel(name="b")
|
||||||
|
mixin_session.add_all([a, b])
|
||||||
|
await mixin_session.flush()
|
||||||
|
|
||||||
|
assert a.id != b.id
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_uuid_server_default_set(self):
|
||||||
|
"""Column has gen_random_uuid() as server default."""
|
||||||
|
col = UUIDModel.__table__.c["id"]
|
||||||
|
assert col.server_default is not None
|
||||||
|
assert "gen_random_uuid" in str(col.server_default.arg)
|
||||||
|
|
||||||
|
|
||||||
|
class TestUpdatedAtMixin:
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_updated_at_set_on_insert(self, mixin_session):
|
||||||
|
"""updated_at is populated after insert."""
|
||||||
|
obj = UpdatedAtModel(name="initial")
|
||||||
|
mixin_session.add(obj)
|
||||||
|
await mixin_session.flush()
|
||||||
|
await mixin_session.refresh(obj)
|
||||||
|
|
||||||
|
assert obj.updated_at is not None
|
||||||
|
assert obj.updated_at.tzinfo is not None # timezone-aware
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_updated_at_changes_on_update(self, mixin_session):
|
||||||
|
"""updated_at is updated when the row is modified."""
|
||||||
|
obj = UpdatedAtModel(name="initial")
|
||||||
|
mixin_session.add(obj)
|
||||||
|
await mixin_session.flush()
|
||||||
|
await mixin_session.refresh(obj)
|
||||||
|
|
||||||
|
original_ts = obj.updated_at
|
||||||
|
|
||||||
|
obj.name = "modified"
|
||||||
|
await mixin_session.flush()
|
||||||
|
await mixin_session.refresh(obj)
|
||||||
|
|
||||||
|
assert obj.updated_at >= original_ts
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_updated_at_column_is_not_nullable(self):
|
||||||
|
"""updated_at column is non-nullable."""
|
||||||
|
col = UpdatedAtModel.__table__.c["updated_at"]
|
||||||
|
assert not col.nullable
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_updated_at_has_server_default(self):
|
||||||
|
"""updated_at column has a server-side default."""
|
||||||
|
col = UpdatedAtModel.__table__.c["updated_at"]
|
||||||
|
assert col.server_default is not None
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_updated_at_has_onupdate(self):
|
||||||
|
"""updated_at column has an onupdate clause."""
|
||||||
|
col = UpdatedAtModel.__table__.c["updated_at"]
|
||||||
|
assert col.onupdate is not None
|
||||||
|
|
||||||
|
|
||||||
|
class TestCreatedAtMixin:
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_created_at_set_on_insert(self, mixin_session):
|
||||||
|
"""created_at is populated after insert."""
|
||||||
|
obj = CreatedAtModel(name="new")
|
||||||
|
mixin_session.add(obj)
|
||||||
|
await mixin_session.flush()
|
||||||
|
await mixin_session.refresh(obj)
|
||||||
|
|
||||||
|
assert obj.created_at is not None
|
||||||
|
assert obj.created_at.tzinfo is not None # timezone-aware
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_created_at_not_changed_on_update(self, mixin_session):
|
||||||
|
"""created_at is not modified when the row is updated."""
|
||||||
|
obj = CreatedAtModel(name="original")
|
||||||
|
mixin_session.add(obj)
|
||||||
|
await mixin_session.flush()
|
||||||
|
await mixin_session.refresh(obj)
|
||||||
|
|
||||||
|
original_ts = obj.created_at
|
||||||
|
|
||||||
|
obj.name = "updated"
|
||||||
|
await mixin_session.flush()
|
||||||
|
await mixin_session.refresh(obj)
|
||||||
|
|
||||||
|
assert obj.created_at == original_ts
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_created_at_column_is_not_nullable(self):
|
||||||
|
"""created_at column is non-nullable."""
|
||||||
|
col = CreatedAtModel.__table__.c["created_at"]
|
||||||
|
assert not col.nullable
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_created_at_has_no_onupdate(self):
|
||||||
|
"""created_at column has no onupdate clause."""
|
||||||
|
col = CreatedAtModel.__table__.c["created_at"]
|
||||||
|
assert col.onupdate is None
|
||||||
|
|
||||||
|
|
||||||
|
class TestTimestampMixin:
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_both_columns_set_on_insert(self, mixin_session):
|
||||||
|
"""created_at and updated_at are both populated after insert."""
|
||||||
|
obj = TimestampModel(name="new")
|
||||||
|
mixin_session.add(obj)
|
||||||
|
await mixin_session.flush()
|
||||||
|
await mixin_session.refresh(obj)
|
||||||
|
|
||||||
|
assert obj.created_at is not None
|
||||||
|
assert obj.updated_at is not None
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_created_at_stable_updated_at_changes_on_update(self, mixin_session):
|
||||||
|
"""On update: created_at stays the same, updated_at advances."""
|
||||||
|
obj = TimestampModel(name="original")
|
||||||
|
mixin_session.add(obj)
|
||||||
|
await mixin_session.flush()
|
||||||
|
await mixin_session.refresh(obj)
|
||||||
|
|
||||||
|
original_created = obj.created_at
|
||||||
|
original_updated = obj.updated_at
|
||||||
|
|
||||||
|
obj.name = "modified"
|
||||||
|
await mixin_session.flush()
|
||||||
|
await mixin_session.refresh(obj)
|
||||||
|
|
||||||
|
assert obj.created_at == original_created
|
||||||
|
assert obj.updated_at >= original_updated
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_timestamp_mixin_has_both_columns(self):
|
||||||
|
"""TimestampModel exposes both created_at and updated_at columns."""
|
||||||
|
col_names = {c.name for c in TimestampModel.__table__.columns}
|
||||||
|
assert "created_at" in col_names
|
||||||
|
assert "updated_at" in col_names
|
||||||
|
|
||||||
|
|
||||||
|
class TestFullMixinModel:
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_combined_mixins_work_together(self, mixin_session):
|
||||||
|
"""UUIDMixin and UpdatedAtMixin can be combined on the same model."""
|
||||||
|
obj = FullMixinModel(name="combined")
|
||||||
|
mixin_session.add(obj)
|
||||||
|
await mixin_session.flush()
|
||||||
|
await mixin_session.refresh(obj)
|
||||||
|
|
||||||
|
assert isinstance(obj.id, uuid.UUID)
|
||||||
|
assert obj.updated_at is not None
|
||||||
|
assert obj.updated_at.tzinfo is not None
|
||||||
@@ -9,7 +9,6 @@ from fastapi_toolsets.schemas import (
|
|||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
OffsetPagination,
|
OffsetPagination,
|
||||||
PaginatedResponse,
|
PaginatedResponse,
|
||||||
Pagination,
|
|
||||||
Response,
|
Response,
|
||||||
ResponseStatus,
|
ResponseStatus,
|
||||||
)
|
)
|
||||||
@@ -199,20 +198,6 @@ class TestOffsetPagination:
|
|||||||
assert data["page"] == 2
|
assert data["page"] == 2
|
||||||
assert data["has_more"] is True
|
assert data["has_more"] is True
|
||||||
|
|
||||||
def test_pagination_alias_is_offset_pagination(self):
|
|
||||||
"""Pagination is a backward-compatible alias for OffsetPagination."""
|
|
||||||
assert Pagination is OffsetPagination
|
|
||||||
|
|
||||||
def test_pagination_alias_constructs_offset_pagination(self):
|
|
||||||
"""Code using Pagination(...) still works unchanged."""
|
|
||||||
pagination = Pagination(
|
|
||||||
total_count=10,
|
|
||||||
items_per_page=5,
|
|
||||||
page=2,
|
|
||||||
has_more=False,
|
|
||||||
)
|
|
||||||
assert isinstance(pagination, OffsetPagination)
|
|
||||||
|
|
||||||
|
|
||||||
class TestCursorPagination:
|
class TestCursorPagination:
|
||||||
"""Tests for CursorPagination schema."""
|
"""Tests for CursorPagination schema."""
|
||||||
@@ -276,7 +261,7 @@ class TestPaginatedResponse:
|
|||||||
|
|
||||||
def test_create_paginated_response(self):
|
def test_create_paginated_response(self):
|
||||||
"""Create PaginatedResponse with data and pagination."""
|
"""Create PaginatedResponse with data and pagination."""
|
||||||
pagination = Pagination(
|
pagination = OffsetPagination(
|
||||||
total_count=30,
|
total_count=30,
|
||||||
items_per_page=10,
|
items_per_page=10,
|
||||||
page=1,
|
page=1,
|
||||||
@@ -294,7 +279,7 @@ class TestPaginatedResponse:
|
|||||||
|
|
||||||
def test_with_custom_message(self):
|
def test_with_custom_message(self):
|
||||||
"""PaginatedResponse with custom message."""
|
"""PaginatedResponse with custom message."""
|
||||||
pagination = Pagination(
|
pagination = OffsetPagination(
|
||||||
total_count=5,
|
total_count=5,
|
||||||
items_per_page=10,
|
items_per_page=10,
|
||||||
page=1,
|
page=1,
|
||||||
@@ -310,7 +295,7 @@ class TestPaginatedResponse:
|
|||||||
|
|
||||||
def test_empty_data(self):
|
def test_empty_data(self):
|
||||||
"""PaginatedResponse with empty data."""
|
"""PaginatedResponse with empty data."""
|
||||||
pagination = Pagination(
|
pagination = OffsetPagination(
|
||||||
total_count=0,
|
total_count=0,
|
||||||
items_per_page=10,
|
items_per_page=10,
|
||||||
page=1,
|
page=1,
|
||||||
@@ -332,7 +317,7 @@ class TestPaginatedResponse:
|
|||||||
id: int
|
id: int
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
pagination = Pagination(
|
pagination = OffsetPagination(
|
||||||
total_count=1,
|
total_count=1,
|
||||||
items_per_page=10,
|
items_per_page=10,
|
||||||
page=1,
|
page=1,
|
||||||
@@ -347,7 +332,7 @@ class TestPaginatedResponse:
|
|||||||
|
|
||||||
def test_serialization(self):
|
def test_serialization(self):
|
||||||
"""PaginatedResponse serializes correctly."""
|
"""PaginatedResponse serializes correctly."""
|
||||||
pagination = Pagination(
|
pagination = OffsetPagination(
|
||||||
total_count=100,
|
total_count=100,
|
||||||
items_per_page=10,
|
items_per_page=10,
|
||||||
page=5,
|
page=5,
|
||||||
@@ -385,16 +370,6 @@ class TestPaginatedResponse:
|
|||||||
)
|
)
|
||||||
assert isinstance(response.pagination, CursorPagination)
|
assert isinstance(response.pagination, CursorPagination)
|
||||||
|
|
||||||
def test_pagination_alias_accepted(self):
|
|
||||||
"""Constructing PaginatedResponse with Pagination (alias) still works."""
|
|
||||||
response = PaginatedResponse(
|
|
||||||
data=[],
|
|
||||||
pagination=Pagination(
|
|
||||||
total_count=0, items_per_page=10, page=1, has_more=False
|
|
||||||
),
|
|
||||||
)
|
|
||||||
assert isinstance(response.pagination, OffsetPagination)
|
|
||||||
|
|
||||||
|
|
||||||
class TestFromAttributes:
|
class TestFromAttributes:
|
||||||
"""Tests for from_attributes config (ORM mode)."""
|
"""Tests for from_attributes config (ORM mode)."""
|
||||||
|
|||||||
2
uv.lock
generated
2
uv.lock
generated
@@ -251,7 +251,7 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastapi-toolsets"
|
name = "fastapi-toolsets"
|
||||||
version = "1.2.0"
|
version = "2.0.0"
|
||||||
source = { editable = "." }
|
source = { editable = "." }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "asyncpg" },
|
{ name = "asyncpg" },
|
||||||
|
|||||||
351
zensical.toml
351
zensical.toml
@@ -1,265 +1,35 @@
|
|||||||
# ============================================================================
|
|
||||||
#
|
|
||||||
# The configuration produced by default is meant to highlight the features
|
|
||||||
# that Zensical provides and to serve as a starting point for your own
|
|
||||||
# projects.
|
|
||||||
#
|
|
||||||
# ============================================================================
|
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
|
|
||||||
# The site_name is shown in the page header and the browser window title
|
|
||||||
#
|
|
||||||
# Read more: https://zensical.org/docs/setup/basics/#site_name
|
|
||||||
site_name = "FastAPI Toolsets"
|
site_name = "FastAPI Toolsets"
|
||||||
|
|
||||||
# The site_description is included in the HTML head and should contain a
|
|
||||||
# meaningful description of the site content for use by search engines.
|
|
||||||
#
|
|
||||||
# Read more: https://zensical.org/docs/setup/basics/#site_description
|
|
||||||
site_description = "Production-ready utilities for FastAPI applications."
|
site_description = "Production-ready utilities for FastAPI applications."
|
||||||
|
|
||||||
# The site_author attribute. This is used in the HTML head element.
|
|
||||||
#
|
|
||||||
# Read more: https://zensical.org/docs/setup/basics/#site_author
|
|
||||||
site_author = "d3vyce"
|
site_author = "d3vyce"
|
||||||
|
|
||||||
# The site_url is the canonical URL for your site. When building online
|
|
||||||
# documentation you should set this.
|
|
||||||
# Read more: https://zensical.org/docs/setup/basics/#site_url
|
|
||||||
site_url = "https://fastapi-toolsets.d3vyce.fr"
|
site_url = "https://fastapi-toolsets.d3vyce.fr"
|
||||||
|
copyright = "Copyright © 2026 d3vyce"
|
||||||
# The copyright notice appears in the page footer and can contain an HTML
|
|
||||||
# fragment.
|
|
||||||
#
|
|
||||||
# Read more: https://zensical.org/docs/setup/basics/#copyright
|
|
||||||
copyright = """
|
|
||||||
Copyright © 2026 d3vyce
|
|
||||||
"""
|
|
||||||
|
|
||||||
repo_url = "https://github.com/d3vyce/fastapi-toolsets"
|
repo_url = "https://github.com/d3vyce/fastapi-toolsets"
|
||||||
|
|
||||||
# Zensical supports both implicit navigation and explicitly defined navigation.
|
|
||||||
# If you decide not to define a navigation here then Zensical will simply
|
|
||||||
# derive the navigation structure from the directory structure of your
|
|
||||||
# "docs_dir". The definition below demonstrates how a navigation structure
|
|
||||||
# can be defined using TOML syntax.
|
|
||||||
#
|
|
||||||
# Read more: https://zensical.org/docs/setup/navigation/
|
|
||||||
# nav = [
|
|
||||||
# { "Get started" = "index.md" },
|
|
||||||
# { "Markdown in 5min" = "markdown.md" },
|
|
||||||
# ]
|
|
||||||
|
|
||||||
# With the "extra_css" option you can add your own CSS styling to customize
|
|
||||||
# your Zensical project according to your needs. You can add any number of
|
|
||||||
# CSS files.
|
|
||||||
#
|
|
||||||
# The path provided should be relative to the "docs_dir".
|
|
||||||
#
|
|
||||||
# Read more: https://zensical.org/docs/customization/#additional-css
|
|
||||||
#
|
|
||||||
#extra_css = ["stylesheets/extra.css"]
|
|
||||||
|
|
||||||
# With the `extra_javascript` option you can add your own JavaScript to your
|
|
||||||
# project to customize the behavior according to your needs.
|
|
||||||
#
|
|
||||||
# The path provided should be relative to the "docs_dir".
|
|
||||||
#
|
|
||||||
# Read more: https://zensical.org/docs/customization/#additional-javascript
|
|
||||||
#extra_javascript = ["javascripts/extra.js"]
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# Section for configuring theme options
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
[project.theme]
|
[project.theme]
|
||||||
|
|
||||||
# change this to "classic" to use the traditional Material for MkDocs look.
|
|
||||||
#variant = "classic"
|
|
||||||
|
|
||||||
# Zensical allows you to override specific blocks, partials, or whole
|
|
||||||
# templates as well as to define your own templates. To do this, uncomment
|
|
||||||
# the custom_dir setting below and set it to a directory in which you
|
|
||||||
# keep your template overrides.
|
|
||||||
#
|
|
||||||
# Read more:
|
|
||||||
# - https://zensical.org/docs/customization/#extending-the-theme
|
|
||||||
#
|
|
||||||
custom_dir = "docs/overrides"
|
custom_dir = "docs/overrides"
|
||||||
|
|
||||||
# With the "favicon" option you can set your own image to use as the icon
|
|
||||||
# browsers will use in the browser title bar or tab bar. The path provided
|
|
||||||
# must be relative to the "docs_dir".
|
|
||||||
#
|
|
||||||
# Read more:
|
|
||||||
# - https://zensical.org/docs/setup/logo-and-icons/#favicon
|
|
||||||
# - https://developer.mozilla.org/en-US/docs/Glossary/Favicon
|
|
||||||
#
|
|
||||||
#favicon = "images/favicon.png"
|
|
||||||
|
|
||||||
# Zensical supports more than 60 different languages. This means that the
|
|
||||||
# labels and tooltips that Zensical's templates produce are translated.
|
|
||||||
# The "language" option allows you to set the language used. This language
|
|
||||||
# is also indicated in the HTML head element to help with accessibility
|
|
||||||
# and guide search engines and translation tools.
|
|
||||||
#
|
|
||||||
# The default language is "en" (English). It is possible to create
|
|
||||||
# sites with multiple languages and configure a language selector. See
|
|
||||||
# the documentation for details.
|
|
||||||
#
|
|
||||||
# Read more:
|
|
||||||
# - https://zensical.org/docs/setup/language/
|
|
||||||
#
|
|
||||||
language = "en"
|
language = "en"
|
||||||
|
|
||||||
# Zensical provides a number of feature toggles that change the behavior
|
|
||||||
# of the documentation site.
|
|
||||||
features = [
|
features = [
|
||||||
# Zensical includes an announcement bar. This feature allows users to
|
|
||||||
# dismiss it when they have read the announcement.
|
|
||||||
# https://zensical.org/docs/setup/header/#announcement-bar
|
|
||||||
"announce.dismiss",
|
"announce.dismiss",
|
||||||
|
|
||||||
# If you have a repository configured and turn on this feature, Zensical
|
|
||||||
# will generate an edit button for the page. This works for common
|
|
||||||
# repository hosting services.
|
|
||||||
# https://zensical.org/docs/setup/repository/#content-actions
|
|
||||||
#"content.action.edit",
|
|
||||||
|
|
||||||
# If you have a repository configured and turn on this feature, Zensical
|
|
||||||
# will generate a button that allows the user to view the Markdown
|
|
||||||
# code for the current page.
|
|
||||||
# https://zensical.org/docs/setup/repository/#content-actions
|
|
||||||
"content.action.view",
|
"content.action.view",
|
||||||
|
|
||||||
# Code annotations allow you to add an icon with a tooltip to your
|
|
||||||
# code blocks to provide explanations at crucial points.
|
|
||||||
# https://zensical.org/docs/authoring/code-blocks/#code-annotations
|
|
||||||
"content.code.annotate",
|
"content.code.annotate",
|
||||||
|
|
||||||
# This feature turns on a button in code blocks that allow users to
|
|
||||||
# copy the content to their clipboard without first selecting it.
|
|
||||||
# https://zensical.org/docs/authoring/code-blocks/#code-copy-button
|
|
||||||
"content.code.copy",
|
"content.code.copy",
|
||||||
|
|
||||||
# Code blocks can include a button to allow for the selection of line
|
|
||||||
# ranges by the user.
|
|
||||||
# https://zensical.org/docs/authoring/code-blocks/#code-selection-button
|
|
||||||
"content.code.select",
|
"content.code.select",
|
||||||
|
|
||||||
# Zensical can render footnotes as inline tooltips, so the user can read
|
|
||||||
# the footnote without leaving the context of the document.
|
|
||||||
# https://zensical.org/docs/authoring/footnotes/#footnote-tooltips
|
|
||||||
"content.footnote.tooltips",
|
"content.footnote.tooltips",
|
||||||
|
|
||||||
# If you have many content tabs that have the same titles (e.g., "Python",
|
|
||||||
# "JavaScript", "Cobol"), this feature causes all of them to switch to
|
|
||||||
# at the same time when the user chooses their language in one.
|
|
||||||
# https://zensical.org/docs/authoring/content-tabs/#linked-content-tabs
|
|
||||||
"content.tabs.link",
|
"content.tabs.link",
|
||||||
|
|
||||||
# With this feature enabled users can add tooltips to links that will be
|
|
||||||
# displayed when the mouse pointer hovers the link.
|
|
||||||
# https://zensical.org/docs/authoring/tooltips/#improved-tooltips
|
|
||||||
"content.tooltips",
|
"content.tooltips",
|
||||||
|
|
||||||
# With this feature enabled, Zensical will automatically hide parts
|
|
||||||
# of the header when the user scrolls past a certain point.
|
|
||||||
# https://zensical.org/docs/setup/header/#automatic-hiding
|
|
||||||
# "header.autohide",
|
|
||||||
|
|
||||||
# Turn on this feature to expand all collapsible sections in the
|
|
||||||
# navigation sidebar by default.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#navigation-expansion
|
|
||||||
# "navigation.expand",
|
|
||||||
|
|
||||||
# This feature turns on navigation elements in the footer that allow the
|
|
||||||
# user to navigate to a next or previous page.
|
|
||||||
# https://zensical.org/docs/setup/footer/#navigation
|
|
||||||
"navigation.footer",
|
"navigation.footer",
|
||||||
|
|
||||||
# When section index pages are enabled, documents can be directly attached
|
|
||||||
# to sections, which is particularly useful for providing overview pages.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#section-index-pages
|
|
||||||
"navigation.indexes",
|
"navigation.indexes",
|
||||||
|
|
||||||
# When instant navigation is enabled, clicks on all internal links will be
|
|
||||||
# intercepted and dispatched via XHR without fully reloading the page.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#instant-navigation
|
|
||||||
"navigation.instant",
|
"navigation.instant",
|
||||||
|
|
||||||
# With instant prefetching, your site will start to fetch a page once the
|
|
||||||
# user hovers over a link. This will reduce the perceived loading time
|
|
||||||
# for the user.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#instant-prefetching
|
|
||||||
"navigation.instant.prefetch",
|
"navigation.instant.prefetch",
|
||||||
|
|
||||||
# In order to provide a better user experience on slow connections when
|
|
||||||
# using instant navigation, a progress indicator can be enabled.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#progress-indicator
|
|
||||||
#"navigation.instant.progress",
|
|
||||||
|
|
||||||
# When navigation paths are activated, a breadcrumb navigation is rendered
|
|
||||||
# above the title of each page
|
|
||||||
# https://zensical.org/docs/setup/navigation/#navigation-path
|
|
||||||
"navigation.path",
|
"navigation.path",
|
||||||
|
|
||||||
# When pruning is enabled, only the visible navigation items are included
|
|
||||||
# in the rendered HTML, reducing the size of the built site by 33% or more.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#navigation-pruning
|
|
||||||
#"navigation.prune",
|
|
||||||
|
|
||||||
# When sections are enabled, top-level sections are rendered as groups in
|
|
||||||
# the sidebar for viewports above 1220px, but remain as-is on mobile.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#navigation-sections
|
|
||||||
"navigation.sections",
|
"navigation.sections",
|
||||||
|
|
||||||
# When tabs are enabled, top-level sections are rendered in a menu layer
|
|
||||||
# below the header for viewports above 1220px, but remain as-is on mobile.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#navigation-tabs
|
|
||||||
"navigation.tabs",
|
"navigation.tabs",
|
||||||
|
|
||||||
# When sticky tabs are enabled, navigation tabs will lock below the header
|
|
||||||
# and always remain visible when scrolling down.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#sticky-navigation-tabs
|
|
||||||
#"navigation.tabs.sticky",
|
|
||||||
|
|
||||||
# A back-to-top button can be shown when the user, after scrolling down,
|
|
||||||
# starts to scroll up again.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#back-to-top-button
|
|
||||||
"navigation.top",
|
"navigation.top",
|
||||||
|
|
||||||
# When anchor tracking is enabled, the URL in the address bar is
|
|
||||||
# automatically updated with the active anchor as highlighted in the table
|
|
||||||
# of contents.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#anchor-tracking
|
|
||||||
"navigation.tracking",
|
"navigation.tracking",
|
||||||
|
|
||||||
# When search highlighting is enabled and a user clicks on a search result,
|
|
||||||
# Zensical will highlight all occurrences after following the link.
|
|
||||||
# https://zensical.org/docs/setup/search/#search-highlighting
|
|
||||||
"search.highlight",
|
"search.highlight",
|
||||||
|
|
||||||
# When anchor following for the table of contents is enabled, the sidebar
|
|
||||||
# is automatically scrolled so that the active anchor is always visible.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#anchor-following
|
|
||||||
# "toc.follow",
|
|
||||||
|
|
||||||
# When navigation integration for the table of contents is enabled, it is
|
|
||||||
# always rendered as part of the navigation sidebar on the left.
|
|
||||||
# https://zensical.org/docs/setup/navigation/#navigation-integration
|
|
||||||
#"toc.integrate",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# In the "palette" subsection you can configure options for the color scheme.
|
|
||||||
# You can configure different color # schemes, e.g., to turn on dark mode,
|
|
||||||
# that the user can switch between. Each color scheme can be further
|
|
||||||
# customized.
|
|
||||||
#
|
|
||||||
# Read more:
|
|
||||||
# - https://zensical.org/docs/setup/colors/
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
[[project.theme.palette]]
|
[[project.theme.palette]]
|
||||||
scheme = "default"
|
scheme = "default"
|
||||||
toggle.icon = "lucide/sun"
|
toggle.icon = "lucide/sun"
|
||||||
@@ -270,43 +40,13 @@ scheme = "slate"
|
|||||||
toggle.icon = "lucide/moon"
|
toggle.icon = "lucide/moon"
|
||||||
toggle.name = "Switch to light mode"
|
toggle.name = "Switch to light mode"
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# In the "font" subsection you can configure the fonts used. By default, fonts
|
|
||||||
# are loaded from Google Fonts, giving you a wide range of choices from a set
|
|
||||||
# of suitably licensed fonts. There are options for a normal text font and for
|
|
||||||
# a monospaced font used in code blocks.
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
[project.theme.font]
|
[project.theme.font]
|
||||||
text = "Inter"
|
text = "Inter"
|
||||||
code = "Jetbrains Mono"
|
code = "Jetbrains Mono"
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# You can configure your own logo to be shown in the header using the "logo"
|
|
||||||
# option in the "icons" subsection. The logo can be a path to a file in your
|
|
||||||
# "docs_dir" or it can be a path to an icon.
|
|
||||||
#
|
|
||||||
# Likewise, you can customize the logo used for the repository section of the
|
|
||||||
# header. Zensical derives the default logo for this from the repository URL.
|
|
||||||
# See below...
|
|
||||||
#
|
|
||||||
# There are other icons you can customize. See the documentation for details.
|
|
||||||
#
|
|
||||||
# Read more:
|
|
||||||
# - https://zensical.org/docs/setup/logo-and-icons
|
|
||||||
# - https://zensical.org/docs/authoring/icons-emojis/#search
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
[project.theme.icon]
|
[project.theme.icon]
|
||||||
#logo = "lucide/smile"
|
|
||||||
repo = "fontawesome/brands/github"
|
repo = "fontawesome/brands/github"
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# The "extra" section contains miscellaneous settings.
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
#[[project.extra.social]]
|
|
||||||
#icon = "fontawesome/brands/github"
|
|
||||||
#link = "https://github.com/user/repo"
|
|
||||||
|
|
||||||
|
|
||||||
[project.plugins.mkdocstrings.handlers.python]
|
[project.plugins.mkdocstrings.handlers.python]
|
||||||
inventories = ["https://docs.python.org/3/objects.inv"]
|
inventories = ["https://docs.python.org/3/objects.inv"]
|
||||||
paths = ["src"]
|
paths = ["src"]
|
||||||
@@ -316,3 +56,92 @@ docstring_style = "google"
|
|||||||
inherited_members = true
|
inherited_members = true
|
||||||
show_source = false
|
show_source = false
|
||||||
show_root_heading = true
|
show_root_heading = true
|
||||||
|
|
||||||
|
[project.markdown_extensions]
|
||||||
|
abbr = {}
|
||||||
|
admonition = {}
|
||||||
|
attr_list = {}
|
||||||
|
def_list = {}
|
||||||
|
footnotes = {}
|
||||||
|
md_in_html = {}
|
||||||
|
"pymdownx.arithmatex" = {generic = true}
|
||||||
|
"pymdownx.betterem" = {}
|
||||||
|
"pymdownx.caret" = {}
|
||||||
|
"pymdownx.details" = {}
|
||||||
|
"pymdownx.emoji" = {}
|
||||||
|
"pymdownx.inlinehilite" = {}
|
||||||
|
"pymdownx.keys" = {}
|
||||||
|
"pymdownx.magiclink" = {}
|
||||||
|
"pymdownx.mark" = {}
|
||||||
|
"pymdownx.smartsymbols" = {}
|
||||||
|
"pymdownx.tasklist" = {custom_checkbox = true}
|
||||||
|
"pymdownx.tilde" = {}
|
||||||
|
|
||||||
|
[project.markdown_extensions.pymdownx.emoji]
|
||||||
|
emoji_index = "zensical.extensions.emoji.twemoji"
|
||||||
|
emoji_generator = "zensical.extensions.emoji.to_svg"
|
||||||
|
|
||||||
|
[project.markdown_extensions."pymdownx.highlight"]
|
||||||
|
anchor_linenums = true
|
||||||
|
line_spans = "__span"
|
||||||
|
pygments_lang_class = true
|
||||||
|
|
||||||
|
[project.markdown_extensions."pymdownx.superfences"]
|
||||||
|
custom_fences = [{name = "mermaid", class = "mermaid"}]
|
||||||
|
|
||||||
|
[project.markdown_extensions."pymdownx.tabbed"]
|
||||||
|
alternate_style = true
|
||||||
|
combine_header_slug = true
|
||||||
|
|
||||||
|
[project.markdown_extensions."toc"]
|
||||||
|
permalink = true
|
||||||
|
|
||||||
|
[project.markdown_extensions."pymdownx.snippets"]
|
||||||
|
base_path = ["."]
|
||||||
|
check_paths = true
|
||||||
|
|
||||||
|
[[project.nav]]
|
||||||
|
Home = "index.md"
|
||||||
|
|
||||||
|
[[project.nav]]
|
||||||
|
Modules = [
|
||||||
|
{CLI = "module/cli.md"},
|
||||||
|
{CRUD = "module/crud.md"},
|
||||||
|
{Database = "module/db.md"},
|
||||||
|
{Dependencies = "module/dependencies.md"},
|
||||||
|
{Exceptions = "module/exceptions.md"},
|
||||||
|
{Fixtures = "module/fixtures.md"},
|
||||||
|
{Logger = "module/logger.md"},
|
||||||
|
{Metrics = "module/metrics.md"},
|
||||||
|
{Models = "module/models.md"},
|
||||||
|
{Pytest = "module/pytest.md"},
|
||||||
|
{Schemas = "module/schemas.md"},
|
||||||
|
]
|
||||||
|
|
||||||
|
[[project.nav]]
|
||||||
|
Reference = [
|
||||||
|
{CLI = "reference/cli.md"},
|
||||||
|
{CRUD = "reference/crud.md"},
|
||||||
|
{Database = "reference/db.md"},
|
||||||
|
{Dependencies = "reference/dependencies.md"},
|
||||||
|
{Exceptions = "reference/exceptions.md"},
|
||||||
|
{Fixtures = "reference/fixtures.md"},
|
||||||
|
{Logger = "reference/logger.md"},
|
||||||
|
{Metrics = "reference/metrics.md"},
|
||||||
|
{Models = "reference/models.md"},
|
||||||
|
{Pytest = "reference/pytest.md"},
|
||||||
|
{Schemas = "reference/schemas.md"},
|
||||||
|
]
|
||||||
|
|
||||||
|
[[project.nav]]
|
||||||
|
Examples = [
|
||||||
|
{"Pagination & Search" = "examples/pagination-search.md"},
|
||||||
|
]
|
||||||
|
|
||||||
|
[[project.nav]]
|
||||||
|
Migration = [
|
||||||
|
{"v2.0" = "migration/v2.md"},
|
||||||
|
]
|
||||||
|
|
||||||
|
[[project.nav]]
|
||||||
|
"Changelog ↗" = "https://github.com/d3vyce/fastapi-toolsets/releases"
|
||||||
|
|||||||
Reference in New Issue
Block a user