generated from br3ndonland/template-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document and test FastAPI integration
- Loading branch information
1 parent
2da45db
commit 4670b5b
Showing
4 changed files
with
131 additions
and
0 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
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,60 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
from contextlib import asynccontextmanager | ||
from typing import AsyncGenerator, AsyncIterator, Dict, TypedDict | ||
|
||
import pytest | ||
from anyio import Path | ||
from fastapi import FastAPI, Request | ||
from fastapi.testclient import TestClient | ||
|
||
import fastenv | ||
|
||
|
||
class LifespanState(TypedDict): | ||
settings: fastenv.DotEnv | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(_: FastAPI) -> AsyncIterator[LifespanState]: | ||
"""Configure app lifespan. | ||
https://fastapi.tiangolo.com/advanced/events/ | ||
https://www.starlette.io/lifespan/ | ||
""" | ||
env_file = os.environ["ENV_FILE"] | ||
settings = await fastenv.load_dotenv(env_file) | ||
lifespan_state: LifespanState = {"settings": settings} | ||
yield lifespan_state | ||
|
||
|
||
app = FastAPI(lifespan=lifespan) | ||
|
||
|
||
@app.get("/settings") | ||
async def get_settings(request: Request) -> Dict[str, str]: | ||
settings = request.state.settings | ||
return dict(settings) | ||
|
||
|
||
@pytest.fixture | ||
async def test_client( | ||
env_file: Path, monkeypatch: pytest.MonkeyPatch | ||
) -> AsyncGenerator[TestClient, None]: | ||
"""Instantiate a FastAPI test client. | ||
https://fastapi.tiangolo.com/tutorial/testing/ | ||
https://www.starlette.io/testclient/ | ||
""" | ||
monkeypatch.setenv("ENV_FILE", str(env_file)) | ||
with TestClient(app) as test_client: | ||
yield test_client | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_fastapi_with_fastenv(test_client: TestClient) -> None: | ||
"""Test loading a dotenv file into a FastAPI app with fastenv.""" | ||
response = test_client.get("/settings") | ||
response_json = response.json() | ||
assert response_json["AWS_ACCESS_KEY_ID_EXAMPLE"] == "AKIAIOSFODNN7EXAMPLE" |