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 7
7
8
8
if __name__ == "__main__" :
9
9
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 ,
12
13
port = settings .app_port ,
13
14
interface = Interfaces .ASGI ,
14
15
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:
35
35
)
36
36
bootstrapper = LitestarBootstrapper (bootstrap_config = bootstrap_config )
37
37
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):
17
17
18
18
19
19
class Card (CardBase ):
20
- model_config = pydantic .ConfigDict (from_attributes = True )
21
-
22
20
id : PositiveInt
23
21
deck_id : PositiveInt | None = None
24
22
@@ -37,8 +35,6 @@ class DeckCreate(DeckBase):
37
35
38
36
39
37
class Deck (DeckBase ):
40
- model_config = pydantic .ConfigDict (from_attributes = True )
41
-
42
38
id : PositiveInt
43
39
cards : list [Card ] | None
44
40
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ class Settings(pydantic_settings.BaseSettings):
15
15
db_max_overflow : int = 0
16
16
db_pool_pre_ping : bool = True
17
17
18
+ app_host : str = "0.0.0.0" # noqa: S104
18
19
app_port : int = 8000
19
20
20
21
opentelemetry_endpoint : str = ""
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ services:
8
8
- .:/code
9
9
- /code/.venv
10
10
ports :
11
- - " 8001 :8000"
11
+ - " 8000 :8000"
12
12
depends_on :
13
13
db :
14
14
condition : service_healthy
Original file line number Diff line number Diff line change 1
1
[project ]
2
- name = " litestar -sqlalchemy-template"
2
+ name = " fastapi -sqlalchemy-template"
3
3
version = " 0"
4
- description = " Async template on LiteStar and SQLAlchemy 2"
4
+ description = " Async template on FastAPI and SQLAlchemy 2"
5
5
readme = " README.md"
6
6
requires-python = " >=3.13"
7
7
authors = [
@@ -11,10 +11,10 @@ license = "MIT License"
11
11
dependencies = [
12
12
" litestar" ,
13
13
" lite-bootstrap[litestar-all]" ,
14
+ " modern-di-litestar" ,
14
15
" advanced-alchemy" ,
15
16
" pydantic-settings" ,
16
- " granian" ,
17
- " modern-di-litestar" ,
17
+ " granian[uvloop]" ,
18
18
# database
19
19
" alembic" ,
20
20
" psycopg2" ,
@@ -32,6 +32,7 @@ dev = [
32
32
" pytest" ,
33
33
" pytest-cov" ,
34
34
" pytest-asyncio" ,
35
+ " asgi_lifespan" ,
35
36
" ruff" ,
36
37
" mypy" ,
37
38
" asyncpg-stubs" ,
Original file line number Diff line number Diff line change 1
1
import typing
2
2
3
+ import litestar
3
4
import modern_di
4
5
import modern_di_litestar
5
6
import pytest
7
+ from asgi_lifespan import LifespanManager
6
8
from httpx import ASGITransport , AsyncClient
7
9
from sqlalchemy .ext .asyncio import AsyncSession
8
10
9
11
from app import ioc
10
- from app .application import application
12
+ from app .application import build_app
11
13
12
14
13
15
@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 ]:
15
24
async with AsyncClient (
16
- transport = ASGITransport (app = application ), # type: ignore[arg-type]
25
+ transport = ASGITransport (app = app ), # type: ignore[arg-type]
17
26
base_url = "http://test" ,
18
27
) as client :
19
28
yield client
20
29
21
30
22
31
@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 )
27
34
28
35
29
36
@pytest .fixture (autouse = True )
Original file line number Diff line number Diff line change @@ -14,9 +14,5 @@ class CardModelFactory(SQLAlchemyFactory[models.Card]):
14
14
id = None
15
15
16
16
17
- class DeckCreateSchemaFactory (ModelFactory [schemas .DeckCreate ]):
18
- __model__ = schemas .DeckCreate
19
-
20
-
21
17
class CardCreateSchemaFactory (ModelFactory [schemas .CardCreate ]):
22
18
__model__ = schemas .CardCreate
You can’t perform that action at this time.
0 commit comments