Add template fastapi app, test, README.md, this can be used for future extension

This commit is contained in:
dongli 2025-05-11 21:01:11 -07:00
parent bf96f8b301
commit dd8926b2ec
43 changed files with 38 additions and 14 deletions

View File

@ -1,9 +0,0 @@
from fastapi.testclient import TestClient
from app.main import app # Adjust this import if your app is in a different location
client = TestClient(app)
def test_hello_world():
response = client.get("/api/hello_world/")
assert response.status_code == 200
assert response.json() == {"message": "Hello, World!"} # Update if your endpoint returns different data

View File

@ -1,6 +1,6 @@
This is a template backend service based on fastapi + mongodb app This is a template backend service based on fastapi + mongodb app
To start development in local, go to the root directory of the project YOUR_WORKSPACE_PATH/devops/ To start development in local, go to the root directory of the project YOUR_WORKSPACE_PATH/helloworld/
```bash ```bash
docker compose -f app/scripts/mongodb/docker-compose.yml up -d docker compose -f app/scripts/mongodb/docker-compose.yml up -d
``` ```

View File

@ -1,3 +1,6 @@
from app.common.daos.hello_world.hello_world_dao import HelloWorldDao from app.common.daos.hello_world.hello_world_dao import HelloWorldDao
hello_world_dao = HelloWorldDao() hello_world_dao = HelloWorldDao()
def get_hello_world_dao() -> HelloWorldDao:
return hello_world_dao

View File

@ -1,7 +1,7 @@
from fastapi import APIRouter from fastapi import APIRouter, Depends
from loguru import logger from loguru import logger
from app.common.daos.hello_world import hello_world_dao from app.common.daos.hello_world import get_hello_world_dao, HelloWorldDao
router = APIRouter() router = APIRouter()
@ -12,11 +12,14 @@ async def hello_world():
@router.post("/insert") @router.post("/insert")
async def insert_hello_world(msg: str): async def insert_hello_world(msg: str, dao: HelloWorldDao = Depends(get_hello_world_dao)):
""" """
Insert a HelloWorld document into the database. Insert a HelloWorld document into the database.
""" """
hello_world = await hello_world_dao.create_hello_world(msg, 1) hello_world = await dao.create_hello_world(msg, 1)
return hello_world return hello_world

View File

@ -0,0 +1,27 @@
from unittest.mock import AsyncMock, patch
from fastapi.testclient import TestClient
from app.main import app
from app.routes.hello_world.apis import get_hello_world_dao
def test_hello_world():
with TestClient(app) as client:
response = client.get("/api/hello_world/")
assert response.status_code == 200
assert response.json() == {"message": "Hello, World!"}
# mock out initiate_database so it doesnt run during tests
@patch("app.providers.database.initiate_database", new_callable=AsyncMock)
def test_insert_hello_world(mock_db_init):
class MockHelloWorldDao:
async def create_hello_world(self, msg: str, user_id: int):
return {"message": msg, "user_id": user_id}
app.dependency_overrides[get_hello_world_dao] = lambda: MockHelloWorldDao()
with TestClient(app) as client:
response = client.post("/api/hello_world/insert", params={"msg": "Test Message"})
assert response.status_code == 200
assert response.json() == {"message": "Test Message", "user_id": 1}
app.dependency_overrides.clear()