File tree Expand file tree Collapse file tree 9 files changed +205
-173
lines changed Expand file tree Collapse file tree 9 files changed +205
-173
lines changed Original file line number Diff line number Diff line change 77
88if __name__ == "__main__" :
99 granian .Granian ( # type: ignore[attr-defined]
10- target = "app.application:application" ,
11- address = "0.0.0.0" , # noqa: S104
10+ target = "app.application:build_app" ,
11+ factory = True ,
12+ address = settings .app_host ,
1213 port = settings .app_port ,
1314 interface = Interfaces .ASGI ,
1415 log_level = LogLevels (settings .log_level ),
Original file line number Diff line number Diff line change @@ -35,6 +35,3 @@ def build_app() -> litestar.Litestar:
3535 )
3636 bootstrapper = LitestarBootstrapper (bootstrap_config = bootstrap_config )
3737 return bootstrapper .bootstrap ()
38-
39-
40- application = build_app ()
Original file line number Diff line number Diff line change @@ -17,8 +17,6 @@ class CardCreate(CardBase):
1717
1818
1919class Card (CardBase ):
20- model_config = pydantic .ConfigDict (from_attributes = True )
21-
2220 id : PositiveInt
2321 deck_id : PositiveInt | None = None
2422
@@ -37,8 +35,6 @@ class DeckCreate(DeckBase):
3735
3836
3937class Deck (DeckBase ):
40- model_config = pydantic .ConfigDict (from_attributes = True )
41-
4238 id : PositiveInt
4339 cards : list [Card ] | None
4440
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ class Settings(pydantic_settings.BaseSettings):
1515 db_max_overflow : int = 0
1616 db_pool_pre_ping : bool = True
1717
18+ app_host : str = "0.0.0.0" # noqa: S104
1819 app_port : int = 8000
1920
2021 opentelemetry_endpoint : str = ""
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ services:
88 - .:/code
99 - /code/.venv
1010 ports :
11- - " 8001 :8000"
11+ - " 8000 :8000"
1212 depends_on :
1313 db :
1414 condition : service_healthy
Original file line number Diff line number Diff line change 11[project ]
2- name = " litestar -sqlalchemy-template"
2+ name = " fastapi -sqlalchemy-template"
33version = " 0"
4- description = " Async template on LiteStar and SQLAlchemy 2"
4+ description = " Async template on FastAPI and SQLAlchemy 2"
55readme = " README.md"
66requires-python = " >=3.13"
77authors = [
@@ -11,10 +11,10 @@ license = "MIT License"
1111dependencies = [
1212 " litestar" ,
1313 " lite-bootstrap[litestar-all]" ,
14+ " modern-di-litestar" ,
1415 " advanced-alchemy" ,
1516 " pydantic-settings" ,
16- " granian" ,
17- " modern-di-litestar" ,
17+ " granian[uvloop]" ,
1818 # database
1919 " alembic" ,
2020 " psycopg2" ,
@@ -32,6 +32,7 @@ dev = [
3232 " pytest" ,
3333 " pytest-cov" ,
3434 " pytest-asyncio" ,
35+ " asgi_lifespan" ,
3536 " ruff" ,
3637 " mypy" ,
3738 " asyncpg-stubs" ,
Original file line number Diff line number Diff line change 11import typing
22
3+ import litestar
34import modern_di
45import modern_di_litestar
56import pytest
7+ from asgi_lifespan import LifespanManager
68from httpx import ASGITransport , AsyncClient
79from sqlalchemy .ext .asyncio import AsyncSession
810
911from app import ioc
10- from app .application import application
12+ from app .application import build_app
1113
1214
1315@pytest .fixture
14- async def client () -> typing .AsyncIterator [AsyncClient ]:
16+ async def app () -> typing .AsyncIterator [litestar .Litestar ]:
17+ app_ = build_app ()
18+ async with LifespanManager (app_ ): # type: ignore[arg-type]
19+ yield app_
20+
21+
22+ @pytest .fixture
23+ async def client (app : litestar .Litestar ) -> typing .AsyncIterator [AsyncClient ]:
1524 async with AsyncClient (
16- transport = ASGITransport (app = application ), # type: ignore[arg-type]
25+ transport = ASGITransport (app = app ), # type: ignore[arg-type]
1726 base_url = "http://test" ,
1827 ) as client :
1928 yield client
2029
2130
2231@pytest .fixture
23- async def di_container () -> typing .AsyncIterator [modern_di .Container ]:
24- di_container_ : typing .Final = modern_di_litestar .fetch_di_container (application )
25- async with di_container_ :
26- yield di_container_
32+ def di_container (app : litestar .Litestar ) -> modern_di .Container :
33+ return modern_di_litestar .fetch_di_container (app )
2734
2835
2936@pytest .fixture (autouse = True )
Original file line number Diff line number Diff line change @@ -14,9 +14,5 @@ class CardModelFactory(SQLAlchemyFactory[models.Card]):
1414 id = None
1515
1616
17- class DeckCreateSchemaFactory (ModelFactory [schemas .DeckCreate ]):
18- __model__ = schemas .DeckCreate
19-
20-
2117class CardCreateSchemaFactory (ModelFactory [schemas .CardCreate ]):
2218 __model__ = schemas .CardCreate
You can’t perform that action at this time.
0 commit comments