-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for sqlalchemy from statement (#1277)
- Loading branch information
Showing
3 changed files
with
52 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import Iterator, Type | ||
|
||
from fastapi import Depends, FastAPI | ||
from pytest import fixture | ||
from sqlalchemy import select, text | ||
from sqlalchemy.orm.session import Session | ||
|
||
from fastapi_pagination import LimitOffsetPage, Page, add_pagination | ||
from fastapi_pagination.ext.sqlalchemy import paginate | ||
|
||
from ..base import BasePaginationTestCase | ||
from ..utils import OptionalLimitOffsetPage, OptionalPage | ||
|
||
|
||
@fixture(scope="session") | ||
def app(sa_user, sa_session: Type[Session], model_cls: Type[object]): | ||
app = FastAPI() | ||
|
||
def get_db() -> Iterator[Session]: | ||
db = sa_session() | ||
try: | ||
yield db | ||
finally: | ||
db.close() | ||
|
||
@app.get("/default", response_model=Page[model_cls]) | ||
@app.get("/limit-offset", response_model=LimitOffsetPage[model_cls]) | ||
@app.get("/optional/default", response_model=OptionalPage[model_cls]) | ||
@app.get("/optional/limit-offset", response_model=OptionalLimitOffsetPage[model_cls]) | ||
def route(db: Session = Depends(get_db)): | ||
return paginate(db, select(sa_user).from_statement(text("SELECT * FROM users"))) | ||
|
||
return add_pagination(app) | ||
|
||
|
||
class TestSQLAlchemyFromStatement(BasePaginationTestCase): | ||
pagination_types = ["default", "optional"] |