generate basic test for fastapi aplication with hexagonal arquitecture
- User
generate basic test for fastapi aplication with hexagonal arquitecture
Assistant
Certainly! Here's an example of a basic test for a FastAPI application using hexagonal architecture:
pythonfrom fastapi.testclient import TestClient from app.main import app from app.domain.user import User from app.application.user_service import UserService client = TestClient(app) def test_get_user(): # Arrange user_id = "123" expected_user = User(id=user_id, name="John Doe", email="john@example.com") user_service = UserService() user_service.get_user = lambda user_id: expected_user # Act response = client.get(f"/users/{user_id}") # Assert assert response.status_code == 200 assert response.json() == { "id": expected_user.id, "name": expected_user.name, "email": expected_user.email } def test_create_user(): # Arrange new_user = { "name": "Jane Smith", "email": "jane@example.com" } expected_user = User(id="456", name=new_user["name"], email=new_user["email"]) user_service = UserService() user_service.create_user = lambda user_data: expected_user # Act response = client.post("/users", json=new_user) # Assert assert response.status_code == 201 assert response.json() == { "id": expected_user.id, "name": expected_user.name, "email": expected_user.email }In this example, we have two test functions:
test_get_userandtest_create_user.-
test_get_user:- Arrange:
- We define the
user_idand create an expectedUserobject. - We mock the
get_usermethod of the to return the expected user.
- We define the
- Arrange:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.