Skip to content

Commit

Permalink
Add tests for from_statement
Browse files Browse the repository at this point in the history
  • Loading branch information
uriyyo committed Sep 8, 2024
1 parent 57cf320 commit f8b8314
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/ext/test_sqlalchemy_from_statement.py
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"]

0 comments on commit f8b8314

Please sign in to comment.