28 lines
1.1 KiB
Python
28 lines
1.1 KiB
Python
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 doesn’t 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()
|